Skip to content

Commit

Permalink
Use github.com/json-iterator/go instead of encoding/json
Browse files Browse the repository at this point in the history
  • Loading branch information
gagliardetto committed Jul 21, 2021
1 parent 26cb759 commit d2b3d86
Show file tree
Hide file tree
Showing 25 changed files with 134 additions and 87 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
github.com/google/go-cmp v0.5.1
github.com/gorilla/rpc v1.2.0
github.com/gorilla/websocket v1.4.2
github.com/json-iterator/go v1.1.11
github.com/klauspost/compress v1.13.1
github.com/kr/pretty v0.2.1 // indirect
github.com/logrusorgru/aurora v2.0.3+incompatible
Expand Down
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.1 h1:JFrFEBb2xKufg6XkJsJr+WbKb4FQlURi5RUcBveYu9k=
github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
Expand Down Expand Up @@ -177,6 +178,8 @@ github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJS
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/json-iterator/go v1.1.11 h1:uVUAXhF2To8cbw/3xN3pxj6kk7TYKs98NIrTqPlMWAQ=
github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
Expand Down Expand Up @@ -215,7 +218,11 @@ github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0Qu
github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/mr-tron/base58 v1.2.0 h1:T/HDJBh4ZCPbU39/+c3rRvE0uKBQlU27+QI8LJ4t64o=
github.com/mr-tron/base58 v1.2.0/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc=
Expand Down
7 changes: 7 additions & 0 deletions json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package solana

import (
jsoniter "github.com/json-iterator/go"
)

var json = jsoniter.ConfigCompatibleWithStandardLibrary
1 change: 0 additions & 1 deletion nativetype_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package solana

