jsonunmarshaler.go 982 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package mapping
  2. import (
  3. "io"
  4. "github.com/tal-tech/go-zero/core/jsonx"
  5. )
  6. const jsonTagKey = "json"
  7. var jsonUnmarshaler = NewUnmarshaler(jsonTagKey)
  8. // UnmarshalJsonBytes unmarshals content into v.
  9. func UnmarshalJsonBytes(content []byte, v interface{}) error {
  10. return unmarshalJsonBytes(content, v, jsonUnmarshaler)
  11. }
  12. // UnmarshalJsonReader unmarshals content from reader into v.
  13. func UnmarshalJsonReader(reader io.Reader, v interface{}) error {
  14. return unmarshalJsonReader(reader, v, jsonUnmarshaler)
  15. }
  16. func unmarshalJsonBytes(content []byte, v interface{}, unmarshaler *Unmarshaler) error {
  17. var m map[string]interface{}
  18. if err := jsonx.Unmarshal(content, &m); err != nil {
  19. return err
  20. }
  21. return unmarshaler.Unmarshal(m, v)
  22. }
  23. func unmarshalJsonReader(reader io.Reader, v interface{}, unmarshaler *Unmarshaler) error {
  24. var m map[string]interface{}
  25. if err := jsonx.UnmarshalFromReader(reader, &m); err != nil {
  26. return err
  27. }
  28. return unmarshaler.Unmarshal(m, v)
  29. }