Files
x-ui/util/json_util/json.go
2021-05-30 22:41:02 +08:00

38 lines
769 B
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package json_util
import (
"encoding/json"
"reflect"
"x-ui/util/reflect_util"
)
/*
MarshalJSON 特殊处理 json.RawMessage
当 json.RawMessage 不为 nil 且 len() 为 0 时MarshalJSON 将会解析报错
*/
func MarshalJSON(i interface{}) ([]byte, error) {
m := map[string]interface{}{}
t := reflect.TypeOf(i).Elem()
v := reflect.ValueOf(i).Elem()
fields := reflect_util.GetFields(t)
for _, field := range fields {
key := field.Tag.Get("json")
if key == "" || key == "-" {
continue
}
fieldV := v.FieldByName(field.Name)
value := fieldV.Interface()
switch value.(type) {
case json.RawMessage:
value := value.(json.RawMessage)
if len(value) > 0 {
m[key] = value
}
default:
m[key] = value
}
}
return json.Marshal(m)
}