mirror of
https://github.com/json-iterator/go.git
synced 2024-11-30 08:36:43 +02:00
88 lines
1.8 KiB
Go
88 lines
1.8 KiB
Go
package jsoniter
|
|
|
|
import (
|
|
"io"
|
|
"bytes"
|
|
)
|
|
|
|
// Unmarshal adapts to json/encoding APIs
|
|
func Unmarshal(data []byte, v interface{}) error {
|
|
iter := ParseBytes(data)
|
|
iter.ReadVal(v)
|
|
if iter.head == iter.tail {
|
|
iter.loadMore()
|
|
}
|
|
if iter.Error == io.EOF {
|
|
return nil
|
|
}
|
|
if iter.Error == nil {
|
|
iter.reportError("UnmarshalAny", "there are bytes left after unmarshal")
|
|
}
|
|
return iter.Error
|
|
}
|
|
|
|
func UnmarshalAny(data []byte) (Any, error) {
|
|
iter := ParseBytes(data)
|
|
any := iter.ReadAny()
|
|
if iter.head == iter.tail {
|
|
iter.loadMore()
|
|
}
|
|
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
|
|
}
|
|
|
|
func UnmarshalFromString(str string, v interface{}) error {
|
|
data := []byte(str)
|
|
iter := ParseBytes(data)
|
|
iter.ReadVal(v)
|
|
if iter.head == iter.tail {
|
|
iter.loadMore()
|
|
}
|
|
if iter.Error == io.EOF {
|
|
return nil
|
|
}
|
|
if iter.Error == nil {
|
|
iter.reportError("UnmarshalFromString", "there are bytes left after unmarshal")
|
|
}
|
|
return iter.Error
|
|
}
|
|
|
|
func UnmarshalAnyFromString(str string) (Any, error) {
|
|
data := []byte(str)
|
|
iter := ParseBytes(data)
|
|
any := iter.ReadAny()
|
|
if iter.head == iter.tail {
|
|
iter.loadMore()
|
|
}
|
|
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
|
|
}
|
|
|
|
func Marshal(v interface{}) ([]byte, error) {
|
|
buf := &bytes.Buffer{}
|
|
stream := NewStream(buf, 4096)
|
|
stream.WriteVal(v)
|
|
stream.Flush()
|
|
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
|
|
} |