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

Lines changed: 10 additions & 2 deletions
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 7 additions & 5 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 7 additions & 0 deletions
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 2 additions & 3 deletions
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

Lines changed: 0 additions & 1 deletion
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"

0 commit comments

Comments
 (0)