github.com/angenalZZZ/gofunc@v0.0.0-20210507121333-48ff1be3917b/f/encoding_json.go (about) 1 package f 2 3 import ( 4 "github.com/angenalZZZ/gofunc/g" 5 json "github.com/json-iterator/go" 6 "github.com/tidwall/gjson" 7 "github.com/tidwall/sjson" 8 "strings" 9 ) 10 11 type Json string 12 13 // NewJson New Json string. 14 func NewJson(s string) Json { 15 if s == "" { 16 return "" 17 } 18 if j := strings.TrimSpace(s); j != "" && len(j) > 1 { 19 return Json(j) 20 } 21 return "" 22 } 23 24 // Parse parses the json and returns a result. 25 // 26 // This function expects that the json is well-formed, and does not validate. 27 // Invalid json will not panic, but it may return back unexpected results. 28 // If you are consuming JSON from an unpredictable source then you may want to 29 // use the Valid function first. 30 func (j Json) Parse() gjson.Result { 31 return gjson.Parse(string(j)) 32 } 33 34 // GetHeader searches json for the specified path. 35 // A path is in dot syntax, such as "name.last" or "age". 36 // When the value is found it's returned immediately. 37 // 38 // A path is a series of keys searated by a dot. 39 // A key may contain special wildcard characters '*' and '?'. 40 // To access an array value use the index as the key. 41 // To get the number of elements in an array or to access a child path, use 42 // the '#' character. 43 // The dot and wildcard character can be escaped with '\'. 44 // 45 // { 46 // "name": {"first": "Tom", "last": "Anderson"}, 47 // "age":37, 48 // "children": ["Sara","Alex","Jack"], 49 // "friends": [ 50 // {"first": "James", "last": "Murphy"}, 51 // {"first": "Roger", "last": "Craig"} 52 // ] 53 // } 54 // "name.last" >> "Anderson" 55 // "age" >> 37 56 // "children" >> ["Sara","Alex","Jack"] 57 // "children.#" >> 3 58 // "children.1" >> "Alex" 59 // "child*.2" >> "Jack" 60 // "c?ildren.0" >> "Sara" 61 // "friends.#.first" >> ["James","Roger"] 62 // 63 // This function expects that the json is well-formed, and does not validate. 64 // Invalid json will not panic, but it may return back unexpected results. 65 // If you are consuming JSON from an unpredictable source then you may want to 66 // use the Valid function first. 67 func (j Json) Get(path string) gjson.Result { 68 return gjson.Get(string(j), path) 69 } 70 71 // GetMany searches json for the multiple paths. 72 // The return value is a Result array where the number of items 73 // will be equal to the number of input paths. 74 func (j Json) GetMany(path ...string) []gjson.Result { 75 return gjson.GetMany(string(j), path...) 76 } 77 78 // SetHeader sets a json value for the specified path. 79 // A path is in dot syntax, such as "name.last" or "age". 80 // This function expects that the json is well-formed, and does not validate. 81 // Invalid json will not panic, but it may return back unexpected results. 82 // An error is returned if the path is not valid. 83 func (j Json) Set(path string, value interface{}) (string, error) { 84 return sjson.Set(string(j), path, value) 85 } 86 87 // Sets return a json value. 88 func (j Json) Sets(path string, value interface{}) Json { 89 if s, err := j.Set(path, value); err == nil { 90 return Json(s) 91 } 92 return j 93 } 94 95 // Delete deletes a value from json for the specified path. 96 func (j Json) Delete(path string) (string, error) { 97 return sjson.Delete(string(j), path) 98 } 99 100 // Deletes return a json value. 101 func (j Json) Deletes(path string) Json { 102 if s, err := sjson.Delete(string(j), path); err == nil { 103 return Json(s) 104 } 105 return j 106 } 107 108 // Map unmarshal to a map. 109 func (j Json) Map() map[string]interface{} { 110 if m, ok := gjson.Parse(string(j)).Value().(map[string]interface{}); ok { 111 return m 112 } 113 return nil 114 } 115 116 // Each to ForEachLine will iterate through JSON lines. 117 func (j Json) Each(iterator func(m map[string]interface{})) { 118 gjson.ForEachLine(string(j), func(line gjson.Result) bool { 119 if m, ok := line.Value().(map[string]interface{}); ok { 120 iterator(m) 121 } 122 return true 123 }) 124 } 125 126 // String gets string from Json. 127 func (j Json) String() string { 128 return string(j) 129 } 130 131 // HasValue gets json not equals empty. 132 func (j Json) HasValue() bool { 133 return j != "" 134 } 135 136 // IsValid Check json string. 137 func (j Json) IsValid() bool { 138 return gjson.Valid(string(j)) 139 } 140 141 // Exists Check for the existence of a value. 142 func (j Json) Exists(path string) bool { 143 return gjson.Get(string(j), path).Exists() 144 } 145 146 // EncodedJson returns json data. 147 func EncodedJson(v interface{}) []byte { 148 if p, err := json.Marshal(v); err != nil { 149 return []byte{} 150 } else { 151 return p 152 } 153 } 154 155 // DecodedJson decode json data to a object v. 156 func DecodedJson(data []byte) interface{} { 157 var v interface{} 158 if err := json.Unmarshal(data, &v); err != nil { 159 return nil 160 } else { 161 return v 162 } 163 } 164 165 // EncodedMap returns map data. 166 func EncodedMap(v interface{}) []byte { 167 if m1, ok := v.(map[string]interface{}); ok { 168 m := make(map[string]interface{}) 169 for k, o := range m1 { 170 if o != nil { 171 m[k] = o 172 } 173 } 174 return EncodedJson(m) 175 } 176 if m2, ok := v.(g.Map); ok { 177 m, m1 := make(map[string]interface{}), map[string]interface{}(m2) 178 for k, o := range m1 { 179 if o != nil { 180 m[k] = o 181 } 182 } 183 return EncodedJson(m) 184 } 185 return EncodedJson(v) 186 } 187 188 // DecodedJson decode map data to a object v. 189 func DecodedMap(data []byte) interface{} { 190 var v interface{} 191 if err := json.Unmarshal(data, &v); err != nil { 192 return nil 193 } else { 194 return v 195 } 196 } 197 198 // EncodeJson encode a object v to json data. 199 func EncodeJson(v interface{}) ([]byte, error) { 200 return json.ConfigCompatibleWithStandardLibrary.Marshal(v) 201 } 202 203 // DecodeJson decode json data to a object v. 204 func DecodeJson(data []byte, v interface{}) error { 205 return json.ConfigCompatibleWithStandardLibrary.Unmarshal(data, v) 206 }