mirror of
https://gitee.com/johng/gf
synced 2026-06-07 02:12:11 +08:00
add function UnmarshalValue feature for package garray/gmap/gset/gtype/gvar/gjson/gconv
This commit is contained in:
@ -30,11 +30,6 @@ type Json struct {
|
||||
vc bool // Violence Check(false in default), which is used to access data when the hierarchical data key contains separator char.
|
||||
}
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
func (j *Json) MarshalJSON() ([]byte, error) {
|
||||
return j.ToJson()
|
||||
}
|
||||
|
||||
// setValue sets <value> to <j> by <pattern>.
|
||||
// Note:
|
||||
// 1. If value is nil and removed is true, means deleting this value;
|
||||
|
||||
31
encoding/gjson/gjson_implements.go
Normal file
31
encoding/gjson/gjson_implements.go
Normal file
@ -0,0 +1,31 @@
|
||||
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package gjson
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
func (j *Json) MarshalJSON() ([]byte, error) {
|
||||
return j.ToJson()
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
|
||||
func (j *Json) UnmarshalJSON(b []byte) error {
|
||||
r, err := LoadContent(b)
|
||||
if r != nil {
|
||||
// Value copy.
|
||||
*j = *r
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// UnmarshalValue is an interface implement which sets any type of value for Json.
|
||||
func (j *Json) UnmarshalValue(value interface{}) error {
|
||||
if r := New(value); r != nil {
|
||||
// Value copy.
|
||||
*j = *r
|
||||
}
|
||||
return nil
|
||||
}
|
||||
72
encoding/gjson/gjson_z_unit_implements_test.go
Normal file
72
encoding/gjson/gjson_z_unit_implements_test.go
Normal file
@ -0,0 +1,72 @@
|
||||
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package gjson_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/encoding/gjson"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/test/gtest"
|
||||
)
|
||||
|
||||
func TestJson_UnmarshalJSON(t *testing.T) {
|
||||
data := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`)
|
||||
gtest.Case(t, func() {
|
||||
j := gjson.New(nil)
|
||||
err := json.Unmarshal(data, j)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(j.Get("n"), "123456789")
|
||||
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 TestJson_UnmarshalValue(t *testing.T) {
|
||||
type T struct {
|
||||
Name string
|
||||
Json *gjson.Json
|
||||
}
|
||||
// JSON
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
err := gconv.Struct(g.Map{
|
||||
"name": "john",
|
||||
"json": []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`),
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Json.Get("n"), "123456789")
|
||||
gtest.Assert(t.Json.Get("m"), g.Map{"k": "v"})
|
||||
gtest.Assert(t.Json.Get("m.k"), "v")
|
||||
gtest.Assert(t.Json.Get("a"), g.Slice{1, 2, 3})
|
||||
gtest.Assert(t.Json.Get("a.1"), 2)
|
||||
})
|
||||
// Map
|
||||
gtest.Case(t, func() {
|
||||
var t *T
|
||||
err := gconv.Struct(g.Map{
|
||||
"name": "john",
|
||||
"json": g.Map{
|
||||
"n": 123456789,
|
||||
"m": g.Map{"k": "v"},
|
||||
"a": g.Slice{1, 2, 3},
|
||||
},
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Json.Get("n"), "123456789")
|
||||
gtest.Assert(t.Json.Get("m"), g.Map{"k": "v"})
|
||||
gtest.Assert(t.Json.Get("m.k"), "v")
|
||||
gtest.Assert(t.Json.Get("a"), g.Slice{1, 2, 3})
|
||||
gtest.Assert(t.Json.Get("a.1"), 2)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user