Skip to content

Commit

Permalink
Try ffjson
Browse files Browse the repository at this point in the history
  • Loading branch information
Alfonso Acosta committed Feb 9, 2016
1 parent ba688ee commit b46d5d1
Show file tree
Hide file tree
Showing 41 changed files with 7,657 additions and 42 deletions.
5 changes: 2 additions & 3 deletions app/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package app

import (
"compress/gzip"
"encoding/json"
"log"
"net/http"
"net/url"
"strings"

"github.com/PuerkitoBio/ghost/handlers"
"github.com/gorilla/mux"
"github.com/pquerna/ffjson/ffjson"

"github.com/weaveworks/scope/common/hostname"
"github.com/weaveworks/scope/common/xfer"
Expand Down Expand Up @@ -131,8 +131,7 @@ func RegisterReportPostHandler(a Adder, router *mux.Router) {
}
}

decoder := json.NewDecoder(reader).Decode
if err := decoder(&rpt); err != nil {
if err := ffjson.NewDecoder().DecodeReader(reader, &rpt); err != nil {
log.Printf("Failed to decode body: %s\n", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
Expand Down
4 changes: 2 additions & 2 deletions probe/appclient/app_client.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package appclient

import (
"encoding/json"
"fmt"
"io"
"net/http"
Expand All @@ -11,6 +10,7 @@ import (

log "github.com/Sirupsen/logrus"
"github.com/gorilla/websocket"
"github.com/pquerna/ffjson/ffjson"

"github.com/weaveworks/scope/common/sanitize"
"github.com/weaveworks/scope/common/xfer"
Expand Down Expand Up @@ -149,7 +149,7 @@ func (c *appClient) Details() (xfer.Details, error) {
return result, err
}
defer resp.Body.Close()
return result, json.NewDecoder(resp.Body).Decode(&result)
return result, ffjson.NewDecoder().DecodeReader(resp.Body, &result)
}

func (c *appClient) doWithBackoff(msg string, f func() (bool, error)) {
Expand Down
4 changes: 2 additions & 2 deletions probe/appclient/report_publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package appclient
import (
"bytes"
"compress/gzip"
"encoding/json"
"github.com/pquerna/ffjson/ffjson"

"github.com/weaveworks/scope/report"
)
Expand All @@ -25,7 +25,7 @@ func NewReportPublisher(publisher Publisher) *ReportPublisher {
func (p *ReportPublisher) Publish(r report.Report) error {
buf := &bytes.Buffer{}
gzwriter := gzip.NewWriter(buf)
if err := json.NewEncoder(gzwriter).Encode(r); err != nil {
if err := ffjson.NewEncoder(gzwriter).Encode(r); err != nil {
return err
}
gzwriter.Close() // otherwise the content won't get flushed to the output stream
Expand Down
6 changes: 3 additions & 3 deletions probe/docker/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package docker

import (
"bufio"
"encoding/json"
"fmt"
"io"
"net"
Expand All @@ -16,6 +15,7 @@ import (

log "github.com/Sirupsen/logrus"
docker "github.com/fsouza/go-dockerclient"
"github.com/pquerna/ffjson/ffjson"

"github.com/weaveworks/scope/common/mtime"
"github.com/weaveworks/scope/report"
Expand Down Expand Up @@ -203,8 +203,8 @@ func (c *container) StartGatheringStats() error {
}()

var stats docker.Stats
decoder := json.NewDecoder(resp.Body)
for err := decoder.Decode(&stats); err != io.EOF; err = decoder.Decode(&stats) {
decoder := ffjson.NewDecoder()
for err := decoder.DecodeReader(resp.Body, &stats); err != io.EOF; err = decoder.DecodeReader(resp.Body, &stats) {
if err != nil {
log.Errorf("docker container: error reading event, did container stop? %v", err)
return
Expand Down
4 changes: 2 additions & 2 deletions probe/overlay/weave.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package overlay

import (
"bufio"
"encoding/json"
"fmt"
"net/http"
"regexp"
Expand All @@ -11,6 +10,7 @@ import (
"time"

log "github.com/Sirupsen/logrus"
"github.com/pquerna/ffjson/ffjson"
"github.com/weaveworks/scope/common/exec"
"github.com/weaveworks/scope/common/sanitize"
"github.com/weaveworks/scope/probe/docker"
Expand Down Expand Up @@ -138,7 +138,7 @@ func (w *Weave) Tick() error {
}

var result weaveStatus
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
if err := ffjson.NewDecoder().DecodeReader(resp.Body, &result); err != nil {
return err
}

Expand Down
5 changes: 3 additions & 2 deletions render/detailed/metadata.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package detailed

import (
"encoding/json"
"strconv"
"strings"

"github.com/pquerna/ffjson/ffjson"

"github.com/weaveworks/scope/probe/docker"
"github.com/weaveworks/scope/probe/host"
"github.com/weaveworks/scope/probe/kubernetes"
Expand Down Expand Up @@ -117,7 +118,7 @@ func (m MetadataRow) Copy() MetadataRow {
// MarshalJSON marshals this MetadataRow to json. It adds a label before
// rendering.
func (m MetadataRow) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
return ffjson.Marshal(struct {
ID string `json:"id"`
Label string `json:"label"`
Value string `json:"value"`
Expand Down
5 changes: 3 additions & 2 deletions render/detailed/metrics.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package detailed

import (
"encoding/json"
"math"

"github.com/pquerna/ffjson/ffjson"

"github.com/weaveworks/scope/probe/docker"
"github.com/weaveworks/scope/probe/host"
"github.com/weaveworks/scope/probe/process"
Expand Down Expand Up @@ -62,7 +63,7 @@ func (m MetricRow) Copy() MetricRow {
// MarshalJSON marshals this MetricRow to json. It takes the basic Metric
// rendering, then adds some row-specific fields.
func (m MetricRow) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
return ffjson.Marshal(struct {
ID string `json:"id"`
Label string `json:"label"`
Format string `json:"format,omitempty"`
Expand Down
6 changes: 3 additions & 3 deletions report/controls.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package report

import (
"bytes"
"encoding/json"
"time"

"github.com/pquerna/ffjson/ffjson"
"github.com/weaveworks/scope/common/mtime"
)

Expand Down Expand Up @@ -87,7 +87,7 @@ type WireNodeControls struct {
// MarshalJSON implements json.Marshaller
func (nc NodeControls) MarshalJSON() ([]byte, error) {
buf := bytes.Buffer{}
err := json.NewEncoder(&buf).Encode(WireNodeControls{
err := ffjson.NewEncoder(&buf).Encode(WireNodeControls{
Timestamp: renderTime(nc.Timestamp),
Controls: nc.Controls,
})
Expand All @@ -97,7 +97,7 @@ func (nc NodeControls) MarshalJSON() ([]byte, error) {
// UnmarshalJSON implements json.Unmarshaler
func (nc *NodeControls) UnmarshalJSON(input []byte) error {
in := WireNodeControls{}
if err := json.NewDecoder(bytes.NewBuffer(input)).Decode(&in); err != nil {
if err := ffjson.NewDecoder().Decode(input, &in); err != nil {
return err
}
*nc = NodeControls{
Expand Down
8 changes: 4 additions & 4 deletions report/counters.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package report
import (
"bytes"
"encoding/gob"
"encoding/json"
"fmt"
"reflect"
"sort"

"github.com/mndrix/ps"
"github.com/pquerna/ffjson/ffjson"
)

// Counters is a string->int map.
Expand Down Expand Up @@ -149,15 +149,15 @@ func (c Counters) fromIntermediate(in map[string]int) Counters {
// MarshalJSON implements json.Marshaller
func (c Counters) MarshalJSON() ([]byte, error) {
if c.psMap != nil {
return json.Marshal(c.toIntermediate())
return ffjson.Marshal(c.toIntermediate())
}
return json.Marshal(nil)
return ffjson.Marshal(nil)
}

// UnmarshalJSON implements json.Unmarshaler
func (c *Counters) UnmarshalJSON(input []byte) error {
in := map[string]int{}
if err := json.Unmarshal(input, &in); err != nil {
if err := ffjson.Unmarshal(input, &in); err != nil {
return err
}
*c = Counters{}.fromIntermediate(in)
Expand Down
8 changes: 4 additions & 4 deletions report/edge_metadatas.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package report
import (
"bytes"
"encoding/gob"
"encoding/json"
"fmt"
"reflect"
"sort"

"github.com/mndrix/ps"
"github.com/pquerna/ffjson/ffjson"
)

// EdgeMetadatas collect metadata about each edge in a topology. Keys are the
Expand Down Expand Up @@ -168,15 +168,15 @@ func (c EdgeMetadatas) fromIntermediate(in map[string]EdgeMetadata) EdgeMetadata
// MarshalJSON implements json.Marshaller
func (c EdgeMetadatas) MarshalJSON() ([]byte, error) {
if c.psMap != nil {
return json.Marshal(c.toIntermediate())
return ffjson.Marshal(c.toIntermediate())
}
return json.Marshal(nil)
return ffjson.Marshal(nil)
}

// UnmarshalJSON implements json.Unmarshaler
func (c *EdgeMetadatas) UnmarshalJSON(input []byte) error {
in := map[string]EdgeMetadata{}
if err := json.Unmarshal(input, &in); err != nil {
if err := ffjson.Unmarshal(input, &in); err != nil {
return err
}
*c = EdgeMetadatas{}.fromIntermediate(in)
Expand Down
8 changes: 4 additions & 4 deletions report/latest_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package report
import (
"bytes"
"encoding/gob"
"encoding/json"
"fmt"
"sort"
"time"

"github.com/mndrix/ps"
"github.com/pquerna/ffjson/ffjson"
)

// LatestMap is a persitent map which support latest-win merges. We have to
Expand Down Expand Up @@ -184,17 +184,17 @@ func (m LatestMap) MarshalJSON() ([]byte, error) {
buf := bytes.Buffer{}
var err error
if m.Map != nil {
err = json.NewEncoder(&buf).Encode(m.toIntermediate())
err = ffjson.NewEncoder(&buf).Encode(m.toIntermediate())
} else {
err = json.NewEncoder(&buf).Encode(nil)
err = ffjson.NewEncoder(&buf).Encode(nil)
}
return buf.Bytes(), err
}

// UnmarshalJSON implements json.Unmarshaler
func (m *LatestMap) UnmarshalJSON(input []byte) error {
in := map[string]LatestEntry{}
if err := json.NewDecoder(bytes.NewBuffer(input)).Decode(&in); err != nil {
if err := ffjson.NewDecoder().Decode(input, &in); err != nil {
return err
}
*m = LatestMap{}.fromIntermediate(in)
Expand Down
6 changes: 3 additions & 3 deletions report/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package report
import (
"bytes"
"encoding/gob"
"encoding/json"
"math"
"time"

"github.com/mndrix/ps"
"github.com/pquerna/ffjson/ffjson"
)

// Metrics is a string->metric map.
Expand Down Expand Up @@ -271,14 +271,14 @@ func (m WireMetrics) fromIntermediate() Metric {
func (m Metric) MarshalJSON() ([]byte, error) {
buf := bytes.Buffer{}
in := m.ToIntermediate()
err := json.NewEncoder(&buf).Encode(in)
err := ffjson.NewEncoder(&buf).Encode(in)
return buf.Bytes(), err
}

// UnmarshalJSON implements json.Unmarshaler
func (m *Metric) UnmarshalJSON(input []byte) error {
in := WireMetrics{}
if err := json.NewDecoder(bytes.NewBuffer(input)).Decode(&in); err != nil {
if err := ffjson.NewDecoder().Decode(input, &in); err != nil {
return err
}
*m = in.fromIntermediate()
Expand Down
8 changes: 4 additions & 4 deletions report/node_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package report
import (
"bytes"
"encoding/gob"
"encoding/json"
"fmt"
"reflect"
"sort"

"github.com/mndrix/ps"
"github.com/pquerna/ffjson/ffjson"
)

// NodeSet is a set of nodes keyed on (Topology, ID). Clients must use
Expand Down Expand Up @@ -164,15 +164,15 @@ func (n NodeSet) fromIntermediate(nodes []Node) NodeSet {
// MarshalJSON implements json.Marshaller
func (n NodeSet) MarshalJSON() ([]byte, error) {
if n.psMap != nil {
return json.Marshal(n.toIntermediate())
return ffjson.Marshal(n.toIntermediate())
}
return json.Marshal(nil)
return ffjson.Marshal(nil)
}

// UnmarshalJSON implements json.Unmarshaler
func (n *NodeSet) UnmarshalJSON(input []byte) error {
in := []Node{}
if err := json.Unmarshal(input, &in); err != nil {
if err := ffjson.Unmarshal(input, &in); err != nil {
return err
}
*n = NodeSet{}.fromIntermediate(in)
Expand Down
8 changes: 4 additions & 4 deletions report/sets.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package report
import (
"bytes"
"encoding/gob"
"encoding/json"
"fmt"
"reflect"
"sort"

"github.com/mndrix/ps"
"github.com/pquerna/ffjson/ffjson"
)

// Sets is a string->set-of-strings map.
Expand Down Expand Up @@ -159,15 +159,15 @@ func (s Sets) fromIntermediate(in map[string]StringSet) Sets {
// MarshalJSON implements json.Marshaller
func (s Sets) MarshalJSON() ([]byte, error) {
if s.psMap != nil {
return json.Marshal(s.toIntermediate())
return ffjson.Marshal(s.toIntermediate())
}
return json.Marshal(nil)
return ffjson.Marshal(nil)
}

// UnmarshalJSON implements json.Unmarshaler
func (s *Sets) UnmarshalJSON(input []byte) error {
in := map[string]StringSet{}
if err := json.Unmarshal(input, &in); err != nil {
if err := ffjson.Unmarshal(input, &in); err != nil {
return err
}
*s = Sets{}.fromIntermediate(in)
Expand Down
Loading

0 comments on commit b46d5d1

Please sign in to comment.