Fix: 修复 pkg/json 单元测试, 移除 go-json

This commit is contained in:
杨红岩 2024-01-03 20:06:17 +08:00
parent 3a66f948ae
commit c331b2f704
7 changed files with 181 additions and 45 deletions

View File

@ -11,8 +11,8 @@
package argument
import (
goJSON "encoding/json"
"github.com/energye/energy/v2/pkgs/json"
jsoniter "github.com/json-iterator/go"
"reflect"
)
@ -46,7 +46,7 @@ func UnList(data []byte) IList {
return nil
}
var v = &List{}
if err := jsoniter.Unmarshal(data, v); err == nil {
if err := goJSON.Unmarshal(data, v); err == nil {
return v
}
return nil
@ -120,7 +120,7 @@ func (m *List) Bytes() []byte {
if m.bytesData != nil {
return m.bytesData
}
if byt, err := jsoniter.Marshal(m); err == nil {
if byt, err := goJSON.Marshal(m); err == nil {
m.bytesData = byt
return byt
}

View File

@ -8,7 +8,7 @@ func TestList(t *testing.T) {
list := &List{
Data: make([]string, 0),
}
json := list.JSON()
json := list.JSON().JSONArray()
json.Add("value1")
json.Add("value2")
json.Add("value3")

View File

@ -11,9 +11,9 @@
package callback
import (
goJSON "encoding/json"
"github.com/energye/energy/v2/cef/ipc/context"
"github.com/energye/energy/v2/pkgs/json"
jsoniter "github.com/json-iterator/go"
"reflect"
)
@ -160,7 +160,7 @@ func (m *ArgumentCallback) Invoke(context context.IContext) {
// struct
if jsonBytes := argsValue.Bytes(); jsonBytes != nil {
v := reflect.New(inType)
if err := jsoniter.Unmarshal(jsonBytes, v.Interface()); err == nil {
if err := goJSON.Unmarshal(jsonBytes, v.Interface()); err == nil {
inArgsValues[i] = v.Elem()
}
}
@ -171,7 +171,7 @@ func (m *ArgumentCallback) Invoke(context context.IContext) {
if inType.Elem().Kind() != reflect.Interface {
if jsonBytes := argsValue.Bytes(); jsonBytes != nil {
vv := reflect.New(inType)
if err := jsoniter.Unmarshal(jsonBytes, vv.Interface()); err == nil {
if err := goJSON.Unmarshal(jsonBytes, vv.Interface()); err == nil {
inArgsValues[i] = vv.Elem()
}
}
@ -185,7 +185,7 @@ func (m *ArgumentCallback) Invoke(context context.IContext) {
if inType.Elem().Kind() != reflect.Interface {
if jsonBytes := argsValue.Bytes(); jsonBytes != nil {
vv := reflect.New(inType)
if err := jsoniter.Unmarshal(jsonBytes, vv.Interface()); err == nil {
if err := goJSON.Unmarshal(jsonBytes, vv.Interface()); err == nil {
inArgsValues[i] = vv.Elem()
}
}

View File

@ -15,13 +15,13 @@
package cef
import (
goJSON "encoding/json"
"errors"
"github.com/energye/energy/v2/cef/internal/def"
"github.com/energye/energy/v2/common/imports"
"github.com/energye/energy/v2/consts"
"github.com/energye/energy/v2/pkgs/json"
"github.com/energye/golcl/lcl/api"
jsoniter "github.com/json-iterator/go"
"reflect"
"unsafe"
)
@ -328,7 +328,7 @@ func (m *v8ValueProcessMessageConvert) V8ValueToProcessMessageBytes(v8value *ICe
return nil
}
if v := m.V8ValueToProcessMessageArray(v8value); v != nil {
if v, err := jsoniter.Marshal(v); err == nil {
if v, err := goJSON.Marshal(v); err == nil {
return v
}
}

4
go.mod
View File

@ -3,7 +3,7 @@ module github.com/energye/energy/v2
go 1.16
require (
github.com/energye/golcl v1.0.7
github.com/energye/golcl v1.0.8
github.com/energye/liblclbinres/v2 v2.3.5
)
@ -12,8 +12,6 @@ require (
atomicgo.dev/keyboard v0.2.9
github.com/godbus/dbus/v5 v5.1.0
github.com/jessevdk/go-flags v1.5.0
github.com/json-iterator/go v1.1.12
github.com/kenshaw/snaker v0.2.0 // indirect
github.com/lithammer/fuzzysearch v1.1.8
github.com/mattn/go-pointer v0.0.1
github.com/pterm/pterm v0.12.66

34
go.sum
View File

@ -8,7 +8,6 @@ atomicgo.dev/keyboard v0.2.9 h1:tOsIid3nlPLZ3lwgG8KZMp/SFmr7P0ssEN5JUsm78K8=
atomicgo.dev/keyboard v0.2.9/go.mod h1:BC4w9g00XkxH/f1HXhW2sXmJFOCWbKn9xrOunSFtExQ=
atomicgo.dev/schedule v0.1.0 h1:nTthAbhZS5YZmgYbb2+DH8uQIZcTlIrd4eYr3UQxEjs=
atomicgo.dev/schedule v0.1.0/go.mod h1:xeUa3oAkiuHYh8bKiQBRojqAMq3PXXbJujjb0hw8pEU=
github.com/Knetic/govaluate v3.0.0+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
github.com/MarvinJWendt/testza v0.1.0/go.mod h1:7AxNvlfeHP7Z/hDQ5JtE3OKYT3XFUeLCDE2DQninSqs=
github.com/MarvinJWendt/testza v0.2.1/go.mod h1:God7bhG8n6uQxwdScay+gjm9/LnO4D3kkcZX4hv9Rp8=
github.com/MarvinJWendt/testza v0.2.8/go.mod h1:nwIcjmr0Zz+Rcwfh3/4UhBp7ePKVhuBExvZqnKYWlII=
@ -20,29 +19,19 @@ github.com/MarvinJWendt/testza v0.4.3/go.mod h1:CpXaOfceNEYnLDtNIyTrPPcCpDJYqzZn
github.com/MarvinJWendt/testza v0.5.1/go.mod h1:L7csM8IBqCc0HH4TRYZSPCIRg6zJeqzM1pm3FSYZBso=
github.com/MarvinJWendt/testza v0.5.2 h1:53KDo64C1z/h/d/stCYCPY69bt/OSwjq5KpFNwi+zB4=
github.com/MarvinJWendt/testza v0.5.2/go.mod h1:xu53QFE5sCdjtMCKk8YMQ2MnymimEctc4n3EjyIYvEY=
github.com/ajstarks/svgo v0.0.0-20181006003313-6ce6a3bcf6cd/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw=
github.com/atomicgo/cursor v0.0.1/go.mod h1:cBON2QmmrysudxNBFthvMtN32r3jxVRIvzkUiF/RuIk=
github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw=
github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/disintegration/gift v1.2.1/go.mod h1:Jh2i7f7Q2BM7Ezno3PhfezbR1xpUg9dUg3/RlKGr4HI=
github.com/energye/dylib v0.0.0-20230814115552-32dda0972e9e/go.mod h1:N8GS4d3Ix83Cff6mLHb+09iiPg9huBB4XETDfuMN5LM=
github.com/energye/golcl v1.0.7 h1:m0vlhMEn2I36v/uUaU2uTG8oFXg/aQkWVg3AL3mWMn0=
github.com/energye/golcl v1.0.7/go.mod h1:GaCETyoCuMqjEJlWUSc60t0OunboH21OIqm1G4LoEvQ=
github.com/energye/golcl v1.0.8 h1:C9rjAcIxrN8PnDxKxQErItQLcIc6gyYXwqwjRx/Kwf8=
github.com/energye/golcl v1.0.8/go.mod h1:0wCrDx7NnHT5+gylUkKi1a9DKYewmMWZQf7xYyZuq9Y=
github.com/energye/liblclbinres/v2 v2.3.5 h1:kX8im2qQKQVzd2zT91CoxroceidGcttB0uzW7rsii7g=
github.com/energye/liblclbinres/v2 v2.3.5/go.mod h1:y17NgjdZYfiQ1hlBdNHyZ6Tq9KbWTuP+o2/pmBvINm4=
github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
github.com/go-gl/gl v0.0.0-20180407155706-68e253793080/go.mod h1:482civXOzJJCPzJ4ZOX/pwvXBWSnzD4OKMdH4ClKGbk=
github.com/go-gl/glfw v0.0.0-20180426074136-46a8d530c326/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk=
github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/gomodule/redigo v2.0.0+incompatible/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/gookit/color v1.4.2/go.mod h1:fqRyamkC1W8uxl+lxCQxOT09l/vYfZ+QeiX3rKQHCoQ=
github.com/gookit/color v1.5.0/go.mod h1:43aQb+Zerm/BWh2GnrgOQm7ffz7tvQXEKV6BFMl7wAo=
github.com/gookit/color v1.5.2/go.mod h1:w8h4bGiHeeBpvQVePTutdbERIUf3oJE5lZ8HM0UgXyg=
@ -50,11 +39,6 @@ github.com/gookit/color v1.5.4 h1:FZmqs7XOyGgCAxmWyPslpiok1k05wmY3SJTytgvYFs0=
github.com/gookit/color v1.5.4/go.mod h1:pZJOeOS8DM43rXbp4AZo1n9zCU2qjpcRko0b6/QJi9w=
github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LFvc=
github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/jung-kurt/gofpdf v1.0.0/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes=
github.com/kenshaw/snaker v0.2.0 h1:DPlxCtAv9mw1wSsvIN1khUAPJUIbFJUckMIDWSQ7TC8=
github.com/kenshaw/snaker v0.2.0/go.mod h1:DNyRUqHMZ18/zioxr6R7m4kSxxf2+QmB0BXoORsXRaY=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.0.10/go.mod h1:g2LTdtYhdyuGPqyWyv7qRAmj1WBqxuObKfj5c0PQa7c=
github.com/klauspost/cpuid/v2 v2.0.12/go.mod h1:g2LTdtYhdyuGPqyWyv7qRAmj1WBqxuObKfj5c0PQa7c=
@ -68,18 +52,12 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/lithammer/fuzzysearch v1.1.5/go.mod h1:1R1LRNk7yKid1BaQkmuLQaHruxcC4HmAH30Dh61Ih1Q=
github.com/lithammer/fuzzysearch v1.1.8 h1:/HIuJnjHuXS8bKaiTMeeDlW2/AyIWk2brx1V8LFgLN4=
github.com/lithammer/fuzzysearch v1.1.8/go.mod h1:IdqeyBClc3FFqSzYq/MXESsS4S0FsZ5ajtkr5xPLts4=
github.com/llgcode/draw2d v0.0.0-20180825133448-f52c8a71aff0/go.mod h1:mVa0dA29Db2S4LVqDYLlsePDzRJLDfdhVZiI15uY0FA=
github.com/llgcode/ps v0.0.0-20150911083025-f1443b32eedb/go.mod h1:1l8ky+Ew27CMX29uG+a2hNOKpeNYEQjjtiALiBlFQbY=
github.com/mattn/go-pointer v0.0.1 h1:n+XhsuGeVO6MEAp7xyEukFINEa+Quek5psIR/ylA6o0=
github.com/mattn/go-pointer v0.0.1/go.mod h1:2zXcozF6qYGgmsG+SeTZz3oAbFLdD3OWqnUbNvJZAlc=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@ -103,7 +81,6 @@ github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNX
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
@ -115,9 +92,6 @@ github.com/tc-hib/winres v0.2.0 h1:gly/ivDWGvlhl7ENtEmA7wPQ6dWab1LlLq/DgcZECKE=
github.com/tc-hib/winres v0.2.0/go.mod h1:uG6S5M2Q0/kThoqsCSYvGJODUQP9O9R0SNxUPmFIegw=
github.com/tevino/abool v0.0.0-20220530134649-2bfc934cb23c h1:coVla7zpsycc+kA9NXpcvv2E4I7+ii6L5hZO2S6C3kw=
github.com/tevino/abool v0.0.0-20220530134649-2bfc934cb23c/go.mod h1:qc66Pna1RiIsPa7O4Egxxs9OqkuxDX55zznh9K07Tzg=
github.com/tryor/gdiplus v0.0.0-20200830101413-c570de9579b3/go.mod h1:TrDGUugAfbUWjSKyYBuUWpjK/A7pwedAB8x+cZWobzs=
github.com/tryor/winapi v0.0.0-20200525040926-cd87d62e2f9b/go.mod h1:NUZCLXgdFgm/L5ovQjuZxttpvHVGcRL+9lZ8N/KeBls=
github.com/vdobler/chart v1.0.0/go.mod h1:gRwLtqIJLDw1CkK9kxJXv3X9OaMfM4dYsbZtWtVLxvM=
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
@ -126,10 +100,7 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E=
golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE=
golang.org/x/image v0.0.0-20180708004352-c73c2afc3b81/go.mod h1:ux5Hcp/YLpHSI86hEcLt0YII63i6oz57MZXIpbrjZUs=
golang.org/x/image v0.0.0-20181030002151-69cc3646b96e/go.mod h1:ux5Hcp/YLpHSI86hEcLt0YII63i6oz57MZXIpbrjZUs=
golang.org/x/image v0.1.0/go.mod h1:iyPr49SD/G/TBxYVB/9RRtGUT5eNbo2u4NamWeQcD5c=
golang.org/x/image v0.5.0/go.mod h1:FVC7BI/5Ym8R25iw5OLsgshdUBbT1h5jZTpA+mvAdZ4=
golang.org/x/image v0.12.0 h1:w13vZbU4o5rKOFFR8y7M+c4A5jXDC0uXTdHYRP8X2DQ=
golang.org/x/image v0.12.0/go.mod h1:Lu90jvHG7GfemOIcldsh9A2hS01ocl6oNO7ype5mEnk=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
@ -142,7 +113,6 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

168
pkgs/json/json_test.go Normal file
View File

@ -0,0 +1,168 @@
package json
import (
"testing"
)
func TestJSONObject(t *testing.T) {
testObject := NewJSONObject(nil)
testObject.Set("str", "字符串")
testObject.Set("str1", "字符串1")
testObject.Set("int1", 100)
testObject.Set("int2", 200)
testObject.Set("bool1", true)
testObject.Set("bool2", false)
t.Log("testObject", testObject.ToJSONString())
t.Log("testObject-size:", testObject.Size(), "==6")
testObject.RemoveByKey("bool1")
t.Log("testObject.remove-size:", testObject.Size(), "==5")
//
test2Object := NewJSONObject(nil)
test2Object.Set("key1", "key1value")
test2Object.Set("key2", testObject)
t.Log("test2Object-size:", test2Object.Size(), "==2")
//t.Log("testObject", test2Object.JsonData().V)
t.Log("test2Object:", test2Object.ToJSONString())
test2Key2 := test2Object.GetObjectByKey("key2")
test2Key2.Set("bool1", true)
t.Log("test2Object.test2Key2:", test2Key2.ToJSONString())
t.Log("test2Object.test2Key2:", testObject.ToJSONString())
t.Log("test2Object.key2-testObject-size:", test2Key2.Size(), testObject.Size(), "=6")
test2Key2.Set("bool5", true)
t.Log("test2Object.test2Key2:", test2Key2.ToJSONString())
t.Log("test2Object.test2Key2:", testObject.ToJSONString())
t.Log("test2Object.key2-testObject-size:", test2Key2.Size(), testObject.Size(), "=7")
t.Log("--------------------------------------------------- 1 ---------------------------------------------------------")
test2Key2.RemoveByKey("bool1")
t.Log("test2Object.test2Key2:", test2Key2.ToJSONString())
t.Log("test2Object.test2Key2:", testObject.ToJSONString())
t.Log("test2Object.key2-testObject-size:", test2Key2.Size(), testObject.Size(), "=6")
test2Key2.RemoveByKey("bool5")
t.Log("test2Object.test2Key2:", test2Key2.ToJSONString())
t.Log("test2Object.test2Key2:", testObject.ToJSONString())
t.Log("test2Object.key2-testObject-size:", test2Key2.Size(), testObject.Size(), "=5")
t.Log("--------------------------------------------------- 2 ---------------------------------------------------------")
t.Log("test2Object-key1:", test2Object.GetStringByKey("key1"), "=key1value")
t.Log("--------------------------------------------------- 3 ---------------------------------------------------------")
testArray := NewJSONArray(nil)
testArray.Add("第1个值", 2222, 3333.33, true)
testArray.Add("第5个值")
t.Log("testArray-size:", testArray.Size(), "=5")
t.Log("testArray-remove-3")
testArray.RemoveByIndex(3)
t.Log("testArray-size:", testArray.Size(), "=4")
t.Log("testArray-add")
testArray.Add("第5个新值")
t.Log("testArray-size:", testArray.Size(), "=5")
t.Log("testArray-3:", testArray.GetStringByIndex(3), "=第5个值")
t.Log("--------------------------------------------------- 4 ---------------------------------------------------------")
testArray.SetByIndex(3, true)
t.Log("testArray-3:", testArray.GetBoolByIndex(3), "=true")
t.Log("--------------------------------------------------- 5 ---------------------------------------------------------")
testArray.SetByIndex(3, testObject)
object3 := testArray.GetObjectByIndex(3)
t.Log("testArray-3.IsObject:", object3.IsObject(), "=true")
t.Log("testArray.3-object3-size:", object3.Size(), testObject.Size(), "=5")
object3.Set("key7", "字符串7")
t.Log("testArray.3-object3-size:", object3.Size(), testObject.Size(), "=6")
t.Log("testArray.3-object3-key7:", object3.GetStringByKey("key7"), testObject.GetStringByKey("key7"), "=字符串7")
t.Log("--------------------------------------------------- 6 ---------------------------------------------------------")
//
testObject.Set("array", testArray)
t.Log("testObject", testObject.ToJSONString())
t.Log("testObject-size:", testObject.Size())
testObjectKeyArray := testObject.GetArrayByKey("array")
t.Log("testObject.array-size:", testObjectKeyArray.Size(), testArray.Size())
testObjectKeyArray.Add("添加了一个字符串")
t.Log("testObject.array-size:", testObjectKeyArray.Size(), testArray.Size())
//
t.Log("--------------------------------------------------- 7 ---------------------------------------------------------")
testArray.Add(test2Object)
testArrayObject := testArray.GetByIndex(testArray.Size() - 1)
t.Log("testArray-last-IsObject", testArrayObject.IsObject())
t.Log("testArray-last-object-size:", testArrayObject.Size(), test2Object.Size(), "=2")
testArrayObject.JSONObject().Set("addadd", "添加添加")
t.Log("testArray-last-object-size:", testArrayObject.Size(), test2Object.Size(), "=3")
t.Log("--------------------------------------------------- 8 ---------------------------------------------------------")
t.Log("testObject", testObject.ToJSONString())
t.Log("--------------------------------------------------- 9 ---------------------------------------------------------")
type Stt struct {
Name string
Age int
Items []string
}
var stt = &Stt{
Name: "TestName",
Age: 333,
Items: []string{"Crimson", "Red", "Ruby", "Maroon"},
}
sttJSON := NewJSONObject(stt)
t.Log("sttJSON", sttJSON.ToJSONString())
t.Log("--------------------------------------------------- 10 ---------------------------------------------------------")
var stts = make([]*Stt, 2)
stts[0] = stt
stts[1] = stt
sttsArray := NewJSONArray(stts)
t.Log("sttsArray", sttsArray.ToJSONString())
t.Log("--------------------------------------------------- 11 ---------------------------------------------------------")
var mapTT = make(map[string]interface{})
mapTT["stt"] = stt
mapTT["stts"] = stts
mapTTJSON := NewJSONObject(mapTT)
t.Log("mapTTJSON", mapTTJSON.ToJSONString())
t.Log("--------------------------------------------------- 12 ---------------------------------------------------------")
t.Log("mapTTJSON.stt.Name", mapTTJSON.GetObjectByKey("stt").GetStringByKey("Name"), "=TestName")
t.Log("mapTTJSON.stt.Age", mapTTJSON.GetObjectByKey("stt").GetIntByKey("Age"), "=333")
mapTTJSON.GetObjectByKey("stt").Set("Name", "张三")
t.Log("mapTTJSON.stt.Name", mapTTJSON.GetObjectByKey("stt").GetStringByKey("Name"), "=张三")
t.Log("--------------------------------------------------- end ---------------------------------------------------------")
}
func TestJSONArray(t *testing.T) {
var jsonStr = "[\"stringValue\",345,true,23666644.66666666666,\"字符串?\",30344.66,{\"stringField\":\"stringFieldValue\",\"intField\":1000,\"arrayField\":[100,200,\"数组里的字符串\",66996.99],\"doubleField\":999991.102,\"booleanField\":true},[100,200,\"数组里的字符串\",66996.99,{\"stringField\":\"stringFieldValue\",\"intField\":1000,\"arrayField\":[100,200,\"数组里的字符串\",66996.99],\"doubleField\":999991.102,\"booleanField\":true},true,false],8888888889233,\"null\",\"undefined\"]"
var jsonBytes = []byte(jsonStr)
t.Log("jsonStr", jsonStr)
t.Log("jsonBytes", jsonBytes)
argsJSONString := NewJSONArray(jsonStr)
t.Log("argsJSONString.1:", argsJSONString.GetByIndex(1).IsInt(), argsJSONString.GetByIndex(1).Int(), "=true =345")
t.Log("argsJSONString.3:", argsJSONString.GetByIndex(3).IsFloat(), argsJSONString.GetByIndex(3).Int(), argsJSONString.GetByIndex(3).Float(), argsJSONString.GetByIndex(3).String())
argsJSONBytes := NewJSON(jsonBytes).JSONArray()
t.Log("argsJSONBytes.1:", argsJSONBytes.GetByIndex(1).IsInt(), argsJSONBytes.GetByIndex(1).Int(), "=true =345")
t.Log("argsJSONBytes.3:", argsJSONBytes.GetByIndex(3).IsFloat(), argsJSONBytes.GetByIndex(3).Int(), argsJSONBytes.GetByIndex(3).Float(), argsJSONBytes.GetByIndex(3).String())
t.Log("argsJSONBytes.6:", argsJSONBytes.GetByIndex(6).IsObject(), "=true")
var mapObject = make(map[string]interface{})
mapObject["k1"] = "子子"
mapObject["k2"] = 55555
mapObject["k3"] = 7777.999
var arrayObject = []interface{}{"aaaaaa", "bbbbbb", 33333}
jsonArray := NewJSONArray(nil)
jsonArray.Add("字符串", 11111, true, mapObject, arrayObject)
t.Log("jsonArray-size:", jsonArray.Size(), "=5")
t.Log("jsonArray.1:", jsonArray.GetIntByIndex(1), "=11111")
t.Log("jsonArray.bytes:", jsonArray.Bytes())
t.Log("jsonArray.ToJSONString:", jsonArray.ToJSONString())
mp := jsonArray.GetByIndex(3).JSONObject() //map
t.Log("jsonArray.3.ToJSONString:", mp.ToJSONString())
t.Log("jsonArray.3.k1:", mp.GetStringByKey("k1"), "=子子")
t.Log("jsonArray.3.k2:", mp.GetIntByKey("k2"), "=55555")
jsonArray.GetByIndex(3).JSONObject().Set("k2", "杨红岩")
t.Log("jsonArray.3.k2:", mp.GetIntByKey("k2"), "=0")
t.Log("jsonArray.3.k2:", jsonArray.GetByIndex(3).JSONObject().GetStringByKey("k2"), "=杨红岩")
index3 := jsonArray.GetByIndex(3)
t.Log("jsonArray.3.Data:", index3.Data())
index3.JSONObject().Set("k2", "index.k2=杨红岩")
t.Log("jsonArray.3.k2:", jsonArray.GetByIndex(3).JSONObject().GetStringByKey("k2"), "=index.k2=杨红岩")
t.Log("jsonArray.4.1:", jsonArray.GetByIndex(4).JSONArray().GetStringByIndex(1), "=bbbbbb")
jsonArray.GetByIndex(4).JSONArray().SetByIndex(1, "数组.4变字符串")
t.Log("jsonArray.4.1:", jsonArray.GetByIndex(4).JSONArray().GetStringByIndex(1), "=数组.4变字符串")
jsonArray.SetByIndex(4, "jsonArray.4=数组变字符串")
t.Log("jsonArray.4.type:", jsonArray.GetByIndex(4).Type(), "=")
t.Log("jsonArray.4:", jsonArray.GetByIndex(4).String(), "=jsonArray.4=数组变字符串")
t.Log("jsonArray.ToJSONString:", jsonArray.ToJSONString())
jsonArray.GetByIndex(4).SetValue(111111)
t.Log("jsonArray.4.type:", jsonArray.GetByIndex(4).Type())
t.Log("jsonArray.ToJSONString:", jsonArray.ToJSONString())
jsonArray.GetByIndex(4).SetValue(3333.33)
t.Log("jsonArray.4.type:", jsonArray.GetByIndex(4).Type())
t.Log("jsonArray.ToJSONString:", jsonArray.ToJSONString())
}