From 23016c1162fa0337ba1738190a6fc548d1b22994 Mon Sep 17 00:00:00 2001 From: seekwe Date: Thu, 15 Feb 2024 14:56:03 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9E=20fix:=20zjson.Bytes()=20Conversio?= =?UTF-8?q?n=20error?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- zjson/format_test.go | 2 +- zjson/get.go | 2 +- zjson/get_test.go | 12 ++++++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/zjson/format_test.go b/zjson/format_test.go index a8a759d..861dcb6 100644 --- a/zjson/format_test.go +++ b/zjson/format_test.go @@ -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"}]}` diff --git a/zjson/get.go b/zjson/get.go index c941b30..29f25d1 100644 --- a/zjson/get.go +++ b/zjson/get.go @@ -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 { diff --git a/zjson/get_test.go b/zjson/get_test.go index 76ea69f..96553e8 100644 --- a/zjson/get_test.go +++ b/zjson/get_test.go @@ -7,6 +7,7 @@ import ( "github.com/sohaha/zlsgo" "github.com/sohaha/zlsgo/zstring" + "github.com/sohaha/zlsgo/ztype" ) type Demo struct { @@ -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()) @@ -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++ {