| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- package jsoniter
- import (
- "io"
- "unsafe"
- "bytes"
- )
- // Unmarshal adapts to json/encoding APIs
- func Unmarshal(data []byte, v interface{}) error {
- iter := ParseBytes(data)
- iter.ReadVal(v)
- if iter.Error == io.EOF {
- return nil
- }
- return 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))
- iter := ParseBytes(data)
- iter.ReadVal(v)
- if iter.Error == io.EOF {
- return nil
- }
- return 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
- }
|