github.com/gogf/gf/v2@v2.7.4/encoding/gjson/gjson_z_example_conversion_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package gjson_test 8 9 import ( 10 "fmt" 11 12 "github.com/gogf/gf/v2/encoding/gjson" 13 ) 14 15 func ExampleConversionNormalFormats() { 16 data := 17 `{ 18 "users" : { 19 "count" : 1, 20 "array" : ["John", "Ming"] 21 } 22 }` 23 24 if j, err := gjson.DecodeToJson(data); err != nil { 25 panic(err) 26 } else { 27 fmt.Println("JSON:") 28 fmt.Println(j.MustToJsonString()) 29 fmt.Println("======================") 30 31 fmt.Println("XML:") 32 fmt.Println(j.MustToXmlString()) 33 fmt.Println("======================") 34 35 fmt.Println("YAML:") 36 fmt.Println(j.MustToYamlString()) 37 fmt.Println("======================") 38 39 fmt.Println("TOML:") 40 fmt.Println(j.MustToTomlString()) 41 } 42 43 // Output: 44 // JSON: 45 // {"users":{"array":["John","Ming"],"count":1}} 46 // ====================== 47 // XML: 48 // <users><array>John</array><array>Ming</array><count>1</count></users> 49 // ====================== 50 // YAML: 51 // users: 52 // array: 53 // - John 54 // - Ming 55 // count: 1 56 // 57 // ====================== 58 // TOML: 59 // [users] 60 // array = ["John", "Ming"] 61 // count = 1.0 62 } 63 64 func ExampleJson_ConversionGetStruct() { 65 data := 66 `{ 67 "users" : { 68 "count" : 1, 69 "array" : ["John", "Ming"] 70 } 71 }` 72 if j, err := gjson.DecodeToJson(data); err != nil { 73 panic(err) 74 } else { 75 type Users struct { 76 Count int 77 Array []string 78 } 79 users := new(Users) 80 if err := j.Get("users").Scan(users); err != nil { 81 panic(err) 82 } 83 fmt.Printf(`%+v`, users) 84 } 85 86 // Output: 87 // &{Count:1 Array:[John Ming]} 88 } 89 90 func ExampleJson_ConversionToStruct() { 91 data := 92 ` 93 { 94 "count" : 1, 95 "array" : ["John", "Ming"] 96 }` 97 if j, err := gjson.DecodeToJson(data); err != nil { 98 panic(err) 99 } else { 100 type Users struct { 101 Count int 102 Array []string 103 } 104 users := new(Users) 105 if err := j.Var().Scan(users); err != nil { 106 panic(err) 107 } 108 fmt.Printf(`%+v`, users) 109 } 110 111 // Output: 112 // &{Count:1 Array:[John Ming]} 113 } 114 115 func ExampleValid() { 116 data1 := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`) 117 data2 := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]`) 118 fmt.Println(gjson.Valid(data1)) 119 fmt.Println(gjson.Valid(data2)) 120 121 // Output: 122 // true 123 // false 124 } 125 126 func ExampleMarshal() { 127 data := map[string]interface{}{ 128 "name": "john", 129 "score": 100, 130 } 131 132 jsonData, _ := gjson.Marshal(data) 133 fmt.Println(string(jsonData)) 134 135 type BaseInfo struct { 136 Name string 137 Age int 138 } 139 140 info := BaseInfo{ 141 Name: "Guo Qiang", 142 Age: 18, 143 } 144 145 infoData, _ := gjson.Marshal(info) 146 fmt.Println(string(infoData)) 147 148 // Output: 149 // {"name":"john","score":100} 150 // {"Name":"Guo Qiang","Age":18} 151 } 152 153 func ExampleMarshalIndent() { 154 type BaseInfo struct { 155 Name string 156 Age int 157 } 158 159 info := BaseInfo{ 160 Name: "John", 161 Age: 18, 162 } 163 164 infoData, _ := gjson.MarshalIndent(info, "", "\t") 165 fmt.Println(string(infoData)) 166 167 // Output: 168 // { 169 // "Name": "John", 170 // "Age": 18 171 // } 172 } 173 174 func ExampleUnmarshal() { 175 type BaseInfo struct { 176 Name string 177 Score int 178 } 179 180 var info BaseInfo 181 182 jsonContent := "{\"name\":\"john\",\"score\":100}" 183 gjson.Unmarshal([]byte(jsonContent), &info) 184 fmt.Printf("%+v", info) 185 186 // Output: 187 // {Name:john Score:100} 188 } 189 190 func ExampleEncode() { 191 type BaseInfo struct { 192 Name string 193 Age int 194 } 195 196 info := BaseInfo{ 197 Name: "John", 198 Age: 18, 199 } 200 201 infoData, _ := gjson.Encode(info) 202 fmt.Println(string(infoData)) 203 204 // Output: 205 // {"Name":"John","Age":18} 206 } 207 208 func ExampleMustEncode() { 209 type BaseInfo struct { 210 Name string 211 Age int 212 } 213 214 info := BaseInfo{ 215 Name: "John", 216 Age: 18, 217 } 218 219 infoData := gjson.MustEncode(info) 220 fmt.Println(string(infoData)) 221 222 // Output: 223 // {"Name":"John","Age":18} 224 } 225 226 func ExampleEncodeString() { 227 type BaseInfo struct { 228 Name string 229 Age int 230 } 231 232 info := BaseInfo{ 233 Name: "John", 234 Age: 18, 235 } 236 237 infoData, _ := gjson.EncodeString(info) 238 fmt.Println(infoData) 239 240 // Output: 241 // {"Name":"John","Age":18} 242 } 243 244 func ExampleMustEncodeString() { 245 type BaseInfo struct { 246 Name string 247 Age int 248 } 249 250 info := BaseInfo{ 251 Name: "John", 252 Age: 18, 253 } 254 255 infoData := gjson.MustEncodeString(info) 256 fmt.Println(infoData) 257 258 // Output: 259 // {"Name":"John","Age":18} 260 } 261 262 func ExampleDecode() { 263 jsonContent := `{"name":"john","score":100}` 264 info, _ := gjson.Decode([]byte(jsonContent)) 265 fmt.Println(info) 266 267 // Output: 268 // map[name:john score:100] 269 } 270 271 func ExampleDecodeTo() { 272 type BaseInfo struct { 273 Name string 274 Score int 275 } 276 277 var info BaseInfo 278 279 jsonContent := "{\"name\":\"john\",\"score\":100}" 280 gjson.DecodeTo([]byte(jsonContent), &info) 281 fmt.Printf("%+v", info) 282 283 // Output: 284 // {Name:john Score:100} 285 } 286 287 func ExampleDecodeToJson() { 288 jsonContent := `{"name":"john","score":100}"` 289 j, _ := gjson.DecodeToJson([]byte(jsonContent)) 290 fmt.Println(j.Map()) 291 292 // May Output: 293 // map[name:john score:100] 294 }