Skip to content

Commit

Permalink
json: Use segmentio/encoding/json for unmarshaling
Browse files Browse the repository at this point in the history
For maximum compatibility it's used only for unmarshaling at the
moment. The existing unit tests look good, benchmarks are promising.

name \ time/op     bench_encoding_json.txt  bench_go_json.txt  bench_jsoniter.txt  bench_segmentio.txt
UnmarshalOrders-8               185ms ± 0%          89ms ± 0%          116ms ± 0%            97ms ± 0%

name \ alloc/op    bench_encoding_json.txt  bench_go_json.txt  bench_jsoniter.txt  bench_segmentio.txt
UnmarshalOrders-8              19.1MB ± 0%        30.7MB ± 0%         27.2MB ± 0%          28.3MB ± 0%

name \ allocs/op   bench_encoding_json.txt  bench_go_json.txt  bench_jsoniter.txt  bench_segmentio.txt
UnmarshalOrders-8                388k ± 0%          335k ± 0%           686k ± 0%            332k ± 0%

Using this library for marshaling produces different results from
encoding/json due tue the handling of embedded pointers. See
segmentio/encoding#63
  • Loading branch information
buztard committed Sep 27, 2021
1 parent 1b16fe2 commit 4405f70
Show file tree
Hide file tree
Showing 36 changed files with 476 additions and 410 deletions.
2 changes: 1 addition & 1 deletion code-generate/generate_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ func generateStructUnmarshalJSON(object *RamlType, discriminatedAttributes []*Ra
c = c.Block(
jen.Type().Id("Alias").Id(object.CodeName),
jen.If(
jen.Err().Op(":=").Qual("encoding/json", "Unmarshal").Call(
jen.Err().Op(":=").Qual("github.com/segmentio/encoding/json", "Unmarshal").Call(
jen.Id("data"),
jen.Parens(jen.Op("*").Id("Alias")).Parens(jen.Id("obj")),
),
Expand Down
2 changes: 1 addition & 1 deletion commercetools/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package commercetools
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
Expand All @@ -15,6 +14,7 @@ import (
"github.com/opentracing-contrib/go-stdlib/nethttp"
"github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
"github.com/segmentio/encoding/json"
"golang.org/x/oauth2"
"golang.org/x/oauth2/clientcredentials"
)
Expand Down
2 changes: 1 addition & 1 deletion commercetools/client_graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package commercetools
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"

mapstructure "github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
"github.com/segmentio/encoding/json"
)

type GraphQLQuery struct {
Expand Down
2 changes: 1 addition & 1 deletion commercetools/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestClientGetBadRequestJson(t *testing.T) {
defer server.Close()

_, err := client.ProductGetWithID(context.TODO(), "fake-id")
assert.Equal(t, "invalid character ',' looking for beginning of value", err.Error())
assert.Equal(t, "json: invalid character ',' looking for beginning of value: ,", err.Error())
assert.IsType(t, commercetools.ErrorResponse{}, err)
}

Expand Down
3 changes: 2 additions & 1 deletion commercetools/date.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package commercetools

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

"github.com/segmentio/encoding/json"
)

// Date holds date information for Commercetools API format
Expand Down

0 comments on commit 4405f70

Please sign in to comment.