github.com/iEvan-lhr/exciting-tool@v0.0.0-20230504054234-8e983f73cdd2/json.go (about) 1 package tools 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "reflect" 7 ) 8 9 // TransHtmlJson 公共方法 处理Go原生json不会替换html字符的问题 10 func transHtmlJson(data []byte) []byte { 11 data = bytes.Replace(data, []byte("\\u0026"), []byte("&"), -1) 12 data = bytes.Replace(data, []byte("\\u003c"), []byte("<"), -1) 13 data = bytes.Replace(data, []byte("\\u003e"), []byte(">"), -1) 14 return data 15 } 16 17 // Unmarshal 公共方法 解析数据至空模板 18 func Unmarshal(v interface{}, str interface{}) { 19 var s []byte 20 switch v.(type) { 21 case string: 22 s = transHtmlJson([]byte(v.(string))) 23 case []byte: 24 s = transHtmlJson(v.([]byte)) 25 default: 26 s = transHtmlJson(ReturnValueByTwo(json.Marshal(v)).([]byte)) 27 } 28 switch str.(type) { 29 case string: 30 reflect.ValueOf(str).Elem().Set(reflect.ValueOf(string(s))) 31 case reflect.Value: 32 33 default: 34 ExecError(json.Unmarshal(s, &str)) 35 } 36 } 37 38 func UnmarshalByOriginal(v interface{}, str interface{}) { 39 switch v.(type) { 40 case string: 41 ExecError(json.Unmarshal([]byte(v.(string)), &str)) 42 case []byte: 43 ExecError(json.Unmarshal(v.([]byte), &str)) 44 default: 45 ExecError(json.Unmarshal(ReturnValueByTwo(json.Marshal(v)).([]byte), &str)) 46 } 47 } 48 49 func UMarshal(v, str interface{}) { 50 var marshal []byte 51 var m map[string]any 52 switch v.(type) { 53 case []byte: 54 marshal = v.([]byte) 55 eatError(json.Unmarshal(marshal, &m)) 56 case string: 57 marshal = []byte(v.(string)) 58 eatError(json.Unmarshal(marshal, &m)) 59 default: 60 marshal, m = marshalMap(v) 61 eatError(json.Unmarshal(marshal, &m)) 62 } 63 eatError(json.Unmarshal(marshal, str)) 64 values, typ := returnValAndTyp(str) 65 if typ.Kind() == reflect.Map { 66 values.Set(reflect.ValueOf(m)) 67 return 68 } 69 for j := 0; j < typ.NumField(); j++ { 70 switch values.Field(j).Interface().(type) { 71 case *String: 72 flo := m[typ.Field(j).Tag.Get("json")].(float64) 73 if float64(int(flo)) == flo { 74 values.Field(j).Set(reflect.ValueOf(Make(int(flo)))) 75 } else { 76 values.Field(j).Set(reflect.ValueOf(Make(flo))) 77 } 78 } 79 } 80 return 81 } 82 83 func Marshal(v interface{}) []byte { 84 values, typ := returnValAndTyp(v) 85 m := make(map[string]string) 86 for j := 0; j < typ.NumField(); j++ { 87 if !values.Field(j).IsZero() { 88 m[typ.Field(j).Tag.Get("json")] = Make(values.Field(j).Interface()).string() 89 } 90 } 91 return ReturnValue(json.Marshal(m)).([]byte) 92 } 93 94 func marshalMap(v interface{}) ([]byte, map[string]any) { 95 values, typ := returnValAndTyp(v) 96 m := make(map[string]any) 97 for j := 0; j < typ.NumField(); j++ { 98 if values.Field(j).Kind() != reflect.Slice && values.Field(j).Kind() != reflect.Map && !values.Field(j).IsZero() { 99 switch values.Field(j).Interface().(type) { 100 case *String: 101 m[typ.Field(j).Tag.Get("json")] = Make(values.Field(j).Interface()) 102 default: 103 m[typ.Field(j).Tag.Get("json")] = values.Field(j).Interface() 104 } 105 106 } 107 } 108 return ReturnValue(json.Marshal(m)).([]byte), m 109 }