2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2020-01-20 19:56:42 +08:00
|
|
|
//
|
|
|
|
// 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.
|
2022-01-19 16:55:57 +08:00
|
|
|
func (j Json) MarshalJSON() ([]byte, error) {
|
2020-01-20 19:56:42 +08:00
|
|
|
return j.ToJson()
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
|
|
|
|
func (j *Json) UnmarshalJSON(b []byte) error {
|
2022-04-11 20:49:33 +08:00
|
|
|
r, err := loadContentWithOptions(b, Options{
|
|
|
|
Type: ContentTypeJson,
|
|
|
|
StrNumber: true,
|
|
|
|
})
|
2020-01-20 19:56:42 +08:00
|
|
|
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 {
|
2022-04-11 23:45:11 +08:00
|
|
|
if r := NewWithOptions(value, Options{
|
|
|
|
StrNumber: true,
|
|
|
|
}); r != nil {
|
2020-01-20 19:56:42 +08:00
|
|
|
// Value copy.
|
|
|
|
*j = *r
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-12-20 00:32:23 +08:00
|
|
|
|
|
|
|
// MapStrAny implements interface function MapStrAny().
|
|
|
|
func (j *Json) MapStrAny() map[string]interface{} {
|
2022-10-08 11:46:38 +08:00
|
|
|
if j == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2021-12-20 00:32:23 +08:00
|
|
|
return j.Map()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Interfaces implements interface function Interfaces().
|
|
|
|
func (j *Json) Interfaces() []interface{} {
|
2022-10-08 11:46:38 +08:00
|
|
|
if j == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2021-12-20 00:32:23 +08:00
|
|
|
return j.Array()
|
|
|
|
}
|
2023-01-03 11:00:23 +08:00
|
|
|
|
|
|
|
// String returns current Json object as string.
|
|
|
|
func (j *Json) String() string {
|
|
|
|
if j.IsNil() {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return j.MustToJsonString()
|
|
|
|
}
|