Skip to content

Commit

Permalink
refactor: make encoding/json swappable for jsoniter
Browse files Browse the repository at this point in the history
  • Loading branch information
mgjules committed Apr 11, 2022
1 parent dc1757e commit 99924e8
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
4 changes: 2 additions & 2 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package cache

import (
"github.com/JulesMike/spoty/config"
"github.com/JulesMike/spoty/json"
"github.com/dgraph-io/ristretto"
jsoniter "github.com/json-iterator/go"
"go.uber.org/fx"
)

Expand All @@ -26,7 +26,7 @@ func New(cfg *config.Config) (*Cache, error) {
MaxCost: cfg.CacheMaxCost,
BufferItems: _defaultBufferItems,
Cost: func(value interface{}) int64 {
test, err := jsoniter.Marshal(value)
test, err := json.Marshal(value)
if err != nil {
return 1
}
Expand Down
22 changes: 22 additions & 0 deletions json/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//go:build !jsoniter
// +build !jsoniter

package json

import "encoding/json"

var (
// Marshal refers to 'encoding/json.Marshal'.
Marshal = json.Marshal
// Unmarshal refers to 'encoding/json.Unmarshal'.
Unmarshal = json.Unmarshal
// MarshalIndent refers to 'encoding/json.MarshalIndent'.
MarshalIndent = json.MarshalIndent
// NewDecoder refers to 'encoding/json.NewDecoder'.
NewDecoder = json.NewDecoder
// NewEncoder refers to 'encoding/json.NewEncoder'.
NewEncoder = json.NewEncoder
)

// RawMessage refers to 'encoding/json.RawMessage'.
type RawMessage = json.RawMessage
27 changes: 27 additions & 0 deletions json/jsoniter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//go:build jsoniter
// +build jsoniter

package json

import (
stdjson "encoding/json"

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

var (
json = jsoniter.ConfigCompatibleWithStandardLibrary
// Marshal refers to 'github.com/json-iterator/go.Marshal'.
Marshal = json.Marshal
// Unmarshal refers to 'github.com/json-iterator/go.Unmarshal'.
Unmarshal = json.Unmarshal
// MarshalIndent refers to 'github.com/json-iterator/go.MarshalIndent'.
MarshalIndent = json.MarshalIndent
// NewDecoder refers to 'github.com/json-iterator/go.NewDecoder'.
NewDecoder = json.NewDecoder
// NewEncoder refers to 'github.com/json-iterator/go.NewEncoder'.
NewEncoder = json.NewEncoder
)

// RawMessage refers to 'encoding/json.RawMessage'.
type RawMessage = stdjson.RawMessage

0 comments on commit 99924e8

Please sign in to comment.