1
0
mirror of https://github.com/json-iterator/go.git synced 2025-06-15 22:50:24 +02:00

int lazy any

This commit is contained in:
Tao Wen
2017-01-22 23:29:48 +08:00
parent 9df37bbd68
commit ba410b045b
7 changed files with 248 additions and 0 deletions

View File

@ -13,9 +13,24 @@ func Unmarshal(data []byte, v interface{}) error {
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.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 {
// safe to do the unsafe cast here, as str is always referenced in this scope
data := *(*[]byte)(unsafe.Pointer(&str))
@ -24,9 +39,26 @@ func UnmarshalFromString(str string, v interface{}) error {
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) {
// safe to do the unsafe cast here, as str is always referenced in this scope
data := *(*[]byte)(unsafe.Pointer(&str))
iter := ParseBytes(data)
any := iter.ReadAny()
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)