feature_adapter.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. func lastNotSpacePos(data []byte) int {
  16. for i := len(data) - 1; i >= 0; i-- {
  17. if data[i] != ' ' && data[i] != '\t' && data[i] != '\r' && data[i] != '\n' {
  18. return i + 1
  19. }
  20. }
  21. return 0
  22. }
  23. // UnmarshalFromString convenient method to read from string instead of []byte
  24. func UnmarshalFromString(str string, v interface{}) error {
  25. return ConfigDefault.UnmarshalFromString(str, v)
  26. }
  27. // Get quick method to get value from deeply nested JSON structure
  28. func Get(data []byte, path ...interface{}) Any {
  29. return ConfigDefault.Get(data, path...)
  30. }
  31. // Marshal adapts to json/encoding Marshal API
  32. //
  33. // Marshal returns the JSON encoding of v, adapts to json/encoding Marshal API
  34. // Refer to https://godoc.org/encoding/json#Marshal for more information
  35. func Marshal(v interface{}) ([]byte, error) {
  36. return ConfigDefault.Marshal(v)
  37. }
  38. // MarshalIndent same as json.MarshalIndent. Prefix is not supported.
  39. func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) {
  40. return ConfigDefault.MarshalIndent(v, prefix, indent)
  41. }
  42. // MarshalToString convenient method to write as string instead of []byte
  43. func MarshalToString(v interface{}) (string, error) {
  44. return ConfigDefault.MarshalToString(v)
  45. }
  46. // NewDecoder adapts to json/stream NewDecoder API.
  47. //
  48. // NewDecoder returns a new decoder that reads from r.
  49. //
  50. // Instead of a json/encoding Decoder, an Decoder is returned
  51. // Refer to https://godoc.org/encoding/json#NewDecoder for more information
  52. func NewDecoder(reader io.Reader) *Decoder {
  53. return ConfigDefault.NewDecoder(reader)
  54. }
  55. // Decoder reads and decodes JSON values from an input stream.
  56. // Decoder provides identical APIs with json/stream Decoder (Token() and UseNumber() are in progress)
  57. type Decoder struct {
  58. iter *Iterator
  59. }
  60. // Decode decode JSON into interface{}
  61. func (adapter *Decoder) Decode(obj interface{}) error {
  62. adapter.iter.ReadVal(obj)
  63. err := adapter.iter.Error
  64. if err == io.EOF {
  65. return nil
  66. }
  67. return adapter.iter.Error
  68. }
  69. // More is there more?
  70. func (adapter *Decoder) More() bool {
  71. return adapter.iter.head != adapter.iter.tail
  72. }
  73. // Buffered remaining buffer
  74. func (adapter *Decoder) Buffered() io.Reader {
  75. remaining := adapter.iter.buf[adapter.iter.head:adapter.iter.tail]
  76. return bytes.NewReader(remaining)
  77. }
  78. // UseNumber for number JSON element, use float64 or json.NumberValue (alias of string)
  79. func (adapter *Decoder) UseNumber() {
  80. origCfg := adapter.iter.cfg.configBeforeFrozen
  81. origCfg.UseNumber = true
  82. adapter.iter.cfg = origCfg.Froze().(*frozenConfig)
  83. }
  84. // NewEncoder same as json.NewEncoder
  85. func NewEncoder(writer io.Writer) *Encoder {
  86. return ConfigDefault.NewEncoder(writer)
  87. }
  88. // Encoder same as json.Encoder
  89. type Encoder struct {
  90. stream *Stream
  91. }
  92. // Encode encode interface{} as JSON to io.Writer
  93. func (adapter *Encoder) Encode(val interface{}) error {
  94. adapter.stream.WriteVal(val)
  95. adapter.stream.WriteRaw("\n")
  96. adapter.stream.Flush()
  97. return adapter.stream.Error
  98. }
  99. // SetIndent set the indention. Prefix is not supported
  100. func (adapter *Encoder) SetIndent(prefix, indent string) {
  101. adapter.stream.cfg.indentionStep = len(indent)
  102. }
  103. // SetEscapeHTML escape html by default, set to false to disable
  104. func (adapter *Encoder) SetEscapeHTML(escapeHTML bool) {
  105. config := adapter.stream.cfg.configBeforeFrozen
  106. config.EscapeHTML = escapeHTML
  107. adapter.stream.cfg = config.Froze().(*frozenConfig)
  108. }
  109. // Valid reports whether data is a valid JSON encoding.
  110. func Valid(data []byte) bool {
  111. return ConfigDefault.Valid(data)
  112. }