go-json/json.go

44 lines
884 B
Go
Raw Normal View History

2020-04-19 13:51:22 +03:00
package json
2020-04-21 08:19:50 +03:00
import "bytes"
2020-04-19 13:51:22 +03:00
func Marshal(v interface{}) ([]byte, error) {
2020-04-21 08:19:50 +03:00
var b *bytes.Buffer
enc := NewEncoder(b)
2020-05-02 17:35:41 +03:00
enc.SetIndent("", "")
bytes, err := enc.encodeForMarshal(v)
if err != nil {
enc.release()
return nil, err
}
enc.release()
return bytes, nil
}
func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) {
var b *bytes.Buffer
enc := NewEncoder(b)
enc.SetIndent(prefix, indent)
2020-04-24 14:23:26 +03:00
bytes, err := enc.encodeForMarshal(v)
if err != nil {
enc.release()
return nil, err
}
enc.release()
return bytes, nil
2020-04-19 13:51:22 +03:00
}
2020-04-22 07:06:52 +03:00
func Unmarshal(data []byte, v interface{}) error {
src := make([]byte, len(data))
copy(src, data)
var dec Decoder
return dec.decodeForUnmarshal(src, v)
}
2020-04-24 15:57:11 +03:00
func UnmarshalNoEscape(data []byte, v interface{}) error {
src := make([]byte, len(data))
copy(src, data)
var dec Decoder
return dec.decodeForUnmarshalNoEscape(src, v)
}