feature_adapter.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. // Marshal is an adapter to json.Marshal
  78. //
  79. // Marshal returns the JSON encoding of v, adapts to json/encoding Marshal API
  80. func Marshal(v interface{}) ([]byte, error) {
  81. buf := &bytes.Buffer{}
  82. stream := NewStream(buf, 512)
  83. stream.WriteVal(v)
  84. stream.Flush()
  85. if stream.Error != nil {
  86. return nil, stream.Error
  87. }
  88. return buf.Bytes(), nil
  89. }
  90. func MarshalToString(v interface{}) (string, error) {
  91. buf, err := Marshal(v)
  92. if err != nil {
  93. return "", err
  94. }
  95. return string(buf), nil
  96. }
  97. func NewDecoder(reader io.Reader) *AdaptedDecoder {
  98. iter := Parse(reader, 512)
  99. return &AdaptedDecoder{iter}
  100. }
  101. type AdaptedDecoder struct {
  102. iter *Iterator
  103. }
  104. func (adapter *AdaptedDecoder) Decode(obj interface{}) error {
  105. adapter.iter.ReadVal(obj)
  106. err := adapter.iter.Error
  107. if err == io.EOF {
  108. return nil
  109. }
  110. return adapter.iter.Error
  111. }
  112. func (adapter *AdaptedDecoder) More() bool {
  113. return adapter.iter.head != adapter.iter.tail
  114. }
  115. func (adapter *AdaptedDecoder) Buffered() io.Reader {
  116. remaining := adapter.iter.buf[adapter.iter.head:adapter.iter.tail]
  117. return bytes.NewReader(remaining)
  118. }
  119. func NewEncoder(writer io.Writer) *AdaptedEncoder {
  120. stream := NewStream(writer, 512)
  121. return &AdaptedEncoder{stream}
  122. }
  123. type AdaptedEncoder struct {
  124. stream *Stream
  125. }
  126. func (adapter *AdaptedEncoder) Encode(val interface{}) error {
  127. adapter.stream.WriteVal(val)
  128. adapter.stream.Flush()
  129. return adapter.stream.Error
  130. }
  131. func (adapter *AdaptedEncoder) SetIndent(prefix, indent string) {
  132. // not implemented yet
  133. }