gitee.com/sy_183/go-common@v1.0.5-0.20231205030221-958cfe129b47/config.v2/json_test.go (about) 1 package config 2 3 import ( 4 "fmt" 5 "gitee.com/sy_183/go-common/assert" 6 "strings" 7 "testing" 8 ) 9 10 func TestJsonParser_Parse(t *testing.T) { 11 j := `{ 12 "string": "string value", 13 "int": 1234, 14 "float": 3.14, 15 "bool": true, 16 17 "strings": ["string1", "string2", "string3"], 18 "ints": [1, 2, 3, 4], 19 "floats": [0.03, 4.5, 6.31, 9.83, 4.15, 10.0], 20 21 "stringMap": { 22 "key1": "value1", 23 "key2": "value2", 24 "key3": "value3" 25 }, 26 "anyMap": { 27 "key1": 5467, 28 "key2": "value2", 29 "key3": ["test", 798, false, null], 30 "key4": { 31 "subKey1": "subValue1", 32 "subKey2": 1234 33 } 34 }, 35 36 "null": null 37 }` 38 node := assert.Must(JsonParser{}.Parse(strings.NewReader(j))) 39 js := struct { 40 String string `yaml:"string"` 41 Int int `yaml:"int"` 42 Float string `yaml:"float"` 43 Bool string `yaml:"bool"` 44 45 Strings []string `yaml:"strings"` 46 Ints []int `yaml:"ints"` 47 Floats []float64 `yaml:"floats"` 48 49 StringMap map[string]string `yaml:"stringMap"` 50 AnyMap map[string]any `yaml:"anyMap"` 51 52 Null any `yaml:"null"` 53 }{} 54 assert.MustSuccess(node.Decode(&js)) 55 fmt.Println(js) 56 }