feature_adapter.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Package jsoniter implements encoding and decoding of JSON as defined in
  2. // RFC 4627 and provides interfaces with identical syntax of standard lib encoding/json.
  3. // Converting from encoding/json to jsoniter is no more than replacing the package with jsoniter
  4. // and variable type declarations (if any).
  5. // jsoniter interfaces gives 100% compatibility with code using standard lib.
  6. //
  7. // "JSON and Go"
  8. // (https://golang.org/doc/articles/json_and_go.html)
  9. // gives a description of how Marshal/Unmarshal operate
  10. // between arbitrary or predefined json objects and bytes,
  11. // and it applies to jsoniter.Marshal/Unmarshal as well.
  12. package jsoniter
  13. import (
  14. "bytes"
  15. "io"
  16. )
  17. type RawMessage []byte
  18. // Unmarshal adapts to json/encoding Unmarshal API
  19. //
  20. // Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v.
  21. // Refer to https://godoc.org/encoding/json#Unmarshal for more information
  22. func Unmarshal(data []byte, v interface{}) error {
  23. return ConfigDefault.Unmarshal(data, v)
  24. }
  25. func lastNotSpacePos(data []byte) int {
  26. for i := len(data) - 1; i >= 0; i-- {
  27. if data[i] != ' ' && data[i] != '\t' && data[i] != '\r' && data[i] != '\n' {
  28. return i + 1
  29. }
  30. }
  31. return 0
  32. }
  33. func UnmarshalFromString(str string, v interface{}) error {
  34. return ConfigDefault.UnmarshalFromString(str, v)
  35. }
  36. func Get(data []byte, path ...interface{}) Any {
  37. return ConfigDefault.Get(data, path...)
  38. }
  39. // Marshal adapts to json/encoding Marshal API
  40. //
  41. // Marshal returns the JSON encoding of v, adapts to json/encoding Marshal API
  42. // Refer to https://godoc.org/encoding/json#Marshal for more information
  43. func Marshal(v interface{}) ([]byte, error) {
  44. return ConfigDefault.Marshal(v)
  45. }
  46. func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) {
  47. return ConfigDefault.MarshalIndent(v, prefix, indent)
  48. }
  49. func MarshalToString(v interface{}) (string, error) {
  50. return ConfigDefault.MarshalToString(v)
  51. }
  52. // NewDecoder adapts to json/stream NewDecoder API.
  53. //
  54. // NewDecoder returns a new decoder that reads from r.
  55. //
  56. // Instead of a json/encoding Decoder, an Decoder is returned
  57. // Refer to https://godoc.org/encoding/json#NewDecoder for more information
  58. func NewDecoder(reader io.Reader) *Decoder {
  59. return ConfigDefault.NewDecoder(reader)
  60. }
  61. // Decoder reads and decodes JSON values from an input stream.
  62. // Decoder provides identical APIs with json/stream Decoder (Token() and UseNumber() are in progress)
  63. type Decoder struct {
  64. iter *Iterator
  65. }
  66. func (adapter *Decoder) Decode(obj interface{}) error {
  67. adapter.iter.ReadVal(obj)
  68. err := adapter.iter.Error
  69. if err == io.EOF {
  70. return nil
  71. }
  72. return adapter.iter.Error
  73. }
  74. func (adapter *Decoder) More() bool {
  75. return adapter.iter.head != adapter.iter.tail
  76. }
  77. func (adapter *Decoder) Buffered() io.Reader {
  78. remaining := adapter.iter.buf[adapter.iter.head:adapter.iter.tail]
  79. return bytes.NewReader(remaining)
  80. }
  81. func (decoder *Decoder) UseNumber() {
  82. origCfg := decoder.iter.cfg.configBeforeFrozen
  83. origCfg.UseNumber = true
  84. decoder.iter.cfg = origCfg.Froze()
  85. }
  86. func NewEncoder(writer io.Writer) *Encoder {
  87. return ConfigDefault.NewEncoder(writer)
  88. }
  89. type Encoder struct {
  90. stream *Stream
  91. }
  92. func (adapter *Encoder) Encode(val interface{}) error {
  93. adapter.stream.WriteVal(val)
  94. adapter.stream.Flush()
  95. return adapter.stream.Error
  96. }
  97. func (adapter *Encoder) SetIndent(prefix, indent string) {
  98. adapter.stream.cfg.indentionStep = len(indent)
  99. }
  100. func (adapter *Encoder) SetEscapeHTML(escapeHtml bool) {
  101. config := adapter.stream.cfg.configBeforeFrozen
  102. config.EscapeHtml = escapeHtml
  103. adapter.stream.cfg = config.Froze()
  104. }