mirror of
https://github.com/json-iterator/go.git
synced 2025-04-17 11:26:35 +02:00
document public symbols
This commit is contained in:
parent
db3f5046d7
commit
3606750b83
@ -5,6 +5,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// RawMessage to make replace json with jsoniter
|
||||||
type RawMessage []byte
|
type RawMessage []byte
|
||||||
|
|
||||||
// Unmarshal adapts to json/encoding Unmarshal API
|
// Unmarshal adapts to json/encoding Unmarshal API
|
||||||
@ -24,10 +25,12 @@ func lastNotSpacePos(data []byte) int {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UnmarshalFromString convenient method to read from string instead of []byte
|
||||||
func UnmarshalFromString(str string, v interface{}) error {
|
func UnmarshalFromString(str string, v interface{}) error {
|
||||||
return ConfigDefault.UnmarshalFromString(str, v)
|
return ConfigDefault.UnmarshalFromString(str, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get quick method to get value from deeply nested JSON structure
|
||||||
func Get(data []byte, path ...interface{}) Any {
|
func Get(data []byte, path ...interface{}) Any {
|
||||||
return ConfigDefault.Get(data, path...)
|
return ConfigDefault.Get(data, path...)
|
||||||
}
|
}
|
||||||
@ -40,10 +43,12 @@ func Marshal(v interface{}) ([]byte, error) {
|
|||||||
return ConfigDefault.Marshal(v)
|
return ConfigDefault.Marshal(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MarshalIndent same as json.MarshalIndent. Prefix is not supported.
|
||||||
func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) {
|
func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) {
|
||||||
return ConfigDefault.MarshalIndent(v, prefix, indent)
|
return ConfigDefault.MarshalIndent(v, prefix, indent)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MarshalToString convenient method to write as string instead of []byte
|
||||||
func MarshalToString(v interface{}) (string, error) {
|
func MarshalToString(v interface{}) (string, error) {
|
||||||
return ConfigDefault.MarshalToString(v)
|
return ConfigDefault.MarshalToString(v)
|
||||||
}
|
}
|
||||||
@ -64,6 +69,7 @@ type Decoder struct {
|
|||||||
iter *Iterator
|
iter *Iterator
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Decode decode JSON into interface{}
|
||||||
func (adapter *Decoder) Decode(obj interface{}) error {
|
func (adapter *Decoder) Decode(obj interface{}) error {
|
||||||
adapter.iter.ReadVal(obj)
|
adapter.iter.ReadVal(obj)
|
||||||
err := adapter.iter.Error
|
err := adapter.iter.Error
|
||||||
@ -73,41 +79,49 @@ func (adapter *Decoder) Decode(obj interface{}) error {
|
|||||||
return adapter.iter.Error
|
return adapter.iter.Error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// More is there more?
|
||||||
func (adapter *Decoder) More() bool {
|
func (adapter *Decoder) More() bool {
|
||||||
return adapter.iter.head != adapter.iter.tail
|
return adapter.iter.head != adapter.iter.tail
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Buffered remaining buffer
|
||||||
func (adapter *Decoder) Buffered() io.Reader {
|
func (adapter *Decoder) Buffered() io.Reader {
|
||||||
remaining := adapter.iter.buf[adapter.iter.head:adapter.iter.tail]
|
remaining := adapter.iter.buf[adapter.iter.head:adapter.iter.tail]
|
||||||
return bytes.NewReader(remaining)
|
return bytes.NewReader(remaining)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (decoder *Decoder) UseNumber() {
|
// UseNumber for number JSON element, use float64 or json.Number (alias of string)
|
||||||
origCfg := decoder.iter.cfg.configBeforeFrozen
|
func (adapter *Decoder) UseNumber() {
|
||||||
|
origCfg := adapter.iter.cfg.configBeforeFrozen
|
||||||
origCfg.UseNumber = true
|
origCfg.UseNumber = true
|
||||||
decoder.iter.cfg = origCfg.Froze().(*frozenConfig)
|
adapter.iter.cfg = origCfg.Froze().(*frozenConfig)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewEncoder same as json.NewEncoder
|
||||||
func NewEncoder(writer io.Writer) *Encoder {
|
func NewEncoder(writer io.Writer) *Encoder {
|
||||||
return ConfigDefault.NewEncoder(writer)
|
return ConfigDefault.NewEncoder(writer)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Encoder same as json.Encoder
|
||||||
type Encoder struct {
|
type Encoder struct {
|
||||||
stream *Stream
|
stream *Stream
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Encode encode interface{} as JSON to io.Writer
|
||||||
func (adapter *Encoder) Encode(val interface{}) error {
|
func (adapter *Encoder) Encode(val interface{}) error {
|
||||||
adapter.stream.WriteVal(val)
|
adapter.stream.WriteVal(val)
|
||||||
adapter.stream.Flush()
|
adapter.stream.Flush()
|
||||||
return adapter.stream.Error
|
return adapter.stream.Error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetIndent set the indention. Prefix is not supported
|
||||||
func (adapter *Encoder) SetIndent(prefix, indent string) {
|
func (adapter *Encoder) SetIndent(prefix, indent string) {
|
||||||
adapter.stream.cfg.indentionStep = len(indent)
|
adapter.stream.cfg.indentionStep = len(indent)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (adapter *Encoder) SetEscapeHTML(escapeHtml bool) {
|
// SetEscapeHTML escape html by default, set to false to disable
|
||||||
|
func (adapter *Encoder) SetEscapeHTML(escapeHTML bool) {
|
||||||
config := adapter.stream.cfg.configBeforeFrozen
|
config := adapter.stream.cfg.configBeforeFrozen
|
||||||
config.EscapeHTML = escapeHtml
|
config.EscapeHTML = escapeHTML
|
||||||
adapter.stream.cfg = config.Froze().(*frozenConfig)
|
adapter.stream.cfg = config.Froze().(*frozenConfig)
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,8 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Any generic object representation.
|
||||||
|
// The lazy json implementation holds []byte and parse lazily.
|
||||||
type Any interface {
|
type Any interface {
|
||||||
LastError() error
|
LastError() error
|
||||||
ValueType() ValueType
|
ValueType() ValueType
|
||||||
@ -47,30 +49,37 @@ func (any *baseAny) ToVal(obj interface{}) {
|
|||||||
panic("not implemented")
|
panic("not implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WrapInt32 turn int32 into Any interface
|
||||||
func WrapInt32(val int32) Any {
|
func WrapInt32(val int32) Any {
|
||||||
return &int32Any{baseAny{}, val}
|
return &int32Any{baseAny{}, val}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WrapInt64 turn int64 into Any interface
|
||||||
func WrapInt64(val int64) Any {
|
func WrapInt64(val int64) Any {
|
||||||
return &int64Any{baseAny{}, val}
|
return &int64Any{baseAny{}, val}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WrapUint32 turn uint32 into Any interface
|
||||||
func WrapUint32(val uint32) Any {
|
func WrapUint32(val uint32) Any {
|
||||||
return &uint32Any{baseAny{}, val}
|
return &uint32Any{baseAny{}, val}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WrapUint64 turn uint64 into Any interface
|
||||||
func WrapUint64(val uint64) Any {
|
func WrapUint64(val uint64) Any {
|
||||||
return &uint64Any{baseAny{}, val}
|
return &uint64Any{baseAny{}, val}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WrapFloat64 turn float64 into Any interface
|
||||||
func WrapFloat64(val float64) Any {
|
func WrapFloat64(val float64) Any {
|
||||||
return &floatAny{baseAny{}, val}
|
return &floatAny{baseAny{}, val}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WrapString turn string into Any interface
|
||||||
func WrapString(val string) Any {
|
func WrapString(val string) Any {
|
||||||
return &stringAny{baseAny{}, val}
|
return &stringAny{baseAny{}, val}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Wrap turn a go object into Any interface
|
||||||
func Wrap(val interface{}) Any {
|
func Wrap(val interface{}) Any {
|
||||||
if val == nil {
|
if val == nil {
|
||||||
return &nilAny{}
|
return &nilAny{}
|
||||||
@ -79,8 +88,8 @@ func Wrap(val interface{}) Any {
|
|||||||
if isAny {
|
if isAny {
|
||||||
return asAny
|
return asAny
|
||||||
}
|
}
|
||||||
type_ := reflect.TypeOf(val)
|
typ := reflect.TypeOf(val)
|
||||||
switch type_.Kind() {
|
switch typ.Kind() {
|
||||||
case reflect.Slice:
|
case reflect.Slice:
|
||||||
return wrapArray(val)
|
return wrapArray(val)
|
||||||
case reflect.Struct:
|
case reflect.Struct:
|
||||||
@ -116,13 +125,13 @@ func Wrap(val interface{}) Any {
|
|||||||
case reflect.Bool:
|
case reflect.Bool:
|
||||||
if val.(bool) == true {
|
if val.(bool) == true {
|
||||||
return &trueAny{}
|
return &trueAny{}
|
||||||
} else {
|
}
|
||||||
return &falseAny{}
|
return &falseAny{}
|
||||||
}
|
}
|
||||||
}
|
return &invalidAny{baseAny{}, fmt.Errorf("unsupported type: %v", typ)}
|
||||||
return &invalidAny{baseAny{}, fmt.Errorf("unsupported type: %v", type_)}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ReadAny read next JSON element as an Any object. It is a better json.RawMessage.
|
||||||
func (iter *Iterator) ReadAny() Any {
|
func (iter *Iterator) ReadAny() Any {
|
||||||
return iter.readAny()
|
return iter.readAny()
|
||||||
}
|
}
|
||||||
@ -220,9 +229,8 @@ func locatePath(iter *Iterator, path []interface{}) Any {
|
|||||||
case int32:
|
case int32:
|
||||||
if '*' == pathKey {
|
if '*' == pathKey {
|
||||||
return iter.readAny().Get(path[i:]...)
|
return iter.readAny().Get(path[i:]...)
|
||||||
} else {
|
|
||||||
return newInvalidAny(path[i:])
|
|
||||||
}
|
}
|
||||||
|
return newInvalidAny(path[i:])
|
||||||
default:
|
default:
|
||||||
return newInvalidAny(path[i:])
|
return newInvalidAny(path[i:])
|
||||||
}
|
}
|
||||||
|
@ -112,6 +112,7 @@ func (encoder *funcEncoder) IsEmpty(ptr unsafe.Pointer) bool {
|
|||||||
|
|
||||||
// DecoderFunc the function form of TypeDecoder
|
// DecoderFunc the function form of TypeDecoder
|
||||||
type DecoderFunc func(ptr unsafe.Pointer, iter *Iterator)
|
type DecoderFunc func(ptr unsafe.Pointer, iter *Iterator)
|
||||||
|
|
||||||
// EncoderFunc the function form of TypeEncoder
|
// EncoderFunc the function form of TypeEncoder
|
||||||
type EncoderFunc func(ptr unsafe.Pointer, stream *Stream)
|
type EncoderFunc func(ptr unsafe.Pointer, stream *Stream)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user