json.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 json provides json operations wrapping ignoring stdlib or third-party lib json.
  7. package json
  8. import (
  9. "bytes"
  10. "encoding/json"
  11. "io"
  12. "github.com/gogf/gf/v2/errors/gerror"
  13. )
  14. // RawMessage is a raw encoded JSON value.
  15. // It implements Marshaler and Unmarshaler and can
  16. // be used to delay JSON decoding or precompute a JSON encoding.
  17. type RawMessage = json.RawMessage
  18. // Marshal adapts to json/encoding Marshal API.
  19. //
  20. // Marshal returns the JSON encoding of v, adapts to json/encoding Marshal API
  21. // Refer to https://godoc.org/encoding/json#Marshal for more information.
  22. func Marshal(v interface{}) (marshaledBytes []byte, err error) {
  23. marshaledBytes, err = json.Marshal(v)
  24. if err != nil {
  25. err = gerror.Wrap(err, `json.Marshal failed`)
  26. }
  27. return
  28. }
  29. // MarshalIndent same as json.MarshalIndent.
  30. func MarshalIndent(v interface{}, prefix, indent string) (marshaledBytes []byte, err error) {
  31. marshaledBytes, err = json.MarshalIndent(v, prefix, indent)
  32. if err != nil {
  33. err = gerror.Wrap(err, `json.MarshalIndent failed`)
  34. }
  35. return
  36. }
  37. // Unmarshal adapts to json/encoding Unmarshal API
  38. //
  39. // Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v.
  40. // Refer to https://godoc.org/encoding/json#Unmarshal for more information.
  41. func Unmarshal(data []byte, v interface{}) (err error) {
  42. err = json.Unmarshal(data, v)
  43. if err != nil {
  44. err = gerror.Wrap(err, `json.Unmarshal failed`)
  45. }
  46. return
  47. }
  48. // UnmarshalUseNumber decodes the json data bytes to target interface using number option.
  49. func UnmarshalUseNumber(data []byte, v interface{}) (err error) {
  50. decoder := NewDecoder(bytes.NewReader(data))
  51. decoder.UseNumber()
  52. err = decoder.Decode(v)
  53. if err != nil {
  54. err = gerror.Wrap(err, `json.UnmarshalUseNumber failed`)
  55. }
  56. return
  57. }
  58. // NewEncoder same as json.NewEncoder
  59. func NewEncoder(writer io.Writer) *json.Encoder {
  60. return json.NewEncoder(writer)
  61. }
  62. // NewDecoder adapts to json/stream NewDecoder API.
  63. //
  64. // NewDecoder returns a new decoder that reads from r.
  65. //
  66. // Instead of a json/encoding Decoder, a Decoder is returned
  67. // Refer to https://godoc.org/encoding/json#NewDecoder for more information.
  68. func NewDecoder(reader io.Reader) *json.Decoder {
  69. return json.NewDecoder(reader)
  70. }
  71. // Valid reports whether data is a valid JSON encoding.
  72. func Valid(data []byte) bool {
  73. return json.Valid(data)
  74. }