json.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright 2020 gf Author(https://github.com/gogf/gf). 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. "encoding/json"
  10. "io"
  11. )
  12. // Marshal adapts to json/encoding Marshal API.
  13. //
  14. // Marshal returns the JSON encoding of v, adapts to json/encoding Marshal API
  15. // Refer to https://godoc.org/encoding/json#Marshal for more information.
  16. func Marshal(v interface{}) ([]byte, error) {
  17. return json.Marshal(v)
  18. }
  19. // MarshalIndent same as json.MarshalIndent. Prefix is not supported.
  20. func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) {
  21. return json.MarshalIndent(v, prefix, indent)
  22. }
  23. // Unmarshal adapts to json/encoding Unmarshal API
  24. //
  25. // Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v.
  26. // Refer to https://godoc.org/encoding/json#Unmarshal for more information.
  27. func Unmarshal(data []byte, v interface{}) error {
  28. return json.Unmarshal(data, v)
  29. }
  30. // NewEncoder same as json.NewEncoder
  31. func NewEncoder(writer io.Writer) *json.Encoder {
  32. return json.NewEncoder(writer)
  33. }
  34. // NewDecoder adapts to json/stream NewDecoder API.
  35. //
  36. // NewDecoder returns a new decoder that reads from r.
  37. //
  38. // Instead of a json/encoding Decoder, an Decoder is returned
  39. // Refer to https://godoc.org/encoding/json#NewDecoder for more information.
  40. func NewDecoder(reader io.Reader) *json.Decoder {
  41. return json.NewDecoder(reader)
  42. }
  43. // Valid reports whether data is a valid JSON encoding.
  44. func Valid(data []byte) bool {
  45. return json.Valid(data)
  46. }