1
0
mirror of https://github.com/json-iterator/go.git synced 2025-04-20 11:28:49 +02:00
json-iterator/feature_adapter.go

102 lines
2.1 KiB
Go
Raw Normal View History

2016-12-04 22:50:53 +08:00
package jsoniter
2017-01-06 20:17:47 +08:00
import (
"io"
2017-01-07 12:28:16 +08:00
"bytes"
2017-01-06 20:17:47 +08:00
)
2016-12-05 13:20:27 +08:00
// Unmarshal adapts to json/encoding APIs
2016-12-04 22:50:53 +08:00
func Unmarshal(data []byte, v interface{}) error {
2017-05-05 17:44:09 +08:00
data = data[:lastNotSpacePos(data)]
2016-12-04 22:50:53 +08:00
iter := ParseBytes(data)
2017-01-09 17:47:21 +08:00
iter.ReadVal(v)
2017-01-23 08:33:43 +08:00
if iter.head == iter.tail {
iter.loadMore()
}
2016-12-05 13:20:27 +08:00
if iter.Error == io.EOF {
return nil
}
2017-01-22 23:29:48 +08:00
if iter.Error == nil {
2017-02-07 09:24:36 +08:00
iter.reportError("Unmarshal", "there are bytes left after unmarshal")
2017-01-22 23:29:48 +08:00
}
2016-12-04 22:50:53 +08:00
return iter.Error
}
2017-01-06 20:17:47 +08:00
2017-01-22 23:29:48 +08:00
func UnmarshalAny(data []byte) (Any, error) {
2017-05-05 17:44:09 +08:00
data = data[:lastNotSpacePos(data)]
2017-01-22 23:29:48 +08:00
iter := ParseBytes(data)
any := iter.ReadAny()
2017-01-23 08:33:43 +08:00
if iter.head == iter.tail {
iter.loadMore()
}
2017-01-22 23:29:48 +08: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 17:44:09 +08: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 12:28:16 +08:00
func UnmarshalFromString(str string, v interface{}) error {
2017-01-23 08:33:43 +08:00
data := []byte(str)
2017-05-05 17:44:09 +08:00
data = data[:lastNotSpacePos(data)]
2017-01-06 20:17:47 +08:00
iter := ParseBytes(data)
2017-01-09 17:47:21 +08:00
iter.ReadVal(v)
2017-01-23 08:33:43 +08:00
if iter.head == iter.tail {
iter.loadMore()
}
2017-01-06 20:17:47 +08:00
if iter.Error == io.EOF {
return nil
}
2017-01-22 23:29:48 +08:00
if iter.Error == nil {
iter.reportError("UnmarshalFromString", "there are bytes left after unmarshal")
}
2017-01-06 20:17:47 +08:00
return iter.Error
}
2017-01-07 12:28:16 +08:00
2017-01-22 23:29:48 +08:00
func UnmarshalAnyFromString(str string) (Any, error) {
2017-01-23 08:33:43 +08:00
data := []byte(str)
2017-05-05 17:44:09 +08:00
data = data[:lastNotSpacePos(data)]
2017-01-22 23:29:48 +08:00
iter := ParseBytes(data)
any := iter.ReadAny()
2017-01-23 08:33:43 +08:00
if iter.head == iter.tail {
iter.loadMore()
}
2017-01-22 23:29:48 +08: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 12:28:16 +08:00
func Marshal(v interface{}) ([]byte, error) {
buf := &bytes.Buffer{}
stream := NewStream(buf, 4096)
stream.WriteVal(v)
2017-01-09 19:19:48 +08:00
stream.Flush()
2017-01-07 12:28:16 +08: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 17:44:09 +08:00
}