import (
"encoding/json"
"strconv"
"testing"

Expand Down
1 change: 0 additions & 1 deletion nativetypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package solana

import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
Expand Down
1 change: 0 additions & 1 deletion programs/serum/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package serum

import (
"encoding/hex"
"encoding/json"
"io/ioutil"
"os"
"testing"
Expand Down
7 changes: 7 additions & 0 deletions programs/serum/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package serum

import (
jsoniter "github.com/json-iterator/go"
)

var json = jsoniter.ConfigCompatibleWithStandardLibrary
1 change: 0 additions & 1 deletion programs/serum/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package serum
import (
"bytes"
"context"
"encoding/json"
"fmt"

rice "github.com/GeertJohan/go.rice"
Expand Down
1 change: 0 additions & 1 deletion programs/serum/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package serum

import (
"context"
"encoding/json"
"fmt"
"os"
"testing"
Expand Down
1 change: 0 additions & 1 deletion programs/serum/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package serum
import (
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"strings"
Expand Down
7 changes: 7 additions & 0 deletions programs/token/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package token

import (
jsoniter "github.com/json-iterator/go"
)

var json = jsoniter.ConfigCompatibleWithStandardLibrary
1 change: 0 additions & 1 deletion programs/token/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package token
import (
"bytes"
"context"
"encoding/json"
"os"
"testing"

Expand Down
112 changes: 56 additions & 56 deletions rpc/client_test.go

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions rpc/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package rpc

import (
jsoniter "github.com/json-iterator/go"
)

var json = jsoniter.ConfigCompatibleWithStandardLibrary
10 changes: 7 additions & 3 deletions rpc/jsonrpc/jsonrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ package jsonrpc
import (
"bytes"
"context"
"encoding/json"
stdjson "encoding/json"
"errors"
"fmt"
"net/http"
"reflect"
"strconv"

jsoniter "github.com/json-iterator/go"
)

var json = jsoniter.ConfigCompatibleWithStandardLibrary

const (
jsonrpcVersion = "2.0"
)
Expand Down Expand Up @@ -579,7 +583,7 @@ func Params(params ...interface{}) interface{} {
//
// If result was not an integer an error is returned.
func (RPCResponse *RPCResponse) GetInt() (int64, error) {
val, ok := RPCResponse.Result.(json.Number)
val, ok := RPCResponse.Result.(stdjson.Number)
if !ok {
return 0, fmt.Errorf("could not parse int64 from %s", RPCResponse.Result)
}
Expand All @@ -596,7 +600,7 @@ func (RPCResponse *RPCResponse) GetInt() (int64, error) {
//
// If result was not an float64 an error is returned.
func (RPCResponse *RPCResponse) GetFloat() (float64, error) {
val, ok := RPCResponse.Result.(json.Number)
val, ok := RPCResponse.Result.(stdjson.Number)
if !ok {
return 0, fmt.Errorf("could not parse float64 from %s", RPCResponse.Result)
}
Expand Down
4 changes: 2 additions & 2 deletions rpc/rpc_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package rpc

import (
"encoding/json"
stdjson "encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
Expand All @@ -23,7 +23,7 @@ func mockJSONRPC(t *testing.T, response interface{}) (mock *mockJSONRPCServer, c
require.NoError(t, err)

var responseBody []byte
if v, ok := response.(json.RawMessage); ok {
if v, ok := response.(stdjson.RawMessage); ok {
responseBody = v
} else {
responseBody, err = json.Marshal(response)
Expand Down
8 changes: 4 additions & 4 deletions rpc/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package rpc

import (
"encoding/json"
stdjson "encoding/json"
"fmt"
"time"

Expand Down Expand Up @@ -219,7 +219,7 @@ type Account struct {
type DataBytesOrJSON struct {
rawDataEncoding solana.EncodingType
asDecodedBinary solana.Data
asJSON json.RawMessage
asJSON stdjson.RawMessage
}

func (dt DataBytesOrJSON) MarshalJSON() ([]byte, error) {
Expand Down Expand Up @@ -270,9 +270,9 @@ func (dt *DataBytesOrJSON) GetBinary() []byte {
return dt.asDecodedBinary.Content
}

// GetRawJSON returns a json.RawMessage when the data
// GetRawJSON returns a stdjson.RawMessage when the data
// encoding is "jsonParsed".
func (dt *DataBytesOrJSON) GetRawJSON() json.RawMessage {
func (dt *DataBytesOrJSON) GetRawJSON() stdjson.RawMessage {
return dt.asJSON
}

Expand Down
6 changes: 3 additions & 3 deletions rpc/types_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package rpc

import (
"encoding/json"
stdjson "encoding/json"
"testing"

"github.com/gagliardetto/solana-go"
Expand Down Expand Up @@ -65,7 +65,7 @@ func TestData_jsonParsed(t *testing.T) {
assert.NoError(t, err)

assert.Equal(t,
json.RawMessage(in),
stdjson.RawMessage(in),
data.GetRawJSON(),
)
assert.Equal(t,
Expand All @@ -84,7 +84,7 @@ func TestData_jsonParsed_empty(t *testing.T) {
assert.NoError(t, err)

assert.Equal(t,
json.RawMessage(in),
stdjson.RawMessage(in),
data.GetRawJSON(),
)
assert.Equal(t,
Expand Down
3 changes: 1 addition & 2 deletions rpc/ws/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package ws

import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -276,7 +275,7 @@ func (c *Client) subscribe(
return sub, nil
}

func decodeResponse(r io.Reader, reply interface{}) (err error) {
func decodeResponseFromReader(r io.Reader, reply interface{}) (err error) {
var c *response
if err := json.NewDecoder(r).Decode(&c); err != nil {
return err
Expand Down
8 changes: 8 additions & 0 deletions rpc/ws/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package ws

import (
// stdjson "encoding/json"
jsoniter "github.com/json-iterator/go"
)

var json = jsoniter.ConfigCompatibleWithStandardLibrary
12 changes: 6 additions & 6 deletions rpc/ws/types.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ws

import (
"encoding/json"
stdjson "encoding/json"
"fmt"
"math/rand"
)
Expand Down Expand Up @@ -34,12 +34,12 @@ func (c *request) encode() ([]byte, error) {
}

type response struct {
Version string `json:"jsonrpc"`
Params *params `json:"params"`
Error *json.RawMessage `json:"error"`
Version string `json:"jsonrpc"`
Params *params `json:"params"`
Error *stdjson.RawMessage `json:"error"`
}

type params struct {
Result *json.RawMessage `json:"result"`
Subscription int `json:"subscription"`
Result *stdjson.RawMessage `json:"result"`
Subscription int `json:"subscription"`
}
7 changes: 7 additions & 0 deletions vault/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package vault

import (
jsoniter "github.com/json-iterator/go"
)

var json = jsoniter.ConfigCompatibleWithStandardLibrary
1 change: 0 additions & 1 deletion vault/kmsgcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"context"
"crypto/rand"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"sync"
Expand Down
1 change: 0 additions & 1 deletion vault/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package vault

import (
"encoding/json"
"fmt"
"os"

Expand Down
6 changes: 4 additions & 2 deletions zap-box/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package zapbox

import (
"encoding/base64"
"encoding/json"
"math"
"os"
"path"
Expand All @@ -11,6 +10,7 @@ import (
"time"
"unicode/utf8"

jsoniter "github.com/json-iterator/go"
"github.com/logrusorgru/aurora"
. "github.com/logrusorgru/aurora"
"go.uber.org/zap"
Expand All @@ -19,6 +19,8 @@ import (
"golang.org/x/crypto/ssh/terminal"
)

var json = jsoniter.ConfigCompatibleWithStandardLibrary

const (
ansiColorEscape = "\033["
clearANSIModifier = ansiColorEscape + "0m"
Expand Down Expand Up @@ -244,7 +246,7 @@ type jsonEncoder struct {

// for encoding generic values by reflection
reflectBuf *buffer.Buffer
reflectEnc *json.Encoder
reflectEnc *jsoniter.Encoder
}

func newJSONEncoder(cfg zapcore.EncoderConfig, spaced bool) *jsonEncoder {
Expand Down

0 comments on commit d2b3d86

Please sign in to comment.