Skip to content

Commit c13cb3d

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

34 files changed

+36
-60
lines changed

cache.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414

1515
package spec
1616

17-
import "sync"
17+
import (
18+
"sync"
19+
20+
jsoniter "github.com/json-iterator/go"
21+
)
1822

1923
// ResolutionCache a cache for resolving urls
2024
type ResolutionCache interface {
@@ -45,9 +49,13 @@ func (s *simpleCache) Set(uri string, data interface{}) {
4549
s.lock.Unlock()
4650
}
4751

48-
var resCache ResolutionCache
52+
var (
53+
resCache ResolutionCache
54+
json jsoniter.API
55+
)
4956

5057
func init() {
58+
json = jsoniter.ConfigFastest
5159
resCache = initResolutionCache()
5260
}
5361

expander.go

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

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

expander_test.go

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

1717
import (
18-
"encoding/json"
1918
"io/ioutil"
2019
"log"
2120
"net/http"
@@ -27,6 +26,8 @@ import (
2726
"strings"
2827
"testing"
2928

29+
stdjson "encoding/json"
30+
3031
"github.com/go-openapi/jsonpointer"
3132
"github.com/go-openapi/swag"
3233
"github.com/stretchr/testify/assert"
@@ -39,12 +40,12 @@ var (
3940
rex = regexp.MustCompile(`"\$ref":\s*"(.+)"`)
4041
)
4142

42-
func jsonDoc(path string) (json.RawMessage, error) {
43+
func jsonDoc(path string) (stdjson.RawMessage, error) {
4344
data, err := swag.LoadFromFileOrHTTP(path)
4445
if err != nil {
4546
return nil, err
4647
}
47-
return json.RawMessage(data), nil
48+
return stdjson.RawMessage(data), nil
4849
}
4950

5051
// tests that paths are normalized correctly
@@ -1030,8 +1031,9 @@ func TestSchemaExpansion(t *testing.T) {
10301031
}
10311032

10321033
func TestDefaultResolutionCache(t *testing.T) {
1033-
10341034
cache := initResolutionCache()
1035+
jsonSchema := MustLoadJSONSchemaDraft04()
1036+
swaggerSchema := MustLoadSwagger20Schema()
10351037

10361038
sch, ok := cache.Get("not there")
10371039
assert.False(t, ok)
@@ -1760,7 +1762,7 @@ func TestResolveExtraItem(t *testing.T) {
17601762
}
17611763

17621764
// PetStoreJSONMessage json raw message for Petstore20
1763-
var PetStoreJSONMessage = json.RawMessage([]byte(PetStore20))
1765+
var PetStoreJSONMessage = stdjson.RawMessage([]byte(PetStore20))
17641766

17651767
// PetStore20 json doc for swagger 2.0 pet store
17661768
const PetStore20 = `{

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ require (
44
github.com/go-openapi/jsonpointer v0.19.3
55
github.com/go-openapi/jsonreference v0.19.2
66
github.com/go-openapi/swag v0.19.5
7+
github.com/json-iterator/go v1.1.7
78
github.com/stretchr/testify v1.3.0
89
golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297 // indirect
910
gopkg.in/yaml.v2 v2.2.4

go.sum

+7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ github.com/go-openapi/swag v0.19.2 h1:jvO6bCMBEilGwMfHhrd61zIID4oIFdwb76V17SM88d
1515
github.com/go-openapi/swag v0.19.2/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk=
1616
github.com/go-openapi/swag v0.19.5 h1:lTz6Ys4CmqqCQmZPBlbQENR1/GucA2bzYTE12Pw4tFY=
1717
github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk=
18+
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
19+
github.com/json-iterator/go v1.1.7 h1:KfgG9LzI+pYjr4xvmz/5H4FXjokeP+rlHLhv3iH62Fo=
20+
github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
1821
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
1922
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
2023
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
@@ -24,6 +27,10 @@ github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63 h1:nTT4s92Dgz2HlrB
2427
github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
2528
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e h1:hB2xlXdHp/pmPZq0y3QnmWAArdw9PqbmotexnWx/FU8=
2629
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
30+
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
31+
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
32+
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
33+
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
2734
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2835
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
2936
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

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/stretchr/testify/assert"

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
"strings"
2019

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

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"
@@ -55,9 +54,9 @@ var info = Info{
5554
}
5655

5756
func TestIntegrationInfo_Serialize(t *testing.T) {
58-
b, err := json.MarshalIndent(info, "", "\t")
57+
b, err := json.MarshalIndent(info, "", " ")
5958
if assert.NoError(t, err) {
60-
assert.Equal(t, infoJSON, string(b))
59+
assert.JSONEq(t, infoJSON, string(b))
6160
}
6261
}
6362

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"

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/stretchr/testify/assert"

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"

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"

response.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
)

response_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"

responses.go

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

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

schema.go

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

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

schema_loader.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
"log"
2121
"net/url"
@@ -26,15 +26,15 @@ import (
2626
)
2727

2828
// PathLoader function to use when loading remote refs
29-
var PathLoader func(string) (json.RawMessage, error)
29+
var PathLoader func(string) (stdjson.RawMessage, error)
3030

3131
func init() {
32-
PathLoader = func(path string) (json.RawMessage, error) {
32+
PathLoader = func(path string) (stdjson.RawMessage, error) {
3333
data, err := swag.LoadFromFileOrHTTP(path)
3434
if err != nil {
3535
return nil, err
3636
}
37-
return json.RawMessage(data), nil
37+
return stdjson.RawMessage(data), nil
3838
}
3939
}
4040

@@ -62,7 +62,7 @@ type schemaLoader struct {
6262
options *ExpandOptions
6363
cache ResolutionCache
6464
context *resolverContext
65-
loadDoc func(string) (json.RawMessage, error)
65+
loadDoc func(string) (stdjson.RawMessage, error)
6666
}
6767

6868
func (r *schemaLoader) transitiveResolver(basePath string, ref Ref) (*schemaLoader, error) {
@@ -268,7 +268,7 @@ func defaultSchemaLoader(
268268
options: expandOptions,
269269
cache: cache,
270270
context: context,
271-
loadDoc: func(path string) (json.RawMessage, error) {
271+
loadDoc: func(path string) (stdjson.RawMessage, error) {
272272
debugLog("fetching document at %q", path)
273273
return PathLoader(path)
274274
},

schema_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"

security_scheme.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
)

spec.go

-12
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414

1515
package spec
1616

17-
import "encoding/json"
18-
1917
//go:generate curl -L --progress -o ./schemas/v2/schema.json http://swagger.io/v2/schema.json
2018
//go:generate curl -L --progress -o ./schemas/jsonschema-draft-04.json http://json-schema.org/draft-04/schema
2119
//go:generate go-bindata -pkg=spec -prefix=./schemas -ignore=.*\.md ./schemas/...
@@ -28,16 +26,6 @@ const (
2826
JSONSchemaURL = "http://json-schema.org/draft-04/schema#"
2927
)
3028

31-
var (
32-
jsonSchema *Schema
33-
swaggerSchema *Schema
34-
)
35-
36-
func init() {
37-
jsonSchema = MustLoadJSONSchemaDraft04()
38-
swaggerSchema = MustLoadSwagger20Schema()
39-
}
40-
4129
// MustLoadJSONSchemaDraft04 panics when Swagger20Schema returns an error
4230
func MustLoadJSONSchemaDraft04() *Schema {
4331
d, e := JSONSchemaDraft04()

0 commit comments

Comments
 (0)