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

128 lines
3.7 KiB
Go
Raw Normal View History

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-07-09 10:26:30 +02:00
// RawMessage to make replace json with jsoniter
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-07-09 10:26:30 +02:00
// UnmarshalFromString convenient method to read from string instead of []byte
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
2017-07-09 10:26:30 +02:00
// Get quick method to get value from deeply nested JSON structure
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-07-09 10:26:30 +02:00
// MarshalIndent same as json.MarshalIndent. Prefix is not supported.
2017-06-29 14:48:27 +02:00
func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) {
return ConfigDefault.MarshalIndent(v, prefix, indent)
}
2017-07-09 10:26:30 +02:00
// MarshalToString convenient method to write as string instead of []byte
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
}
2017-07-09 10:26:30 +02:00
// Decode decode JSON into interface{}
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
}
2017-07-09 10:26:30 +02:00
// More is there more?
func (adapter *Decoder) More() bool {
2017-06-02 10:00:12 +02:00
return adapter.iter.head != adapter.iter.tail
}
2017-07-09 10:26:30 +02:00
// Buffered remaining buffer
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
}
// UseNumber for number JSON element, use float64 or json.NumberValue (alias of string)
2017-07-09 10:26:30 +02:00
func (adapter *Decoder) UseNumber() {
origCfg := adapter.iter.cfg.configBeforeFrozen
origCfg.UseNumber = true
2017-07-09 10:26:30 +02:00
adapter.iter.cfg = origCfg.Froze().(*frozenConfig)
2017-06-06 17:15:15 +02:00
}
2017-07-09 10:26:30 +02:00
// NewEncoder same as json.NewEncoder
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
}
2017-07-09 10:26:30 +02:00
// Encoder same as json.Encoder
type Encoder struct {
2017-06-02 12:46:44 +02:00
stream *Stream
}
2017-07-09 10:26:30 +02:00
// Encode encode interface{} as JSON to io.Writer
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
}
2017-07-09 10:26:30 +02:00
// SetIndent set the indention. Prefix is not supported
func (adapter *Encoder) SetIndent(prefix, indent string) {
adapter.stream.cfg.indentionStep = len(indent)
}
2017-06-15 18:10:05 +02:00
2017-07-09 10:26:30 +02:00
// SetEscapeHTML escape html by default, set to false to disable
func (adapter *Encoder) SetEscapeHTML(escapeHTML bool) {
2017-06-15 18:10:05 +02:00
config := adapter.stream.cfg.configBeforeFrozen
2017-07-09 10:26:30 +02:00
config.EscapeHTML = escapeHTML
2017-07-09 10:09:23 +02:00
adapter.stream.cfg = config.Froze().(*frozenConfig)
2017-06-15 18:10:05 +02:00
}