Browse Source

update jsonutil

marion 4 years ago
parent
commit
d8d46182fb
2 changed files with 9 additions and 81 deletions
  1. 4 58
      utils/jsonutil/jsoniter_adapter.go
  2. 5 23
      utils/jsonutil/official_adapter.go

+ 4 - 58
utils/jsonutil/jsoniter_adapter.go

@@ -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

+ 5 - 23
utils/jsonutil/official_adapter.go

@@ -8,32 +8,14 @@ import (
 )
 
 var (
-	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 json.Marshal(v)
-		}
-	}
+	Marshal         = json.Marshal
 	Unmarshal       = json.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 {
-			data, err := json.Marshal(v)
-			if nil != err {
-				return "", err
-			}
-			return string(data), nil
+		data, err := json.Marshal(v)
+		if nil != err {
+			return "", err
 		}
+		return string(data), nil
 	}
 	UnmarshalFromString = func(str string, v interface{}) error {
 		str = strings.TrimSpace(str)