feature_adapter.go 3.2 KB

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