github.com/gogf/gf@v1.16.9/encoding/gjson/gjson_z_unit_load_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 "testing" 11 12 "github.com/gogf/gf/encoding/gjson" 13 "github.com/gogf/gf/frame/g" 14 "github.com/gogf/gf/os/gfile" 15 "github.com/gogf/gf/test/gtest" 16 ) 17 18 func Test_Load_JSON1(t *testing.T) { 19 data := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`) 20 // JSON 21 gtest.C(t, func(t *gtest.T) { 22 j, err := gjson.LoadContent(data) 23 t.Assert(err, nil) 24 t.Assert(j.Get("n"), "123456789") 25 t.Assert(j.Get("m"), g.Map{"k": "v"}) 26 t.Assert(j.Get("m.k"), "v") 27 t.Assert(j.Get("a"), g.Slice{1, 2, 3}) 28 t.Assert(j.Get("a.1"), 2) 29 }) 30 // JSON 31 gtest.C(t, func(t *gtest.T) { 32 path := "test.json" 33 gfile.PutBytes(path, data) 34 defer gfile.Remove(path) 35 j, err := gjson.Load(path) 36 t.Assert(err, nil) 37 t.Assert(j.Get("n"), "123456789") 38 t.Assert(j.Get("m"), g.Map{"k": "v"}) 39 t.Assert(j.Get("m.k"), "v") 40 t.Assert(j.Get("a"), g.Slice{1, 2, 3}) 41 t.Assert(j.Get("a.1"), 2) 42 }) 43 } 44 45 func Test_Load_JSON2(t *testing.T) { 46 data := []byte(`{"n":123456789000000000000, "m":{"k":"v"}, "a":[1,2,3]}`) 47 gtest.C(t, func(t *gtest.T) { 48 j, err := gjson.LoadContent(data) 49 t.Assert(err, nil) 50 t.Assert(j.Get("n"), "123456789000000000000") 51 t.Assert(j.Get("m"), g.Map{"k": "v"}) 52 t.Assert(j.Get("m.k"), "v") 53 t.Assert(j.Get("a"), g.Slice{1, 2, 3}) 54 t.Assert(j.Get("a.1"), 2) 55 }) 56 } 57 58 func Test_Load_XML(t *testing.T) { 59 data := []byte(`<doc><a>1</a><a>2</a><a>3</a><m><k>v</k></m><n>123456789</n></doc>`) 60 // XML 61 gtest.C(t, func(t *gtest.T) { 62 j, err := gjson.LoadContent(data) 63 t.Assert(err, nil) 64 t.Assert(j.Get("doc.n"), "123456789") 65 t.Assert(j.Get("doc.m"), g.Map{"k": "v"}) 66 t.Assert(j.Get("doc.m.k"), "v") 67 t.Assert(j.Get("doc.a"), g.Slice{"1", "2", "3"}) 68 t.Assert(j.Get("doc.a.1"), 2) 69 }) 70 // XML 71 gtest.C(t, func(t *gtest.T) { 72 path := "test.xml" 73 gfile.PutBytes(path, data) 74 defer gfile.Remove(path) 75 j, err := gjson.Load(path) 76 t.Assert(err, nil) 77 t.Assert(j.Get("doc.n"), "123456789") 78 t.Assert(j.Get("doc.m"), g.Map{"k": "v"}) 79 t.Assert(j.Get("doc.m.k"), "v") 80 t.Assert(j.Get("doc.a"), g.Slice{"1", "2", "3"}) 81 t.Assert(j.Get("doc.a.1"), 2) 82 }) 83 84 // XML 85 gtest.C(t, func(t *gtest.T) { 86 xml := `<?xml version="1.0"?> 87 88 <Output type="o"> 89 <itotalSize>0</itotalSize> 90 <ipageSize>1</ipageSize> 91 <ipageIndex>2</ipageIndex> 92 <itotalRecords>GF框架</itotalRecords> 93 <nworkOrderDtos/> 94 <nworkOrderFrontXML/> 95 </Output>` 96 j, err := gjson.LoadContent(xml) 97 t.Assert(err, nil) 98 t.Assert(j.Get("Output.ipageIndex"), "2") 99 t.Assert(j.Get("Output.itotalRecords"), "GF框架") 100 }) 101 } 102 103 func Test_Load_YAML1(t *testing.T) { 104 data := []byte(` 105 a: 106 - 1 107 - 2 108 - 3 109 m: 110 k: v 111 "n": 123456789 112 `) 113 // YAML 114 gtest.C(t, func(t *gtest.T) { 115 j, err := gjson.LoadContent(data) 116 t.Assert(err, nil) 117 t.Assert(j.Get("n"), "123456789") 118 t.Assert(j.Get("m"), g.Map{"k": "v"}) 119 t.Assert(j.Get("m.k"), "v") 120 t.Assert(j.Get("a"), g.Slice{1, 2, 3}) 121 t.Assert(j.Get("a.1"), 2) 122 }) 123 // YAML 124 gtest.C(t, func(t *gtest.T) { 125 path := "test.yaml" 126 gfile.PutBytes(path, data) 127 defer gfile.Remove(path) 128 j, err := gjson.Load(path) 129 t.Assert(err, nil) 130 t.Assert(j.Get("n"), "123456789") 131 t.Assert(j.Get("m"), g.Map{"k": "v"}) 132 t.Assert(j.Get("m.k"), "v") 133 t.Assert(j.Get("a"), g.Slice{1, 2, 3}) 134 t.Assert(j.Get("a.1"), 2) 135 }) 136 } 137 138 func Test_Load_YAML2(t *testing.T) { 139 data := []byte("i : 123456789") 140 gtest.C(t, func(t *gtest.T) { 141 j, err := gjson.LoadContent(data) 142 t.Assert(err, nil) 143 t.Assert(j.Get("i"), "123456789") 144 }) 145 } 146 147 func Test_Load_TOML1(t *testing.T) { 148 data := []byte(` 149 a = ["1", "2", "3"] 150 n = 123456789 151 152 [m] 153 k = "v" 154 `) 155 // TOML 156 gtest.C(t, func(t *gtest.T) { 157 j, err := gjson.LoadContent(data) 158 t.Assert(err, nil) 159 t.Assert(j.Get("n"), "123456789") 160 t.Assert(j.Get("m"), g.Map{"k": "v"}) 161 t.Assert(j.Get("m.k"), "v") 162 t.Assert(j.Get("a"), g.Slice{"1", "2", "3"}) 163 t.Assert(j.Get("a.1"), 2) 164 }) 165 // TOML 166 gtest.C(t, func(t *gtest.T) { 167 path := "test.toml" 168 gfile.PutBytes(path, data) 169 defer gfile.Remove(path) 170 j, err := gjson.Load(path) 171 t.Assert(err, nil) 172 t.Assert(j.Get("n"), "123456789") 173 t.Assert(j.Get("m"), g.Map{"k": "v"}) 174 t.Assert(j.Get("m.k"), "v") 175 t.Assert(j.Get("a"), g.Slice{"1", "2", "3"}) 176 t.Assert(j.Get("a.1"), 2) 177 }) 178 } 179 180 func Test_Load_TOML2(t *testing.T) { 181 data := []byte("i=123456789") 182 gtest.C(t, func(t *gtest.T) { 183 j, err := gjson.LoadContent(data) 184 t.Assert(err, nil) 185 t.Assert(j.Get("i"), "123456789") 186 }) 187 } 188 189 func Test_Load_Basic(t *testing.T) { 190 gtest.C(t, func(t *gtest.T) { 191 j := gjson.New(nil) 192 t.Assert(j.Value(), nil) 193 _, err := gjson.Decode(nil) 194 t.AssertNE(err, nil) 195 _, err = gjson.DecodeToJson(nil) 196 t.AssertNE(err, nil) 197 j, err = gjson.LoadContent(nil) 198 t.Assert(err, nil) 199 t.Assert(j.Value(), nil) 200 201 j, err = gjson.LoadContent(`{"name": "gf"}`) 202 t.Assert(err, nil) 203 204 j, err = gjson.LoadContent(`{"name": "gf"""}`) 205 t.AssertNE(err, nil) 206 207 j = gjson.New(&g.Map{"name": "gf"}) 208 t.Assert(j.GetString("name"), "gf") 209 210 }) 211 } 212 213 func Test_Load_Ini(t *testing.T) { 214 var data = ` 215 216 ;注释 217 218 [addr] 219 ip = 127.0.0.1 220 port=9001 221 enable=true 222 223 [DBINFO] 224 type=mysql 225 user=root 226 password=password 227 228 ` 229 230 gtest.C(t, func(t *gtest.T) { 231 json, err := gjson.LoadContent(data) 232 if err != nil { 233 gtest.Fatal(err) 234 } 235 236 t.Assert(json.GetString("addr.ip"), "127.0.0.1") 237 t.Assert(json.GetString("addr.port"), "9001") 238 t.Assert(json.GetString("addr.enable"), "true") 239 t.Assert(json.GetString("DBINFO.type"), "mysql") 240 t.Assert(json.GetString("DBINFO.user"), "root") 241 t.Assert(json.GetString("DBINFO.password"), "password") 242 243 _, err = json.ToIni() 244 if err != nil { 245 gtest.Fatal(err) 246 } 247 }) 248 }