1
0
mirror of https://github.com/json-iterator/go.git synced 2024-11-24 08:22:14 +02:00
json-iterator/feature_adapter.go

102 lines
2.1 KiB
Go
Raw Normal View History

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
// 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{}
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
}