github.com/wangyougui/gf/v2@v2.6.5/encoding/gjson/gjson_implements.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/wangyougui/gf.
     6  
     7  package gjson
     8  
     9  // MarshalJSON implements the interface MarshalJSON for json.Marshal.
    10  func (j Json) MarshalJSON() ([]byte, error) {
    11  	return j.ToJson()
    12  }
    13  
    14  // UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
    15  func (j *Json) UnmarshalJSON(b []byte) error {
    16  	r, err := loadContentWithOptions(b, Options{
    17  		Type:      ContentTypeJson,
    18  		StrNumber: true,
    19  	})
    20  	if r != nil {
    21  		// Value copy.
    22  		*j = *r
    23  	}
    24  	return err
    25  }
    26  
    27  // UnmarshalValue is an interface implement which sets any type of value for Json.
    28  func (j *Json) UnmarshalValue(value interface{}) error {
    29  	if r := NewWithOptions(value, Options{
    30  		StrNumber: true,
    31  	}); r != nil {
    32  		// Value copy.
    33  		*j = *r
    34  	}
    35  	return nil
    36  }
    37  
    38  // MapStrAny implements interface function MapStrAny().
    39  func (j *Json) MapStrAny() map[string]interface{} {
    40  	if j == nil {
    41  		return nil
    42  	}
    43  	return j.Map()
    44  }
    45  
    46  // Interfaces implements interface function Interfaces().
    47  func (j *Json) Interfaces() []interface{} {
    48  	if j == nil {
    49  		return nil
    50  	}
    51  	return j.Array()
    52  }
    53  
    54  // String returns current Json object as string.
    55  func (j *Json) String() string {
    56  	if j.IsNil() {
    57  		return ""
    58  	}
    59  	return j.MustToJsonString()
    60  }