|
@@ -3,16 +3,37 @@
|
|
|
package jsonutil
|
|
|
|
|
|
import (
|
|
|
+ "encoding/json"
|
|
|
"github.com/json-iterator/go"
|
|
|
)
|
|
|
|
|
|
var (
|
|
|
- json = jsoniter.ConfigCompatibleWithStandardLibrary
|
|
|
- Marshal = json.Marshal
|
|
|
- Unmarshal = json.Unmarshal
|
|
|
- MarshalToString = json.MarshalToString
|
|
|
- UnmarshalFromString = json.UnmarshalFromString
|
|
|
- MarshalIndent = json.MarshalIndent
|
|
|
- NewDecoder = json.NewDecoder
|
|
|
- NewEncoder = json.NewEncoder
|
|
|
+ adapter = jsoniter.ConfigCompatibleWithStandardLibrary
|
|
|
+ Marshal = func(v interface{}) ([]byte, error) {
|
|
|
+ if nil == v {
|
|
|
+ return nil, nil
|
|
|
+ } else if m, ok := v.(json.Marshaler); ok {
|
|
|
+ return m.MarshalJSON()
|
|
|
+ } else {
|
|
|
+ return adapter.Marshal(v)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Unmarshal = adapter.Unmarshal
|
|
|
+ MarshalToString = func(v interface{}) (string, error) {
|
|
|
+ if nil == v {
|
|
|
+ return "", nil
|
|
|
+ } else if m, ok := v.(json.Marshaler); ok {
|
|
|
+ data, err := m.MarshalJSON()
|
|
|
+ if nil != err {
|
|
|
+ return "", err
|
|
|
+ }
|
|
|
+ return string(data), err
|
|
|
+ } else {
|
|
|
+ return adapter.MarshalToString(v)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ UnmarshalFromString = adapter.UnmarshalFromString
|
|
|
+ MarshalIndent = adapter.MarshalIndent
|
|
|
+ NewDecoder = adapter.NewDecoder
|
|
|
+ NewEncoder = adapter.NewEncoder
|
|
|
)
|