adapter.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package jsoniter
  2. import (
  3. "bytes"
  4. "io"
  5. )
  6. // RawMessage to make replace json with jsoniter
  7. type RawMessage []byte
  8. // Unmarshal adapts to json/encoding Unmarshal API
  9. //
  10. // Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v.
  11. // Refer to https://godoc.org/encoding/json#Unmarshal for more information
  12. func Unmarshal(data []byte, v interface{}) error {
  13. return ConfigDefault.Unmarshal(data, v)
  14. }
  15. // UnmarshalFromString is a convenient method to read from string instead of []byte
  16. func UnmarshalFromString(str string, v interface{}) error {
  17. return ConfigDefault.UnmarshalFromString(str, v)
  18. }
  19. // Get quick method to get value from deeply nested JSON structure
  20. func Get(data []byte, path ...interface{}) Any {
  21. return ConfigDefault.Get(data, path...)
  22. }
  23. // Marshal adapts to json/encoding Marshal API
  24. //
  25. // Marshal returns the JSON encoding of v, adapts to json/encoding Marshal API
  26. // Refer to https://godoc.org/encoding/json#Marshal for more information
  27. func Marshal(v interface{}) ([]byte, error) {
  28. return ConfigDefault.Marshal(v)
  29. }
  30. // MarshalIndent same as json.MarshalIndent. Prefix is not supported.
  31. func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) {
  32. return ConfigDefault.MarshalIndent(v, prefix, indent)
  33. }
  34. // MarshalToString convenient method to write as string instead of []byte
  35. func MarshalToString(v interface{}) (string, error) {
  36. return ConfigDefault.MarshalToString(v)
  37. }
  38. // NewDecoder adapts to json/stream NewDecoder API.
  39. //
  40. // NewDecoder returns a new decoder that reads from r.
  41. //
  42. // Instead of a json/encoding Decoder, an Decoder is returned
  43. // Refer to https://godoc.org/encoding/json#NewDecoder for more information
  44. func NewDecoder(reader io.Reader) *Decoder {
  45. return ConfigDefault.NewDecoder(reader)
  46. }
  47. // Decoder reads and decodes JSON values from an input stream.
  48. // Decoder provides identical APIs with json/stream Decoder (Token() and UseNumber() are in progress)
  49. type Decoder struct {
  50. iter *Iterator
  51. }
  52. // Decode decode JSON into interface{}
  53. func (adapter *Decoder) Decode(obj interface{}) error {
  54. if adapter.iter.head == adapter.iter.tail && adapter.iter.reader != nil {
  55. if !adapter.iter.loadMore() {
  56. return io.EOF
  57. }
  58. }
  59. adapter.iter.ReadVal(obj)
  60. err := adapter.iter.Error
  61. if err == io.EOF {
  62. return nil
  63. }
  64. return adapter.iter.Error
  65. }
  66. // More is there more?
  67. func (adapter *Decoder) More() bool {
  68. iter := adapter.iter
  69. if iter.Error != nil {
  70. return false
  71. }
  72. c := iter.nextToken()
  73. if c == 0 {
  74. return false
  75. }
  76. iter.unreadByte()
  77. return c != ']' && c != '}'
  78. }
  79. // Buffered remaining buffer
  80. func (adapter *Decoder) Buffered() io.Reader {
  81. remaining := adapter.iter.buf[adapter.iter.head:adapter.iter.tail]
  82. return bytes.NewReader(remaining)
  83. }
  84. // UseNumber causes the Decoder to unmarshal a number into an interface{} as a
  85. // Number instead of as a float64.
  86. func (adapter *Decoder) UseNumber() {
  87. cfg := adapter.iter.cfg.configBeforeFrozen
  88. cfg.UseNumber = true
  89. adapter.iter.cfg = cfg.frozeWithCacheReuse(adapter.iter.cfg.extraExtensions)
  90. }
  91. // DisallowUnknownFields causes the Decoder to return an error when the destination
  92. // is a struct and the input contains object keys which do not match any
  93. // non-ignored, exported fields in the destination.
  94. func (adapter *Decoder) DisallowUnknownFields() {
  95. cfg := adapter.iter.cfg.configBeforeFrozen
  96. cfg.DisallowUnknownFields = true
  97. adapter.iter.cfg = cfg.frozeWithCacheReuse(adapter.iter.cfg.extraExtensions)
  98. }
  99. // NewEncoder same as json.NewEncoder
  100. func NewEncoder(writer io.Writer) *Encoder {
  101. return ConfigDefault.NewEncoder(writer)
  102. }
  103. // Encoder same as json.Encoder
  104. type Encoder struct {
  105. stream *Stream
  106. }
  107. // Encode encode interface{} as JSON to io.Writer
  108. func (adapter *Encoder) Encode(val interface{}) error {
  109. adapter.stream.WriteVal(val)
  110. adapter.stream.WriteRaw("\n")
  111. adapter.stream.Flush()
  112. return adapter.stream.Error
  113. }
  114. // SetIndent set the indention. Prefix is not supported
  115. func (adapter *Encoder) SetIndent(prefix, indent string) {
  116. config := adapter.stream.cfg.configBeforeFrozen
  117. config.IndentionStep = len(indent)
  118. adapter.stream.cfg = config.frozeWithCacheReuse(adapter.stream.cfg.extraExtensions)
  119. }
  120. // SetEscapeHTML escape html by default, set to false to disable
  121. func (adapter *Encoder) SetEscapeHTML(escapeHTML bool) {
  122. config := adapter.stream.cfg.configBeforeFrozen
  123. config.EscapeHTML = escapeHTML
  124. adapter.stream.cfg = config.frozeWithCacheReuse(adapter.stream.cfg.extraExtensions)
  125. }
  126. // Valid reports whether data is a valid JSON encoding.
  127. func Valid(data []byte) bool {
  128. return ConfigDefault.Valid(data)
  129. }