|
@@ -3,68 +3,14 @@
|
|
|
package jsonutil
|
|
|
|
|
|
import (
|
|
|
- "encoding/json"
|
|
|
"github.com/json-iterator/go"
|
|
|
- "reflect"
|
|
|
)
|
|
|
|
|
|
var (
|
|
|
- adapter = jsoniter.ConfigCompatibleWithStandardLibrary
|
|
|
- Marshal = func(v interface{}) ([]byte, error) {
|
|
|
- if nil == v {
|
|
|
- return nil, nil
|
|
|
- }
|
|
|
-
|
|
|
- iKind := reflect.TypeOf(v).Kind()
|
|
|
- ele := reflect.ValueOf(v)
|
|
|
- if iKind == reflect.Ptr {
|
|
|
- ele = ele.Elem()
|
|
|
- }
|
|
|
- if ele.IsZero() {
|
|
|
- return nil, nil
|
|
|
- }
|
|
|
- v = ele.Interface()
|
|
|
- if nil == v {
|
|
|
- return nil, nil
|
|
|
- }
|
|
|
- if m, ok := v.(json.Marshaler); ok {
|
|
|
- if nil == m {
|
|
|
- return nil, nil
|
|
|
- }
|
|
|
- return m.MarshalJSON()
|
|
|
- }
|
|
|
- return adapter.Marshal(v)
|
|
|
- }
|
|
|
- Unmarshal = adapter.Unmarshal
|
|
|
- MarshalToString = func(v interface{}) (string, error) {
|
|
|
- if nil == v {
|
|
|
- return "", nil
|
|
|
- }
|
|
|
-
|
|
|
- iKind := reflect.TypeOf(v).Kind()
|
|
|
- ele := reflect.ValueOf(v)
|
|
|
- if iKind == reflect.Ptr {
|
|
|
- ele = ele.Elem()
|
|
|
- }
|
|
|
- if ele.IsZero() {
|
|
|
- return "", nil
|
|
|
- }
|
|
|
- v = ele.Interface()
|
|
|
- if nil == v {
|
|
|
- return "", nil
|
|
|
- }
|
|
|
- if m, ok := v.(json.Marshaler); ok {
|
|
|
- if nil == m {
|
|
|
- return "", nil
|
|
|
- }
|
|
|
- data, err := m.MarshalJSON()
|
|
|
- if nil != err {
|
|
|
- return "", err
|
|
|
- }
|
|
|
- return string(data), err
|
|
|
- }
|
|
|
- return adapter.MarshalToString(v)
|
|
|
- }
|
|
|
+ adapter = jsoniter.ConfigCompatibleWithStandardLibrary
|
|
|
+ Marshal = adapter.Marshal
|
|
|
+ Unmarshal = adapter.Unmarshal
|
|
|
+ MarshalToString = adapter.MarshalToString
|
|
|
UnmarshalFromString = adapter.UnmarshalFromString
|
|
|
MarshalIndent = adapter.MarshalIndent
|
|
|
NewDecoder = adapter.NewDecoder
|