feature_adapter.go 3.7 KB

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