2016-12-04 16:50:53 +02:00
|
|
|
package jsoniter
|
|
|
|
|
2017-01-06 14:17:47 +02:00
|
|
|
import (
|
|
|
|
"io"
|
2017-01-07 06:28:16 +02:00
|
|
|
"bytes"
|
2017-01-06 14:17:47 +02:00
|
|
|
)
|
2016-12-05 07:20:27 +02:00
|
|
|
|
2017-01-05 15:23:08 +02:00
|
|
|
// Unmarshal adapts to json/encoding APIs
|
2016-12-04 16:50:53 +02:00
|
|
|
func Unmarshal(data []byte, v interface{}) error {
|
2017-05-05 11:44:09 +02:00
|
|
|
data = data[:lastNotSpacePos(data)]
|
2016-12-04 16:50:53 +02:00
|
|
|
iter := ParseBytes(data)
|
2017-01-09 11:47:21 +02:00
|
|
|
iter.ReadVal(v)
|
2017-01-23 02:33:43 +02:00
|
|
|
if iter.head == iter.tail {
|
|
|
|
iter.loadMore()
|
|
|
|
}
|
2016-12-05 07:20:27 +02:00
|
|
|
if iter.Error == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
2017-01-22 17:29:48 +02:00
|
|
|
if iter.Error == nil {
|
2017-02-07 03:24:36 +02:00
|
|
|
iter.reportError("Unmarshal", "there are bytes left after unmarshal")
|
2017-01-22 17:29:48 +02:00
|
|
|
}
|
2016-12-04 16:50:53 +02:00
|
|
|
return iter.Error
|
|
|
|
}
|
2017-01-06 14:17:47 +02:00
|
|
|
|
2017-01-22 17:29:48 +02:00
|
|
|
func UnmarshalAny(data []byte) (Any, error) {
|
2017-05-05 11:44:09 +02:00
|
|
|
data = data[:lastNotSpacePos(data)]
|
2017-01-22 17:29:48 +02:00
|
|
|
iter := ParseBytes(data)
|
|
|
|
any := iter.ReadAny()
|
2017-01-23 02:33:43 +02:00
|
|
|
if iter.head == iter.tail {
|
|
|
|
iter.loadMore()
|
|
|
|
}
|
2017-01-22 17:29:48 +02:00
|
|
|
if iter.Error == io.EOF {
|
|
|
|
return any, nil
|
|
|
|
}
|
|
|
|
if iter.Error == nil {
|
|
|
|
iter.reportError("UnmarshalAny", "there are bytes left after unmarshal")
|
|
|
|
}
|
|
|
|
return any, iter.Error
|
|
|
|
}
|
|
|
|
|
2017-05-05 11:44:09 +02:00
|
|
|
func lastNotSpacePos(data []byte) int {
|
|
|
|
for i := len(data) - 1; i >= 0; i-- {
|
|
|
|
if data[i] != ' ' && data[i] != '\t' && data[i] != '\r' && data[i] != '\n' {
|
|
|
|
return i + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2017-01-07 06:28:16 +02:00
|
|
|
func UnmarshalFromString(str string, v interface{}) error {
|
2017-01-23 02:33:43 +02:00
|
|
|
data := []byte(str)
|
2017-05-05 11:44:09 +02:00
|
|
|
data = data[:lastNotSpacePos(data)]
|
2017-01-06 14:17:47 +02:00
|
|
|
iter := ParseBytes(data)
|
2017-01-09 11:47:21 +02:00
|
|
|
iter.ReadVal(v)
|
2017-01-23 02:33:43 +02:00
|
|
|
if iter.head == iter.tail {
|
|
|
|
iter.loadMore()
|
|
|
|
}
|
2017-01-06 14:17:47 +02:00
|
|
|
if iter.Error == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
2017-01-22 17:29:48 +02:00
|
|
|
if iter.Error == nil {
|
|
|
|
iter.reportError("UnmarshalFromString", "there are bytes left after unmarshal")
|
|
|
|
}
|
2017-01-06 14:17:47 +02:00
|
|
|
return iter.Error
|
|
|
|
}
|
2017-01-07 06:28:16 +02:00
|
|
|
|
2017-01-22 17:29:48 +02:00
|
|
|
func UnmarshalAnyFromString(str string) (Any, error) {
|
2017-01-23 02:33:43 +02:00
|
|
|
data := []byte(str)
|
2017-05-05 11:44:09 +02:00
|
|
|
data = data[:lastNotSpacePos(data)]
|
2017-01-22 17:29:48 +02:00
|
|
|
iter := ParseBytes(data)
|
|
|
|
any := iter.ReadAny()
|
2017-01-23 02:33:43 +02:00
|
|
|
if iter.head == iter.tail {
|
|
|
|
iter.loadMore()
|
|
|
|
}
|
2017-01-22 17:29:48 +02:00
|
|
|
if iter.Error == io.EOF {
|
|
|
|
return any, nil
|
|
|
|
}
|
|
|
|
if iter.Error == nil {
|
|
|
|
iter.reportError("UnmarshalAnyFromString", "there are bytes left after unmarshal")
|
|
|
|
}
|
|
|
|
return nil, iter.Error
|
|
|
|
}
|
|
|
|
|
2017-01-07 06:28:16 +02:00
|
|
|
func Marshal(v interface{}) ([]byte, error) {
|
|
|
|
buf := &bytes.Buffer{}
|
2017-05-26 01:52:54 +02:00
|
|
|
stream := NewStream(buf, 512)
|
2017-01-07 06:28:16 +02:00
|
|
|
stream.WriteVal(v)
|
2017-01-09 13:19:48 +02:00
|
|
|
stream.Flush()
|
2017-01-07 06:28:16 +02:00
|
|
|
if stream.Error != nil {
|
|
|
|
return nil, stream.Error
|
|
|
|
}
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func MarshalToString(v interface{}) (string, error) {
|
|
|
|
buf, err := Marshal(v)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(buf), nil
|
2017-05-05 11:44:09 +02:00
|
|
|
}
|
2017-06-02 09:38:20 +02:00
|
|
|
|
|
|
|
func NewDecoder(reader io.Reader) *AdaptedDecoder {
|
|
|
|
iter := Parse(reader, 512)
|
|
|
|
return &AdaptedDecoder{iter}
|
|
|
|
}
|
|
|
|
|
|
|
|
type AdaptedDecoder struct {
|
|
|
|
iter *Iterator
|
|
|
|
}
|
|
|
|
|
|
|
|
func (adapter *AdaptedDecoder) Decode(obj interface{}) error {
|
|
|
|
adapter.iter.ReadVal(obj)
|
2017-06-02 10:06:33 +02:00
|
|
|
err := adapter.iter.Error
|
|
|
|
if err == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
2017-06-02 09:38:20 +02:00
|
|
|
return adapter.iter.Error
|
2017-06-02 10:00:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (adapter *AdaptedDecoder) More() bool {
|
|
|
|
return adapter.iter.head != adapter.iter.tail
|
|
|
|
}
|
|
|
|
|
|
|
|
func (adapter *AdaptedDecoder) Buffered() io.Reader {
|
|
|
|
remaining := adapter.iter.buf[adapter.iter.head:adapter.iter.tail]
|
|
|
|
return bytes.NewReader(remaining)
|
2017-06-02 09:38:20 +02:00
|
|
|
}
|