feature_adapter.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. // UnmarshalAny adapts to
  25. func UnmarshalAny(data []byte) (Any, error) {
  26. return ConfigDefault.UnmarshalAny(data)
  27. }
  28. func lastNotSpacePos(data []byte) int {
  29. for i := len(data) - 1; i >= 0; i-- {
  30. if data[i] != ' ' && data[i] != '\t' && data[i] != '\r' && data[i] != '\n' {
  31. return i + 1
  32. }
  33. }
  34. return 0
  35. }
  36. func UnmarshalFromString(str string, v interface{}) error {
  37. return ConfigDefault.UnmarshalFromString(str, v)
  38. }
  39. func UnmarshalAnyFromString(str string) (Any, error) {
  40. return ConfigDefault.UnmarshalAnyFromString(str)
  41. }
  42. // Marshal adapts to json/encoding Marshal API
  43. //
  44. // Marshal returns the JSON encoding of v, adapts to json/encoding Marshal API
  45. // Refer to https://godoc.org/encoding/json#Marshal for more information
  46. func Marshal(v interface{}) ([]byte, error) {
  47. return ConfigDefault.Marshal(v)
  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 AdaptedDecoder is returned
  57. // Refer to https://godoc.org/encoding/json#NewDecoder for more information
  58. func NewDecoder(reader io.Reader) *AdaptedDecoder {
  59. return ConfigDefault.NewDecoder(reader)
  60. }
  61. // AdaptedDecoder reads and decodes JSON values from an input stream.
  62. // AdaptedDecoder provides identical APIs with json/stream Decoder (Token() and UseNumber() are in progress)
  63. type AdaptedDecoder struct {
  64. iter *Iterator
  65. }
  66. func (adapter *AdaptedDecoder) 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 *AdaptedDecoder) More() bool {
  75. return adapter.iter.head != adapter.iter.tail
  76. }
  77. func (adapter *AdaptedDecoder) Buffered() io.Reader {
  78. remaining := adapter.iter.buf[adapter.iter.head:adapter.iter.tail]
  79. return bytes.NewReader(remaining)
  80. }
  81. func (decoder *AdaptedDecoder) UseNumber() {
  82. origCfg := decoder.iter.cfg.configBeforeFrozen
  83. origCfg.UseNumber = true
  84. decoder.iter.cfg = origCfg.Froze()
  85. }
  86. func NewEncoder(writer io.Writer) *AdaptedEncoder {
  87. return ConfigDefault.NewEncoder(writer)
  88. }
  89. type AdaptedEncoder struct {
  90. stream *Stream
  91. }
  92. func (adapter *AdaptedEncoder) Encode(val interface{}) error {
  93. adapter.stream.WriteVal(val)
  94. adapter.stream.Flush()
  95. return adapter.stream.Error
  96. }
  97. func (adapter *AdaptedEncoder) SetIndent(prefix, indent string) {
  98. adapter.stream.cfg.indentionStep = len(indent)
  99. }
  100. func (adapter *AdaptedEncoder) SetEscapeHTML(escapeHtml bool) {
  101. config := adapter.stream.cfg.configBeforeFrozen
  102. config.EscapeHtml = escapeHtml
  103. adapter.stream.cfg = config.Froze()
  104. }