feature_adapter.go 4.0 KB

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