Skip to content

Commit

Permalink
🐞 fix: zjson.Bytes() Conversion error
Browse files Browse the repository at this point in the history
  • Loading branch information
sohaha committed Feb 15, 2024
1 parent 78e57d6 commit 23016c1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion zjson/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

var demo = `{
"i":100,"f":1.6,"time":"2019-09-10 13:48:22","index.key":"66.6",
"i":100,"f":1.6,"ii":-999,"time":"2019-09-10 13:48:22","index.key":"66.6",
"quality":"highLevel","user":{"name":"暴龙兽"},"children":["阿古兽","暴龙兽","机器暴龙兽",{}],"other":["\"",666,"1.8","$1",{"rank":["t",1,2,3]}],"bool":false,"boolTrue":true,"none":"","friends":[{"name":"天使兽","quality":"highLevel","age":1},{"age":5,"name":"天女兽",
"quality":"super"}]}`

Expand Down
2 changes: 1 addition & 1 deletion zjson/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (r *Res) Raw() string {
}

func (r *Res) Bytes() []byte {
return zstring.String2Bytes(r.raw)
return zstring.String2Bytes(r.String())
}

func (r *Res) String() string {
Expand Down
12 changes: 12 additions & 0 deletions zjson/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/sohaha/zlsgo"
"github.com/sohaha/zlsgo/zstring"
"github.com/sohaha/zlsgo/ztype"
)

type Demo struct {
Expand Down Expand Up @@ -74,6 +75,8 @@ func TestGet(t *testing.T) {
other := Get(demo, "other")
t.Log(other.Array(), other.Raw())
tt.EqualExit("暴龙兽", name)
tt.EqualExit(name, string(user.Get("name").Bytes()))
tt.EqualExit("-999", Get(demo, "ii").String())
tt.EqualExit(666, Get(demo, "other.1").Int())
tt.Log(Get(demo, "other.1").typ.String())
tt.EqualExit(0, Get(demo, "other.2").Int())
Expand Down Expand Up @@ -281,6 +284,15 @@ func TestModifiers(t *testing.T) {
t.Log(Get(demo, "friends|@format").String())
}

func TestType(t *testing.T) {
tt := zlsgo.NewTest(t)
tt.EqualExit(float64(1), Get(`{"a":true}`, "a").Float())
tt.EqualExit(float64(0), Get(`{"a":false}`, "a").Float())
tt.EqualExit(ztype.Map{}, Get(`{}`, "a").Map())
tt.EqualExit(ztype.Maps{}, Get(`{}`, "a").Maps())
tt.EqualExit([]*Res{}, Get(`{}`, "a").Array())
}

func BenchmarkGet(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
Expand Down

0 comments on commit 23016c1

Please sign in to comment.