Skip to content

Commit 3942079

Browse files
committed
replaced stdlib encoding/json by jsoniter
Signed-off-by: Frederic BIDON <[email protected]>
1 parent ed00e71 commit 3942079

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+58
-75
lines changed

cache.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ package spec
1616

1717
import (
1818
"sync"
19+
20+
jsoniter "github.com/json-iterator/go"
1921
)
2022

2123
// ResolutionCache a cache for resolving urls
@@ -71,14 +73,19 @@ var (
7173
resCache *simpleCache
7274
onceCache sync.Once
7375

74-
_ ResolutionCache = &simpleCache{}
76+
_ ResolutionCache = &simpleCache{}
77+
json jsoniter.API
7578
)
7679

7780
// initResolutionCache initializes the URI resolution cache. To be wrapped in a sync.Once.Do call.
7881
func initResolutionCache() {
7982
resCache = defaultResolutionCache()
8083
}
8184

85+
func init() {
86+
json = jsoniter.ConfigFastest
87+
}
88+
8289
func defaultResolutionCache() *simpleCache {
8390
return &simpleCache{store: map[string]interface{}{
8491
"http://swagger.io/v2/schema.json": MustLoadSwagger20Schema(),

circular_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package spec
22

33
import (
4-
"encoding/json"
54
"net/http"
65
"net/http/httptest"
76
"os"

contact_info.go

-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
package spec
1616

1717
import (
18-
"encoding/json"
19-
2018
"github.com/go-openapi/swag"
2119
)
2220

contact_info_test.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,17 @@
1515
package spec
1616

1717
import (
18-
"encoding/json"
1918
"testing"
2019

2120
"github.com/stretchr/testify/assert"
2221
"github.com/stretchr/testify/require"
2322
)
2423

2524
const contactInfoJSON = `{
26-
"name": "wordnik api team",
27-
"url": "http://developer.wordnik.com",
28-
"email": "[email protected]",
29-
"x-teams": "test team"
25+
"name": "wordnik api team",
26+
"url": "http://developer.wordnik.com",
27+
"email": "[email protected]",
28+
"x-teams": "test team"
3029
}`
3130

3231
var contactInfo = ContactInfo{ContactInfoProps: ContactInfoProps{
@@ -36,9 +35,9 @@ var contactInfo = ContactInfo{ContactInfoProps: ContactInfoProps{
3635
}, VendorExtensible: VendorExtensible{Extensions: map[string]interface{}{"x-teams": "test team"}}}
3736

3837
func TestIntegrationContactInfo(t *testing.T) {
39-
b, err := json.MarshalIndent(contactInfo, "", "\t")
38+
b, err := json.MarshalIndent(contactInfo, "", " ")
4039
require.NoError(t, err)
41-
assert.Equal(t, contactInfoJSON, string(b))
40+
assert.JSONEq(t, contactInfoJSON, string(b))
4241

4342
actual := ContactInfo{}
4443
err = json.Unmarshal([]byte(contactInfoJSON), &actual)

expander.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
package spec
1616

1717
import (
18-
"encoding/json"
18+
stdjson "encoding/json"
1919
"fmt"
2020
)
2121

@@ -28,11 +28,11 @@ import (
2828
//
2929
// PathLoader injects a document loading method. By default, this resolves to the function provided by the SpecLoader package variable.
3030
type ExpandOptions struct {
31-
RelativeBase string // the path to the root document to expand. This is a file, not a directory
32-
SkipSchemas bool // do not expand schemas, just paths, parameters and responses
33-
ContinueOnError bool // continue expanding even after and error is found
34-
PathLoader func(string) (json.RawMessage, error) `json:"-"` // the document loading method that takes a path as input and yields a json document
35-
AbsoluteCircularRef bool // circular $ref remaining after expansion remain absolute URLs
31+
RelativeBase string // the path to the root document to expand. This is a file, not a directory
32+
SkipSchemas bool // do not expand schemas, just paths, parameters and responses
33+
ContinueOnError bool // continue expanding even after and error is found
34+
PathLoader func(string) (stdjson.RawMessage, error) `json:"-"` // the document loading method that takes a path as input and yields a json document
35+
AbsoluteCircularRef bool // circular $ref remaining after expansion remain absolute URLs
3636
}
3737

3838
func optionsOrDefault(opts *ExpandOptions) *ExpandOptions {

expander_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
package spec
1616

1717
import (
18-
"encoding/json"
18+
stdjson "encoding/json"
1919
"io"
2020
"log"
2121
"net/http"
@@ -38,7 +38,7 @@ const (
3838

3939
var (
4040
// PetStoreJSONMessage json raw message for Petstore20
41-
PetStoreJSONMessage = json.RawMessage([]byte(PetStore20))
41+
PetStoreJSONMessage = stdjson.RawMessage([]byte(PetStore20))
4242
specs = filepath.Join("fixtures", "specs")
4343
)
4444

go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ require (
44
github.com/go-openapi/jsonpointer v0.20.2
55
github.com/go-openapi/jsonreference v0.20.4
66
github.com/go-openapi/swag v0.22.6
7+
github.com/json-iterator/go v1.1.12
78
github.com/stretchr/testify v1.8.4
89
gopkg.in/yaml.v3 v3.0.1
910
)
@@ -12,6 +13,8 @@ require (
1213
github.com/davecgh/go-spew v1.1.1 // indirect
1314
github.com/josharian/intern v1.0.0 // indirect
1415
github.com/mailru/easyjson v0.7.7 // indirect
16+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
17+
github.com/modern-go/reflect2 v1.0.2 // indirect
1518
github.com/pmezard/go-difflib v1.0.0 // indirect
1619
)
1720

go.sum

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
12
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
23
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
34
github.com/go-openapi/jsonpointer v0.20.2 h1:mQc3nmndL8ZBzStEo3JYF8wzmeWffDH4VbXz58sAx6Q=
@@ -6,15 +7,25 @@ github.com/go-openapi/jsonreference v0.20.4 h1:bKlDxQxQJgwpUSgOENiMPzCTBVuc7vTdX
67
github.com/go-openapi/jsonreference v0.20.4/go.mod h1:5pZJyJP2MnYCpoeoMAql78cCHauHj0V9Lhc506VOpw4=
78
github.com/go-openapi/swag v0.22.6 h1:dnqg1XfHXL9aBxSbktBqFR5CxVyVI+7fYWhAf1JOeTw=
89
github.com/go-openapi/swag v0.22.6/go.mod h1:Gl91UqO+btAM0plGGxHqJcQZ1ZTy6jbmridBTsDy8A0=
10+
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
911
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
1012
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
13+
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
14+
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
1115
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
1216
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
1317
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
1418
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
19+
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
20+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
21+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
22+
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
23+
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
1524
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1625
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1726
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
27+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
28+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
1829
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
1930
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
2031
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

header.go

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package spec
1616

1717
import (
18-
"encoding/json"
1918
"strings"
2019

2120
"github.com/go-openapi/jsonpointer"

header_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package spec
1616

1717
import (
18-
"encoding/json"
1918
"testing"
2019

2120
"github.com/go-openapi/swag"

helpers_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package spec
22

33
import (
4-
"encoding/json"
4+
stdjson "encoding/json"
55
"fmt"
66
"regexp"
77
"strings"
@@ -14,12 +14,12 @@ import (
1414

1515
var rex = regexp.MustCompile(`"\$ref":\s*"(.*?)"`)
1616

17-
func jsonDoc(path string) (json.RawMessage, error) {
17+
func jsonDoc(path string) (stdjson.RawMessage, error) {
1818
data, err := swag.LoadFromFileOrHTTP(path)
1919
if err != nil {
2020
return nil, err
2121
}
22-
return json.RawMessage(data), nil
22+
return stdjson.RawMessage(data), nil
2323
}
2424

2525
func docAndOpts(t testing.TB, fixturePath string) ([]byte, *ExpandOptions) {

info.go

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package spec
1616

1717
import (
18-
"encoding/json"
1918
"strconv"
2019
"strings"
2120

info_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package spec
1616

1717
import (
18-
"encoding/json"
1918
"testing"
2019

2120
"github.com/stretchr/testify/assert"
@@ -57,9 +56,9 @@ var info = Info{
5756
}
5857

5958
func TestIntegrationInfo_Serialize(t *testing.T) {
60-
b, err := json.MarshalIndent(info, "", "\t")
59+
b, err := json.MarshalIndent(info, "", " ")
6160
require.NoError(t, err)
62-
assert.Equal(t, infoJSON, string(b))
61+
assert.JSONEq(t, infoJSON, string(b))
6362
}
6463

6564
func TestIntegrationInfo_Deserialize(t *testing.T) {

items.go

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package spec
1616

1717
import (
18-
"encoding/json"
1918
"strings"
2019

2120
"github.com/go-openapi/jsonpointer"

items_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package spec
1616

1717
import (
18-
"encoding/json"
1918
"testing"
2019

2120
"github.com/go-openapi/swag"

license.go

-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
package spec
1616

1717
import (
18-
"encoding/json"
19-
2018
"github.com/go-openapi/swag"
2119
)
2220

license_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package spec
1616

1717
import (
18-
"encoding/json"
1918
"testing"
2019

2120
"github.com/stretchr/testify/assert"
@@ -36,9 +35,9 @@ func TestIntegrationLicense(t *testing.T) {
3635

3736
// const licenseYAML = "name: the name\nurl: the url\n"
3837

39-
b, err := json.MarshalIndent(license, "", "\t")
38+
b, err := json.MarshalIndent(license, "", " ")
4039
require.NoError(t, err)
41-
assert.Equal(t, licenseJSON, string(b))
40+
assert.JSONEq(t, licenseJSON, string(b))
4241

4342
actual := License{}
4443
err = json.Unmarshal([]byte(licenseJSON), &actual)

operation.go

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package spec
1717
import (
1818
"bytes"
1919
"encoding/gob"
20-
"encoding/json"
2120
"sort"
2221

2322
"github.com/go-openapi/jsonpointer"

operation_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package spec
1717
import (
1818
"bytes"
1919
"encoding/gob"
20-
"encoding/json"
2120
"testing"
2221

2322
"github.com/stretchr/testify/assert"

parameter.go

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package spec
1616

1717
import (
18-
"encoding/json"
1918
"strings"
2019

2120
"github.com/go-openapi/jsonpointer"

parameters_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package spec
1616

1717
import (
18-
"encoding/json"
1918
"testing"
2019

2120
"github.com/go-openapi/swag"

path_item.go

-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
package spec
1616

1717
import (
18-
"encoding/json"
19-
2018
"github.com/go-openapi/jsonpointer"
2119
"github.com/go-openapi/swag"
2220
)

path_item_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package spec
1616

1717
import (
18-
"encoding/json"
1918
"testing"
2019

2120
"github.com/stretchr/testify/assert"

paths.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515
package spec
1616

1717
import (
18-
"encoding/json"
1918
"fmt"
2019
"strings"
2120

21+
stdjson "encoding/json"
22+
2223
"github.com/go-openapi/swag"
2324
)
2425

@@ -46,7 +47,7 @@ func (p Paths) JSONLookup(token string) (interface{}, error) {
4647

4748
// UnmarshalJSON hydrates this items instance with the data from JSON
4849
func (p *Paths) UnmarshalJSON(data []byte) error {
49-
var res map[string]json.RawMessage
50+
var res map[string]stdjson.RawMessage
5051
if err := json.Unmarshal(data, &res); err != nil {
5152
return err
5253
}

paths_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package spec
1616

1717
import (
18-
"encoding/json"
1918
"testing"
2019

2120
"github.com/stretchr/testify/assert"

properties.go

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package spec
22

33
import (
44
"bytes"
5-
"encoding/json"
65
"reflect"
76
"sort"
87
)

ref.go

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package spec
1717
import (
1818
"bytes"
1919
"encoding/gob"
20-
"encoding/json"
2120
"net/http"
2221
"os"
2322
"path/filepath"

ref_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package spec
1717
import (
1818
"bytes"
1919
"encoding/gob"
20-
"encoding/json"
2120
"testing"
2221

2322
"github.com/stretchr/testify/assert"

resolver_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package spec
22

33
import (
4-
"encoding/json"
54
"net/http"
65
"net/http/httptest"
76
"os"

0 commit comments

Comments
 (0)