feature_adapter.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. // Unmarshal adapts to json/encoding Unmarshal API
  18. //
  19. // Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v.
  20. // Refer to https://godoc.org/encoding/json#Unmarshal for more information
  21. func Unmarshal(data []byte, v interface{}) error {
  22. return ConfigDefault.Unmarshal(data, v)
  23. }
  24. func lastNotSpacePos(data []byte) int {
  25. for i := len(data) - 1; i >= 0; i-- {
  26. if data[i] != ' ' && data[i] != '\t' && data[i] != '\r' && data[i] != '\n' {
  27. return i + 1
  28. }
  29. }
  30. return 0
  31. }
  32. func UnmarshalFromString(str string, v interface{}) error {
  33. return ConfigDefault.UnmarshalFromString(str, v)
  34. }
  35. func Get(data []byte, path ...interface{}) Any {
  36. return ConfigDefault.Get(data, path...)
  37. }
  38. // Marshal adapts to json/encoding Marshal API
  39. //
  40. // Marshal returns the JSON encoding of v, adapts to json/encoding Marshal API
  41. // Refer to https://godoc.org/encoding/json#Marshal for more information
  42. func Marshal(v interface{}) ([]byte, error) {
  43. return ConfigDefault.Marshal(v)
  44. }
  45. func MarshalToString(v interface{}) (string, error) {
  46. return ConfigDefault.MarshalToString(v)
  47. }
  48. // NewDecoder adapts to json/stream NewDecoder API.
  49. //
  50. // NewDecoder returns a new decoder that reads from r.
  51. //
  52. // Instead of a json/encoding Decoder, an AdaptedDecoder is returned
  53. // Refer to https://godoc.org/encoding/json#NewDecoder for more information
  54. func NewDecoder(reader io.Reader) *AdaptedDecoder {
  55. return ConfigDefault.NewDecoder(reader)
  56. }
  57. // AdaptedDecoder reads and decodes JSON values from an input stream.
  58. // AdaptedDecoder provides identical APIs with json/stream Decoder (Token() and UseNumber() are in progress)
  59. type AdaptedDecoder struct {
  60. iter *Iterator
  61. }
  62. func (adapter *AdaptedDecoder) Decode(obj interface{}) error {
  63. adapter.iter.ReadVal(obj)
  64. err := adapter.iter.Error
  65. if err == io.EOF {
  66. return nil
  67. }
  68. return adapter.iter.Error
  69. }
  70. func (adapter *AdaptedDecoder) More() bool {
  71. return adapter.iter.head != adapter.iter.tail
  72. }
  73. func (adapter *AdaptedDecoder) Buffered() io.Reader {
  74. remaining := adapter.iter.buf[adapter.iter.head:adapter.iter.tail]
  75. return bytes.NewReader(remaining)
  76. }
  77. func (decoder *AdaptedDecoder) UseNumber() {
  78. origCfg := decoder.iter.cfg.configBeforeFrozen
  79. origCfg.UseNumber = true
  80. decoder.iter.cfg = origCfg.Froze()
  81. }
  82. func NewEncoder(writer io.Writer) *AdaptedEncoder {
  83. return ConfigDefault.NewEncoder(writer)
  84. }
  85. type AdaptedEncoder struct {
  86. stream *Stream
  87. }
  88. func (adapter *AdaptedEncoder) Encode(val interface{}) error {
  89. adapter.stream.WriteVal(val)
  90. adapter.stream.Flush()
  91. return adapter.stream.Error
  92. }
  93. func (adapter *AdaptedEncoder) SetIndent(prefix, indent string) {
  94. adapter.stream.cfg.indentionStep = len(indent)
  95. }
  96. func (adapter *AdaptedEncoder) SetEscapeHTML(escapeHtml bool) {
  97. config := adapter.stream.cfg.configBeforeFrozen
  98. config.EscapeHtml = escapeHtml
  99. adapter.stream.cfg = config.Froze()
  100. }