feature_adapter.go 3.6 KB

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