adapter.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 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. if iter.head != iter.tail {
  73. return true
  74. }
  75. return iter.loadMore()
  76. }
  77. // Buffered remaining buffer
  78. func (adapter *Decoder) Buffered() io.Reader {
  79. remaining := adapter.iter.buf[adapter.iter.head:adapter.iter.tail]
  80. return bytes.NewReader(remaining)
  81. }
  82. // UseNumber causes the Decoder to unmarshal a number into an interface{} as a
  83. // Number instead of as a float64.
  84. func (adapter *Decoder) UseNumber() {
  85. cfg := adapter.iter.cfg.configBeforeFrozen
  86. cfg.UseNumber = true
  87. adapter.iter.cfg = cfg.frozeWithCacheReuse()
  88. }
  89. // DisallowUnknownFields causes the Decoder to return an error when the destination
  90. // is a struct and the input contains object keys which do not match any
  91. // non-ignored, exported fields in the destination.
  92. func (adapter *Decoder) DisallowUnknownFields() {
  93. cfg := adapter.iter.cfg.configBeforeFrozen
  94. cfg.DisallowUnknownFields = true
  95. adapter.iter.cfg = cfg.frozeWithCacheReuse()
  96. }
  97. // NewEncoder same as json.NewEncoder
  98. func NewEncoder(writer io.Writer) *Encoder {
  99. return ConfigDefault.NewEncoder(writer)
  100. }
  101. // Encoder same as json.Encoder
  102. type Encoder struct {
  103. stream *Stream
  104. }
  105. // Encode encode interface{} as JSON to io.Writer
  106. func (adapter *Encoder) Encode(val interface{}) error {
  107. adapter.stream.WriteVal(val)
  108. adapter.stream.WriteRaw("\n")
  109. adapter.stream.Flush()
  110. return adapter.stream.Error
  111. }
  112. // SetIndent set the indention. Prefix is not supported
  113. func (adapter *Encoder) SetIndent(prefix, indent string) {
  114. config := adapter.stream.cfg.configBeforeFrozen
  115. config.IndentionStep = len(indent)
  116. adapter.stream.cfg = config.frozeWithCacheReuse()
  117. }
  118. // SetEscapeHTML escape html by default, set to false to disable
  119. func (adapter *Encoder) SetEscapeHTML(escapeHTML bool) {
  120. config := adapter.stream.cfg.configBeforeFrozen
  121. config.EscapeHTML = escapeHTML
  122. adapter.stream.cfg = config.frozeWithCacheReuse()
  123. }
  124. // Valid reports whether data is a valid JSON encoding.
  125. func Valid(data []byte) bool {
  126. return ConfigDefault.Valid(data)
  127. }