feature_adapter.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package jsoniter
  2. import (
  3. "io"
  4. "bytes"
  5. )
  6. // Unmarshal adapts to json/encoding Unmarshal API
  7. //
  8. // Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v.
  9. // Refer to https://godoc.org/encoding/json#Unmarshal for more information
  10. func Unmarshal(data []byte, v interface{}) error {
  11. data = data[:lastNotSpacePos(data)]
  12. iter := ParseBytes(data)
  13. iter.ReadVal(v)
  14. if iter.head == iter.tail {
  15. iter.loadMore()
  16. }
  17. if iter.Error == io.EOF {
  18. return nil
  19. }
  20. if iter.Error == nil {
  21. iter.reportError("Unmarshal", "there are bytes left after unmarshal")
  22. }
  23. return iter.Error
  24. }
  25. func UnmarshalAny(data []byte) (Any, error) {
  26. data = data[:lastNotSpacePos(data)]
  27. iter := ParseBytes(data)
  28. any := iter.ReadAny()
  29. if iter.head == iter.tail {
  30. iter.loadMore()
  31. }
  32. if iter.Error == io.EOF {
  33. return any, nil
  34. }
  35. if iter.Error == nil {
  36. iter.reportError("UnmarshalAny", "there are bytes left after unmarshal")
  37. }
  38. return any, iter.Error
  39. }
  40. func lastNotSpacePos(data []byte) int {
  41. for i := len(data) - 1; i >= 0; i-- {
  42. if data[i] != ' ' && data[i] != '\t' && data[i] != '\r' && data[i] != '\n' {
  43. return i + 1
  44. }
  45. }
  46. return 0
  47. }
  48. func UnmarshalFromString(str string, v interface{}) error {
  49. data := []byte(str)
  50. data = data[:lastNotSpacePos(data)]
  51. iter := ParseBytes(data)
  52. iter.ReadVal(v)
  53. if iter.head == iter.tail {
  54. iter.loadMore()
  55. }
  56. if iter.Error == io.EOF {
  57. return nil
  58. }
  59. if iter.Error == nil {
  60. iter.reportError("UnmarshalFromString", "there are bytes left after unmarshal")
  61. }
  62. return iter.Error
  63. }
  64. func UnmarshalAnyFromString(str string) (Any, error) {
  65. data := []byte(str)
  66. data = data[:lastNotSpacePos(data)]
  67. iter := ParseBytes(data)
  68. any := iter.ReadAny()
  69. if iter.head == iter.tail {
  70. iter.loadMore()
  71. }
  72. if iter.Error == io.EOF {
  73. return any, nil
  74. }
  75. if iter.Error == nil {
  76. iter.reportError("UnmarshalAnyFromString", "there are bytes left after unmarshal")
  77. }
  78. return nil, iter.Error
  79. }
  80. // Marshal adapts to json/encoding Marshal API
  81. //
  82. // Marshal returns the JSON encoding of v, adapts to json/encoding Marshal API
  83. // Refer to https://godoc.org/encoding/json#Marshal for more information
  84. func Marshal(v interface{}) ([]byte, error) {
  85. stream := NewStream(nil, 256)
  86. stream.WriteVal(v)
  87. if stream.Error != nil {
  88. return nil, stream.Error
  89. }
  90. return stream.Buffer(), nil
  91. }
  92. func MarshalToString(v interface{}) (string, error) {
  93. buf, err := Marshal(v)
  94. if err != nil {
  95. return "", err
  96. }
  97. return string(buf), nil
  98. }
  99. // NewDecoder adapts to json/stream NewDecoder API.
  100. //
  101. // NewDecoder returns a new decoder that reads from r.
  102. //
  103. // Instead of a json/encoding Decoder, an AdaptedDecoder is returned
  104. // Refer to https://godoc.org/encoding/json#NewDecoder for more information
  105. func NewDecoder(reader io.Reader) *AdaptedDecoder {
  106. iter := Parse(reader, 512)
  107. return &AdaptedDecoder{iter}
  108. }
  109. // AdaptedDecoder reads and decodes JSON values from an input stream.
  110. // AdaptedDecoder provides identical APIs with json/stream Decoder (Token() and UseNumber() are in progress)
  111. type AdaptedDecoder struct {
  112. iter *Iterator
  113. }
  114. func (adapter *AdaptedDecoder) Decode(obj interface{}) error {
  115. adapter.iter.ReadVal(obj)
  116. err := adapter.iter.Error
  117. if err == io.EOF {
  118. return nil
  119. }
  120. return adapter.iter.Error
  121. }
  122. func (adapter *AdaptedDecoder) More() bool {
  123. return adapter.iter.head != adapter.iter.tail
  124. }
  125. func (adapter *AdaptedDecoder) Buffered() io.Reader {
  126. remaining := adapter.iter.buf[adapter.iter.head:adapter.iter.tail]
  127. return bytes.NewReader(remaining)
  128. }
  129. func NewEncoder(writer io.Writer) *AdaptedEncoder {
  130. stream := NewStream(writer, 512)
  131. return &AdaptedEncoder{stream}
  132. }
  133. type AdaptedEncoder struct {
  134. stream *Stream
  135. }
  136. func (adapter *AdaptedEncoder) Encode(val interface{}) error {
  137. adapter.stream.WriteVal(val)
  138. adapter.stream.Flush()
  139. return adapter.stream.Error
  140. }
  141. func (adapter *AdaptedEncoder) SetIndent(prefix, indent string) {
  142. // not implemented yet
  143. }