gjson_implements.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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/gogf/gf.
  6. package gjson
  7. // MarshalJSON implements the interface MarshalJSON for json.Marshal.
  8. func (j Json) MarshalJSON() ([]byte, error) {
  9. return j.ToJson()
  10. }
  11. // UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
  12. func (j *Json) UnmarshalJSON(b []byte) error {
  13. r, err := loadContentWithOptions(b, Options{
  14. Type: ContentTypeJson,
  15. StrNumber: true,
  16. })
  17. if r != nil {
  18. // Value copy.
  19. *j = *r
  20. }
  21. return err
  22. }
  23. // UnmarshalValue is an interface implement which sets any type of value for Json.
  24. func (j *Json) UnmarshalValue(value interface{}) error {
  25. if r := NewWithOptions(value, Options{
  26. StrNumber: true,
  27. }); r != nil {
  28. // Value copy.
  29. *j = *r
  30. }
  31. return nil
  32. }
  33. // MapStrAny implements interface function MapStrAny().
  34. func (j *Json) MapStrAny() map[string]interface{} {
  35. if j == nil {
  36. return nil
  37. }
  38. return j.Map()
  39. }
  40. // Interfaces implements interface function Interfaces().
  41. func (j *Json) Interfaces() []interface{} {
  42. if j == nil {
  43. return nil
  44. }
  45. return j.Array()
  46. }