1
0
mirror of https://github.com/json-iterator/go.git synced 2025-06-06 22:36:25 +02:00
This commit is contained in:
Tao Wen 2017-06-12 10:13:22 +08:00
commit 64e500f3c8
5 changed files with 44 additions and 10 deletions

10
.idea/libraries/Go_SDK.xml generated Normal file
View File

@ -0,0 +1,10 @@
<component name="libraryTable">
<library name="Go SDK">
<CLASSES>
<root url="file:///usr/local/go/src" />
</CLASSES>
<SOURCES>
<root url="file:///usr/local/go/src" />
</SOURCES>
</library>
</component>

View File

@ -1,3 +1,14 @@
// 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)
// gives a description of how Marshal/Unmarshal operate
// between arbitrary or predefined json objects and bytes,
// and it applies to jsoniter.Marshal/Unmarshal as well.
package jsoniter package jsoniter
import ( import (
@ -34,6 +45,7 @@ func Unmarshal(data []byte, v interface{}) error {
return iter.Error return iter.Error
} }
// UnmarshalAny adapts to
func UnmarshalAny(data []byte) (Any, error) { func UnmarshalAny(data []byte) (Any, error) {
data = data[:lastNotSpacePos(data)] data = data[:lastNotSpacePos(data)]
iter := ParseBytes(data) iter := ParseBytes(data)

View File

@ -1,3 +1,9 @@
//
// Besides, jsoniter.Iterator provides a different set of interfaces
// iterating given bytes/string/reader
// and yielding parsed elements one by one.
// This set of interfaces reads input as required and gives
// better performance.
package jsoniter package jsoniter
import ( import (

View File

@ -9,19 +9,23 @@ import (
"unsafe" "unsafe"
) )
/* // Decoder is an internal type registered to cache as needed.
Reflection on type to create decoders, which is then cached // Don't confuse jsoniter.Decoder with json.Decoder.
Reflection on value is avoided as we can, as the reflect.Value itself will allocate, with following exceptions // For json.Decoder's adapter, refer to jsoniter.AdapterDecoder(todo link).
1. create instance of new value, for example *int will need a int to be allocated //
2. append to slice, if the existing cap is not enough, allocate will be done using Reflect.New // Reflection on type to create decoders, which is then cached
3. assignment to map, both key and value will be reflect.Value // Reflection on value is avoided as we can, as the reflect.Value itself will allocate, with following exceptions
For a simple struct binding, it will be reflect.Value free and allocation free // 1. create instance of new value, for example *int will need a int to be allocated
*/ // 2. append to slice, if the existing cap is not enough, allocate will be done using Reflect.New
// 3. assignment to map, both key and value will be reflect.Value
// For a simple struct binding, it will be reflect.Value free and allocation free
type Decoder interface { type Decoder interface {
decode(ptr unsafe.Pointer, iter *Iterator) decode(ptr unsafe.Pointer, iter *Iterator)
} }
// Encoder is an internal type registered to cache as needed.
// Don't confuse jsoniter.Encoder with json.Encoder.
// For json.Encoder's adapter, refer to jsoniter.AdapterEncoder(todo godoc link).
type Encoder interface { type Encoder interface {
isEmpty(ptr unsafe.Pointer) bool isEmpty(ptr unsafe.Pointer) bool
encode(ptr unsafe.Pointer, stream *Stream) encode(ptr unsafe.Pointer, stream *Stream)
@ -166,7 +170,7 @@ func CleanDecoders() {
atomic.StorePointer(&DECODERS, unsafe.Pointer(&map[string]Decoder{})) atomic.StorePointer(&DECODERS, unsafe.Pointer(&map[string]Decoder{}))
} }
// CleanEncoders cleans decoders registered or cached // CleanEncoders cleans encoders registered or cached
func CleanEncoders() { func CleanEncoders() {
typeEncoders = map[string]Encoder{} typeEncoders = map[string]Encoder{}
fieldEncoders = map[string]Encoder{} fieldEncoders = map[string]Encoder{}

View File

@ -75,6 +75,8 @@ func (stream *Stream) WriteFloat64Lossy(val float64) {
} }
} }
// EnableLossyFloatMarshalling keeps 10**(-6) precision
// for float variables for better performance.
func EnableLossyFloatMarshalling() { func EnableLossyFloatMarshalling() {
// for better performance // for better performance
RegisterTypeEncoder("float32", func(ptr unsafe.Pointer, stream *Stream) { RegisterTypeEncoder("float32", func(ptr unsafe.Pointer, stream *Stream) {