feature_adapter.go 4.0 KB

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