mirror of
https://gitee.com/johng/gf
synced 2026-06-06 02:25:47 +08:00
remove UseNumber for json decoding in package gjson
This commit is contained in:
19
.example/encoding/gjson/issue360.go
Normal file
19
.example/encoding/gjson/issue360.go
Normal file
@ -0,0 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/encoding/gjson"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := `
|
||||
{"apiVersion":"v1","kind":"Service","metadata":{"labels":{"name":"http-daemon"},"name":"http-daemon","namespace":"default"},"spec":{"ports":[{"name":"http-daemon","port":8080,"protocol":"TCP","targetPort":9212}],"selector":{"app":"http-daemon","version":"v0930-082326"}}}
|
||||
`
|
||||
js, err := gjson.DecodeToJson(s)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
//g.Dump(js.ToMap())
|
||||
y, _ := js.ToYamlString()
|
||||
fmt.Println(y)
|
||||
}
|
||||
@ -1,32 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
)
|
||||
|
||||
// 演示slice类型属性的赋值
|
||||
func main() {
|
||||
type User struct {
|
||||
Scores []int
|
||||
}
|
||||
|
||||
user := new(User)
|
||||
scores := []interface{}{99, 100, 60, 140}
|
||||
|
||||
// 通过map映射转换
|
||||
if err := gconv.Struct(g.Map{"Scores": scores}, user); err != nil {
|
||||
fmt.Println(err)
|
||||
} else {
|
||||
g.Dump(user)
|
||||
}
|
||||
|
||||
// 通过变量映射转换,直接slice赋值
|
||||
if err := gconv.Struct(scores, user); err != nil {
|
||||
fmt.Println(err)
|
||||
} else {
|
||||
g.Dump(user)
|
||||
}
|
||||
}
|
||||
@ -21,8 +21,8 @@ func main() {
|
||||
|
||||
user1 := new(User1)
|
||||
user2 := new(User2)
|
||||
scores := map[string]interface{}{
|
||||
"Scores": map[string]interface{}{
|
||||
scores := g.Map{
|
||||
"Scores": g.Map{
|
||||
"Name": "john",
|
||||
"Result": 100,
|
||||
},
|
||||
|
||||
@ -17,8 +17,8 @@ func main() {
|
||||
}
|
||||
|
||||
user := new(User)
|
||||
scores := map[string]interface{}{
|
||||
"Scores": map[string]interface{}{
|
||||
scores := g.Map{
|
||||
"Scores": g.Map{
|
||||
"Name": "john",
|
||||
"Result": 100,
|
||||
},
|
||||
|
||||
@ -17,13 +17,13 @@ func main() {
|
||||
}
|
||||
|
||||
user := new(User)
|
||||
scores := map[string]interface{}{
|
||||
"Scores": []interface{}{
|
||||
map[string]interface{}{
|
||||
scores := g.Map{
|
||||
"Scores": g.Slice{
|
||||
g.Map{
|
||||
"Name": "john",
|
||||
"Result": 100,
|
||||
},
|
||||
map[string]interface{}{
|
||||
g.Map{
|
||||
"Name": "smith",
|
||||
"Result": 60,
|
||||
},
|
||||
|
||||
@ -103,7 +103,10 @@ func Decode(data interface{}) (interface{}, error) {
|
||||
// The <v> should be a pointer type.
|
||||
func DecodeTo(data interface{}, v interface{}) error {
|
||||
decoder := json.NewDecoder(bytes.NewReader(gconv.Bytes(data)))
|
||||
decoder.UseNumber()
|
||||
// Do not use number, it converts float64 to json.Number type,
|
||||
// which actually a string type. It causes converting issue for other data formats,
|
||||
// for example: yaml.
|
||||
//decoder.UseNumber()
|
||||
return decoder.Decode(v)
|
||||
}
|
||||
|
||||
@ -184,7 +187,10 @@ func doLoadContent(dataType string, data []byte, safe ...bool) (*Json, error) {
|
||||
return nil, err
|
||||
}
|
||||
decoder := json.NewDecoder(bytes.NewReader(data))
|
||||
decoder.UseNumber()
|
||||
// Do not use number, it converts float64 to json.Number type,
|
||||
// which actually a string type. It causes converting issue for other data formats,
|
||||
// for example: yaml.
|
||||
//decoder.UseNumber()
|
||||
if err := decoder.Decode(&result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@ -15,7 +15,7 @@ import (
|
||||
"github.com/gogf/gf/test/gtest"
|
||||
)
|
||||
|
||||
func Test_Load_JSON(t *testing.T) {
|
||||
func Test_Load_JSON1(t *testing.T) {
|
||||
data := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`)
|
||||
// JSON
|
||||
gtest.Case(t, func() {
|
||||
@ -42,6 +42,19 @@ func Test_Load_JSON(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Load_JSON2(t *testing.T) {
|
||||
data := []byte(`{"n":123456789000000000000, "m":{"k":"v"}, "a":[1,2,3]}`)
|
||||
gtest.Case(t, func() {
|
||||
j, err := gjson.LoadContent(data)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(j.Get("n"), "123456789000000000000")
|
||||
gtest.Assert(j.Get("m"), g.Map{"k": "v"})
|
||||
gtest.Assert(j.Get("m.k"), "v")
|
||||
gtest.Assert(j.Get("a"), g.Slice{1, 2, 3})
|
||||
gtest.Assert(j.Get("a.1"), 2)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Load_XML(t *testing.T) {
|
||||
data := []byte(`<doc><a>1</a><a>2</a><a>3</a><m><k>v</k></m><n>123456789</n></doc>`)
|
||||
// XML
|
||||
|
||||
Reference in New Issue
Block a user