1
0
mirror of https://github.com/json-iterator/go.git synced 2024-11-30 08:36:43 +02:00
json-iterator/feature_adapter.go

125 lines
3.5 KiB
Go
Raw Normal View History

2017-06-11 09:32:58 +02:00
// Package jsoniter implements encoding and decoding of JSON as defined in
// RFC 4627 and provides interfaces with identical syntax of standard lib encoding/json.
// Converting from encoding/json to jsoniter is no more than replacing the package with jsoniter
// and variable type declarations (if any).
// jsoniter interfaces gives 100% compatibility with code using standard lib.
//
// "JSON and Go"
// (https://golang.org/doc/articles/json_and_go.html)
2017-06-11 10:28:31 +02:00
// gives a description of how Marshal/Unmarshal operate
2017-06-11 09:32:58 +02:00
// between arbitrary or predefined json objects and bytes,
2017-06-11 10:28:31 +02:00
// and it applies to jsoniter.Marshal/Unmarshal as well.
2016-12-04 16:50:53 +02:00
package jsoniter
2017-01-06 14:17:47 +02:00
import (
2017-01-07 06:28:16 +02:00
"bytes"
"io"
2017-01-06 14:17:47 +02:00
)
2016-12-05 07:20:27 +02:00
2017-06-19 17:10:20 +02:00
type RawMessage []byte
2017-06-05 13:31:30 +02:00
// Unmarshal adapts to json/encoding Unmarshal API
//
// Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v.
2017-06-05 13:57:20 +02:00
// Refer to https://godoc.org/encoding/json#Unmarshal for more information
2016-12-04 16:50:53 +02:00
func Unmarshal(data []byte, v interface{}) error {
2017-06-17 04:21:37 +02:00
return ConfigDefault.Unmarshal(data, v)
2016-12-04 16:50:53 +02:00
}
2017-01-06 14:17:47 +02:00
2017-05-05 11:44:09 +02: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 06:28:16 +02:00
func UnmarshalFromString(str string, v interface{}) error {
2017-06-17 04:21:37 +02:00
return ConfigDefault.UnmarshalFromString(str, v)
2017-01-06 14:17:47 +02:00
}
2017-01-07 06:28:16 +02:00
func Get(data []byte, path ...interface{}) Any {
return ConfigDefault.Get(data, path...)
2017-01-22 17:29:48 +02:00
}
2017-06-05 13:31:30 +02:00
// Marshal adapts to json/encoding Marshal API
2017-06-05 13:14:40 +02:00
//
// Marshal returns the JSON encoding of v, adapts to json/encoding Marshal API
2017-06-05 13:57:20 +02:00
// Refer to https://godoc.org/encoding/json#Marshal for more information
2017-01-07 06:28:16 +02:00
func Marshal(v interface{}) ([]byte, error) {
2017-06-17 04:21:37 +02:00
return ConfigDefault.Marshal(v)
2017-01-07 06:28:16 +02:00
}
2017-06-29 14:48:27 +02:00
func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) {
return ConfigDefault.MarshalIndent(v, prefix, indent)
}
2017-01-07 06:28:16 +02:00
func MarshalToString(v interface{}) (string, error) {
2017-06-17 04:21:37 +02:00
return ConfigDefault.MarshalToString(v)
2017-05-05 11:44:09 +02:00
}
2017-06-02 09:38:20 +02:00
2017-06-05 13:31:30 +02:00
// NewDecoder adapts to json/stream NewDecoder API.
//
// NewDecoder returns a new decoder that reads from r.
2017-06-05 13:57:20 +02:00
//
// Instead of a json/encoding Decoder, an Decoder is returned
2017-06-05 13:57:20 +02:00
// Refer to https://godoc.org/encoding/json#NewDecoder for more information
func NewDecoder(reader io.Reader) *Decoder {
2017-06-17 04:21:37 +02:00
return ConfigDefault.NewDecoder(reader)
2017-06-02 09:38:20 +02:00
}
// Decoder reads and decodes JSON values from an input stream.
// Decoder provides identical APIs with json/stream Decoder (Token() and UseNumber() are in progress)
type Decoder struct {
2017-06-02 09:38:20 +02:00
iter *Iterator
}
func (adapter *Decoder) Decode(obj interface{}) error {
2017-06-02 09:38:20 +02:00
adapter.iter.ReadVal(obj)
2017-06-02 10:06:33 +02:00
err := adapter.iter.Error
if err == io.EOF {
return nil
}
2017-06-02 09:38:20 +02:00
return adapter.iter.Error
2017-06-02 10:00:12 +02:00
}
func (adapter *Decoder) More() bool {
2017-06-02 10:00:12 +02:00
return adapter.iter.head != adapter.iter.tail
}
func (adapter *Decoder) Buffered() io.Reader {
2017-06-02 10:00:12 +02:00
remaining := adapter.iter.buf[adapter.iter.head:adapter.iter.tail]
return bytes.NewReader(remaining)
2017-06-02 12:46:44 +02:00
}
func (decoder *Decoder) UseNumber() {
origCfg := decoder.iter.cfg.configBeforeFrozen
origCfg.UseNumber = true
decoder.iter.cfg = origCfg.Froze()
2017-06-06 17:15:15 +02:00
}
func NewEncoder(writer io.Writer) *Encoder {
2017-06-17 04:21:37 +02:00
return ConfigDefault.NewEncoder(writer)
2017-06-02 12:46:44 +02:00
}
type Encoder struct {
2017-06-02 12:46:44 +02:00
stream *Stream
}
func (adapter *Encoder) Encode(val interface{}) error {
2017-06-02 12:46:44 +02:00
adapter.stream.WriteVal(val)
adapter.stream.Flush()
return adapter.stream.Error
}
func (adapter *Encoder) SetIndent(prefix, indent string) {
adapter.stream.cfg.indentionStep = len(indent)
}
2017-06-15 18:10:05 +02:00
func (adapter *Encoder) SetEscapeHTML(escapeHtml bool) {
2017-06-15 18:10:05 +02:00
config := adapter.stream.cfg.configBeforeFrozen
config.EscapeHtml = escapeHtml
adapter.stream.cfg = config.Froze()
}