/
gjson_implements.go
60 lines (53 loc) · 1.38 KB
/
gjson_implements.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright GoFrame Author(https://goframe.org). 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 := loadContentWithOptions(b, Options{
Type: ContentTypeJson,
StrNumber: true,
})
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 := NewWithOptions(value, Options{
StrNumber: true,
}); r != nil {
// Value copy.
*j = *r
}
return nil
}
// MapStrAny implements interface function MapStrAny().
func (j *Json) MapStrAny() map[string]interface{} {
if j == nil {
return nil
}
return j.Map()
}
// Interfaces implements interface function Interfaces().
func (j *Json) Interfaces() []interface{} {
if j == nil {
return nil
}
return j.Array()
}
// String returns current Json object as string.
func (j *Json) String() string {
if j.IsNil() {
return ""
}
return j.MustToJsonString()
}