1
0
mirror of https://github.com/json-iterator/go.git synced 2025-06-15 22:50:24 +02:00

57 Commits

Author SHA1 Message Date
69bc64b6d8 #54 support sort map keys 2017-06-16 16:46:30 +08:00
e0e2423e9a #53 test compatibility without html escape 2017-06-16 16:03:02 +08:00
a6ea770365 #53 implement SetEscapeHtml 2017-06-16 00:10:05 +08:00
5f22e50c89 #53 support escapeHtml 2017-06-15 23:55:04 +08:00
d867c8ba5c #53 split config into Config and frozenConfig 2017-06-13 18:49:35 +08:00
d0418857ce #53 move current config EnableXXX 2017-06-13 17:47:40 +08:00
48e9f6ec84 move IndentionStep to config 2017-06-13 17:03:27 +08:00
acddcf5bbf #53 extract out config 2017-06-13 16:58:53 +08:00
788918b85d #56 nil map or array should be null not [] or {} 2017-06-13 09:14:19 +08:00
6e5817b773 Merge pull request #55 from thockin/output_tests
WIP: Tests to compare against stdlib
2017-06-12 01:47:50 -05:00
7480e41836 Add output tests for maps of builtins
This tests for exact stdlib compatibility.
2017-06-11 21:09:56 -07:00
9215b3c508 Add output tests for builtin types
This fuzzes a type, marshals it with stdlib and json-iterator, compares,
then unmarshals with stdlib and json-iterator and compares.  This is
checking for literal, byte-for-byte compatibility.

In every case the test is exactly the same.

It also include benchmark functions to compare stdlib vs json-iterator.

This depends on a couple PRs to be merged in gofuzz.
2017-06-11 21:04:59 -07:00
64e500f3c8 Merge branch 'master' of https://github.com/json-iterator/go 2017-06-12 10:13:22 +08:00
3307ce3ba2 #50 map key unlike object field, can contain escaped char 2017-06-12 10:13:13 +08:00
6f50f15678 decoder/encoder;float precision doc 2017-06-11 16:30:31 +08:00
cee09816e3 decoder/encoder;float precision doc 2017-06-11 16:28:31 +08:00
cdbad22d22 test more package description 2017-06-11 15:35:45 +08:00
b0c9f047e2 test more than one pakcage description 2017-06-11 15:32:58 +08:00
6bd13c2948 Merge branch 'master' of https://github.com/json-iterator/go 2017-06-09 17:06:38 +08:00
84ad508437 #48 should return error if concrete tpye unknown 2017-06-09 17:06:27 +08:00
4f909776cf Merge pull request #49 from zhaitianduo/master
Use jsoniter instead of json in example
2017-06-09 03:32:31 -05:00
962c470806 fix import not use 2017-06-09 16:28:20 +08:00
46d443fbad use jsoniter for example 2017-06-09 16:25:58 +08:00
2608d40f2a example unmarshal 2017-06-08 12:08:47 +08:00
3cf822853f example unmarshal 2017-06-08 12:07:03 +08:00
26708bccc9 report error when string end not found 2017-06-08 09:46:19 +08:00
d75b539bad add test for scientific float 2017-06-07 21:34:56 +08:00
cfffa29c8a gofmt 2017-06-06 23:27:00 +08:00
925df245d3 good enough indent implementation 2017-06-06 23:18:37 +08:00
962a8cd303 #40 support UseNumber 2017-06-06 23:15:15 +08:00
6509ba05df Merge pull request #41 from 1046102779/master
解析时,如果输出参数不是指针类型,直接报错,避免程序挂掉
2017-06-06 10:03:06 -05:00
579dbf3c1d Merge pull request #42 from 1046102779/patch-1
把floatDigits改为intDigits
2017-06-06 10:02:26 -05:00
aa5181db67 把floatDigits改为intDigits 2017-06-06 21:08:04 +08:00
67be6df2b1 Update feature_adapter.go 2017-06-06 20:01:43 +08:00
0f5379494a unmarshal failed return non-pointer error 2017-06-06 19:36:33 +08:00
d09e2419ba update benchmark 2017-06-06 16:55:32 +08:00
e1a71f6ba1 update benchmark 2017-06-06 16:54:26 +08:00
dcb78991c4 flush when buffer is large enough 2017-06-06 14:16:54 +08:00
9e8238cdc6 remove unused file 2017-06-06 12:41:13 +08:00
a4e5abf492 support []byte; marshal without copy 2017-06-06 09:44:56 +08:00
3979955e69 support TextMarshaler as map key 2017-06-06 00:09:33 +08:00
5fd09f0e02 remove mapInterfaceEncoder 2017-06-05 23:56:37 +08:00
af4982b22c support decode int key map 2017-06-05 23:53:48 +08:00
29dc1d407d write map with int key 2017-06-05 23:01:00 +08:00
5b27aaa62c update test 2017-06-05 22:10:01 +08:00
106636a191 update test 2017-06-05 22:08:28 +08:00
f50c4cfbbe Merge branch 'master' of https://github.com/json-iterator/go 2017-06-05 22:05:02 +08:00
87149ae489 add simple marshal benchmark 2017-06-05 22:04:52 +08:00
c0a4ad72e1 example test 2017-06-05 20:37:08 +08:00
404c0ee44b Decoder doc 2017-06-05 19:57:20 +08:00
10c1506f87 link test 2017-06-05 19:38:34 +08:00
9a43fe6468 adapter api comment 2017-06-05 19:31:30 +08:00
95e03f2937 Marshal comment 2017-06-05 19:19:46 +08:00
4406ed9e62 Marshal comment 2017-06-05 19:18:12 +08:00
ff027701f5 Marshal comment 2017-06-05 19:15:56 +08:00
c69b61f879 Marshal comment 2017-06-05 19:14:40 +08:00
d97f5db769 Marshal comment 2017-06-05 19:11:16 +08:00
199 changed files with 12750 additions and 935 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

@ -2,6 +2,23 @@
jsoniter (json-iterator) is fast and flexible JSON parser available in [Java](https://github.com/json-iterator/java) and [Go](https://github.com/json-iterator/go)
# Benchmark
![benchmark](http://jsoniter.com/benchmarks/go-benchmark.png)
Source code: https://github.com/json-iterator/go-benchmark/blob/master/src/github.com/json-iterator/go-benchmark/benchmark_medium_payload_test.go
Raw Result (easyjson requires static code generation)
| | ns/op | allocation bytes | allocation times |
| --- | --- | --- | --- |
| std decode | 35510 ns/op | 1960 B/op | 99 allocs/op |
| easyjson decode | 8499 ns/op | 160 B/op | 4 allocs/op |
| jsoniter decode | 5623 ns/op | 160 B/op | 3 allocs/op |
| std encode | 2213 ns/op | 712 B/op | 5 allocs/op |
| easyjson encode | 883 ns/op | 576 B/op | 3 allocs/op |
| jsoniter encode | 837 ns/op | 384 B/op | 4 allocs/op |
# Usage
100% compatibility with standard lib

View File

@ -1014,4 +1014,4 @@ func typeAndKind(v interface{}) (reflect.Type, reflect.Kind) {
k = t.Kind()
}
return t, k
}
}

47
example_test.go Normal file
View File

@ -0,0 +1,47 @@
package jsoniter_test
import (
"fmt"
"os"
"github.com/json-iterator/go"
)
func ExampleMarshal() {
type ColorGroup struct {
ID int
Name string
Colors []string
}
group := ColorGroup{
ID: 1,
Name: "Reds",
Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
}
b, err := jsoniter.Marshal(group)
if err != nil {
fmt.Println("error:", err)
}
os.Stdout.Write(b)
// Output:
// {"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]}
}
func ExampleUnmarshal() {
var jsonBlob = []byte(`[
{"Name": "Platypus", "Order": "Monotremata"},
{"Name": "Quoll", "Order": "Dasyuromorphia"}
]`)
type Animal struct {
Name string
Order string
}
var animals []Animal
err := jsoniter.Unmarshal(jsonBlob, &animals)
if err != nil {
fmt.Println("error:", err)
}
fmt.Printf("%+v", animals)
// Output:
// [{Name:Platypus Order:Monotremata} {Name:Quoll Order:Dasyuromorphia}]
}

View File

@ -1,41 +1,34 @@
// 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
import (
"io"
"bytes"
"encoding/json"
"io"
"unsafe"
)
// Unmarshal adapts to json/encoding APIs
// Unmarshal adapts to json/encoding Unmarshal API
//
// Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v.
// Refer to https://godoc.org/encoding/json#Unmarshal for more information
func Unmarshal(data []byte, v interface{}) error {
data = data[:lastNotSpacePos(data)]
iter := ParseBytes(data)
iter.ReadVal(v)
if iter.head == iter.tail {
iter.loadMore()
}
if iter.Error == io.EOF {
return nil
}
if iter.Error == nil {
iter.reportError("Unmarshal", "there are bytes left after unmarshal")
}
return iter.Error
return ConfigOfDefault.Unmarshal(data, v)
}
// UnmarshalAny adapts to
func UnmarshalAny(data []byte) (Any, error) {
data = data[:lastNotSpacePos(data)]
iter := ParseBytes(data)
any := iter.ReadAny()
if iter.head == iter.tail {
iter.loadMore()
}
if iter.Error == io.EOF {
return any, nil
}
if iter.Error == nil {
iter.reportError("UnmarshalAny", "there are bytes left after unmarshal")
}
return any, iter.Error
return ConfigOfDefault.UnmarshalAny(data)
}
func lastNotSpacePos(data []byte) int {
@ -48,63 +41,37 @@ func lastNotSpacePos(data []byte) int {
}
func UnmarshalFromString(str string, v interface{}) error {
data := []byte(str)
data = data[:lastNotSpacePos(data)]
iter := ParseBytes(data)
iter.ReadVal(v)
if iter.head == iter.tail {
iter.loadMore()
}
if iter.Error == io.EOF {
return nil
}
if iter.Error == nil {
iter.reportError("UnmarshalFromString", "there are bytes left after unmarshal")
}
return iter.Error
return ConfigOfDefault.UnmarshalFromString(str, v)
}
func UnmarshalAnyFromString(str string) (Any, error) {
data := []byte(str)
data = data[:lastNotSpacePos(data)]
iter := ParseBytes(data)
any := iter.ReadAny()
if iter.head == iter.tail {
iter.loadMore()
}
if iter.Error == io.EOF {
return any, nil
}
if iter.Error == nil {
iter.reportError("UnmarshalAnyFromString", "there are bytes left after unmarshal")
}
return nil, iter.Error
return ConfigOfDefault.UnmarshalAnyFromString(str)
}
// Marshal adapts to json/encoding Marshal API
//
// Marshal returns the JSON encoding of v, adapts to json/encoding Marshal API
// Refer to https://godoc.org/encoding/json#Marshal for more information
func Marshal(v interface{}) ([]byte, error) {
buf := &bytes.Buffer{}
stream := NewStream(buf, 512)
stream.WriteVal(v)
stream.Flush()
if stream.Error != nil {
return nil, stream.Error
}
return buf.Bytes(), nil
return ConfigOfDefault.Marshal(v)
}
func MarshalToString(v interface{}) (string, error) {
buf, err := Marshal(v)
if err != nil {
return "", err
}
return string(buf), nil
return ConfigOfDefault.MarshalToString(v)
}
// NewDecoder adapts to json/stream NewDecoder API.
//
// NewDecoder returns a new decoder that reads from r.
//
// Instead of a json/encoding Decoder, an AdaptedDecoder is returned
// Refer to https://godoc.org/encoding/json#NewDecoder for more information
func NewDecoder(reader io.Reader) *AdaptedDecoder {
iter := Parse(reader, 512)
return &AdaptedDecoder{iter}
return ConfigOfDefault.NewDecoder(reader)
}
// AdaptedDecoder reads and decodes JSON values from an input stream.
// AdaptedDecoder provides identical APIs with json/stream Decoder (Token() and UseNumber() are in progress)
type AdaptedDecoder struct {
iter *Iterator
}
@ -127,9 +94,18 @@ func (adapter *AdaptedDecoder) Buffered() io.Reader {
return bytes.NewReader(remaining)
}
func (decoder *AdaptedDecoder) UseNumber() {
RegisterTypeDecoder("interface {}", func(ptr unsafe.Pointer, iter *Iterator) {
if iter.WhatIsNext() == Number {
*((*interface{})(ptr)) = json.Number(iter.readNumberAsString())
} else {
*((*interface{})(ptr)) = iter.Read()
}
})
}
func NewEncoder(writer io.Writer) *AdaptedEncoder {
stream := NewStream(writer, 512)
return &AdaptedEncoder{stream}
return ConfigOfDefault.NewEncoder(writer)
}
type AdaptedEncoder struct {
@ -143,5 +119,11 @@ func (adapter *AdaptedEncoder) Encode(val interface{}) error {
}
func (adapter *AdaptedEncoder) SetIndent(prefix, indent string) {
// not implemented yet
}
adapter.stream.cfg.indentionStep = len(indent)
}
func (adapter *AdaptedEncoder) SetEscapeHTML(escapeHtml bool) {
config := adapter.stream.cfg.configBeforeFrozen
config.EscapeHtml = escapeHtml
adapter.stream.cfg = config.Froze()
}

View File

@ -1,9 +1,9 @@
package jsoniter
import (
"unsafe"
"fmt"
"reflect"
"unsafe"
)
type arrayLazyAny struct {
@ -22,7 +22,7 @@ func (any *arrayLazyAny) ValueType() ValueType {
func (any *arrayLazyAny) Parse() *Iterator {
iter := any.iter
if iter == nil {
iter = NewIterator()
iter = NewIterator(ConfigOfDefault)
any.iter = iter
}
iter.ResetBytes(any.remaining)
@ -44,7 +44,7 @@ func (any *arrayLazyAny) fillCacheUntil(target int) Any {
return any.cache[target]
}
iter := any.Parse()
if (len(any.remaining) == len(any.buf)) {
if len(any.remaining) == len(any.buf) {
iter.head++
c := iter.nextToken()
if c != ']' {
@ -287,7 +287,7 @@ func (any *arrayLazyAny) IterateArray() (func() (Any, bool), bool) {
// read from buffer
iter := any.iter
if iter == nil {
iter = NewIterator()
iter = NewIterator(ConfigOfDefault)
any.iter = iter
}
iter.ResetBytes(remaining)
@ -337,9 +337,9 @@ func (any *arrayLazyAny) GetInterface() interface{} {
type arrayAny struct {
baseAny
err error
cache []Any
val reflect.Value
err error
cache []Any
val reflect.Value
}
func wrapArray(val interface{}) *arrayAny {
@ -536,4 +536,4 @@ func (any *arrayAny) WriteTo(stream *Stream) {
func (any *arrayAny) GetInterface() interface{} {
any.fillCache()
return any.cache
}
}

View File

@ -2,22 +2,22 @@ package jsoniter
import (
"io"
"unsafe"
"strconv"
"unsafe"
)
type float64LazyAny struct {
baseAny
buf []byte
iter *Iterator
err error
buf []byte
iter *Iterator
err error
cache float64
}
func (any *float64LazyAny) Parse() *Iterator {
iter := any.iter
if iter == nil {
iter = NewIterator()
iter = NewIterator(ConfigOfDefault)
}
iter.ResetBytes(any.buf)
return iter
@ -163,4 +163,4 @@ func (any *floatAny) WriteTo(stream *Stream) {
func (any *floatAny) GetInterface() interface{} {
return any.val
}
}

View File

@ -67,4 +67,4 @@ func (any *int32Any) Parse() *Iterator {
func (any *int32Any) GetInterface() interface{} {
return any.val
}
}

View File

@ -2,8 +2,8 @@ package jsoniter
import (
"io"
"unsafe"
"strconv"
"unsafe"
)
type int64LazyAny struct {
@ -21,7 +21,7 @@ func (any *int64LazyAny) ValueType() ValueType {
func (any *int64LazyAny) Parse() *Iterator {
iter := any.iter
if iter == nil {
iter = NewIterator()
iter = NewIterator(ConfigOfDefault)
}
iter.ResetBytes(any.buf)
return iter
@ -163,4 +163,4 @@ func (any *int64Any) Parse() *Iterator {
func (any *int64Any) GetInterface() interface{} {
return any.val
}
}

View File

@ -62,4 +62,4 @@ func (any *nilAny) Parse() *Iterator {
func (any *nilAny) GetInterface() interface{} {
return nil
}
}

View File

@ -1,9 +1,9 @@
package jsoniter
import (
"unsafe"
"fmt"
"reflect"
"unsafe"
)
type objectLazyAny struct {
@ -22,7 +22,7 @@ func (any *objectLazyAny) ValueType() ValueType {
func (any *objectLazyAny) Parse() *Iterator {
iter := any.iter
if iter == nil {
iter = NewIterator()
iter = NewIterator(ConfigOfDefault)
any.iter = iter
}
iter.ResetBytes(any.remaining)
@ -308,7 +308,7 @@ func (any *objectLazyAny) IterateObject() (func() (string, Any, bool), bool) {
// read from buffer
iter := any.iter
if iter == nil {
iter = NewIterator()
iter = NewIterator(ConfigOfDefault)
any.iter = iter
}
iter.ResetBytes(remaining)

View File

@ -5,7 +5,7 @@ import (
"strconv"
)
type stringLazyAny struct{
type stringLazyAny struct {
baseAny
buf []byte
iter *Iterator
@ -20,7 +20,7 @@ func (any *stringLazyAny) ValueType() ValueType {
func (any *stringLazyAny) Parse() *Iterator {
iter := any.iter
if iter == nil {
iter = NewIterator()
iter = NewIterator(ConfigOfDefault)
any.iter = iter
}
iter.ResetBytes(any.buf)
@ -136,9 +136,9 @@ func (any *stringLazyAny) GetInterface() interface{} {
return any.cache
}
type stringAny struct{
type stringAny struct {
baseAny
err error
err error
val string
}
@ -146,7 +146,6 @@ func (any *stringAny) Parse() *Iterator {
return nil
}
func (any *stringAny) ValueType() ValueType {
return String
}
@ -228,4 +227,4 @@ func (any *stringAny) WriteTo(stream *Stream) {
func (any *stringAny) GetInterface() interface{} {
return any.val
}
}

View File

@ -1,12 +1,11 @@
package jsoniter
import (
"io"
"strconv"
"unsafe"
"io"
)
type uint64LazyAny struct {
baseAny
buf []byte
@ -22,7 +21,7 @@ func (any *uint64LazyAny) ValueType() ValueType {
func (any *uint64LazyAny) Parse() *Iterator {
iter := any.iter
if iter == nil {
iter = NewIterator()
iter = NewIterator(ConfigOfDefault)
}
iter.ResetBytes(any.buf)
return iter
@ -164,4 +163,4 @@ func (any *uint64Any) Parse() *Iterator {
func (any *uint64Any) GetInterface() interface{} {
return any.val
}
}

239
feature_config.go Normal file
View File

@ -0,0 +1,239 @@
package jsoniter
import (
"io"
"reflect"
"sync/atomic"
"unsafe"
"errors"
)
type Config struct {
IndentionStep int
MarshalFloatWith6Digits bool
SupportUnexportedStructFields bool
EscapeHtml bool
SortMapKeys bool
}
type frozenConfig struct {
configBeforeFrozen Config
sortMapKeys bool
indentionStep int
decoderCache unsafe.Pointer
encoderCache unsafe.Pointer
extensions []ExtensionFunc
}
var ConfigOfDefault = Config{}.Froze()
// Trying to be 100% compatible with standard library behavior
var ConfigCompatibleWithStandardLibrary = Config{
EscapeHtml: true,
SortMapKeys: true,
}.Froze()
func (cfg Config) Froze() *frozenConfig {
frozenConfig := &frozenConfig{
sortMapKeys: cfg.SortMapKeys,
indentionStep: cfg.IndentionStep,
}
atomic.StorePointer(&frozenConfig.decoderCache, unsafe.Pointer(&map[string]Decoder{}))
atomic.StorePointer(&frozenConfig.encoderCache, unsafe.Pointer(&map[string]Encoder{}))
if cfg.MarshalFloatWith6Digits {
frozenConfig.marshalFloatWith6Digits()
}
if cfg.SupportUnexportedStructFields {
frozenConfig.supportUnexportedStructFields()
}
if cfg.EscapeHtml {
frozenConfig.escapeHtml()
}
frozenConfig.configBeforeFrozen = cfg
return frozenConfig
}
// RegisterExtension can register a custom extension
func (cfg *frozenConfig) registerExtension(extension ExtensionFunc) {
cfg.extensions = append(cfg.extensions, extension)
}
func (cfg *frozenConfig) supportUnexportedStructFields() {
cfg.registerExtension(func(type_ reflect.Type, field *reflect.StructField) ([]string, EncoderFunc, DecoderFunc) {
return []string{field.Name}, nil, nil
})
}
// EnableLossyFloatMarshalling keeps 10**(-6) precision
// for float variables for better performance.
func (cfg *frozenConfig) marshalFloatWith6Digits() {
// for better performance
cfg.addEncoderToCache(reflect.TypeOf((*float32)(nil)).Elem(), &funcEncoder{func(ptr unsafe.Pointer, stream *Stream) {
val := *((*float32)(ptr))
stream.WriteFloat32Lossy(val)
}})
cfg.addEncoderToCache(reflect.TypeOf((*float64)(nil)).Elem(), &funcEncoder{func(ptr unsafe.Pointer, stream *Stream) {
val := *((*float64)(ptr))
stream.WriteFloat64Lossy(val)
}})
}
func (cfg *frozenConfig) escapeHtml() {
// for better performance
cfg.addEncoderToCache(reflect.TypeOf((*string)(nil)).Elem(), &funcEncoder{func(ptr unsafe.Pointer, stream *Stream) {
val := *((*string)(ptr))
stream.WriteStringWithHtmlEscaped(val)
}})
}
func (cfg *frozenConfig) addDecoderToCache(cacheKey reflect.Type, decoder Decoder) {
done := false
for !done {
ptr := atomic.LoadPointer(&cfg.decoderCache)
cache := *(*map[reflect.Type]Decoder)(ptr)
copied := map[reflect.Type]Decoder{}
for k, v := range cache {
copied[k] = v
}
copied[cacheKey] = decoder
done = atomic.CompareAndSwapPointer(&cfg.decoderCache, ptr, unsafe.Pointer(&copied))
}
}
func (cfg *frozenConfig) addEncoderToCache(cacheKey reflect.Type, encoder Encoder) {
done := false
for !done {
ptr := atomic.LoadPointer(&cfg.encoderCache)
cache := *(*map[reflect.Type]Encoder)(ptr)
copied := map[reflect.Type]Encoder{}
for k, v := range cache {
copied[k] = v
}
copied[cacheKey] = encoder
done = atomic.CompareAndSwapPointer(&cfg.encoderCache, ptr, unsafe.Pointer(&copied))
}
}
func (cfg *frozenConfig) getDecoderFromCache(cacheKey reflect.Type) Decoder {
ptr := atomic.LoadPointer(&cfg.decoderCache)
cache := *(*map[reflect.Type]Decoder)(ptr)
return cache[cacheKey]
}
func (cfg *frozenConfig) getEncoderFromCache(cacheKey reflect.Type) Encoder {
ptr := atomic.LoadPointer(&cfg.encoderCache)
cache := *(*map[reflect.Type]Encoder)(ptr)
return cache[cacheKey]
}
// CleanDecoders cleans decoders registered or cached
func (cfg *frozenConfig) CleanDecoders() {
typeDecoders = map[string]Decoder{}
fieldDecoders = map[string]Decoder{}
atomic.StorePointer(&cfg.decoderCache, unsafe.Pointer(&map[string]Decoder{}))
}
// CleanEncoders cleans encoders registered or cached
func (cfg *frozenConfig) CleanEncoders() {
typeEncoders = map[string]Encoder{}
fieldEncoders = map[string]Encoder{}
atomic.StorePointer(&cfg.encoderCache, unsafe.Pointer(&map[string]Encoder{}))
}
func (cfg *frozenConfig) MarshalToString(v interface{}) (string, error) {
buf, err := cfg.Marshal(v)
if err != nil {
return "", err
}
return string(buf), nil
}
func (cfg *frozenConfig) Marshal(v interface{}) ([]byte, error) {
stream := NewStream(cfg, nil, 256)
stream.WriteVal(v)
if stream.Error != nil {
return nil, stream.Error
}
return stream.Buffer(), nil
}
func (cfg *frozenConfig) UnmarshalFromString(str string, v interface{}) error {
data := []byte(str)
data = data[:lastNotSpacePos(data)]
iter := ParseBytes(cfg, data)
iter.ReadVal(v)
if iter.head == iter.tail {
iter.loadMore()
}
if iter.Error == io.EOF {
return nil
}
if iter.Error == nil {
iter.reportError("UnmarshalFromString", "there are bytes left after unmarshal")
}
return iter.Error
}
func (cfg *frozenConfig) NewEncoder(writer io.Writer) *AdaptedEncoder {
stream := NewStream(cfg, writer, 512)
return &AdaptedEncoder{stream}
}
func (cfg *frozenConfig) NewDecoder(reader io.Reader) *AdaptedDecoder {
iter := Parse(cfg, reader, 512)
return &AdaptedDecoder{iter}
}
func (cfg *frozenConfig) UnmarshalAnyFromString(str string) (Any, error) {
data := []byte(str)
data = data[:lastNotSpacePos(data)]
iter := ParseBytes(cfg, data)
any := iter.ReadAny()
if iter.head == iter.tail {
iter.loadMore()
}
if iter.Error == io.EOF {
return any, nil
}
if iter.Error == nil {
iter.reportError("UnmarshalAnyFromString", "there are bytes left after unmarshal")
}
return nil, iter.Error
}
func (cfg *frozenConfig) UnmarshalAny(data []byte) (Any, error) {
data = data[:lastNotSpacePos(data)]
iter := ParseBytes(cfg, data)
any := iter.ReadAny()
if iter.head == iter.tail {
iter.loadMore()
}
if iter.Error == io.EOF {
return any, nil
}
if iter.Error == nil {
iter.reportError("UnmarshalAny", "there are bytes left after unmarshal")
}
return any, iter.Error
}
func (cfg *frozenConfig) Unmarshal(data []byte, v interface{}) error {
data = data[:lastNotSpacePos(data)]
iter := ParseBytes(cfg, data)
typ := reflect.TypeOf(v)
if typ.Kind() != reflect.Ptr {
// return non-pointer error
return errors.New("the second param must be ptr type")
}
iter.ReadVal(v)
if iter.head == iter.tail {
iter.loadMore()
}
if iter.Error == io.EOF {
return nil
}
if iter.Error == nil {
iter.reportError("Unmarshal", "there are bytes left after unmarshal")
}
return iter.Error
}

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
import (
@ -60,6 +66,7 @@ func init() {
// Iterator is a fast and flexible JSON parser
type Iterator struct {
cfg *frozenConfig
reader io.Reader
buf []byte
head int
@ -68,8 +75,9 @@ type Iterator struct {
}
// Create creates an empty Iterator instance
func NewIterator() *Iterator {
func NewIterator(cfg *frozenConfig) *Iterator {
return &Iterator{
cfg: cfg,
reader: nil,
buf: nil,
head: 0,
@ -78,8 +86,9 @@ func NewIterator() *Iterator {
}
// Parse parses a json buffer in io.Reader into an Iterator instance
func Parse(reader io.Reader, bufSize int) *Iterator {
func Parse(cfg *frozenConfig, reader io.Reader, bufSize int) *Iterator {
return &Iterator{
cfg: cfg,
reader: reader,
buf: make([]byte, bufSize),
head: 0,
@ -88,8 +97,9 @@ func Parse(reader io.Reader, bufSize int) *Iterator {
}
// ParseBytes parses a json byte slice into an Iterator instance
func ParseBytes(input []byte) *Iterator {
func ParseBytes(cfg *frozenConfig, input []byte) *Iterator {
return &Iterator{
cfg: cfg,
reader: nil,
buf: input,
head: 0,
@ -98,8 +108,8 @@ func ParseBytes(input []byte) *Iterator {
}
// ParseString parses a json string into an Iterator instance
func ParseString(input string) *Iterator {
return ParseBytes([]byte(input))
func ParseString(cfg *frozenConfig, input string) *Iterator {
return ParseBytes(cfg, []byte(input))
}
// Reset can reset an Iterator instance for another json buffer in io.Reader
@ -276,4 +286,3 @@ func (iter *Iterator) ReadBase64() (ret []byte) {
}
return ret[:n]
}

View File

@ -18,12 +18,11 @@ func (iter *Iterator) ReadArray() (ret bool) {
case ',':
return true
default:
iter.reportError("ReadArray", "expect [ or , or ] or n, but found: " + string([]byte{c}))
iter.reportError("ReadArray", "expect [ or , or ] or n, but found: "+string([]byte{c}))
return
}
}
func (iter *Iterator) ReadArrayCB(callback func(*Iterator) bool) (ret bool) {
c := iter.nextToken()
if c == '[' {
@ -46,6 +45,6 @@ func (iter *Iterator) ReadArrayCB(callback func(*Iterator) bool) (ret bool) {
iter.skipFixedBytes(3)
return true // null
}
iter.reportError("ReadArrayCB", "expect [ or n, but found: " + string([]byte{c}))
iter.reportError("ReadArrayCB", "expect [ or n, but found: "+string([]byte{c}))
return false
}
}

View File

@ -2,12 +2,13 @@ package jsoniter
import (
"io"
"math/big"
"strconv"
"unsafe"
"math/big"
)
var floatDigits []int8
const invalidCharForNumber = int8(-1)
const endOfNumber = int8(-2)
const dotInNumber = int8(-3)
@ -75,7 +76,7 @@ func (iter *Iterator) readPositiveFloat32() (ret float32) {
value := uint64(0)
c := byte(' ')
i := iter.head
non_decimal_loop:
non_decimal_loop:
for ; i < iter.tail; i++ {
c = iter.buf[i]
ind := floatDigits[c]
@ -91,22 +92,22 @@ func (iter *Iterator) readPositiveFloat32() (ret float32) {
if value > uint64SafeToMultiple10 {
return iter.readFloat32SlowPath()
}
value = (value << 3) + (value << 1) + uint64(ind); // value = value * 10 + ind;
value = (value << 3) + (value << 1) + uint64(ind) // value = value * 10 + ind;
}
if c == '.' {
i++
decimalPlaces := 0;
decimalPlaces := 0
for ; i < iter.tail; i++ {
c = iter.buf[i]
ind := floatDigits[c];
ind := floatDigits[c]
switch ind {
case endOfNumber:
if decimalPlaces > 0 && decimalPlaces < len(POW10) {
if decimalPlaces > 0 && decimalPlaces < len(_POW10) {
iter.head = i
return float32(float64(value) / float64(POW10[decimalPlaces]))
return float32(float64(value) / float64(_POW10[decimalPlaces]))
}
// too many decimal places
return iter.readFloat32SlowPath()
return iter.readFloat32SlowPath()
case invalidCharForNumber:
fallthrough
case dotInNumber:
@ -125,7 +126,7 @@ func (iter *Iterator) readPositiveFloat32() (ret float32) {
func (iter *Iterator) readNumberAsString() (ret string) {
strBuf := [16]byte{}
str := strBuf[0:0]
load_loop:
load_loop:
for {
for i := iter.head; i < iter.tail; i++ {
c := iter.buf[i]
@ -178,7 +179,7 @@ func (iter *Iterator) readPositiveFloat64() (ret float64) {
value := uint64(0)
c := byte(' ')
i := iter.head
non_decimal_loop:
non_decimal_loop:
for ; i < iter.tail; i++ {
c = iter.buf[i]
ind := floatDigits[c]
@ -194,19 +195,19 @@ func (iter *Iterator) readPositiveFloat64() (ret float64) {
if value > uint64SafeToMultiple10 {
return iter.readFloat64SlowPath()
}
value = (value << 3) + (value << 1) + uint64(ind); // value = value * 10 + ind;
value = (value << 3) + (value << 1) + uint64(ind) // value = value * 10 + ind;
}
if c == '.' {
i++
decimalPlaces := 0;
decimalPlaces := 0
for ; i < iter.tail; i++ {
c = iter.buf[i]
ind := floatDigits[c];
ind := floatDigits[c]
switch ind {
case endOfNumber:
if decimalPlaces > 0 && decimalPlaces < len(POW10) {
if decimalPlaces > 0 && decimalPlaces < len(_POW10) {
iter.head = i
return float64(value) / float64(POW10[decimalPlaces])
return float64(value) / float64(_POW10[decimalPlaces])
}
// too many decimal places
return iter.readFloat64SlowPath()

View File

@ -6,8 +6,8 @@ import (
var intDigits []int8
const uint32SafeToMultiply10 = uint32(0xffffffff) / 10 - 1
const uint64SafeToMultiple10 = uint64(0xffffffffffffffff) / 10 - 1
const uint32SafeToMultiply10 = uint32(0xffffffff)/10 - 1
const uint64SafeToMultiple10 = uint64(0xffffffffffffffff)/10 - 1
const int64Max = uint64(0x7fffffffffffffff)
const int32Max = uint32(0x7fffffff)
const int16Max = uint32(0x7fff)
@ -17,7 +17,7 @@ const uint8Max = uint32(0xffff)
func init() {
intDigits = make([]int8, 256)
for i := 0; i < len(floatDigits); i++ {
for i := 0; i < len(intDigits); i++ {
intDigits[i] = invalidCharForNumber
}
for i := int8('0'); i <= int8('9'); i++ {
@ -37,15 +37,15 @@ func (iter *Iterator) ReadInt8() (ret int8) {
c := iter.nextToken()
if c == '-' {
val := iter.readUint32(iter.readByte())
if val > int8Max + 1 {
iter.reportError("ReadInt8", "overflow: " + strconv.FormatInt(int64(val), 10))
if val > int8Max+1 {
iter.reportError("ReadInt8", "overflow: "+strconv.FormatInt(int64(val), 10))
return
}
return -int8(val)
} else {
val := iter.readUint32(c)
if val > int8Max {
iter.reportError("ReadInt8", "overflow: " + strconv.FormatInt(int64(val), 10))
iter.reportError("ReadInt8", "overflow: "+strconv.FormatInt(int64(val), 10))
return
}
return int8(val)
@ -55,7 +55,7 @@ func (iter *Iterator) ReadInt8() (ret int8) {
func (iter *Iterator) ReadUint8() (ret uint8) {
val := iter.readUint32(iter.nextToken())
if val > uint8Max {
iter.reportError("ReadUint8", "overflow: " + strconv.FormatInt(int64(val), 10))
iter.reportError("ReadUint8", "overflow: "+strconv.FormatInt(int64(val), 10))
return
}
return uint8(val)
@ -65,15 +65,15 @@ func (iter *Iterator) ReadInt16() (ret int16) {
c := iter.nextToken()
if c == '-' {
val := iter.readUint32(iter.readByte())
if val > int16Max + 1 {
iter.reportError("ReadInt16", "overflow: " + strconv.FormatInt(int64(val), 10))
if val > int16Max+1 {
iter.reportError("ReadInt16", "overflow: "+strconv.FormatInt(int64(val), 10))
return
}
return -int16(val)
} else {
val := iter.readUint32(c)
if val > int16Max {
iter.reportError("ReadInt16", "overflow: " + strconv.FormatInt(int64(val), 10))
iter.reportError("ReadInt16", "overflow: "+strconv.FormatInt(int64(val), 10))
return
}
return int16(val)
@ -83,7 +83,7 @@ func (iter *Iterator) ReadInt16() (ret int16) {
func (iter *Iterator) ReadUint16() (ret uint16) {
val := iter.readUint32(iter.nextToken())
if val > uint16Max {
iter.reportError("ReadUint16", "overflow: " + strconv.FormatInt(int64(val), 10))
iter.reportError("ReadUint16", "overflow: "+strconv.FormatInt(int64(val), 10))
return
}
return uint16(val)
@ -93,15 +93,15 @@ func (iter *Iterator) ReadInt32() (ret int32) {
c := iter.nextToken()
if c == '-' {
val := iter.readUint32(iter.readByte())
if val > int32Max + 1 {
iter.reportError("ReadInt32", "overflow: " + strconv.FormatInt(int64(val), 10))
if val > int32Max+1 {
iter.reportError("ReadInt32", "overflow: "+strconv.FormatInt(int64(val), 10))
return
}
return -int32(val)
} else {
val := iter.readUint32(c)
if val > int32Max {
iter.reportError("ReadInt32", "overflow: " + strconv.FormatInt(int64(val), 10))
iter.reportError("ReadInt32", "overflow: "+strconv.FormatInt(int64(val), 10))
return
}
return int32(val)
@ -118,11 +118,11 @@ func (iter *Iterator) readUint32(c byte) (ret uint32) {
return 0 // single zero
}
if ind == invalidCharForNumber {
iter.reportError("readUint32", "unexpected character: " + string([]byte{byte(ind)}))
iter.reportError("readUint32", "unexpected character: "+string([]byte{byte(ind)}))
return
}
value := uint32(ind)
if iter.tail - iter.head > 10 {
if iter.tail-iter.head > 10 {
i := iter.head
ind2 := intDigits[iter.buf[i]]
if ind2 == invalidCharForNumber {
@ -133,7 +133,7 @@ func (iter *Iterator) readUint32(c byte) (ret uint32) {
ind3 := intDigits[iter.buf[i]]
if ind3 == invalidCharForNumber {
iter.head = i
return value * 10 + uint32(ind2)
return value*10 + uint32(ind2)
}
//iter.head = i + 1
//value = value * 100 + uint32(ind2) * 10 + uint32(ind3)
@ -141,35 +141,35 @@ func (iter *Iterator) readUint32(c byte) (ret uint32) {
ind4 := intDigits[iter.buf[i]]
if ind4 == invalidCharForNumber {
iter.head = i
return value * 100 + uint32(ind2) * 10 + uint32(ind3)
return value*100 + uint32(ind2)*10 + uint32(ind3)
}
i++
ind5 := intDigits[iter.buf[i]]
if ind5 == invalidCharForNumber {
iter.head = i
return value * 1000 + uint32(ind2) * 100 + uint32(ind3) * 10 + uint32(ind4)
return value*1000 + uint32(ind2)*100 + uint32(ind3)*10 + uint32(ind4)
}
i++
ind6 := intDigits[iter.buf[i]]
if ind6 == invalidCharForNumber {
iter.head = i
return value * 10000 + uint32(ind2) * 1000 + uint32(ind3) * 100 + uint32(ind4) * 10 + uint32(ind5)
return value*10000 + uint32(ind2)*1000 + uint32(ind3)*100 + uint32(ind4)*10 + uint32(ind5)
}
i++
ind7 := intDigits[iter.buf[i]]
if ind7 == invalidCharForNumber {
iter.head = i
return value * 100000 + uint32(ind2) * 10000 + uint32(ind3) * 1000 + uint32(ind4) * 100 + uint32(ind5) * 10 + uint32(ind6)
return value*100000 + uint32(ind2)*10000 + uint32(ind3)*1000 + uint32(ind4)*100 + uint32(ind5)*10 + uint32(ind6)
}
i++
ind8 := intDigits[iter.buf[i]]
if ind8 == invalidCharForNumber {
iter.head = i
return value * 1000000 + uint32(ind2) * 100000 + uint32(ind3) * 10000 + uint32(ind4) * 1000 + uint32(ind5) * 100 + uint32(ind6) * 10 + uint32(ind7)
return value*1000000 + uint32(ind2)*100000 + uint32(ind3)*10000 + uint32(ind4)*1000 + uint32(ind5)*100 + uint32(ind6)*10 + uint32(ind7)
}
i++
ind9 := intDigits[iter.buf[i]]
value = value * 10000000 + uint32(ind2) * 1000000 + uint32(ind3) * 100000 + uint32(ind4) * 10000 + uint32(ind5) * 1000 + uint32(ind6) * 100 + uint32(ind7) * 10 + uint32(ind8)
value = value*10000000 + uint32(ind2)*1000000 + uint32(ind3)*100000 + uint32(ind4)*10000 + uint32(ind5)*1000 + uint32(ind6)*100 + uint32(ind7)*10 + uint32(ind8)
iter.head = i
if ind9 == invalidCharForNumber {
return value
@ -194,7 +194,7 @@ func (iter *Iterator) readUint32(c byte) (ret uint32) {
}
value = (value << 3) + (value << 1) + uint32(ind)
}
if (!iter.loadMore()) {
if !iter.loadMore() {
return value
}
}
@ -204,15 +204,15 @@ func (iter *Iterator) ReadInt64() (ret int64) {
c := iter.nextToken()
if c == '-' {
val := iter.readUint64(iter.readByte())
if val > int64Max + 1 {
iter.reportError("ReadInt64", "overflow: " + strconv.FormatUint(uint64(val), 10))
if val > int64Max+1 {
iter.reportError("ReadInt64", "overflow: "+strconv.FormatUint(uint64(val), 10))
return
}
return -int64(val)
} else {
val := iter.readUint64(c)
if val > int64Max {
iter.reportError("ReadInt64", "overflow: " + strconv.FormatUint(uint64(val), 10))
iter.reportError("ReadInt64", "overflow: "+strconv.FormatUint(uint64(val), 10))
return
}
return int64(val)
@ -229,7 +229,7 @@ func (iter *Iterator) readUint64(c byte) (ret uint64) {
return 0 // single zero
}
if ind == invalidCharForNumber {
iter.reportError("readUint64", "unexpected character: " + string([]byte{byte(ind)}))
iter.reportError("readUint64", "unexpected character: "+string([]byte{byte(ind)}))
return
}
value := uint64(ind)
@ -252,7 +252,7 @@ func (iter *Iterator) readUint64(c byte) (ret uint64) {
}
value = (value << 3) + (value << 1) + uint64(ind)
}
if (!iter.loadMore()) {
if !iter.loadMore() {
return value
}
}

View File

@ -44,10 +44,10 @@ func (iter *Iterator) readFieldHash() int32 {
b += 'a' - 'A'
}
if b == '"' {
iter.head = i+1
iter.head = i + 1
c = iter.nextToken()
if c != ':' {
iter.reportError("readFieldHash", `expect :, but found ` + string([]byte{c}))
iter.reportError("readFieldHash", `expect :, but found `+string([]byte{c}))
}
return int32(hash)
}
@ -60,7 +60,7 @@ func (iter *Iterator) readFieldHash() int32 {
}
}
}
iter.reportError("readFieldHash", `expect ", but found ` + string([]byte{c}))
iter.reportError("readFieldHash", `expect ", but found `+string([]byte{c}))
return 0
}
@ -84,7 +84,7 @@ func (iter *Iterator) ReadObjectCB(callback func(*Iterator, string) bool) bool {
return false
}
for iter.nextToken() == ',' {
field := string(iter.readObjectFieldAsBytes())
field = string(iter.readObjectFieldAsBytes())
if !callback(iter, field) {
return false
}
@ -105,6 +105,46 @@ func (iter *Iterator) ReadObjectCB(callback func(*Iterator, string) bool) bool {
return false
}
func (iter *Iterator) ReadMapCB(callback func(*Iterator, string) bool) bool {
c := iter.nextToken()
if c == '{' {
c = iter.nextToken()
if c == '"' {
iter.unreadByte()
field := iter.ReadString()
if iter.nextToken() != ':' {
iter.reportError("ReadMapCB", "expect : after object field")
return false
}
if !callback(iter, field) {
return false
}
for iter.nextToken() == ',' {
field = iter.ReadString()
if iter.nextToken() != ':' {
iter.reportError("ReadMapCB", "expect : after object field")
return false
}
if !callback(iter, field) {
return false
}
}
return true
}
if c == '}' {
return true
}
iter.reportError("ReadMapCB", `expect " after }`)
return false
}
if c == 'n' {
iter.skipFixedBytes(3)
return true // null
}
iter.reportError("ReadMapCB", `expect { or n`)
return false
}
func (iter *Iterator) readObjectStart() bool {
c := iter.nextToken()
if c == '{' {

View File

@ -29,7 +29,6 @@ func (iter *Iterator) ReadBool() (ret bool) {
return
}
func (iter *Iterator) SkipAndReturnBytes() []byte {
if iter.reader != nil {
panic("reader input does not support this api")
@ -40,7 +39,6 @@ func (iter *Iterator) SkipAndReturnBytes() []byte {
return iter.buf[before:after]
}
// Skip skips a json object and positions to relatively the next json object
func (iter *Iterator) Skip() {
c := iter.nextToken()
@ -204,15 +202,15 @@ func (iter *Iterator) skipUntilBreak() {
}
func (iter *Iterator) skipFixedBytes(n int) {
iter.head += n;
if (iter.head >= iter.tail) {
more := iter.head - iter.tail;
iter.head += n
if iter.head >= iter.tail {
more := iter.head - iter.tail
if !iter.loadMore() {
if more > 0 {
iter.reportError("skipFixedBytes", "unexpected end");
iter.reportError("skipFixedBytes", "unexpected end")
}
return
}
iter.head += more;
iter.head += more
}
}
}

View File

@ -7,7 +7,7 @@ import (
func (iter *Iterator) ReadString() (ret string) {
c := iter.nextToken()
if c == '"' {
for i := iter.head ; i < iter.tail; i++ {
for i := iter.head; i < iter.tail; i++ {
c := iter.buf[i]
if c == '"' {
ret = string(iter.buf[iter.head:i])
@ -92,6 +92,7 @@ func (iter *Iterator) readStringSlowPath() (ret string) {
str = append(str, c)
}
}
iter.reportError("ReadString", "unexpected end of input")
return
}
@ -103,13 +104,13 @@ func (iter *Iterator) ReadStringAsSlice() (ret []byte) {
// for: field name, base64, number
if iter.buf[i] == '"' {
// fast path: reuse the underlying buffer
ret = iter.buf[iter.head : i]
ret = iter.buf[iter.head:i]
iter.head = i + 1
return ret
}
}
readLen := iter.tail - iter.head
copied := make([]byte, readLen, readLen * 2)
copied := make([]byte, readLen, readLen*2)
copy(copied, iter.buf[iter.head:iter.tail])
iter.head = iter.tail
for iter.Error == nil {
@ -132,11 +133,11 @@ func (iter *Iterator) readU4() (ret rune) {
return
}
if c >= '0' && c <= '9' {
ret = ret * 16 + rune(c - '0')
ret = ret*16 + rune(c-'0')
} else if c >= 'a' && c <= 'f' {
ret = ret * 16 + rune(c - 'a' + 10)
ret = ret*16 + rune(c-'a'+10)
} else if c >= 'A' && c <= 'F' {
ret = ret * 16 + rune(c - 'A' + 10)
ret = ret*16 + rune(c-'A'+10)
} else {
iter.reportError("readU4", "expects 0~9 or a~f")
return
@ -158,14 +159,14 @@ const (
mask3 = 0x0F // 0000 1111
mask4 = 0x07 // 0000 0111
rune1Max = 1 << 7 - 1
rune2Max = 1 << 11 - 1
rune3Max = 1 << 16 - 1
rune1Max = 1<<7 - 1
rune2Max = 1<<11 - 1
rune3Max = 1<<16 - 1
surrogateMin = 0xD800
surrogateMax = 0xDFFF
maxRune = '\U0010FFFF' // Maximum valid Unicode code point.
maxRune = '\U0010FFFF' // Maximum valid Unicode code point.
runeError = '\uFFFD' // the "error" Rune or "Unicode replacement character"
)
@ -176,22 +177,22 @@ func appendRune(p []byte, r rune) []byte {
p = append(p, byte(r))
return p
case i <= rune2Max:
p = append(p, t2 | byte(r >> 6))
p = append(p, tx | byte(r) & maskx)
p = append(p, t2|byte(r>>6))
p = append(p, tx|byte(r)&maskx)
return p
case i > maxRune, surrogateMin <= i && i <= surrogateMax:
r = runeError
fallthrough
case i <= rune3Max:
p = append(p, t3 | byte(r >> 12))
p = append(p, tx | byte(r >> 6) & maskx)
p = append(p, tx | byte(r) & maskx)
p = append(p, t3|byte(r>>12))
p = append(p, tx|byte(r>>6)&maskx)
p = append(p, tx|byte(r)&maskx)
return p
default:
p = append(p, t4 | byte(r >> 18))
p = append(p, tx | byte(r >> 12) & maskx)
p = append(p, tx | byte(r >> 6) & maskx)
p = append(p, tx | byte(r) & maskx)
p = append(p, t4|byte(r>>18))
p = append(p, tx|byte(r>>12)&maskx)
p = append(p, tx|byte(r>>6)&maskx)
p = append(p, tx|byte(r)&maskx)
return p
}
}

View File

@ -1,26 +1,30 @@
package jsoniter
import (
"encoding"
"encoding/json"
"fmt"
"reflect"
"sync/atomic"
"unsafe"
"encoding/json"
)
/*
Reflection on type to create decoders, which is then cached
Reflection on value is avoided as we can, as the reflect.Value itself will allocate, with following exceptions
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
*/
// Decoder is an internal type registered to cache as needed.
// Don't confuse jsoniter.Decoder with json.Decoder.
// For json.Decoder's adapter, refer to jsoniter.AdapterDecoder(todo link).
//
// Reflection on type to create decoders, which is then cached
// Reflection on value is avoided as we can, as the reflect.Value itself will allocate, with following exceptions
// 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 {
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 {
isEmpty(ptr unsafe.Pointer) bool
encode(ptr unsafe.Pointer, stream *Stream)
@ -29,6 +33,10 @@ type Encoder interface {
func writeToStream(val interface{}, stream *Stream, encoder Encoder) {
e := (*emptyInterface)(unsafe.Pointer(&val))
if e.word == nil {
stream.WriteNil()
return
}
if reflect.TypeOf(val).Kind() == reflect.Ptr {
encoder.encode(unsafe.Pointer(&e.word), stream)
} else {
@ -64,9 +72,6 @@ func (encoder *funcEncoder) isEmpty(ptr unsafe.Pointer) bool {
return false
}
var DECODERS unsafe.Pointer
var ENCODERS unsafe.Pointer
var typeDecoders map[string]Decoder
var fieldDecoders map[string]Decoder
var typeEncoders map[string]Encoder
@ -77,6 +82,7 @@ var jsonRawMessageType reflect.Type
var anyType reflect.Type
var marshalerType reflect.Type
var unmarshalerType reflect.Type
var textUnmarshalerType reflect.Type
func init() {
typeDecoders = map[string]Decoder{}
@ -84,53 +90,12 @@ func init() {
typeEncoders = map[string]Encoder{}
fieldEncoders = map[string]Encoder{}
extensions = []ExtensionFunc{}
atomic.StorePointer(&DECODERS, unsafe.Pointer(&map[string]Decoder{}))
atomic.StorePointer(&ENCODERS, unsafe.Pointer(&map[string]Encoder{}))
jsonNumberType = reflect.TypeOf((*json.Number)(nil)).Elem()
jsonRawMessageType = reflect.TypeOf((*json.RawMessage)(nil)).Elem()
anyType = reflect.TypeOf((*Any)(nil)).Elem()
marshalerType = reflect.TypeOf((*json.Marshaler)(nil)).Elem()
unmarshalerType = reflect.TypeOf((*json.Unmarshaler)(nil)).Elem()
}
func addDecoderToCache(cacheKey reflect.Type, decoder Decoder) {
done := false
for !done {
ptr := atomic.LoadPointer(&DECODERS)
cache := *(*map[reflect.Type]Decoder)(ptr)
copied := map[reflect.Type]Decoder{}
for k, v := range cache {
copied[k] = v
}
copied[cacheKey] = decoder
done = atomic.CompareAndSwapPointer(&DECODERS, ptr, unsafe.Pointer(&copied))
}
}
func addEncoderToCache(cacheKey reflect.Type, encoder Encoder) {
done := false
for !done {
ptr := atomic.LoadPointer(&ENCODERS)
cache := *(*map[reflect.Type]Encoder)(ptr)
copied := map[reflect.Type]Encoder{}
for k, v := range cache {
copied[k] = v
}
copied[cacheKey] = encoder
done = atomic.CompareAndSwapPointer(&ENCODERS, ptr, unsafe.Pointer(&copied))
}
}
func getDecoderFromCache(cacheKey reflect.Type) Decoder {
ptr := atomic.LoadPointer(&DECODERS)
cache := *(*map[reflect.Type]Decoder)(ptr)
return cache[cacheKey]
}
func getEncoderFromCache(cacheKey reflect.Type) Encoder {
ptr := atomic.LoadPointer(&ENCODERS)
cache := *(*map[reflect.Type]Encoder)(ptr)
return cache[cacheKey]
textUnmarshalerType = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem()
}
// RegisterTypeDecoder can register a type for json object
@ -156,17 +121,6 @@ func RegisterExtension(extension ExtensionFunc) {
extensions = append(extensions, extension)
}
// CleanDecoders cleans decoders registered
func CleanDecoders() {
typeDecoders = map[string]Decoder{}
fieldDecoders = map[string]Decoder{}
}
func CleanEncoders() {
typeEncoders = map[string]Encoder{}
fieldEncoders = map[string]Encoder{}
}
type optionalDecoder struct {
valueType reflect.Type
valueDecoder Decoder
@ -260,21 +214,20 @@ type nonEmptyInterface struct {
func (iter *Iterator) ReadVal(obj interface{}) {
typ := reflect.TypeOf(obj)
cacheKey := typ.Elem()
cachedDecoder := getDecoderFromCache(cacheKey)
cachedDecoder := iter.cfg.getDecoderFromCache(cacheKey)
if cachedDecoder == nil {
decoder, err := decoderOfType(cacheKey)
decoder, err := decoderOfType(iter.cfg, cacheKey)
if err != nil {
iter.Error = err
return
}
cachedDecoder = decoder
addDecoderToCache(cacheKey, decoder)
iter.cfg.addDecoderToCache(cacheKey, decoder)
}
e := (*emptyInterface)(unsafe.Pointer(&obj))
cachedDecoder.decode(e.word, iter)
}
func (stream *Stream) WriteVal(val interface{}) {
if nil == val {
stream.WriteNil()
@ -282,15 +235,15 @@ func (stream *Stream) WriteVal(val interface{}) {
}
typ := reflect.TypeOf(val)
cacheKey := typ
cachedEncoder := getEncoderFromCache(cacheKey)
cachedEncoder := stream.cfg.getEncoderFromCache(cacheKey)
if cachedEncoder == nil {
encoder, err := encoderOfType(cacheKey)
encoder, err := encoderOfType(stream.cfg, cacheKey)
if err != nil {
stream.Error = err
return
}
cachedEncoder = encoder
addEncoderToCache(cacheKey, encoder)
stream.cfg.addEncoderToCache(cacheKey, encoder)
}
cachedEncoder.encodeInterface(val, stream)
}
@ -311,7 +264,7 @@ func (p prefix) addToEncoder(encoder Encoder, err error) (Encoder, error) {
return encoder, err
}
func decoderOfType(typ reflect.Type) (Decoder, error) {
func decoderOfType(cfg *frozenConfig, typ reflect.Type) (Decoder, error) {
typeName := typ.String()
typeDecoder := typeDecoders[typeName]
if typeDecoder != nil {
@ -320,23 +273,26 @@ func decoderOfType(typ reflect.Type) (Decoder, error) {
if typ.Kind() == reflect.Ptr {
typeDecoder := typeDecoders[typ.Elem().String()]
if typeDecoder != nil {
return &optionalDecoder{typ.Elem(),typeDecoder}, nil
return &optionalDecoder{typ.Elem(), typeDecoder}, nil
}
}
cacheKey := typ
cachedDecoder := getDecoderFromCache(cacheKey)
cachedDecoder := cfg.getDecoderFromCache(cacheKey)
if cachedDecoder != nil {
return cachedDecoder, nil
}
placeholder := &placeholderDecoder{}
addDecoderToCache(cacheKey, placeholder)
newDecoder, err := createDecoderOfType(typ)
cfg.addDecoderToCache(cacheKey, placeholder)
newDecoder, err := createDecoderOfType(cfg, typ)
placeholder.valueDecoder = newDecoder
addDecoderToCache(cacheKey, newDecoder)
cfg.addDecoderToCache(cacheKey, newDecoder)
return newDecoder, err
}
func createDecoderOfType(typ reflect.Type) (Decoder, error) {
func createDecoderOfType(cfg *frozenConfig, typ reflect.Type) (Decoder, error) {
if typ.String() == "[]uint8" {
return &base64Codec{}, nil
}
if typ.AssignableTo(jsonRawMessageType) {
return &jsonRawMessageCodec{}, nil
}
@ -386,19 +342,19 @@ func createDecoderOfType(typ reflect.Type) (Decoder, error) {
return &nonEmptyInterfaceCodec{}, nil
}
case reflect.Struct:
return prefix(fmt.Sprintf("[%s]", typ.String())).addToDecoder(decoderOfStruct(typ))
return prefix(fmt.Sprintf("[%s]", typ.String())).addToDecoder(decoderOfStruct(cfg, typ))
case reflect.Slice:
return prefix("[slice]").addToDecoder(decoderOfSlice(typ))
return prefix("[slice]").addToDecoder(decoderOfSlice(cfg, typ))
case reflect.Map:
return prefix("[map]").addToDecoder(decoderOfMap(typ))
return prefix("[map]").addToDecoder(decoderOfMap(cfg, typ))
case reflect.Ptr:
return prefix("[optional]").addToDecoder(decoderOfOptional(typ))
return prefix("[optional]").addToDecoder(decoderOfOptional(cfg, typ))
default:
return nil, fmt.Errorf("unsupported type: %v", typ)
}
}
func encoderOfType(typ reflect.Type) (Encoder, error) {
func encoderOfType(cfg *frozenConfig, typ reflect.Type) (Encoder, error) {
typeName := typ.String()
typeEncoder := typeEncoders[typeName]
if typeEncoder != nil {
@ -411,19 +367,22 @@ func encoderOfType(typ reflect.Type) (Encoder, error) {
}
}
cacheKey := typ
cachedEncoder := getEncoderFromCache(cacheKey)
cachedEncoder := cfg.getEncoderFromCache(cacheKey)
if cachedEncoder != nil {
return cachedEncoder, nil
}
placeholder := &placeholderEncoder{}
addEncoderToCache(cacheKey, placeholder)
newEncoder, err := createEncoderOfType(typ)
cfg.addEncoderToCache(cacheKey, placeholder)
newEncoder, err := createEncoderOfType(cfg, typ)
placeholder.valueEncoder = newEncoder
addEncoderToCache(cacheKey, newEncoder)
cfg.addEncoderToCache(cacheKey, newEncoder)
return newEncoder, err
}
func createEncoderOfType(typ reflect.Type) (Encoder, error) {
func createEncoderOfType(cfg *frozenConfig, typ reflect.Type) (Encoder, error) {
if typ.String() == "[]uint8" {
return &base64Codec{}, nil
}
if typ.AssignableTo(jsonRawMessageType) {
return &jsonRawMessageCodec{}, nil
}
@ -474,58 +433,62 @@ func createEncoderOfType(typ reflect.Type) (Encoder, error) {
return &nonEmptyInterfaceCodec{}, nil
}
case reflect.Struct:
return prefix(fmt.Sprintf("[%s]", typ.String())).addToEncoder(encoderOfStruct(typ))
return prefix(fmt.Sprintf("[%s]", typ.String())).addToEncoder(encoderOfStruct(cfg, typ))
case reflect.Slice:
return prefix("[slice]").addToEncoder(encoderOfSlice(typ))
return prefix("[slice]").addToEncoder(encoderOfSlice(cfg, typ))
case reflect.Map:
return prefix("[map]").addToEncoder(encoderOfMap(typ))
return prefix("[map]").addToEncoder(encoderOfMap(cfg, typ))
case reflect.Ptr:
return prefix("[optional]").addToEncoder(encoderOfOptional(typ))
return prefix("[optional]").addToEncoder(encoderOfOptional(cfg, typ))
default:
return nil, fmt.Errorf("unsupported type: %v", typ)
}
}
func decoderOfOptional(typ reflect.Type) (Decoder, error) {
func decoderOfOptional(cfg *frozenConfig, typ reflect.Type) (Decoder, error) {
elemType := typ.Elem()
decoder, err := decoderOfType(elemType)
decoder, err := decoderOfType(cfg, elemType)
if err != nil {
return nil, err
}
return &optionalDecoder{elemType, decoder}, nil
}
func encoderOfOptional(typ reflect.Type) (Encoder, error) {
func encoderOfOptional(cfg *frozenConfig, typ reflect.Type) (Encoder, error) {
elemType := typ.Elem()
decoder, err := encoderOfType(elemType)
elemEncoder, err := encoderOfType(cfg, elemType)
if err != nil {
return nil, err
}
return &optionalEncoder{ decoder}, nil
encoder := &optionalEncoder{elemEncoder}
if elemType.Kind() == reflect.Map {
encoder = &optionalEncoder{encoder}
}
return encoder, nil
}
func decoderOfMap(typ reflect.Type) (Decoder, error) {
decoder, err := decoderOfType(typ.Elem())
func decoderOfMap(cfg *frozenConfig, typ reflect.Type) (Decoder, error) {
decoder, err := decoderOfType(cfg, typ.Elem())
if err != nil {
return nil, err
}
mapInterface := reflect.New(typ).Interface()
return &mapDecoder{typ, typ.Elem(), decoder, extractInterface(mapInterface)}, nil
return &mapDecoder{typ, typ.Key(), typ.Elem(), decoder, extractInterface(mapInterface)}, nil
}
func extractInterface(val interface{}) emptyInterface {
return *((*emptyInterface)(unsafe.Pointer(&val)))
}
func encoderOfMap(typ reflect.Type) (Encoder, error) {
func encoderOfMap(cfg *frozenConfig, typ reflect.Type) (Encoder, error) {
elemType := typ.Elem()
encoder, err := encoderOfType(elemType)
encoder, err := encoderOfType(cfg, elemType)
if err != nil {
return nil, err
}
mapInterface := reflect.New(typ).Elem().Interface()
if elemType.Kind() == reflect.Interface && elemType.NumMethod() == 0 {
return &mapInterfaceEncoder{typ, elemType, encoder, *((*emptyInterface)(unsafe.Pointer(&mapInterface)))}, nil
if cfg.sortMapKeys {
return &sortKeysMapEncoder{typ, elemType, encoder, *((*emptyInterface)(unsafe.Pointer(&mapInterface)))}, nil
} else {
return &mapEncoder{typ, elemType, encoder, *((*emptyInterface)(unsafe.Pointer(&mapInterface)))}, nil
}

View File

@ -1,27 +1,27 @@
package jsoniter
import (
"unsafe"
"reflect"
"io"
"fmt"
"io"
"reflect"
"unsafe"
)
func decoderOfSlice(typ reflect.Type) (Decoder, error) {
decoder, err := decoderOfType(typ.Elem())
func decoderOfSlice(cfg *frozenConfig, typ reflect.Type) (Decoder, error) {
decoder, err := decoderOfType(cfg, typ.Elem())
if err != nil {
return nil, err
}
return &sliceDecoder{typ, typ.Elem(), decoder}, nil
}
func encoderOfSlice(typ reflect.Type) (Encoder, error) {
encoder, err := encoderOfType(typ.Elem())
func encoderOfSlice(cfg *frozenConfig, typ reflect.Type) (Encoder, error) {
encoder, err := encoderOfType(cfg, typ.Elem())
if err != nil {
return nil, err
}
if typ.Elem().Kind() == reflect.Map {
encoder = &optionalEncoder{ encoder}
encoder = &optionalEncoder{encoder}
}
return &sliceEncoder{typ, typ.Elem(), encoder}, nil
}
@ -34,6 +34,10 @@ type sliceEncoder struct {
func (encoder *sliceEncoder) encode(ptr unsafe.Pointer, stream *Stream) {
slice := (*sliceHeader)(ptr)
if slice.Data == nil {
stream.WriteNil()
return
}
if slice.Len == 0 {
stream.WriteEmptyArray()
return
@ -88,30 +92,30 @@ func (decoder *sliceDecoder) doDecode(ptr unsafe.Pointer, iter *Iterator) {
return
}
offset := uintptr(0)
decoder.elemDecoder.decode(unsafe.Pointer(uintptr(slice.Data) + offset), iter)
decoder.elemDecoder.decode(unsafe.Pointer(uintptr(slice.Data)+offset), iter)
if !iter.ReadArray() {
slice.Len = 1
return
}
offset += decoder.elemType.Size()
decoder.elemDecoder.decode(unsafe.Pointer(uintptr(slice.Data) + offset), iter)
decoder.elemDecoder.decode(unsafe.Pointer(uintptr(slice.Data)+offset), iter)
if !iter.ReadArray() {
slice.Len = 2
return
}
offset += decoder.elemType.Size()
decoder.elemDecoder.decode(unsafe.Pointer(uintptr(slice.Data) + offset), iter)
decoder.elemDecoder.decode(unsafe.Pointer(uintptr(slice.Data)+offset), iter)
if !iter.ReadArray() {
slice.Len = 3
return
}
offset += decoder.elemType.Size()
decoder.elemDecoder.decode(unsafe.Pointer(uintptr(slice.Data) + offset), iter)
decoder.elemDecoder.decode(unsafe.Pointer(uintptr(slice.Data)+offset), iter)
slice.Len = 4
for iter.ReadArray() {
growOne(slice, decoder.sliceType, decoder.elemType)
offset += decoder.elemType.Size()
decoder.elemDecoder.decode(unsafe.Pointer(uintptr(slice.Data) + offset), iter)
decoder.elemDecoder.decode(unsafe.Pointer(uintptr(slice.Data)+offset), iter)
}
}
@ -155,4 +159,4 @@ func reuseSlice(slice *sliceHeader, sliceType reflect.Type, expectedCap int) {
dst := unsafe.Pointer(reflect.MakeSlice(sliceType, 0, expectedCap).Pointer())
slice.Cap = expectedCap
slice.Data = dst
}
}

View File

@ -1,12 +1,17 @@
package jsoniter
import (
"unsafe"
"encoding"
"encoding/json"
"reflect"
"strconv"
"unsafe"
"sort"
)
type mapDecoder struct {
mapType reflect.Type
keyType reflect.Type
elemType reflect.Type
elemDecoder Decoder
mapInterface emptyInterface
@ -21,12 +26,47 @@ func (decoder *mapDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
if realVal.IsNil() {
realVal.Set(reflect.MakeMap(realVal.Type()))
}
for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
iter.ReadMapCB(func(iter *Iterator, keyStr string) bool {
elem := reflect.New(decoder.elemType)
decoder.elemDecoder.decode(unsafe.Pointer(elem.Pointer()), iter)
// to put into map, we have to use reflection
realVal.SetMapIndex(reflect.ValueOf(string([]byte(field))), elem.Elem())
}
keyType := decoder.keyType
switch {
case keyType.Kind() == reflect.String:
realVal.SetMapIndex(reflect.ValueOf(keyStr), elem.Elem())
return true
case keyType.Implements(textUnmarshalerType):
textUnmarshaler := reflect.New(keyType.Elem()).Interface().(encoding.TextUnmarshaler)
err := textUnmarshaler.UnmarshalText([]byte(keyStr))
if err != nil {
iter.reportError("read map key as TextUnmarshaler", err.Error())
return false
}
realVal.SetMapIndex(reflect.ValueOf(textUnmarshaler), elem.Elem())
return true
default:
switch keyType.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
n, err := strconv.ParseInt(keyStr, 10, 64)
if err != nil || reflect.Zero(keyType).OverflowInt(n) {
iter.reportError("read map key as int64", "read int64 failed")
return false
}
realVal.SetMapIndex(reflect.ValueOf(n).Convert(keyType), elem.Elem())
return true
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
n, err := strconv.ParseUint(keyStr, 10, 64)
if err != nil || reflect.Zero(keyType).OverflowUint(n) {
iter.reportError("read map key as uint64", "read uint64 failed")
return false
}
realVal.SetMapIndex(reflect.ValueOf(n).Convert(keyType), elem.Elem())
return true
}
}
iter.reportError("read map key", "unexpected map key type "+keyType.String())
return true
})
}
type mapEncoder struct {
@ -47,13 +87,45 @@ func (encoder *mapEncoder) encode(ptr unsafe.Pointer, stream *Stream) {
if i != 0 {
stream.WriteMore()
}
stream.WriteObjectField(key.String())
encodeMapKey(key, stream)
stream.writeByte(':')
val := realVal.MapIndex(key).Interface()
encoder.elemEncoder.encodeInterface(val, stream)
}
stream.WriteObjectEnd()
}
func encodeMapKey(key reflect.Value, stream *Stream) {
if key.Kind() == reflect.String {
stream.WriteString(key.String())
return
}
if tm, ok := key.Interface().(encoding.TextMarshaler); ok {
buf, err := tm.MarshalText()
if err != nil {
stream.Error = err
return
}
stream.writeByte('"')
stream.Write(buf)
stream.writeByte('"')
return
}
switch key.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
stream.writeByte('"')
stream.WriteInt64(key.Int())
stream.writeByte('"')
return
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
stream.writeByte('"')
stream.WriteUint64(key.Uint())
stream.writeByte('"')
return
}
stream.Error = &json.UnsupportedTypeError{key.Type()}
}
func (encoder *mapEncoder) encodeInterface(val interface{}, stream *Stream) {
writeToStream(val, stream, encoder)
}
@ -66,40 +138,80 @@ func (encoder *mapEncoder) isEmpty(ptr unsafe.Pointer) bool {
return realVal.Len() == 0
}
type mapInterfaceEncoder struct {
type sortKeysMapEncoder struct {
mapType reflect.Type
elemType reflect.Type
elemEncoder Encoder
mapInterface emptyInterface
}
func (encoder *mapInterfaceEncoder) encode(ptr unsafe.Pointer, stream *Stream) {
func (encoder *sortKeysMapEncoder) encode(ptr unsafe.Pointer, stream *Stream) {
mapInterface := encoder.mapInterface
mapInterface.word = ptr
realInterface := (*interface{})(unsafe.Pointer(&mapInterface))
realVal := reflect.ValueOf(*realInterface)
// Extract and sort the keys.
keys := realVal.MapKeys()
sv := make([]reflectWithString, len(keys))
for i, v := range keys {
sv[i].v = v
if err := sv[i].resolve(); err != nil {
stream.Error = err
return
}
}
sort.Slice(sv, func(i, j int) bool { return sv[i].s < sv[j].s })
stream.WriteObjectStart()
for i, key := range realVal.MapKeys() {
for i, key := range sv {
if i != 0 {
stream.WriteMore()
}
stream.WriteObjectField(key.String())
val := realVal.MapIndex(key).Interface()
encoder.elemEncoder.encode(unsafe.Pointer(&val), stream)
encodeMapKey(key.v, stream)
stream.writeByte(':')
val := realVal.MapIndex(key.v).Interface()
encoder.elemEncoder.encodeInterface(val, stream)
}
stream.WriteObjectEnd()
}
func (encoder *mapInterfaceEncoder) encodeInterface(val interface{}, stream *Stream) {
type reflectWithString struct {
v reflect.Value
s string
}
func (w *reflectWithString) resolve() error {
if w.v.Kind() == reflect.String {
w.s = w.v.String()
return nil
}
if tm, ok := w.v.Interface().(encoding.TextMarshaler); ok {
buf, err := tm.MarshalText()
w.s = string(buf)
return err
}
switch w.v.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
w.s = strconv.FormatInt(w.v.Int(), 10)
return nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
w.s = strconv.FormatUint(w.v.Uint(), 10)
return nil
}
panic("unexpected map key type")
}
func (encoder *sortKeysMapEncoder) encodeInterface(val interface{}, stream *Stream) {
writeToStream(val, stream, encoder)
}
func (encoder *mapInterfaceEncoder) isEmpty(ptr unsafe.Pointer) bool {
func (encoder *sortKeysMapEncoder) isEmpty(ptr unsafe.Pointer) bool {
mapInterface := encoder.mapInterface
mapInterface.word = ptr
realInterface := (*interface{})(unsafe.Pointer(&mapInterface))
realVal := reflect.ValueOf(*realInterface)
return realVal.Len() == 0
}
}

View File

@ -1,8 +1,9 @@
package jsoniter
import (
"unsafe"
"encoding/base64"
"encoding/json"
"unsafe"
)
type stringCodec struct {
@ -296,6 +297,10 @@ type nonEmptyInterfaceCodec struct {
func (codec *nonEmptyInterfaceCodec) decode(ptr unsafe.Pointer, iter *Iterator) {
nonEmptyInterface := (*nonEmptyInterface)(ptr)
if nonEmptyInterface.itab == nil {
iter.reportError("read non-empty interface", "do not know which concrete type to decode to")
return
}
var i interface{}
e := (*emptyInterface)(unsafe.Pointer(&i))
e.typ = nonEmptyInterface.itab.typ
@ -379,6 +384,49 @@ func (encoder *jsonRawMessageCodec) isEmpty(ptr unsafe.Pointer) bool {
return len(*((*json.RawMessage)(ptr))) == 0
}
type base64Codec struct {
}
func (codec *base64Codec) decode(ptr unsafe.Pointer, iter *Iterator) {
encoding := base64.StdEncoding
src := iter.SkipAndReturnBytes()
src = src[1 : len(src)-1]
decodedLen := encoding.DecodedLen(len(src))
dst := make([]byte, decodedLen)
_, err := encoding.Decode(dst, src)
if err != nil {
iter.reportError("decode base64", err.Error())
} else {
*((*[]byte)(ptr)) = dst
}
}
func (codec *base64Codec) encode(ptr unsafe.Pointer, stream *Stream) {
encoding := base64.StdEncoding
stream.writeByte('"')
src := *((*[]byte)(ptr))
toGrow := encoding.EncodedLen(len(src))
stream.ensure(toGrow)
encoding.Encode(stream.buf[stream.n:], src)
stream.n += toGrow
stream.writeByte('"')
}
func (encoder *base64Codec) encodeInterface(val interface{}, stream *Stream) {
encoding := base64.StdEncoding
stream.writeByte('"')
src := val.([]byte)
toGrow := encoding.EncodedLen(len(src))
stream.ensure(toGrow)
encoding.Encode(stream.buf[stream.n:], src)
stream.n += toGrow
stream.writeByte('"')
}
func (encoder *base64Codec) isEmpty(ptr unsafe.Pointer) bool {
return len(*((*[]byte)(ptr))) == 0
}
type stringNumberDecoder struct {
elemDecoder Decoder
}
@ -447,4 +495,4 @@ func (decoder *unmarshalerDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
if err != nil {
iter.reportError("unmarshaler", err.Error())
}
}
}

View File

@ -1,15 +1,15 @@
package jsoniter
import (
"io"
"fmt"
"io"
"reflect"
"unsafe"
"strings"
"unicode"
"unsafe"
)
func encoderOfStruct(typ reflect.Type) (Encoder, error) {
func encoderOfStruct(cfg *frozenConfig, typ reflect.Type) (Encoder, error) {
structEncoder_ := &structEncoder{}
fields := map[string]*structFieldEncoder{}
for _, field := range listStructFields(typ) {
@ -24,6 +24,15 @@ func encoderOfStruct(typ reflect.Type) (Encoder, error) {
fieldEncoders[fieldEncoderKey] = &funcEncoder{fun}
}
}
for _, extension := range cfg.extensions {
alternativeFieldNames, fun, _ := extension(typ, field)
if alternativeFieldNames != nil {
extensionProvidedFieldNames = alternativeFieldNames
}
if fun != nil {
fieldEncoders[fieldEncoderKey] = &funcEncoder{fun}
}
}
tagParts := strings.Split(field.Tag.Get("json"), ",")
// if fieldNames set by extension, use theirs, otherwise try tags
fieldNames := calcFieldNames(field.Name, tagParts[0], extensionProvidedFieldNames)
@ -36,7 +45,7 @@ func encoderOfStruct(typ reflect.Type) (Encoder, error) {
encoder := fieldEncoders[fieldEncoderKey]
var err error
if encoder == nil && len(fieldNames) > 0 {
encoder, err = encoderOfType(field.Type)
encoder, err = encoderOfType(cfg, field.Type)
if err != nil {
return prefix(fmt.Sprintf("{%s}", field.Name)).addToEncoder(encoder, err)
}
@ -47,7 +56,7 @@ func encoderOfStruct(typ reflect.Type) (Encoder, error) {
}
for _, fieldName := range fieldNames {
fields[fieldName] = &structFieldEncoder{field, fieldName, encoder, omitempty}
}
}
}
if len(fields) == 0 {
return &emptyStructEncoder{}, nil
@ -71,7 +80,7 @@ func listStructFields(typ reflect.Type) []*reflect.StructField {
return fields
}
func decoderOfStruct(typ reflect.Type) (Decoder, error) {
func decoderOfStruct(cfg *frozenConfig, typ reflect.Type) (Decoder, error) {
fields := map[string]*structFieldDecoder{}
for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i)
@ -86,12 +95,21 @@ func decoderOfStruct(typ reflect.Type) (Decoder, error) {
fieldDecoders[fieldDecoderKey] = &funcDecoder{fun}
}
}
for _, extension := range cfg.extensions {
alternativeFieldNames, _, fun := extension(typ, &field)
if alternativeFieldNames != nil {
extensionProviedFieldNames = alternativeFieldNames
}
if fun != nil {
fieldDecoders[fieldDecoderKey] = &funcDecoder{fun}
}
}
decoder := fieldDecoders[fieldDecoderKey]
tagParts := strings.Split(field.Tag.Get("json"), ",")
fieldNames := calcFieldNames(field.Name, tagParts[0], extensionProviedFieldNames)
if decoder == nil && len(fieldNames) > 0 {
var err error
decoder, err = decoderOfType(field.Type)
decoder, err = decoderOfType(cfg, field.Type)
if err != nil {
return prefix(fmt.Sprintf("{%s}", field.Name)).addToDecoder(decoder, err)
}
@ -130,15 +148,9 @@ func calcFieldNames(originalFieldName string, tagProvidedFieldName string, exten
return fieldNames
}
func EnableUnexportedStructFieldsSupport() {
RegisterExtension(func(type_ reflect.Type, field *reflect.StructField) ([]string, EncoderFunc, DecoderFunc) {
return []string{field.Name}, nil, nil
})
}
func createStructDecoder(typ reflect.Type, fields map[string]*structFieldDecoder) (Decoder, error) {
knownHash := map[int32]struct{}{
0: struct{}{},
0: {},
}
switch len(fields) {
case 0:
@ -203,7 +215,7 @@ func createStructDecoder(typ reflect.Type, fields map[string]*structFieldDecoder
}
}
return &threeFieldsStructDecoder{typ,
fieldName1, fieldDecoder1, fieldName2, fieldDecoder2, fieldName3, fieldDecoder3}, nil
fieldName1, fieldDecoder1, fieldName2, fieldDecoder2, fieldName3, fieldDecoder3}, nil
case 4:
var fieldName1 int32
var fieldName2 int32
@ -236,8 +248,8 @@ func createStructDecoder(typ reflect.Type, fields map[string]*structFieldDecoder
}
}
return &fourFieldsStructDecoder{typ,
fieldName1, fieldDecoder1, fieldName2, fieldDecoder2, fieldName3, fieldDecoder3,
fieldName4, fieldDecoder4}, nil
fieldName1, fieldDecoder1, fieldName2, fieldDecoder2, fieldName3, fieldDecoder3,
fieldName4, fieldDecoder4}, nil
case 5:
var fieldName1 int32
var fieldName2 int32
@ -275,8 +287,8 @@ func createStructDecoder(typ reflect.Type, fields map[string]*structFieldDecoder
}
}
return &fiveFieldsStructDecoder{typ,
fieldName1, fieldDecoder1, fieldName2, fieldDecoder2, fieldName3, fieldDecoder3,
fieldName4, fieldDecoder4, fieldName5, fieldDecoder5}, nil
fieldName1, fieldDecoder1, fieldName2, fieldDecoder2, fieldName3, fieldDecoder3,
fieldName4, fieldDecoder4, fieldName5, fieldDecoder5}, nil
case 6:
var fieldName1 int32
var fieldName2 int32
@ -319,8 +331,8 @@ func createStructDecoder(typ reflect.Type, fields map[string]*structFieldDecoder
}
}
return &sixFieldsStructDecoder{typ,
fieldName1, fieldDecoder1, fieldName2, fieldDecoder2, fieldName3, fieldDecoder3,
fieldName4, fieldDecoder4, fieldName5, fieldDecoder5, fieldName6, fieldDecoder6}, nil
fieldName1, fieldDecoder1, fieldName2, fieldDecoder2, fieldName3, fieldDecoder3,
fieldName4, fieldDecoder4, fieldName5, fieldDecoder5, fieldName6, fieldDecoder6}, nil
case 7:
var fieldName1 int32
var fieldName2 int32
@ -368,9 +380,9 @@ func createStructDecoder(typ reflect.Type, fields map[string]*structFieldDecoder
}
}
return &sevenFieldsStructDecoder{typ,
fieldName1, fieldDecoder1, fieldName2, fieldDecoder2, fieldName3, fieldDecoder3,
fieldName4, fieldDecoder4, fieldName5, fieldDecoder5, fieldName6, fieldDecoder6,
fieldName7, fieldDecoder7}, nil
fieldName1, fieldDecoder1, fieldName2, fieldDecoder2, fieldName3, fieldDecoder3,
fieldName4, fieldDecoder4, fieldName5, fieldDecoder5, fieldName6, fieldDecoder6,
fieldName7, fieldDecoder7}, nil
case 8:
var fieldName1 int32
var fieldName2 int32
@ -423,9 +435,9 @@ func createStructDecoder(typ reflect.Type, fields map[string]*structFieldDecoder
}
}
return &eightFieldsStructDecoder{typ,
fieldName1, fieldDecoder1, fieldName2, fieldDecoder2, fieldName3, fieldDecoder3,
fieldName4, fieldDecoder4, fieldName5, fieldDecoder5, fieldName6, fieldDecoder6,
fieldName7, fieldDecoder7, fieldName8, fieldDecoder8}, nil
fieldName1, fieldDecoder1, fieldName2, fieldDecoder2, fieldName3, fieldDecoder3,
fieldName4, fieldDecoder4, fieldName5, fieldDecoder5, fieldName6, fieldDecoder6,
fieldName7, fieldDecoder7, fieldName8, fieldDecoder8}, nil
case 9:
var fieldName1 int32
var fieldName2 int32
@ -483,9 +495,9 @@ func createStructDecoder(typ reflect.Type, fields map[string]*structFieldDecoder
}
}
return &nineFieldsStructDecoder{typ,
fieldName1, fieldDecoder1, fieldName2, fieldDecoder2, fieldName3, fieldDecoder3,
fieldName4, fieldDecoder4, fieldName5, fieldDecoder5, fieldName6, fieldDecoder6,
fieldName7, fieldDecoder7, fieldName8, fieldDecoder8, fieldName9, fieldDecoder9}, nil
fieldName1, fieldDecoder1, fieldName2, fieldDecoder2, fieldName3, fieldDecoder3,
fieldName4, fieldDecoder4, fieldName5, fieldDecoder5, fieldName6, fieldDecoder6,
fieldName7, fieldDecoder7, fieldName8, fieldDecoder8, fieldName9, fieldDecoder9}, nil
case 10:
var fieldName1 int32
var fieldName2 int32
@ -548,10 +560,10 @@ func createStructDecoder(typ reflect.Type, fields map[string]*structFieldDecoder
}
}
return &tenFieldsStructDecoder{typ,
fieldName1, fieldDecoder1, fieldName2, fieldDecoder2, fieldName3, fieldDecoder3,
fieldName4, fieldDecoder4, fieldName5, fieldDecoder5, fieldName6, fieldDecoder6,
fieldName7, fieldDecoder7, fieldName8, fieldDecoder8, fieldName9, fieldDecoder9,
fieldName10, fieldDecoder10}, nil
fieldName1, fieldDecoder1, fieldName2, fieldDecoder2, fieldName3, fieldDecoder3,
fieldName4, fieldDecoder4, fieldName5, fieldDecoder5, fieldName6, fieldDecoder6,
fieldName7, fieldDecoder7, fieldName8, fieldDecoder8, fieldName9, fieldDecoder9,
fieldName10, fieldDecoder10}, nil
}
return &generalStructDecoder{typ, fields}, nil
}

View File

@ -5,16 +5,23 @@ import (
)
type Stream struct {
out io.Writer
buf []byte
n int
Error error
indention int
IndentionStep int
cfg *frozenConfig
out io.Writer
buf []byte
n int
Error error
indention int
}
func NewStream(out io.Writer, bufSize int) *Stream {
return &Stream{out, make([]byte, bufSize), 0, nil, 0, 0}
func NewStream(cfg *frozenConfig, out io.Writer, bufSize int) *Stream {
return &Stream{
cfg: cfg,
out: out,
buf: make([]byte, bufSize),
n: 0,
Error: nil,
indention: 0,
}
}
func (b *Stream) Reset(out io.Writer) {
@ -32,24 +39,32 @@ func (b *Stream) Buffered() int {
return b.n
}
func (b *Stream) Buffer() []byte {
return b.buf[:b.n]
}
// Write writes the contents of p into the buffer.
// It returns the number of bytes written.
// If nn < len(p), it also returns an error explaining
// why the write is short.
func (b *Stream) Write(p []byte) (nn int, err error) {
for len(p) > b.Available() && b.Error == nil {
var n int
if b.Buffered() == 0 {
// Large write, empty buffer.
// Write directly from p to avoid copy.
n, b.Error = b.out.Write(p)
if b.out == nil {
b.growAtLeast(len(p))
} else {
n = copy(b.buf[b.n:], p)
b.n += n
b.Flush()
var n int
if b.Buffered() == 0 {
// Large write, empty buffer.
// Write directly from p to avoid copy.
n, b.Error = b.out.Write(p)
} else {
n = copy(b.buf[b.n:], p)
b.n += n
b.Flush()
}
nn += n
p = p[n:]
}
nn += n
p = p[n:]
}
if b.Error != nil {
return nn, b.Error
@ -60,14 +75,13 @@ func (b *Stream) Write(p []byte) (nn int, err error) {
return nn, nil
}
// WriteByte writes a single byte.
func (b *Stream) writeByte(c byte) {
if b.Error != nil {
return
}
if b.Available() <= 0 && b.Flush() != nil {
return
if b.Available() < 1 {
b.growAtLeast(1)
}
b.buf[b.n] = c
b.n++
@ -77,11 +91,11 @@ func (b *Stream) writeTwoBytes(c1 byte, c2 byte) {
if b.Error != nil {
return
}
if b.Available() <= 1 && b.Flush() != nil {
return
if b.Available() < 2 {
b.growAtLeast(2)
}
b.buf[b.n] = c1
b.buf[b.n + 1] = c2
b.buf[b.n+1] = c2
b.n += 2
}
@ -89,12 +103,12 @@ func (b *Stream) writeThreeBytes(c1 byte, c2 byte, c3 byte) {
if b.Error != nil {
return
}
if b.Available() <= 2 && b.Flush() != nil {
return
if b.Available() < 3 {
b.growAtLeast(3)
}
b.buf[b.n] = c1
b.buf[b.n + 1] = c2
b.buf[b.n + 2] = c3
b.buf[b.n+1] = c2
b.buf[b.n+2] = c3
b.n += 3
}
@ -102,13 +116,13 @@ func (b *Stream) writeFourBytes(c1 byte, c2 byte, c3 byte, c4 byte) {
if b.Error != nil {
return
}
if b.Available() <= 3 && b.Flush() != nil {
return
if b.Available() < 4 {
b.growAtLeast(4)
}
b.buf[b.n] = c1
b.buf[b.n + 1] = c2
b.buf[b.n + 2] = c3
b.buf[b.n + 3] = c4
b.buf[b.n+1] = c2
b.buf[b.n+2] = c3
b.buf[b.n+3] = c4
b.n += 4
}
@ -116,19 +130,22 @@ func (b *Stream) writeFiveBytes(c1 byte, c2 byte, c3 byte, c4 byte, c5 byte) {
if b.Error != nil {
return
}
if b.Available() <= 3 && b.Flush() != nil {
return
if b.Available() < 5 {
b.growAtLeast(5)
}
b.buf[b.n] = c1
b.buf[b.n + 1] = c2
b.buf[b.n + 2] = c3
b.buf[b.n + 3] = c4
b.buf[b.n + 4] = c5
b.buf[b.n+1] = c2
b.buf[b.n+2] = c3
b.buf[b.n+3] = c4
b.buf[b.n+4] = c5
b.n += 5
}
// Flush writes any buffered data to the underlying io.Writer.
func (b *Stream) Flush() error {
if b.out == nil {
return nil
}
if b.Error != nil {
return b.Error
}
@ -141,7 +158,7 @@ func (b *Stream) Flush() error {
}
if err != nil {
if n > 0 && n < b.n {
copy(b.buf[0:b.n - n], b.buf[n:b.n])
copy(b.buf[0:b.n-n], b.buf[n:b.n])
}
b.n -= n
b.Error = err
@ -151,13 +168,28 @@ func (b *Stream) Flush() error {
return nil
}
func (b *Stream) WriteRaw(s string) {
for len(s) > b.Available() && b.Error == nil {
n := copy(b.buf[b.n:], s)
b.n += n
s = s[n:]
b.Flush()
func (b *Stream) ensure(minimal int) {
available := b.Available()
if available < minimal {
if b.n > 1024 {
b.Flush()
}
b.growAtLeast(minimal)
}
}
func (b *Stream) growAtLeast(minimal int) {
toGrow := len(b.buf)
if toGrow < minimal {
toGrow = minimal
}
newBuf := make([]byte, len(b.buf)+toGrow)
copy(newBuf, b.Buffer())
b.buf = newBuf
}
func (b *Stream) WriteRaw(s string) {
b.ensure(len(s))
if b.Error != nil {
return
}
@ -165,69 +197,6 @@ func (b *Stream) WriteRaw(s string) {
b.n += n
}
func (stream *Stream) WriteString(s string) {
valLen := len(s)
toWriteLen := valLen
bufLengthMinusTwo := len(stream.buf) - 2 // make room for the quotes
if stream.n + toWriteLen > bufLengthMinusTwo {
toWriteLen = bufLengthMinusTwo - stream.n
}
if toWriteLen < 0 {
stream.Flush()
if stream.n + toWriteLen > bufLengthMinusTwo {
toWriteLen = bufLengthMinusTwo - stream.n
}
}
n := stream.n
stream.buf[n] = '"'
n++
// write string, the fast path, without utf8 and escape support
i := 0
for ; i < toWriteLen; i++ {
c := s[i]
if c > 31 && c != '"' && c != '\\' {
stream.buf[n] = c
n++
} else {
break;
}
}
if i == valLen {
stream.buf[n] = '"'
n++
stream.n = n
return
}
stream.n = n
// for the remaining parts, we process them char by char
stream.writeStringSlowPath(s, i, valLen);
stream.writeByte('"')
}
func (stream *Stream) writeStringSlowPath(s string, i int, valLen int) {
for ; i < valLen; i++ {
c := s[i]
switch (c) {
case '"':
stream.writeTwoBytes('\\', '"')
case '\\':
stream.writeTwoBytes('\\', '\\')
case '\b':
stream.writeTwoBytes('\\', 'b')
case '\f':
stream.writeTwoBytes('\\', 'f')
case '\n':
stream.writeTwoBytes('\\', 'n')
case '\r':
stream.writeTwoBytes('\\', 'r')
case '\t':
stream.writeTwoBytes('\\', 't')
default:
stream.writeByte(c);
}
}
}
func (stream *Stream) WriteNil() {
stream.writeFourBytes('n', 'u', 'l', 'l')
}
@ -249,7 +218,7 @@ func (stream *Stream) WriteBool(val bool) {
}
func (stream *Stream) WriteObjectStart() {
stream.indention += stream.IndentionStep
stream.indention += stream.cfg.indentionStep
stream.writeByte('{')
stream.writeIndention(0)
}
@ -260,8 +229,8 @@ func (stream *Stream) WriteObjectField(field string) {
}
func (stream *Stream) WriteObjectEnd() {
stream.writeIndention(stream.IndentionStep)
stream.indention -= stream.IndentionStep
stream.writeIndention(stream.cfg.indentionStep)
stream.indention -= stream.cfg.indentionStep
stream.writeByte('}')
}
@ -276,7 +245,7 @@ func (stream *Stream) WriteMore() {
}
func (stream *Stream) WriteArrayStart() {
stream.indention += stream.IndentionStep
stream.indention += stream.cfg.indentionStep
stream.writeByte('[')
stream.writeIndention(0)
}
@ -287,27 +256,20 @@ func (stream *Stream) WriteEmptyArray() {
}
func (stream *Stream) WriteArrayEnd() {
stream.writeIndention(stream.IndentionStep)
stream.indention -= stream.IndentionStep
stream.writeIndention(stream.cfg.indentionStep)
stream.indention -= stream.cfg.indentionStep
stream.writeByte(']')
}
func (stream *Stream) writeIndention(delta int) {
if (stream.indention == 0) {
if stream.indention == 0 {
return
}
stream.writeByte('\n')
toWrite := stream.indention - delta
i := 0
for {
for ; i < toWrite && stream.n < len(stream.buf); i++ {
stream.buf[stream.n] = ' '
stream.n ++
}
if i == toWrite {
break;
} else {
stream.Flush()
}
stream.ensure(toWrite)
for i := 0; i < toWrite && stream.n < len(stream.buf); i++ {
stream.buf[stream.n] = ' '
stream.n++
}
}
}

View File

@ -2,13 +2,12 @@ package jsoniter
import (
"strconv"
"unsafe"
)
var POW10 []uint64
var _POW10 []uint64
func init() {
POW10 = []uint64{1, 10, 100, 1000, 10000, 100000, 1000000}
_POW10 = []uint64{1, 10, 100, 1000, 10000, 100000, 1000000}
}
func (stream *Stream) WriteFloat32(val float32) {
@ -21,26 +20,24 @@ func (stream *Stream) WriteFloat32Lossy(val float32) {
val = -val
}
if val > 0x4ffffff {
stream.WriteRaw(strconv.FormatFloat(float64(val), 'f', -1, 32));
stream.WriteRaw(strconv.FormatFloat(float64(val), 'f', -1, 32))
return
}
precision := 6
exp := uint64(1000000) // 6
lval := uint64(float64(val) * float64(exp) + 0.5)
lval := uint64(float64(val)*float64(exp) + 0.5)
stream.WriteUint64(lval / exp)
fval := lval % exp
if fval == 0 {
return
}
stream.writeByte('.')
if stream.Available() < 10 {
stream.Flush()
}
for p := precision - 1; p > 0 && fval < POW10[p]; p-- {
stream.ensure(10)
for p := precision - 1; p > 0 && fval < _POW10[p]; p-- {
stream.writeByte('0')
}
stream.WriteUint64(fval)
for stream.buf[stream.n - 1] == '0' {
for stream.buf[stream.n-1] == '0' {
stream.n--
}
}
@ -55,38 +52,24 @@ func (stream *Stream) WriteFloat64Lossy(val float64) {
val = -val
}
if val > 0x4ffffff {
stream.WriteRaw(strconv.FormatFloat(val, 'f', -1, 64));
stream.WriteRaw(strconv.FormatFloat(val, 'f', -1, 64))
return
}
precision := 6
exp := uint64(1000000) // 6
lval := uint64(val * float64(exp) + 0.5)
lval := uint64(val*float64(exp) + 0.5)
stream.WriteUint64(lval / exp)
fval := lval % exp
if fval == 0 {
return
}
stream.writeByte('.')
if stream.Available() < 10 {
stream.Flush()
}
for p := precision - 1; p > 0 && fval < POW10[p]; p-- {
stream.ensure(10)
for p := precision - 1; p > 0 && fval < _POW10[p]; p-- {
stream.writeByte('0')
}
stream.WriteUint64(fval)
for stream.buf[stream.n - 1] == '0' {
for stream.buf[stream.n-1] == '0' {
stream.n--
}
}
func EnableLossyFloatMarshalling() {
// for better performance
RegisterTypeEncoder("float32", func(ptr unsafe.Pointer, stream *Stream) {
val := *((*float32)(ptr))
stream.WriteFloat32Lossy(val)
})
RegisterTypeEncoder("float64", func(ptr unsafe.Pointer, stream *Stream) {
val := *((*float64)(ptr))
stream.WriteFloat64Lossy(val)
})
}

View File

@ -1,50 +1,15 @@
package jsoniter
var digits []uint8
var digitTens []uint8
var digitOnes []uint8
var DIGITS []uint32
var _DIGITS []uint32
func init() {
digits = []uint8{
'0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', 'a', 'b',
'c', 'd', 'e', 'f', 'g', 'h',
'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z',
}
digitTens = []uint8{
'0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
'1', '1', '1', '1', '1', '1', '1', '1', '1', '1',
'2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
'3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
'4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
'5', '5', '5', '5', '5', '5', '5', '5', '5', '5',
'6', '6', '6', '6', '6', '6', '6', '6', '6', '6',
'7', '7', '7', '7', '7', '7', '7', '7', '7', '7',
'8', '8', '8', '8', '8', '8', '8', '8', '8', '8',
'9', '9', '9', '9', '9', '9', '9', '9', '9', '9',
}
digitOnes = []uint8{
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
}
DIGITS = make([]uint32, 1000)
_DIGITS = make([]uint32, 1000)
for i := uint32(0); i < 1000; i++ {
DIGITS[i] = (((i / 100) + '0') << 16) + ((((i / 10) % 10) + '0') << 8) + i % 10 + '0';
_DIGITS[i] = (((i / 100) + '0') << 16) + ((((i / 10) % 10) + '0') << 8) + i%10 + '0'
if i < 10 {
DIGITS[i] += 2 << 24
_DIGITS[i] += 2 << 24
} else if i < 100 {
DIGITS[i] += 1 << 24
_DIGITS[i] += 1 << 24
}
}
}
@ -67,56 +32,48 @@ func writeFirstBuf(buf []byte, v uint32, n int) int {
func writeBuf(buf []byte, v uint32, n int) {
buf[n] = byte(v >> 16)
buf[n + 1] = byte(v >> 8)
buf[n + 2] = byte(v)
buf[n+1] = byte(v >> 8)
buf[n+2] = byte(v)
}
func (stream *Stream) WriteUint8(val uint8) {
if stream.Available() < 3 {
stream.Flush()
}
stream.n = writeFirstBuf(stream.buf, DIGITS[val], stream.n)
stream.ensure(3)
stream.n = writeFirstBuf(stream.buf, _DIGITS[val], stream.n)
}
func (stream *Stream) WriteInt8(nval int8) {
if stream.Available() < 4 {
stream.Flush()
}
stream.ensure(4)
n := stream.n
var val uint8
if (nval < 0) {
if nval < 0 {
val = uint8(-nval)
stream.buf[n] = '-'
n++
} else {
val = uint8(nval)
}
stream.n = writeFirstBuf(stream.buf, DIGITS[val], n)
stream.n = writeFirstBuf(stream.buf, _DIGITS[val], n)
}
func (stream *Stream) WriteUint16(val uint16) {
if stream.Available() < 5 {
stream.Flush()
}
stream.ensure(5)
q1 := val / 1000
if q1 == 0 {
stream.n = writeFirstBuf(stream.buf, DIGITS[val], stream.n)
stream.n = writeFirstBuf(stream.buf, _DIGITS[val], stream.n)
return
}
r1 := val - q1 * 1000;
n := writeFirstBuf(stream.buf, DIGITS[q1], stream.n)
writeBuf(stream.buf, DIGITS[r1], n)
r1 := val - q1*1000
n := writeFirstBuf(stream.buf, _DIGITS[q1], stream.n)
writeBuf(stream.buf, _DIGITS[r1], n)
stream.n = n + 3
return
}
func (stream *Stream) WriteInt16(nval int16) {
if stream.Available() < 6 {
stream.Flush()
}
stream.ensure(6)
n := stream.n
var val uint16
if (nval < 0) {
if nval < 0 {
val = uint16(-nval)
stream.buf[n] = '-'
n++
@ -125,57 +82,53 @@ func (stream *Stream) WriteInt16(nval int16) {
}
q1 := val / 1000
if q1 == 0 {
stream.n = writeFirstBuf(stream.buf, DIGITS[val], n)
stream.n = writeFirstBuf(stream.buf, _DIGITS[val], n)
return
}
r1 := val - q1 * 1000;
n = writeFirstBuf(stream.buf, DIGITS[q1], n)
writeBuf(stream.buf, DIGITS[r1], n)
r1 := val - q1*1000
n = writeFirstBuf(stream.buf, _DIGITS[q1], n)
writeBuf(stream.buf, _DIGITS[r1], n)
stream.n = n + 3
return
}
func (stream *Stream) WriteUint32(val uint32) {
if stream.Available() < 10 {
stream.Flush()
}
stream.ensure(10)
n := stream.n
q1 := val / 1000
if q1 == 0 {
stream.n = writeFirstBuf(stream.buf, DIGITS[val], n)
stream.n = writeFirstBuf(stream.buf, _DIGITS[val], n)
return
}
r1 := val - q1 * 1000;
r1 := val - q1*1000
q2 := q1 / 1000
if q2 == 0 {
n := writeFirstBuf(stream.buf, DIGITS[q1], n)
writeBuf(stream.buf, DIGITS[r1], n)
n := writeFirstBuf(stream.buf, _DIGITS[q1], n)
writeBuf(stream.buf, _DIGITS[r1], n)
stream.n = n + 3
return
}
r2 := q1 - q2 * 1000
r2 := q1 - q2*1000
q3 := q2 / 1000
if q3 == 0 {
n = writeFirstBuf(stream.buf, DIGITS[q2], n)
n = writeFirstBuf(stream.buf, _DIGITS[q2], n)
} else {
r3 := q2 - q3 * 1000
r3 := q2 - q3*1000
stream.buf[n] = byte(q3 + '0')
n++
writeBuf(stream.buf, DIGITS[r3], n)
writeBuf(stream.buf, _DIGITS[r3], n)
n += 3
}
writeBuf(stream.buf, DIGITS[r2], n)
writeBuf(stream.buf, DIGITS[r1], n + 3)
writeBuf(stream.buf, _DIGITS[r2], n)
writeBuf(stream.buf, _DIGITS[r1], n+3)
stream.n = n + 6
}
func (stream *Stream) WriteInt32(nval int32) {
if stream.Available() < 11 {
stream.Flush()
}
stream.ensure(11)
n := stream.n
var val uint32
if (nval < 0) {
if nval < 0 {
val = uint32(-nval)
stream.buf[n] = '-'
n++
@ -184,106 +137,102 @@ func (stream *Stream) WriteInt32(nval int32) {
}
q1 := val / 1000
if q1 == 0 {
stream.n = writeFirstBuf(stream.buf, DIGITS[val], n)
stream.n = writeFirstBuf(stream.buf, _DIGITS[val], n)
return
}
r1 := val - q1 * 1000;
r1 := val - q1*1000
q2 := q1 / 1000
if q2 == 0 {
n := writeFirstBuf(stream.buf, DIGITS[q1], n)
writeBuf(stream.buf, DIGITS[r1], n)
n := writeFirstBuf(stream.buf, _DIGITS[q1], n)
writeBuf(stream.buf, _DIGITS[r1], n)
stream.n = n + 3
return
}
r2 := q1 - q2 * 1000
r2 := q1 - q2*1000
q3 := q2 / 1000
if q3 == 0 {
n = writeFirstBuf(stream.buf, DIGITS[q2], n)
n = writeFirstBuf(stream.buf, _DIGITS[q2], n)
} else {
r3 := q2 - q3 * 1000
r3 := q2 - q3*1000
stream.buf[n] = byte(q3 + '0')
n++
writeBuf(stream.buf, DIGITS[r3], n)
writeBuf(stream.buf, _DIGITS[r3], n)
n += 3
}
writeBuf(stream.buf, DIGITS[r2], n)
writeBuf(stream.buf, DIGITS[r1], n + 3)
writeBuf(stream.buf, _DIGITS[r2], n)
writeBuf(stream.buf, _DIGITS[r1], n+3)
stream.n = n + 6
}
func (stream *Stream) WriteUint64(val uint64) {
if stream.Available() < 20 {
stream.Flush()
}
stream.ensure(20)
n := stream.n
q1 := val / 1000
if q1 == 0 {
stream.n = writeFirstBuf(stream.buf, DIGITS[val], n)
stream.n = writeFirstBuf(stream.buf, _DIGITS[val], n)
return
}
r1 := val - q1 * 1000;
r1 := val - q1*1000
q2 := q1 / 1000
if q2 == 0 {
n := writeFirstBuf(stream.buf, DIGITS[q1], n)
writeBuf(stream.buf, DIGITS[r1], n)
n := writeFirstBuf(stream.buf, _DIGITS[q1], n)
writeBuf(stream.buf, _DIGITS[r1], n)
stream.n = n + 3
return
}
r2 := q1 - q2 * 1000
r2 := q1 - q2*1000
q3 := q2 / 1000
if q3 == 0 {
n = writeFirstBuf(stream.buf, DIGITS[q2], n)
writeBuf(stream.buf, DIGITS[r2], n)
writeBuf(stream.buf, DIGITS[r1], n + 3)
n = writeFirstBuf(stream.buf, _DIGITS[q2], n)
writeBuf(stream.buf, _DIGITS[r2], n)
writeBuf(stream.buf, _DIGITS[r1], n+3)
stream.n = n + 6
return
}
r3 := q2 - q3 * 1000
r3 := q2 - q3*1000
q4 := q3 / 1000
if q4 == 0 {
n = writeFirstBuf(stream.buf, DIGITS[q3], n)
writeBuf(stream.buf, DIGITS[r3], n)
writeBuf(stream.buf, DIGITS[r2], n + 3)
writeBuf(stream.buf, DIGITS[r1], n + 6)
n = writeFirstBuf(stream.buf, _DIGITS[q3], n)
writeBuf(stream.buf, _DIGITS[r3], n)
writeBuf(stream.buf, _DIGITS[r2], n+3)
writeBuf(stream.buf, _DIGITS[r1], n+6)
stream.n = n + 9
return
}
r4 := q3 - q4 * 1000
r4 := q3 - q4*1000
q5 := q4 / 1000
if q5 == 0 {
n = writeFirstBuf(stream.buf, DIGITS[q4], n)
writeBuf(stream.buf, DIGITS[r4], n)
writeBuf(stream.buf, DIGITS[r3], n + 3)
writeBuf(stream.buf, DIGITS[r2], n + 6)
writeBuf(stream.buf, DIGITS[r1], n + 9)
n = writeFirstBuf(stream.buf, _DIGITS[q4], n)
writeBuf(stream.buf, _DIGITS[r4], n)
writeBuf(stream.buf, _DIGITS[r3], n+3)
writeBuf(stream.buf, _DIGITS[r2], n+6)
writeBuf(stream.buf, _DIGITS[r1], n+9)
stream.n = n + 12
return
}
r5 := q4 - q5 * 1000
r5 := q4 - q5*1000
q6 := q5 / 1000
if q6 == 0 {
n = writeFirstBuf(stream.buf, DIGITS[q5], n)
n = writeFirstBuf(stream.buf, _DIGITS[q5], n)
} else {
n = writeFirstBuf(stream.buf, DIGITS[q6], n)
r6 := q5 - q6 * 1000
writeBuf(stream.buf, DIGITS[r6], n)
n = writeFirstBuf(stream.buf, _DIGITS[q6], n)
r6 := q5 - q6*1000
writeBuf(stream.buf, _DIGITS[r6], n)
n += 3
}
writeBuf(stream.buf, DIGITS[r5], n)
writeBuf(stream.buf, DIGITS[r4], n + 3)
writeBuf(stream.buf, DIGITS[r3], n + 6)
writeBuf(stream.buf, DIGITS[r2], n + 9)
writeBuf(stream.buf, DIGITS[r1], n + 12)
writeBuf(stream.buf, _DIGITS[r5], n)
writeBuf(stream.buf, _DIGITS[r4], n+3)
writeBuf(stream.buf, _DIGITS[r3], n+6)
writeBuf(stream.buf, _DIGITS[r2], n+9)
writeBuf(stream.buf, _DIGITS[r1], n+12)
stream.n = n + 15
}
func (stream *Stream) WriteInt64(nval int64) {
if stream.Available() < 20 {
stream.Flush()
}
stream.ensure(20)
n := stream.n
var val uint64
if (nval < 0) {
if nval < 0 {
val = uint64(-nval)
stream.buf[n] = '-'
n++
@ -292,63 +241,63 @@ func (stream *Stream) WriteInt64(nval int64) {
}
q1 := val / 1000
if q1 == 0 {
stream.n = writeFirstBuf(stream.buf, DIGITS[val], n)
stream.n = writeFirstBuf(stream.buf, _DIGITS[val], n)
return
}
r1 := val - q1 * 1000;
r1 := val - q1*1000
q2 := q1 / 1000
if q2 == 0 {
n := writeFirstBuf(stream.buf, DIGITS[q1], n)
writeBuf(stream.buf, DIGITS[r1], n)
n := writeFirstBuf(stream.buf, _DIGITS[q1], n)
writeBuf(stream.buf, _DIGITS[r1], n)
stream.n = n + 3
return
}
r2 := q1 - q2 * 1000
r2 := q1 - q2*1000
q3 := q2 / 1000
if q3 == 0 {
n = writeFirstBuf(stream.buf, DIGITS[q2], n)
writeBuf(stream.buf, DIGITS[r2], n)
writeBuf(stream.buf, DIGITS[r1], n + 3)
n = writeFirstBuf(stream.buf, _DIGITS[q2], n)
writeBuf(stream.buf, _DIGITS[r2], n)
writeBuf(stream.buf, _DIGITS[r1], n+3)
stream.n = n + 6
return
}
r3 := q2 - q3 * 1000
r3 := q2 - q3*1000
q4 := q3 / 1000
if q4 == 0 {
n = writeFirstBuf(stream.buf, DIGITS[q3], n)
writeBuf(stream.buf, DIGITS[r3], n)
writeBuf(stream.buf, DIGITS[r2], n + 3)
writeBuf(stream.buf, DIGITS[r1], n + 6)
n = writeFirstBuf(stream.buf, _DIGITS[q3], n)
writeBuf(stream.buf, _DIGITS[r3], n)
writeBuf(stream.buf, _DIGITS[r2], n+3)
writeBuf(stream.buf, _DIGITS[r1], n+6)
stream.n = n + 9
return
}
r4 := q3 - q4 * 1000
r4 := q3 - q4*1000
q5 := q4 / 1000
if q5 == 0 {
n = writeFirstBuf(stream.buf, DIGITS[q4], n)
writeBuf(stream.buf, DIGITS[r4], n)
writeBuf(stream.buf, DIGITS[r3], n + 3)
writeBuf(stream.buf, DIGITS[r2], n + 6)
writeBuf(stream.buf, DIGITS[r1], n + 9)
n = writeFirstBuf(stream.buf, _DIGITS[q4], n)
writeBuf(stream.buf, _DIGITS[r4], n)
writeBuf(stream.buf, _DIGITS[r3], n+3)
writeBuf(stream.buf, _DIGITS[r2], n+6)
writeBuf(stream.buf, _DIGITS[r1], n+9)
stream.n = n + 12
return
}
r5 := q4 - q5 * 1000
r5 := q4 - q5*1000
q6 := q5 / 1000
if q6 == 0 {
n = writeFirstBuf(stream.buf, DIGITS[q5], n)
n = writeFirstBuf(stream.buf, _DIGITS[q5], n)
} else {
stream.buf[n] = byte(q6 + '0')
n++
r6 := q5 - q6 * 1000
writeBuf(stream.buf, DIGITS[r6], n)
r6 := q5 - q6*1000
writeBuf(stream.buf, _DIGITS[r6], n)
n += 3
}
writeBuf(stream.buf, DIGITS[r5], n)
writeBuf(stream.buf, DIGITS[r4], n + 3)
writeBuf(stream.buf, DIGITS[r3], n + 6)
writeBuf(stream.buf, DIGITS[r2], n + 9)
writeBuf(stream.buf, DIGITS[r1], n + 12)
writeBuf(stream.buf, _DIGITS[r5], n)
writeBuf(stream.buf, _DIGITS[r4], n+3)
writeBuf(stream.buf, _DIGITS[r3], n+6)
writeBuf(stream.buf, _DIGITS[r2], n+9)
writeBuf(stream.buf, _DIGITS[r1], n+12)
stream.n = n + 15
}
@ -358,4 +307,4 @@ func (stream *Stream) WriteInt(val int) {
func (stream *Stream) WriteUint(val uint) {
stream.WriteUint64(uint64(val))
}
}

351
feature_stream_string.go Normal file
View File

@ -0,0 +1,351 @@
package jsoniter
import (
"unicode/utf8"
)
// htmlSafeSet holds the value true if the ASCII character with the given
// array position can be safely represented inside a JSON string, embedded
// inside of HTML <script> tags, without any additional escaping.
//
// All values are true except for the ASCII control characters (0-31), the
// double quote ("), the backslash character ("\"), HTML opening and closing
// tags ("<" and ">"), and the ampersand ("&").
var htmlSafeSet = [utf8.RuneSelf]bool{
' ': true,
'!': true,
'"': false,
'#': true,
'$': true,
'%': true,
'&': false,
'\'': true,
'(': true,
')': true,
'*': true,
'+': true,
',': true,
'-': true,
'.': true,
'/': true,
'0': true,
'1': true,
'2': true,
'3': true,
'4': true,
'5': true,
'6': true,
'7': true,
'8': true,
'9': true,
':': true,
';': true,
'<': false,
'=': true,
'>': false,
'?': true,
'@': true,
'A': true,
'B': true,
'C': true,
'D': true,
'E': true,
'F': true,
'G': true,
'H': true,
'I': true,
'J': true,
'K': true,
'L': true,
'M': true,
'N': true,
'O': true,
'P': true,
'Q': true,
'R': true,
'S': true,
'T': true,
'U': true,
'V': true,
'W': true,
'X': true,
'Y': true,
'Z': true,
'[': true,
'\\': false,
']': true,
'^': true,
'_': true,
'`': true,
'a': true,
'b': true,
'c': true,
'd': true,
'e': true,
'f': true,
'g': true,
'h': true,
'i': true,
'j': true,
'k': true,
'l': true,
'm': true,
'n': true,
'o': true,
'p': true,
'q': true,
'r': true,
's': true,
't': true,
'u': true,
'v': true,
'w': true,
'x': true,
'y': true,
'z': true,
'{': true,
'|': true,
'}': true,
'~': true,
'\u007f': true,
}
// safeSet holds the value true if the ASCII character with the given array
// position can be represented inside a JSON string without any further
// escaping.
//
// All values are true except for the ASCII control characters (0-31), the
// double quote ("), and the backslash character ("\").
var safeSet = [utf8.RuneSelf]bool{
' ': true,
'!': true,
'"': false,
'#': true,
'$': true,
'%': true,
'&': true,
'\'': true,
'(': true,
')': true,
'*': true,
'+': true,
',': true,
'-': true,
'.': true,
'/': true,
'0': true,
'1': true,
'2': true,
'3': true,
'4': true,
'5': true,
'6': true,
'7': true,
'8': true,
'9': true,
':': true,
';': true,
'<': true,
'=': true,
'>': true,
'?': true,
'@': true,
'A': true,
'B': true,
'C': true,
'D': true,
'E': true,
'F': true,
'G': true,
'H': true,
'I': true,
'J': true,
'K': true,
'L': true,
'M': true,
'N': true,
'O': true,
'P': true,
'Q': true,
'R': true,
'S': true,
'T': true,
'U': true,
'V': true,
'W': true,
'X': true,
'Y': true,
'Z': true,
'[': true,
'\\': false,
']': true,
'^': true,
'_': true,
'`': true,
'a': true,
'b': true,
'c': true,
'd': true,
'e': true,
'f': true,
'g': true,
'h': true,
'i': true,
'j': true,
'k': true,
'l': true,
'm': true,
'n': true,
'o': true,
'p': true,
'q': true,
'r': true,
's': true,
't': true,
'u': true,
'v': true,
'w': true,
'x': true,
'y': true,
'z': true,
'{': true,
'|': true,
'}': true,
'~': true,
'\u007f': true,
}
var hex = "0123456789abcdef"
func (stream *Stream) WriteStringWithHtmlEscaped(s string) {
stream.ensure(32)
valLen := len(s)
toWriteLen := valLen
bufLengthMinusTwo := len(stream.buf) - 2 // make room for the quotes
if stream.n+toWriteLen > bufLengthMinusTwo {
toWriteLen = bufLengthMinusTwo - stream.n
}
n := stream.n
stream.buf[n] = '"'
n++
// write string, the fast path, without utf8 and escape support
i := 0
for ; i < toWriteLen; i++ {
c := s[i]
if c <= utf8.RuneSelf && htmlSafeSet[c] {
stream.buf[n] = c
n++
} else {
break
}
}
if i == valLen {
stream.buf[n] = '"'
n++
stream.n = n
return
}
stream.n = n
writeStringSlowPath(stream, htmlSafeSet, i, s, valLen)
}
func (stream *Stream) WriteString(s string) {
stream.ensure(32)
valLen := len(s)
toWriteLen := valLen
bufLengthMinusTwo := len(stream.buf) - 2 // make room for the quotes
if stream.n+toWriteLen > bufLengthMinusTwo {
toWriteLen = bufLengthMinusTwo - stream.n
}
n := stream.n
stream.buf[n] = '"'
n++
// write string, the fast path, without utf8 and escape support
i := 0
for ; i < toWriteLen; i++ {
c := s[i]
if c > 31 && c != '"' && c != '\\' {
stream.buf[n] = c
n++
} else {
break
}
}
if i == valLen {
stream.buf[n] = '"'
n++
stream.n = n
return
}
stream.n = n
writeStringSlowPath(stream, safeSet, i, s, valLen)
}
func writeStringSlowPath(stream *Stream, safeSet [utf8.RuneSelf]bool, i int, s string, valLen int) {
start := i
// for the remaining parts, we process them char by char
for ; i < valLen; i++ {
if b := s[i]; b < utf8.RuneSelf {
if safeSet[b] {
i++
continue
}
if start < i {
stream.WriteRaw(s[start:i])
}
switch b {
case '\\', '"':
stream.writeTwoBytes('\\', b)
case '\n':
stream.writeTwoBytes('\\', 'n')
case '\r':
stream.writeTwoBytes('\\', 'r')
case '\t':
stream.writeTwoBytes('\\', 't')
default:
// This encodes bytes < 0x20 except for \t, \n and \r.
// If escapeHTML is set, it also escapes <, >, and &
// because they can lead to security holes when
// user-controlled strings are rendered into JSON
// and served to some browsers.
stream.WriteRaw(`\u00`)
stream.writeTwoBytes(hex[b>>4], hex[b&0xF])
}
i++
start = i
continue
}
c, size := utf8.DecodeRuneInString(s[i:])
if c == utf8.RuneError && size == 1 {
if start < i {
stream.WriteRaw(s[start:i])
}
stream.WriteRaw(`\ufffd`)
i += size
start = i
continue
}
// U+2028 is LINE SEPARATOR.
// U+2029 is PARAGRAPH SEPARATOR.
// They are both technically valid characters in JSON strings,
// but don't work in JSONP, which has to be evaluated as JavaScript,
// and can lead to security holes there. It is valid JSON to
// escape them, so we do so unconditionally.
// See http://timelessrepo.com/json-isnt-a-javascript-subset for discussion.
if c == '\u2028' || c == '\u2029' {
if start < i {
stream.WriteRaw(s[start:i])
}
stream.WriteRaw(`\u202`)
stream.writeByte(hex[c&0xF])
i += size
start = i
continue
}
i += size
}
if start < len(s) {
stream.WriteRaw(s[start:])
}
stream.writeByte('"')
}

View File

@ -1,11 +1,11 @@
package jsoniter
import (
"testing"
"github.com/json-iterator/go/require"
"encoding/json"
"bytes"
"encoding/json"
"github.com/json-iterator/go/require"
"io/ioutil"
"testing"
)
func Test_new_decoder(t *testing.T) {
@ -37,10 +37,26 @@ func Test_new_encoder(t *testing.T) {
should := require.New(t)
buf1 := &bytes.Buffer{}
encoder1 := json.NewEncoder(buf1)
encoder1.SetEscapeHTML(false)
encoder1.Encode([]int{1})
should.Equal("[1]\n", buf1.String())
buf2 := &bytes.Buffer{}
encoder2 := NewEncoder(buf2)
encoder2.SetEscapeHTML(false)
encoder2.Encode([]int{1})
should.Equal("[1]", buf2.String())
}
}
func Test_use_number(t *testing.T) {
should := require.New(t)
decoder1 := json.NewDecoder(bytes.NewBufferString(`123`))
decoder1.UseNumber()
decoder2 := NewDecoder(bytes.NewBufferString(`123`))
decoder2.UseNumber()
var obj1 interface{}
should.Nil(decoder1.Decode(&obj1))
should.Equal(json.Number("123"), obj1)
var obj2 interface{}
should.Nil(decoder2.Decode(&obj2))
should.Equal(json.Number("123"), obj2)
}

View File

@ -1,19 +1,19 @@
package jsoniter
import (
"encoding/json"
"testing"
"github.com/json-iterator/go/require"
"bytes"
"encoding/json"
"github.com/json-iterator/go/require"
"io"
"testing"
)
func Test_empty_array(t *testing.T) {
should := require.New(t)
iter := ParseString(`[]`)
iter := ParseString(ConfigOfDefault, `[]`)
cont := iter.ReadArray()
should.False(cont)
iter = ParseString(`[]`)
iter = ParseString(ConfigOfDefault, `[]`)
iter.ReadArrayCB(func(iter *Iterator) bool {
should.FailNow("should not call")
return true
@ -22,11 +22,11 @@ func Test_empty_array(t *testing.T) {
func Test_one_element(t *testing.T) {
should := require.New(t)
iter := ParseString(`[1]`)
iter := ParseString(ConfigOfDefault, `[1]`)
should.True(iter.ReadArray())
should.Equal(1, iter.ReadInt())
should.False(iter.ReadArray())
iter = ParseString(`[1]`)
iter = ParseString(ConfigOfDefault, `[1]`)
iter.ReadArrayCB(func(iter *Iterator) bool {
should.Equal(1, iter.ReadInt())
return true
@ -35,13 +35,13 @@ func Test_one_element(t *testing.T) {
func Test_two_elements(t *testing.T) {
should := require.New(t)
iter := ParseString(`[1,2]`)
iter := ParseString(ConfigOfDefault, `[1,2]`)
should.True(iter.ReadArray())
should.Equal(int64(1), iter.ReadInt64())
should.True(iter.ReadArray())
should.Equal(int64(2), iter.ReadInt64())
should.False(iter.ReadArray())
iter = ParseString(`[1,2]`)
iter = ParseString(ConfigOfDefault, `[1,2]`)
should.Equal([]interface{}{float64(1), float64(2)}, iter.Read())
}
@ -84,7 +84,7 @@ func Test_read_array_with_any_iterator(t *testing.T) {
func Test_wrap_array(t *testing.T) {
should := require.New(t)
any := Wrap([]int{1,2,3})
any := Wrap([]int{1, 2, 3})
should.Equal("[1,2,3]", any.ToString())
var element Any
var elements []int
@ -93,9 +93,9 @@ func Test_wrap_array(t *testing.T) {
elements = append(elements, element.ToInt())
}
should.Equal([]int{1, 2, 3}, elements)
any = Wrap([]int{1,2,3})
any = Wrap([]int{1, 2, 3})
should.Equal(3, any.Size())
any = Wrap([]int{1,2,3})
any = Wrap([]int{1, 2, 3})
should.Equal(2, any.Get(1).ToInt())
}
@ -103,7 +103,7 @@ func Test_array_lazy_any_get(t *testing.T) {
should := require.New(t)
any, err := UnmarshalAnyFromString("[1,[2,3],4]")
should.Nil(err)
should.Equal(3, any.Get(1,1).ToInt())
should.Equal(3, any.Get(1, 1).ToInt())
should.Equal("[1,[2,3],4]", any.ToString())
}
@ -111,25 +111,25 @@ func Test_array_lazy_any_get_all(t *testing.T) {
should := require.New(t)
any, err := UnmarshalAnyFromString("[[1],[2],[3,4]]")
should.Nil(err)
should.Equal("[1,2,3]", any.Get('*',0).ToString())
should.Equal("[1,2,3]", any.Get('*', 0).ToString())
}
func Test_array_wrapper_any_get_all(t *testing.T) {
should := require.New(t)
any := wrapArray([][]int{
[]int{1, 2},
[]int{3, 4},
[]int{5, 6},
{1, 2},
{3, 4},
{5, 6},
})
should.Equal("[1,3,5]", any.Get('*',0).ToString())
should.Equal("[1,3,5]", any.Get('*', 0).ToString())
}
func Test_array_lazy_any_get_invalid(t *testing.T) {
should := require.New(t)
any, err := UnmarshalAnyFromString("[]")
should.Nil(err)
should.Equal(Invalid, any.Get(1,1).ValueType())
should.NotNil(any.Get(1,1).LastError())
should.Equal(Invalid, any.Get(1, 1).ValueType())
should.NotNil(any.Get(1, 1).LastError())
should.Equal(Invalid, any.Get("1").ValueType())
should.NotNil(any.Get("1").LastError())
}
@ -152,7 +152,7 @@ func Test_invalid_array(t *testing.T) {
}
func Test_whitespace_in_head(t *testing.T) {
iter := ParseString(` [1]`)
iter := ParseString(ConfigOfDefault, ` [1]`)
cont := iter.ReadArray()
if cont != true {
t.FailNow()
@ -163,7 +163,7 @@ func Test_whitespace_in_head(t *testing.T) {
}
func Test_whitespace_after_array_start(t *testing.T) {
iter := ParseString(`[ 1]`)
iter := ParseString(ConfigOfDefault, `[ 1]`)
cont := iter.ReadArray()
if cont != true {
t.FailNow()
@ -174,7 +174,7 @@ func Test_whitespace_after_array_start(t *testing.T) {
}
func Test_whitespace_before_array_end(t *testing.T) {
iter := ParseString(`[1 ]`)
iter := ParseString(ConfigOfDefault, `[1 ]`)
cont := iter.ReadArray()
if cont != true {
t.FailNow()
@ -189,7 +189,7 @@ func Test_whitespace_before_array_end(t *testing.T) {
}
func Test_whitespace_before_comma(t *testing.T) {
iter := ParseString(`[1 ,2]`)
iter := ParseString(ConfigOfDefault, `[1 ,2]`)
cont := iter.ReadArray()
if cont != true {
t.FailNow()
@ -213,8 +213,7 @@ func Test_whitespace_before_comma(t *testing.T) {
func Test_write_array(t *testing.T) {
should := require.New(t)
buf := &bytes.Buffer{}
stream := NewStream(buf, 4096)
stream.IndentionStep = 2
stream := NewStream(Config{IndentionStep: 2}.Froze(), buf, 4096)
stream.WriteArrayStart()
stream.WriteInt(1)
stream.WriteMore()
@ -228,7 +227,7 @@ func Test_write_array(t *testing.T) {
func Test_write_val_array(t *testing.T) {
should := require.New(t)
val := []int{1, 2, 3}
str, err := MarshalToString(val)
str, err := MarshalToString(&val)
should.Nil(err)
should.Equal("[1,2,3]", str)
}
@ -244,7 +243,7 @@ func Test_write_val_empty_array(t *testing.T) {
func Test_write_array_of_interface_in_struct(t *testing.T) {
should := require.New(t)
type TestObject struct {
Field []interface{}
Field []interface{}
Field2 string
}
val := TestObject{[]interface{}{1, 2}, ""}
@ -264,10 +263,31 @@ func Test_json_RawMessage(t *testing.T) {
should.Equal(`[1,2,3]`, str)
}
func Test_encode_byte_array(t *testing.T) {
should := require.New(t)
bytes, err := json.Marshal([]byte{1, 2, 3})
should.Nil(err)
should.Equal(`"AQID"`, string(bytes))
bytes, err = Marshal([]byte{1, 2, 3})
should.Nil(err)
should.Equal(`"AQID"`, string(bytes))
}
func Test_decode_byte_array(t *testing.T) {
should := require.New(t)
data := []byte{}
err := json.Unmarshal([]byte(`"AQID"`), &data)
should.Nil(err)
should.Equal([]byte{1, 2, 3}, data)
err = Unmarshal([]byte(`"AQID"`), &data)
should.Nil(err)
should.Equal([]byte{1, 2, 3}, data)
}
func Benchmark_jsoniter_array(b *testing.B) {
b.ReportAllocs()
input := []byte(`[1,2,3,4,5,6,7,8,9]`)
iter := ParseBytes(input)
iter := ParseBytes(ConfigOfDefault, input)
b.ResetTimer()
for n := 0; n < b.N; n++ {
iter.ResetBytes(input)

File diff suppressed because one or more lines are too long

View File

@ -1,22 +1,22 @@
package jsoniter
import (
"testing"
"bytes"
"github.com/json-iterator/go/require"
"testing"
)
func Test_true(t *testing.T) {
should := require.New(t)
iter := ParseString(`true`)
iter := ParseString(ConfigOfDefault, `true`)
should.True(iter.ReadBool())
iter = ParseString(`true`)
iter = ParseString(ConfigOfDefault, `true`)
should.Equal(true, iter.Read())
}
func Test_false(t *testing.T) {
should := require.New(t)
iter := ParseString(`false`)
iter := ParseString(ConfigOfDefault, `false`)
should.False(iter.ReadBool())
}
@ -30,7 +30,7 @@ func Test_read_bool_as_any(t *testing.T) {
func Test_write_true_false(t *testing.T) {
should := require.New(t)
buf := &bytes.Buffer{}
stream := NewStream(buf, 4096)
stream := NewStream(ConfigOfDefault, buf, 4096)
stream.WriteTrue()
stream.WriteFalse()
stream.Flush()
@ -38,13 +38,12 @@ func Test_write_true_false(t *testing.T) {
should.Equal("truefalse", buf.String())
}
func Test_write_val_bool(t *testing.T) {
should := require.New(t)
buf := &bytes.Buffer{}
stream := NewStream(buf, 4096)
stream := NewStream(ConfigOfDefault, buf, 4096)
stream.WriteVal(true)
stream.Flush()
should.Nil(stream.Error)
should.Equal("true", buf.String())
}
}

View File

@ -1,13 +1,13 @@
package jsoniter
import (
"encoding/json"
"github.com/json-iterator/go/require"
"reflect"
"strconv"
"testing"
"time"
"unsafe"
"github.com/json-iterator/go/require"
"encoding/json"
)
func Test_customize_type_decoder(t *testing.T) {
@ -19,7 +19,7 @@ func Test_customize_type_decoder(t *testing.T) {
}
*((*time.Time)(ptr)) = t
})
defer CleanDecoders()
defer ConfigOfDefault.CleanDecoders()
val := time.Time{}
err := Unmarshal([]byte(`"2016-12-05 08:43:28"`), &val)
if err != nil {
@ -37,7 +37,7 @@ func Test_customize_type_encoder(t *testing.T) {
t := *((*time.Time)(ptr))
stream.WriteString(t.UTC().Format("2006-01-02 15:04:05"))
})
defer CleanEncoders()
defer ConfigOfDefault.CleanEncoders()
val := time.Unix(0, 0)
str, err := MarshalToString(val)
should.Nil(err)
@ -45,12 +45,13 @@ func Test_customize_type_encoder(t *testing.T) {
}
func Test_customize_byte_array_encoder(t *testing.T) {
ConfigOfDefault.CleanEncoders()
should := require.New(t)
RegisterTypeEncoder("[]uint8", func(ptr unsafe.Pointer, stream *Stream) {
t := *((*[]byte)(ptr))
stream.WriteString(string(t))
})
defer CleanEncoders()
defer ConfigOfDefault.CleanEncoders()
val := []byte("abc")
str, err := MarshalToString(val)
should.Nil(err)
@ -59,9 +60,8 @@ func Test_customize_byte_array_encoder(t *testing.T) {
func Test_customize_float_marshal(t *testing.T) {
should := require.New(t)
EnableLossyFloatMarshalling()
defer CleanEncoders()
str, err := MarshalToString(float32(1.23456789))
json := Config{MarshalFloatWith6Digits: true}.Froze()
str, err := json.MarshalToString(float32(1.23456789))
should.Nil(err)
should.Equal("1.234568", str)
}
@ -74,7 +74,7 @@ func Test_customize_field_decoder(t *testing.T) {
RegisterFieldDecoder("jsoniter.Tom", "field1", func(ptr unsafe.Pointer, iter *Iterator) {
*((*string)(ptr)) = strconv.Itoa(iter.ReadInt())
})
defer CleanDecoders()
defer ConfigOfDefault.CleanDecoders()
tom := Tom{}
err := Unmarshal([]byte(`{"field1": 100}`), &tom)
if err != nil {
@ -112,7 +112,7 @@ func Test_customize_field_by_extension(t *testing.T) {
}
func Test_unexported_fields(t *testing.T) {
EnableUnexportedStructFieldsSupport()
jsoniter := Config{SupportUnexportedStructFields: true}.Froze()
should := require.New(t)
type TestObject struct {
field1 string
@ -120,12 +120,12 @@ func Test_unexported_fields(t *testing.T) {
}
obj := TestObject{}
obj.field1 = "hello"
should.Nil(UnmarshalFromString(`{}`, &obj))
should.Nil(jsoniter.UnmarshalFromString(`{}`, &obj))
should.Equal("hello", obj.field1)
should.Nil(UnmarshalFromString(`{"field1": "world", "field-2": "abc"}`, &obj))
should.Nil(jsoniter.UnmarshalFromString(`{"field1": "world", "field-2": "abc"}`, &obj))
should.Equal("world", obj.field1)
should.Equal("abc", obj.field2)
str, err := MarshalToString(obj)
str, err := jsoniter.MarshalToString(obj)
should.Nil(err)
should.Contains(str, `"field-2":"abc"`)
}
@ -178,7 +178,7 @@ func (obj *ObjectImplementedUnmarshaler) UnmarshalJSON([]byte) error {
func Test_unmarshaler(t *testing.T) {
type TestObject struct {
Field *ObjectImplementedUnmarshaler
Field *ObjectImplementedUnmarshaler
Field2 string
}
should := require.New(t)
@ -195,7 +195,7 @@ func Test_unmarshaler(t *testing.T) {
func Test_unmarshaler_and_decoder(t *testing.T) {
type TestObject struct {
Field *ObjectImplementedUnmarshaler
Field *ObjectImplementedUnmarshaler
Field2 string
}
should := require.New(t)
@ -212,4 +212,4 @@ func Test_unmarshaler_and_decoder(t *testing.T) {
err = Unmarshal([]byte(`{"Field":"hello"}`), &obj)
should.Nil(err)
should.Equal(10, int(*obj.Field))
}
}

View File

@ -1,9 +1,10 @@
package jsoniter
import (
"encoding/json"
"fmt"
"testing"
"github.com/json-iterator/go/require"
"testing"
)
func Test_bind_api_demo(t *testing.T) {
@ -15,10 +16,71 @@ func Test_bind_api_demo(t *testing.T) {
}
func Test_iterator_api_demo(t *testing.T) {
iter := ParseString(`[0,1,2,3]`)
iter := ParseString(ConfigOfDefault, `[0,1,2,3]`)
total := 0
for iter.ReadArray() {
total += iter.ReadInt()
}
fmt.Println(total)
}
type People struct {
Name string
Gender string
Age int
Address string
Mobile string
Country string
Height int
}
func jsoniterMarshal(p *People) error {
_, err := Marshal(p)
if nil != err {
return err
}
return nil
}
func stdMarshal(p *People) error {
_, err := json.Marshal(p)
if nil != err {
return err
}
return nil
}
func BenchmarkJosniterMarshal(b *testing.B) {
var p People
p.Address = "上海市徐汇区漕宝路"
p.Age = 30
p.Country = "中国"
p.Gender = "male"
p.Height = 170
p.Mobile = "18502120533"
p.Name = "Elvin"
b.ReportAllocs()
for i := 0; i < b.N; i++ {
err := jsoniterMarshal(&p)
if nil != err {
b.Error(err)
}
}
}
func BenchmarkStdMarshal(b *testing.B) {
var p People
p.Address = "上海市徐汇区漕宝路"
p.Age = 30
p.Country = "中国"
p.Gender = "male"
p.Height = 170
p.Mobile = "18502120533"
p.Name = "Elvin"
b.ReportAllocs()
for i := 0; i < b.N; i++ {
err := stdMarshal(&p)
if nil != err {
b.Error(err)
}
}
}

View File

@ -1,62 +1,62 @@
package jsoniter
import (
"github.com/json-iterator/go/require"
"io"
"testing"
"github.com/json-iterator/go/require"
)
func Test_string_end(t *testing.T) {
end, escaped := ParseString(`abc"`).findStringEnd()
end, escaped := ParseString(ConfigOfDefault, `abc"`).findStringEnd()
if end != 4 {
t.Fatal(end)
}
if escaped != false {
t.Fatal(escaped)
}
end, escaped = ParseString(`abc\\"`).findStringEnd()
end, escaped = ParseString(ConfigOfDefault, `abc\\"`).findStringEnd()
if end != 6 {
t.Fatal(end)
}
if escaped != true {
t.Fatal(escaped)
}
end, escaped = ParseString(`abc\\\\"`).findStringEnd()
end, escaped = ParseString(ConfigOfDefault, `abc\\\\"`).findStringEnd()
if end != 8 {
t.Fatal(end)
}
if escaped != true {
t.Fatal(escaped)
}
end, escaped = ParseString(`abc\"`).findStringEnd()
end, escaped = ParseString(ConfigOfDefault, `abc\"`).findStringEnd()
if end != -1 {
t.Fatal(end)
}
if escaped != false {
t.Fatal(escaped)
}
end, escaped = ParseString(`abc\`).findStringEnd()
end, escaped = ParseString(ConfigOfDefault, `abc\`).findStringEnd()
if end != -1 {
t.Fatal(end)
}
if escaped != true {
t.Fatal(escaped)
}
end, escaped = ParseString(`abc\\`).findStringEnd()
end, escaped = ParseString(ConfigOfDefault, `abc\\`).findStringEnd()
if end != -1 {
t.Fatal(end)
}
if escaped != false {
t.Fatal(escaped)
}
end, escaped = ParseString(`\\`).findStringEnd()
end, escaped = ParseString(ConfigOfDefault, `\\`).findStringEnd()
if end != -1 {
t.Fatal(end)
}
if escaped != false {
t.Fatal(escaped)
}
end, escaped = ParseString(`\`).findStringEnd()
end, escaped = ParseString(ConfigOfDefault, `\`).findStringEnd()
if end != -1 {
t.Fatal(end)
}
@ -91,54 +91,54 @@ func (reader *StagedReader) Read(p []byte) (n int, err error) {
func Test_skip_string(t *testing.T) {
should := require.New(t)
iter := ParseString(`"abc`)
iter := ParseString(ConfigOfDefault, `"abc`)
iter.skipString()
should.Equal(1, iter.head)
iter = ParseString(`\""abc`)
iter = ParseString(ConfigOfDefault, `\""abc`)
iter.skipString()
should.Equal(3, iter.head)
reader := &StagedReader{
r1: `abc`,
r2: `"`,
}
iter = Parse(reader, 4096)
iter = Parse(ConfigOfDefault, reader, 4096)
iter.skipString()
should.Equal(1, iter.head)
reader = &StagedReader{
r1: `abc`,
r2: `1"`,
}
iter = Parse(reader, 4096)
iter = Parse(ConfigOfDefault, reader, 4096)
iter.skipString()
should.Equal(2, iter.head)
reader = &StagedReader{
r1: `abc\`,
r2: `"`,
}
iter = Parse(reader, 4096)
iter = Parse(ConfigOfDefault, reader, 4096)
iter.skipString()
should.NotNil(iter.Error)
reader = &StagedReader{
r1: `abc\`,
r2: `""`,
}
iter = Parse(reader, 4096)
iter = Parse(ConfigOfDefault, reader, 4096)
iter.skipString()
should.Equal(2, iter.head)
}
func Test_skip_object(t *testing.T) {
iter := ParseString(`}`)
iter := ParseString(ConfigOfDefault, `}`)
iter.skipObject()
if iter.head != 1 {
t.Fatal(iter.head)
}
iter = ParseString(`a}`)
iter = ParseString(ConfigOfDefault, `a}`)
iter.skipObject()
if iter.head != 2 {
t.Fatal(iter.head)
}
iter = ParseString(`{}}a`)
iter = ParseString(ConfigOfDefault, `{}}a`)
iter.skipObject()
if iter.head != 3 {
t.Fatal(iter.head)
@ -147,12 +147,12 @@ func Test_skip_object(t *testing.T) {
r1: `{`,
r2: `}}a`,
}
iter = Parse(reader, 4096)
iter = Parse(ConfigOfDefault, reader, 4096)
iter.skipObject()
if iter.head != 2 {
t.Fatal(iter.head)
}
iter = ParseString(`"}"}a`)
iter = ParseString(ConfigOfDefault, `"}"}a`)
iter.skipObject()
if iter.head != 4 {
t.Fatal(iter.head)

View File

@ -1,17 +1,17 @@
package jsoniter
import (
"bytes"
"encoding/json"
"fmt"
"testing"
"github.com/json-iterator/go/require"
"bytes"
"strconv"
"testing"
)
func Test_read_big_float(t *testing.T) {
should := require.New(t)
iter := ParseString(`12.3`)
iter := ParseString(ConfigOfDefault, `12.3`)
val := iter.ReadBigFloat()
val64, _ := val.Float64()
should.Equal(12.3, val64)
@ -19,7 +19,7 @@ func Test_read_big_float(t *testing.T) {
func Test_read_big_int(t *testing.T) {
should := require.New(t)
iter := ParseString(`92233720368547758079223372036854775807`)
iter := ParseString(ConfigOfDefault, `92233720368547758079223372036854775807`)
val := iter.ReadBigInt()
should.NotNil(val)
should.Equal(`92233720368547758079223372036854775807`, val.String())
@ -31,14 +31,14 @@ func Test_read_float(t *testing.T) {
// non-streaming
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
should := require.New(t)
iter := ParseString(input + ",")
iter := ParseString(ConfigOfDefault, input+",")
expected, err := strconv.ParseFloat(input, 32)
should.Nil(err)
should.Equal(float32(expected), iter.ReadFloat32())
})
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
should := require.New(t)
iter := ParseString(input + ",")
iter := ParseString(ConfigOfDefault, input+",")
expected, err := strconv.ParseFloat(input, 64)
should.Nil(err)
should.Equal(expected, iter.ReadFloat64())
@ -46,14 +46,14 @@ func Test_read_float(t *testing.T) {
// streaming
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
should := require.New(t)
iter := Parse(bytes.NewBufferString(input + ","), 2)
iter := Parse(ConfigOfDefault, bytes.NewBufferString(input+","), 2)
expected, err := strconv.ParseFloat(input, 32)
should.Nil(err)
should.Equal(float32(expected), iter.ReadFloat32())
})
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
should := require.New(t)
iter := Parse(bytes.NewBufferString(input + ","), 2)
iter := Parse(ConfigOfDefault, bytes.NewBufferString(input+","), 2)
expected, err := strconv.ParseFloat(input, 64)
should.Nil(err)
should.Equal(expected, iter.ReadFloat64())
@ -63,7 +63,7 @@ func Test_read_float(t *testing.T) {
func Test_read_float_as_interface(t *testing.T) {
should := require.New(t)
iter := ParseString(`12.3`)
iter := ParseString(ConfigOfDefault, `12.3`)
should.Equal(float64(12.3), iter.Read())
}
@ -85,12 +85,12 @@ func Test_wrap_float(t *testing.T) {
func Test_write_float32(t *testing.T) {
vals := []float32{0, 1, -1, 99, 0xff, 0xfff, 0xffff, 0xfffff, 0xffffff, 0x4ffffff, 0xfffffff,
-0x4ffffff, -0xfffffff, 1.2345, 1.23456, 1.234567, 1.001}
-0x4ffffff, -0xfffffff, 1.2345, 1.23456, 1.234567, 1.001}
for _, val := range vals {
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
should := require.New(t)
buf := &bytes.Buffer{}
stream := NewStream(buf, 4096)
stream := NewStream(ConfigOfDefault, buf, 4096)
stream.WriteFloat32Lossy(val)
stream.Flush()
should.Nil(stream.Error)
@ -99,7 +99,7 @@ func Test_write_float32(t *testing.T) {
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
should := require.New(t)
buf := &bytes.Buffer{}
stream := NewStream(buf, 4096)
stream := NewStream(ConfigOfDefault, buf, 4096)
stream.WriteVal(val)
stream.Flush()
should.Nil(stream.Error)
@ -108,7 +108,7 @@ func Test_write_float32(t *testing.T) {
}
should := require.New(t)
buf := &bytes.Buffer{}
stream := NewStream(buf, 10)
stream := NewStream(ConfigOfDefault, buf, 10)
stream.WriteRaw("abcdefg")
stream.WriteFloat32Lossy(1.123456)
stream.Flush()
@ -118,12 +118,12 @@ func Test_write_float32(t *testing.T) {
func Test_write_float64(t *testing.T) {
vals := []float64{0, 1, -1, 99, 0xff, 0xfff, 0xffff, 0xfffff, 0xffffff, 0x4ffffff, 0xfffffff,
-0x4ffffff, -0xfffffff, 1.2345, 1.23456, 1.234567, 1.001}
-0x4ffffff, -0xfffffff, 1.2345, 1.23456, 1.234567, 1.001}
for _, val := range vals {
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
should := require.New(t)
buf := &bytes.Buffer{}
stream := NewStream(buf, 4096)
stream := NewStream(ConfigOfDefault, buf, 4096)
stream.WriteFloat64Lossy(val)
stream.Flush()
should.Nil(stream.Error)
@ -132,7 +132,7 @@ func Test_write_float64(t *testing.T) {
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
should := require.New(t)
buf := &bytes.Buffer{}
stream := NewStream(buf, 4096)
stream := NewStream(ConfigOfDefault, buf, 4096)
stream.WriteVal(val)
stream.Flush()
should.Nil(stream.Error)
@ -141,7 +141,7 @@ func Test_write_float64(t *testing.T) {
}
should := require.New(t)
buf := &bytes.Buffer{}
stream := NewStream(buf, 10)
stream := NewStream(ConfigOfDefault, buf, 10)
stream.WriteRaw("abcdefg")
stream.WriteFloat64Lossy(1.123456)
stream.Flush()
@ -151,17 +151,30 @@ func Test_write_float64(t *testing.T) {
func Test_read_float64_cursor(t *testing.T) {
should := require.New(t)
iter := ParseString("[1.23456789\n,2,3]")
iter := ParseString(ConfigOfDefault, "[1.23456789\n,2,3]")
should.True(iter.ReadArray())
should.Equal(1.23456789, iter.Read())
should.True(iter.ReadArray())
should.Equal(float64(2), iter.Read())
}
func Test_read_float_scientific(t *testing.T) {
should := require.New(t)
var obj interface{}
should.Nil(UnmarshalFromString(`1e1`, &obj))
should.Equal(float64(10), obj)
should.Nil(json.Unmarshal([]byte(`1e1`), &obj))
should.Equal(float64(10), obj)
should.Nil(UnmarshalFromString(`1.0e1`, &obj))
should.Equal(float64(10), obj)
should.Nil(json.Unmarshal([]byte(`1.0e1`), &obj))
should.Equal(float64(10), obj)
}
func Benchmark_jsoniter_float(b *testing.B) {
b.ReportAllocs()
input := []byte(`1.1123,`)
iter := NewIterator()
iter := NewIterator(ConfigOfDefault)
for n := 0; n < b.N; n++ {
iter.ResetBytes(input)
iter.ReadFloat64()

View File

@ -3,17 +3,17 @@ package jsoniter
import (
"bytes"
"encoding/json"
"testing"
"github.com/json-iterator/go/require"
"fmt"
"strconv"
"io/ioutil"
"github.com/json-iterator/go/require"
"io"
"io/ioutil"
"strconv"
"testing"
)
func Test_read_uint64_invalid(t *testing.T) {
should := require.New(t)
iter := ParseString(",")
iter := ParseString(ConfigOfDefault, ",")
iter.ReadUint64()
should.NotNil(iter.Error)
}
@ -23,7 +23,7 @@ func Test_read_int8(t *testing.T) {
for _, input := range inputs {
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
should := require.New(t)
iter := ParseString(input)
iter := ParseString(ConfigOfDefault, input)
expected, err := strconv.ParseInt(input, 10, 8)
should.Nil(err)
should.Equal(int8(expected), iter.ReadInt8())
@ -36,7 +36,7 @@ func Test_read_int16(t *testing.T) {
for _, input := range inputs {
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
should := require.New(t)
iter := ParseString(input)
iter := ParseString(ConfigOfDefault, input)
expected, err := strconv.ParseInt(input, 10, 16)
should.Nil(err)
should.Equal(int16(expected), iter.ReadInt16())
@ -49,14 +49,14 @@ func Test_read_int32(t *testing.T) {
for _, input := range inputs {
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
should := require.New(t)
iter := ParseString(input)
iter := ParseString(ConfigOfDefault, input)
expected, err := strconv.ParseInt(input, 10, 32)
should.Nil(err)
should.Equal(int32(expected), iter.ReadInt32())
})
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
should := require.New(t)
iter := Parse(bytes.NewBufferString(input), 2)
iter := Parse(ConfigOfDefault, bytes.NewBufferString(input), 2)
expected, err := strconv.ParseInt(input, 10, 32)
should.Nil(err)
should.Equal(int32(expected), iter.ReadInt32())
@ -83,7 +83,7 @@ func Test_read_int64_array(t *testing.T) {
func Test_read_int32_overflow(t *testing.T) {
should := require.New(t)
input := "123456789123456789,"
iter := ParseString(input)
iter := ParseString(ConfigOfDefault, input)
iter.ReadInt32()
should.NotNil(iter.Error)
}
@ -93,14 +93,14 @@ func Test_read_int64(t *testing.T) {
for _, input := range inputs {
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
should := require.New(t)
iter := ParseString(input)
iter := ParseString(ConfigOfDefault, input)
expected, err := strconv.ParseInt(input, 10, 64)
should.Nil(err)
should.Equal(expected, iter.ReadInt64())
})
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
should := require.New(t)
iter := Parse(bytes.NewBufferString(input), 2)
iter := Parse(ConfigOfDefault, bytes.NewBufferString(input), 2)
expected, err := strconv.ParseInt(input, 10, 64)
should.Nil(err)
should.Equal(expected, iter.ReadInt64())
@ -111,7 +111,7 @@ func Test_read_int64(t *testing.T) {
func Test_read_int64_overflow(t *testing.T) {
should := require.New(t)
input := "123456789123456789123456789123456789,"
iter := ParseString(input)
iter := ParseString(ConfigOfDefault, input)
iter.ReadInt64()
should.NotNil(iter.Error)
}
@ -146,7 +146,7 @@ func Test_write_uint8(t *testing.T) {
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
should := require.New(t)
buf := &bytes.Buffer{}
stream := NewStream(buf, 4096)
stream := NewStream(ConfigOfDefault, buf, 4096)
stream.WriteUint8(val)
stream.Flush()
should.Nil(stream.Error)
@ -155,7 +155,7 @@ func Test_write_uint8(t *testing.T) {
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
should := require.New(t)
buf := &bytes.Buffer{}
stream := NewStream(buf, 4096)
stream := NewStream(ConfigOfDefault, buf, 4096)
stream.WriteVal(val)
stream.Flush()
should.Nil(stream.Error)
@ -164,7 +164,7 @@ func Test_write_uint8(t *testing.T) {
}
should := require.New(t)
buf := &bytes.Buffer{}
stream := NewStream(buf, 3)
stream := NewStream(ConfigOfDefault, buf, 3)
stream.WriteRaw("a")
stream.WriteUint8(100) // should clear buffer
stream.Flush()
@ -178,7 +178,7 @@ func Test_write_int8(t *testing.T) {
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
should := require.New(t)
buf := &bytes.Buffer{}
stream := NewStream(buf, 4096)
stream := NewStream(ConfigOfDefault, buf, 4096)
stream.WriteInt8(val)
stream.Flush()
should.Nil(stream.Error)
@ -187,7 +187,7 @@ func Test_write_int8(t *testing.T) {
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
should := require.New(t)
buf := &bytes.Buffer{}
stream := NewStream(buf, 4096)
stream := NewStream(ConfigOfDefault, buf, 4096)
stream.WriteVal(val)
stream.Flush()
should.Nil(stream.Error)
@ -196,7 +196,7 @@ func Test_write_int8(t *testing.T) {
}
should := require.New(t)
buf := &bytes.Buffer{}
stream := NewStream(buf, 4)
stream := NewStream(ConfigOfDefault, buf, 4)
stream.WriteRaw("a")
stream.WriteInt8(-100) // should clear buffer
stream.Flush()
@ -210,7 +210,7 @@ func Test_write_uint16(t *testing.T) {
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
should := require.New(t)
buf := &bytes.Buffer{}
stream := NewStream(buf, 4096)
stream := NewStream(ConfigOfDefault, buf, 4096)
stream.WriteUint16(val)
stream.Flush()
should.Nil(stream.Error)
@ -219,7 +219,7 @@ func Test_write_uint16(t *testing.T) {
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
should := require.New(t)
buf := &bytes.Buffer{}
stream := NewStream(buf, 4096)
stream := NewStream(ConfigOfDefault, buf, 4096)
stream.WriteVal(val)
stream.Flush()
should.Nil(stream.Error)
@ -228,7 +228,7 @@ func Test_write_uint16(t *testing.T) {
}
should := require.New(t)
buf := &bytes.Buffer{}
stream := NewStream(buf, 5)
stream := NewStream(ConfigOfDefault, buf, 5)
stream.WriteRaw("a")
stream.WriteUint16(10000) // should clear buffer
stream.Flush()
@ -242,7 +242,7 @@ func Test_write_int16(t *testing.T) {
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
should := require.New(t)
buf := &bytes.Buffer{}
stream := NewStream(buf, 4096)
stream := NewStream(ConfigOfDefault, buf, 4096)
stream.WriteInt16(val)
stream.Flush()
should.Nil(stream.Error)
@ -251,7 +251,7 @@ func Test_write_int16(t *testing.T) {
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
should := require.New(t)
buf := &bytes.Buffer{}
stream := NewStream(buf, 4096)
stream := NewStream(ConfigOfDefault, buf, 4096)
stream.WriteVal(val)
stream.Flush()
should.Nil(stream.Error)
@ -260,7 +260,7 @@ func Test_write_int16(t *testing.T) {
}
should := require.New(t)
buf := &bytes.Buffer{}
stream := NewStream(buf, 6)
stream := NewStream(ConfigOfDefault, buf, 6)
stream.WriteRaw("a")
stream.WriteInt16(-10000) // should clear buffer
stream.Flush()
@ -274,7 +274,7 @@ func Test_write_uint32(t *testing.T) {
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
should := require.New(t)
buf := &bytes.Buffer{}
stream := NewStream(buf, 4096)
stream := NewStream(ConfigOfDefault, buf, 4096)
stream.WriteUint32(val)
stream.Flush()
should.Nil(stream.Error)
@ -283,7 +283,7 @@ func Test_write_uint32(t *testing.T) {
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
should := require.New(t)
buf := &bytes.Buffer{}
stream := NewStream(buf, 4096)
stream := NewStream(ConfigOfDefault, buf, 4096)
stream.WriteVal(val)
stream.Flush()
should.Nil(stream.Error)
@ -292,7 +292,7 @@ func Test_write_uint32(t *testing.T) {
}
should := require.New(t)
buf := &bytes.Buffer{}
stream := NewStream(buf, 10)
stream := NewStream(ConfigOfDefault, buf, 10)
stream.WriteRaw("a")
stream.WriteUint32(0xffffffff) // should clear buffer
stream.Flush()
@ -306,7 +306,7 @@ func Test_write_int32(t *testing.T) {
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
should := require.New(t)
buf := &bytes.Buffer{}
stream := NewStream(buf, 4096)
stream := NewStream(ConfigOfDefault, buf, 4096)
stream.WriteInt32(val)
stream.Flush()
should.Nil(stream.Error)
@ -315,7 +315,7 @@ func Test_write_int32(t *testing.T) {
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
should := require.New(t)
buf := &bytes.Buffer{}
stream := NewStream(buf, 4096)
stream := NewStream(ConfigOfDefault, buf, 4096)
stream.WriteVal(val)
stream.Flush()
should.Nil(stream.Error)
@ -324,7 +324,7 @@ func Test_write_int32(t *testing.T) {
}
should := require.New(t)
buf := &bytes.Buffer{}
stream := NewStream(buf, 11)
stream := NewStream(ConfigOfDefault, buf, 11)
stream.WriteRaw("a")
stream.WriteInt32(-0x7fffffff) // should clear buffer
stream.Flush()
@ -340,7 +340,7 @@ func Test_write_uint64(t *testing.T) {
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
should := require.New(t)
buf := &bytes.Buffer{}
stream := NewStream(buf, 4096)
stream := NewStream(ConfigOfDefault, buf, 4096)
stream.WriteUint64(val)
stream.Flush()
should.Nil(stream.Error)
@ -349,7 +349,7 @@ func Test_write_uint64(t *testing.T) {
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
should := require.New(t)
buf := &bytes.Buffer{}
stream := NewStream(buf, 4096)
stream := NewStream(ConfigOfDefault, buf, 4096)
stream.WriteVal(val)
stream.Flush()
should.Nil(stream.Error)
@ -358,7 +358,7 @@ func Test_write_uint64(t *testing.T) {
}
should := require.New(t)
buf := &bytes.Buffer{}
stream := NewStream(buf, 10)
stream := NewStream(ConfigOfDefault, buf, 10)
stream.WriteRaw("a")
stream.WriteUint64(0xffffffff) // should clear buffer
stream.Flush()
@ -374,7 +374,7 @@ func Test_write_int64(t *testing.T) {
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
should := require.New(t)
buf := &bytes.Buffer{}
stream := NewStream(buf, 4096)
stream := NewStream(ConfigOfDefault, buf, 4096)
stream.WriteInt64(val)
stream.Flush()
should.Nil(stream.Error)
@ -383,7 +383,7 @@ func Test_write_int64(t *testing.T) {
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
should := require.New(t)
buf := &bytes.Buffer{}
stream := NewStream(buf, 4096)
stream := NewStream(ConfigOfDefault, buf, 4096)
stream.WriteVal(val)
stream.Flush()
should.Nil(stream.Error)
@ -392,7 +392,7 @@ func Test_write_int64(t *testing.T) {
}
should := require.New(t)
buf := &bytes.Buffer{}
stream := NewStream(buf, 10)
stream := NewStream(ConfigOfDefault, buf, 10)
stream.WriteRaw("a")
stream.WriteInt64(0xffffffff) // should clear buffer
stream.Flush()
@ -403,7 +403,7 @@ func Test_write_int64(t *testing.T) {
func Test_write_val_int(t *testing.T) {
should := require.New(t)
buf := &bytes.Buffer{}
stream := NewStream(buf, 4096)
stream := NewStream(ConfigOfDefault, buf, 4096)
stream.WriteVal(1001)
stream.Flush()
should.Nil(stream.Error)
@ -413,7 +413,7 @@ func Test_write_val_int(t *testing.T) {
func Test_write_val_int_ptr(t *testing.T) {
should := require.New(t)
buf := &bytes.Buffer{}
stream := NewStream(buf, 4096)
stream := NewStream(ConfigOfDefault, buf, 4096)
val := 1001
stream.WriteVal(&val)
stream.Flush()
@ -433,7 +433,7 @@ func Test_json_number(t *testing.T) {
}
func Benchmark_jsoniter_encode_int(b *testing.B) {
stream := NewStream(ioutil.Discard, 64)
stream := NewStream(ConfigOfDefault, ioutil.Discard, 64)
for n := 0; n < b.N; n++ {
stream.n = 0
stream.WriteUint64(0xffffffff)
@ -447,7 +447,7 @@ func Benchmark_itoa(b *testing.B) {
}
func Benchmark_jsoniter_int(b *testing.B) {
iter := NewIterator()
iter := NewIterator(ConfigOfDefault)
input := []byte(`100`)
for n := 0; n < b.N; n++ {
iter.ResetBytes(input)

View File

@ -1,8 +1,9 @@
package jsoniter
import (
"testing"
"encoding/json"
"github.com/json-iterator/go/require"
"testing"
"unsafe"
)
@ -16,7 +17,7 @@ func Test_write_array_of_interface(t *testing.T) {
func Test_write_map_of_interface(t *testing.T) {
should := require.New(t)
val := map[string]interface{}{"hello":"world"}
val := map[string]interface{}{"hello": "world"}
str, err := MarshalToString(val)
should.Nil(err)
should.Equal(`{"hello":"world"}`, str)
@ -27,7 +28,7 @@ func Test_write_map_of_interface_in_struct(t *testing.T) {
Field map[string]interface{}
}
should := require.New(t)
val := TestObject{map[string]interface{}{"hello":"world"}}
val := TestObject{map[string]interface{}{"hello": "world"}}
str, err := MarshalToString(val)
should.Nil(err)
should.Equal(`{"Field":{"hello":"world"}}`, str)
@ -35,11 +36,11 @@ func Test_write_map_of_interface_in_struct(t *testing.T) {
func Test_write_map_of_interface_in_struct_with_two_fields(t *testing.T) {
type TestObject struct {
Field map[string]interface{}
Field map[string]interface{}
Field2 string
}
should := require.New(t)
val := TestObject{map[string]interface{}{"hello":"world"}, ""}
val := TestObject{map[string]interface{}{"hello": "world"}, ""}
str, err := MarshalToString(val)
should.Nil(err)
should.Contains(str, `"Field":{"hello":"world"}`)
@ -59,7 +60,7 @@ func Test_write_map_of_custom_interface(t *testing.T) {
should := require.New(t)
myStr := MyString("world")
should.Equal("world", myStr.Hello())
val := map[string]MyInterface{"hello":myStr}
val := map[string]MyInterface{"hello": myStr}
str, err := MarshalToString(val)
should.Nil(err)
should.Equal(`{"hello":"world"}`, str)
@ -137,4 +138,17 @@ func Test_encode_object_contain_non_empty_interface(t *testing.T) {
str, err := MarshalToString(obj)
should.Nil(err)
should.Equal(`{"Field":"hello"}`, str)
}
}
func Test_nil_non_empty_interface(t *testing.T) {
ConfigOfDefault.CleanEncoders()
ConfigOfDefault.CleanDecoders()
type TestObject struct {
Field []MyInterface
}
should := require.New(t)
obj := TestObject{}
b := []byte(`{"Field":["AAA"]}`)
should.NotNil(json.Unmarshal(b, &obj))
should.NotNil(Unmarshal(b, &obj))
}

View File

@ -7,7 +7,7 @@ import (
)
func Test_read_by_one(t *testing.T) {
iter := Parse(bytes.NewBufferString("abc"), 1)
iter := Parse(ConfigOfDefault, bytes.NewBufferString("abc"), 1)
b := iter.readByte()
if iter.Error != nil {
t.Fatal(iter.Error)
@ -34,7 +34,7 @@ func Test_read_by_one(t *testing.T) {
}
func Test_read_by_two(t *testing.T) {
iter := Parse(bytes.NewBufferString("abc"), 2)
iter := Parse(ConfigOfDefault, bytes.NewBufferString("abc"), 2)
b := iter.readByte()
if iter.Error != nil {
t.Fatal(iter.Error)
@ -67,7 +67,7 @@ func Test_read_by_two(t *testing.T) {
}
func Test_read_until_eof(t *testing.T) {
iter := Parse(bytes.NewBufferString("abc"), 2)
iter := Parse(ConfigOfDefault, bytes.NewBufferString("abc"), 2)
iter.readByte()
iter.readByte()
b := iter.readByte()

View File

@ -27,7 +27,7 @@ func Benchmark_jsoniter_large_file(b *testing.B) {
b.ReportAllocs()
for n := 0; n < b.N; n++ {
file, _ := os.Open("/tmp/large-file.json")
iter := Parse(file, 4096)
iter := Parse(ConfigOfDefault, file, 4096)
count := 0
for iter.ReadArray() {
iter.Skip()

View File

@ -1,13 +1,15 @@
package jsoniter
import (
"testing"
"encoding/json"
"github.com/json-iterator/go/require"
"math/big"
"testing"
)
func Test_read_map(t *testing.T) {
should := require.New(t)
iter := ParseString(`{"hello": "world"}`)
iter := ParseString(ConfigOfDefault, `{"hello": "world"}`)
m := map[string]string{"1": "2"}
iter.ReadVal(&m)
copy(iter.buf, []byte{0, 0, 0, 0, 0, 0})
@ -16,11 +18,11 @@ func Test_read_map(t *testing.T) {
func Test_read_map_of_interface(t *testing.T) {
should := require.New(t)
iter := ParseString(`{"hello": "world"}`)
iter := ParseString(ConfigOfDefault, `{"hello": "world"}`)
m := map[string]interface{}{"1": "2"}
iter.ReadVal(&m)
should.Equal(map[string]interface{}{"1": "2", "hello": "world"}, m)
iter = ParseString(`{"hello": "world"}`)
iter = ParseString(ConfigOfDefault, `{"hello": "world"}`)
should.Equal(map[string]interface{}{"hello": "world"}, iter.Read())
}
@ -40,12 +42,12 @@ func Test_wrap_map(t *testing.T) {
vals[k] = v.ToString()
}
}
should.Equal(map[string]string{"Field1":"hello"}, vals)
should.Equal(map[string]string{"Field1": "hello"}, vals)
}
func Test_map_wrapper_any_get_all(t *testing.T) {
should := require.New(t)
any := Wrap(map[string][]int{"Field1": []int{1, 2}})
any := Wrap(map[string][]int{"Field1": {1, 2}})
should.Equal(`{"Field1":1}`, any.Get('*', 0).ToString())
}
@ -66,4 +68,74 @@ func Test_slice_of_map(t *testing.T) {
val = []map[string]string{}
should.Nil(UnmarshalFromString(str, &val))
should.Equal("2", val[0]["1"])
}
}
func Test_encode_int_key_map(t *testing.T) {
should := require.New(t)
val := map[int]string{1: "2"}
str, err := MarshalToString(val)
should.Nil(err)
should.Equal(`{"1":"2"}`, str)
}
func Test_decode_int_key_map(t *testing.T) {
should := require.New(t)
var val map[int]string
should.Nil(UnmarshalFromString(`{"1":"2"}`, &val))
should.Equal(map[int]string{1: "2"}, val)
}
func Test_encode_TextMarshaler_key_map(t *testing.T) {
should := require.New(t)
f, _, _ := big.ParseFloat("1", 10, 64, big.ToZero)
val := map[*big.Float]string{f: "2"}
str, err := MarshalToString(val)
should.Nil(err)
should.Equal(`{"1":"2"}`, str)
}
func Test_decode_TextMarshaler_key_map(t *testing.T) {
should := require.New(t)
var val map[*big.Float]string
should.Nil(UnmarshalFromString(`{"1":"2"}`, &val))
str, err := MarshalToString(val)
should.Nil(err)
should.Equal(`{"1":"2"}`, str)
}
func Test_map_key_with_escaped_char(t *testing.T) {
type Ttest struct {
Map map[string]string
}
var jsonBytes = []byte(`
{
"Map":{
"k\"ey": "val"
}
}`)
should := require.New(t)
{
var obj Ttest
should.Nil(json.Unmarshal(jsonBytes, &obj))
should.Equal(map[string]string{"k\"ey": "val"}, obj.Map)
}
{
var obj Ttest
should.Nil(Unmarshal(jsonBytes, &obj))
should.Equal(map[string]string{"k\"ey": "val"}, obj.Map)
}
}
func Test_encode_map_with_sorted_keys(t *testing.T) {
should := require.New(t)
m := map[string]interface{}{
"3": 3,
"1": 1,
"2": 2,
}
bytes, err := json.Marshal(m)
should.Nil(err)
output, err := ConfigCompatibleWithStandardLibrary.MarshalToString(m)
should.Nil(err)
should.Equal(string(bytes), output)
}

View File

@ -15,7 +15,7 @@ type Level2 struct {
}
func Test_nested(t *testing.T) {
iter := ParseString(`{"hello": [{"world": "value1"}, {"world": "value2"}]}`)
iter := ParseString(ConfigOfDefault, `{"hello": [{"world": "value1"}, {"world": "value2"}]}`)
l1 := Level1{}
for l1Field := iter.ReadObject(); l1Field != ""; l1Field = iter.ReadObject() {
switch l1Field {
@ -50,7 +50,7 @@ func Test_nested(t *testing.T) {
func Benchmark_jsoniter_nested(b *testing.B) {
for n := 0; n < b.N; n++ {
iter := ParseString(`{"hello": [{"world": "value1"}, {"world": "value2"}]}`)
iter := ParseString(ConfigOfDefault, `{"hello": [{"world": "value1"}, {"world": "value2"}]}`)
l1 := Level1{}
for l1Field := iter.ReadObject(); l1Field != ""; l1Field = iter.ReadObject() {
switch l1Field {

View File

@ -1,18 +1,19 @@
package jsoniter
import (
"testing"
"github.com/json-iterator/go/require"
"bytes"
"encoding/json"
"github.com/json-iterator/go/require"
"testing"
)
func Test_read_null(t *testing.T) {
should := require.New(t)
iter := ParseString(`null`)
iter := ParseString(ConfigOfDefault, `null`)
should.True(iter.ReadNil())
iter = ParseString(`null`)
iter = ParseString(ConfigOfDefault, `null`)
should.Nil(iter.Read())
iter = ParseString(`null`)
iter = ParseString(ConfigOfDefault, `null`)
any, err := UnmarshalAnyFromString(`null`)
should.Nil(err)
should.Equal(0, any.ToInt())
@ -24,7 +25,7 @@ func Test_read_null(t *testing.T) {
func Test_write_null(t *testing.T) {
should := require.New(t)
buf := &bytes.Buffer{}
stream := NewStream(buf, 4096)
stream := NewStream(ConfigOfDefault, buf, 4096)
stream.WriteNil()
stream.Flush()
should.Nil(stream.Error)
@ -40,7 +41,7 @@ func Test_encode_null(t *testing.T) {
func Test_decode_null_object(t *testing.T) {
should := require.New(t)
iter := ParseString(`[null,"a"]`)
iter := ParseString(ConfigOfDefault, `[null,"a"]`)
iter.ReadArray()
if iter.ReadObject() != "" {
t.FailNow()
@ -58,7 +59,7 @@ func Test_decode_null_object(t *testing.T) {
}
func Test_decode_null_array(t *testing.T) {
iter := ParseString(`[null,"a"]`)
iter := ParseString(ConfigOfDefault, `[null,"a"]`)
iter.ReadArray()
if iter.ReadArray() != false {
t.FailNow()
@ -71,7 +72,7 @@ func Test_decode_null_array(t *testing.T) {
func Test_decode_null_string(t *testing.T) {
should := require.New(t)
iter := ParseString(`[null,"a"]`)
iter := ParseString(ConfigOfDefault, `[null,"a"]`)
should.True(iter.ReadArray())
should.Equal("", iter.ReadString())
should.True(iter.ReadArray())
@ -79,7 +80,7 @@ func Test_decode_null_string(t *testing.T) {
}
func Test_decode_null_skip(t *testing.T) {
iter := ParseString(`[null,"a"]`)
iter := ParseString(ConfigOfDefault, `[null,"a"]`)
iter.ReadArray()
iter.Skip()
iter.ReadArray()
@ -87,3 +88,39 @@ func Test_decode_null_skip(t *testing.T) {
t.FailNow()
}
}
func Test_encode_nil_map(t *testing.T) {
should := require.New(t)
type Ttest map[string]string
var obj1 Ttest
output, err := json.Marshal(obj1)
should.Nil(err)
should.Equal("null", string(output))
output, err = json.Marshal(&obj1)
should.Nil(err)
should.Equal("null", string(output))
output, err = Marshal(obj1)
should.Nil(err)
should.Equal("null", string(output))
output, err = Marshal(&obj1)
should.Nil(err)
should.Equal("null", string(output))
}
func Test_encode_nil_array(t *testing.T) {
should := require.New(t)
type Ttest []string
var obj1 Ttest
output, err := json.Marshal(obj1)
should.Nil(err)
should.Equal("null", string(output))
output, err = json.Marshal(&obj1)
should.Nil(err)
should.Equal("null", string(output))
output, err = Marshal(obj1)
should.Nil(err)
should.Equal("null", string(output))
output, err = Marshal(&obj1)
should.Nil(err)
should.Equal("null", string(output))
}

View File

@ -1,18 +1,18 @@
package jsoniter
import (
"encoding/json"
"testing"
"github.com/json-iterator/go/require"
"bytes"
"encoding/json"
"github.com/json-iterator/go/require"
"testing"
)
func Test_empty_object(t *testing.T) {
should := require.New(t)
iter := ParseString(`{}`)
iter := ParseString(ConfigOfDefault, `{}`)
field := iter.ReadObject()
should.Equal("", field)
iter = ParseString(`{}`)
iter = ParseString(ConfigOfDefault, `{}`)
iter.ReadObjectCB(func(iter *Iterator, field string) bool {
should.FailNow("should not call")
return true
@ -21,14 +21,14 @@ func Test_empty_object(t *testing.T) {
func Test_one_field(t *testing.T) {
should := require.New(t)
iter := ParseString(`{"a": "b"}`)
iter := ParseString(ConfigOfDefault, `{"a": "b"}`)
field := iter.ReadObject()
should.Equal("a", field)
value := iter.ReadString()
should.Equal("b", value)
field = iter.ReadObject()
should.Equal("", field)
iter = ParseString(`{"a": "b"}`)
iter = ParseString(ConfigOfDefault, `{"a": "b"}`)
should.True(iter.ReadObjectCB(func(iter *Iterator, field string) bool {
should.Equal("a", field)
return true
@ -37,7 +37,7 @@ func Test_one_field(t *testing.T) {
func Test_two_field(t *testing.T) {
should := require.New(t)
iter := ParseString(`{ "a": "b" , "c": "d" }`)
iter := ParseString(ConfigOfDefault, `{ "a": "b" , "c": "d" }`)
field := iter.ReadObject()
should.Equal("a", field)
value := iter.ReadString()
@ -48,7 +48,7 @@ func Test_two_field(t *testing.T) {
should.Equal("d", value)
field = iter.ReadObject()
should.Equal("", field)
iter = ParseString(`{"field1": "1", "field2": 2}`)
iter = ParseString(ConfigOfDefault, `{"field1": "1", "field2": 2}`)
for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
switch field {
case "field1":
@ -100,7 +100,7 @@ func Test_object_any_lazy_iterator(t *testing.T) {
should.False(hasNext)
vals[k] = v.ToString()
should.Equal(map[string]string{"a":"b", "c":"d"}, vals)
should.Equal(map[string]string{"a": "b", "c": "d"}, vals)
vals = map[string]string{}
for next, hasNext := any.IterateObject(); hasNext; {
k, v, hasNext = next()
@ -108,7 +108,7 @@ func Test_object_any_lazy_iterator(t *testing.T) {
vals[k] = v.ToString()
}
}
should.Equal(map[string]string{"a":"b", "c":"d"}, vals)
should.Equal(map[string]string{"a": "b", "c": "d"}, vals)
}
func Test_object_any_with_two_lazy_iterators(t *testing.T) {
@ -194,7 +194,7 @@ func Test_wrap_object(t *testing.T) {
vals[k] = v.ToString()
}
}
should.Equal(map[string]string{"Field1":"hello"}, vals)
should.Equal(map[string]string{"Field1": "hello"}, vals)
}
func Test_object_wrapper_any_get_all(t *testing.T) {
@ -210,8 +210,7 @@ func Test_object_wrapper_any_get_all(t *testing.T) {
func Test_write_object(t *testing.T) {
should := require.New(t)
buf := &bytes.Buffer{}
stream := NewStream(buf, 4096)
stream.IndentionStep = 2
stream := NewStream(Config{IndentionStep: 2}.Froze(), buf, 4096)
stream.WriteObjectStart()
stream.WriteObjectField("hello")
stream.WriteInt(1)
@ -230,7 +229,7 @@ func Benchmark_jsoniter_object(b *testing.B) {
Field2 uint64
}
for n := 0; n < b.N; n++ {
iter := ParseString(`{"field1": "1", "field2": 2}`)
iter := ParseString(ConfigOfDefault, `{"field1": "1", "field2": 2}`)
obj := TestObj{}
for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
switch field {

View File

@ -1,8 +1,8 @@
package jsoniter
import (
"testing"
"github.com/json-iterator/go/require"
"testing"
)
func Test_encode_optional_int_pointer(t *testing.T) {
@ -21,26 +21,26 @@ func Test_encode_optional_int_pointer(t *testing.T) {
func Test_decode_struct_with_optional_field(t *testing.T) {
should := require.New(t)
type TestObject struct {
field1 *string
field2 *string
Field1 *string
Field2 *string
}
obj := TestObject{}
UnmarshalFromString(`{"field1": null, "field2": "world"}`, &obj)
should.Nil(obj.field1)
should.Equal("world", *obj.field2)
should.Nil(obj.Field1)
should.Equal("world", *obj.Field2)
}
func Test_encode_struct_with_optional_field(t *testing.T) {
should := require.New(t)
type TestObject struct {
field1 *string
field2 *string
Field1 *string
Field2 *string
}
obj := TestObject{}
world := "world"
obj.field2 = &world
obj.Field2 = &world
str, err := MarshalToString(obj)
should.Nil(err)
should.Contains(str, `"field1":null`)
should.Contains(str, `"field2":"world"`)
}
should.Contains(str, `"Field1":null`)
should.Contains(str, `"Field2":"world"`)
}

View File

@ -1,12 +1,12 @@
package jsoniter
import (
"testing"
"fmt"
"testing"
)
func Test_reflect_str(t *testing.T) {
iter := ParseString(`"hello"`)
iter := ParseString(ConfigOfDefault, `"hello"`)
str := ""
iter.ReadVal(&str)
if str != "hello" {
@ -16,7 +16,7 @@ func Test_reflect_str(t *testing.T) {
}
func Test_reflect_ptr_str(t *testing.T) {
iter := ParseString(`"hello"`)
iter := ParseString(ConfigOfDefault, `"hello"`)
var str *string
iter.ReadVal(&str)
if *str != "hello" {
@ -25,7 +25,7 @@ func Test_reflect_ptr_str(t *testing.T) {
}
func Test_reflect_int(t *testing.T) {
iter := ParseString(`123`)
iter := ParseString(ConfigOfDefault, `123`)
val := int(0)
iter.ReadVal(&val)
if val != 123 {
@ -34,7 +34,7 @@ func Test_reflect_int(t *testing.T) {
}
func Test_reflect_int8(t *testing.T) {
iter := ParseString(`123`)
iter := ParseString(ConfigOfDefault, `123`)
val := int8(0)
iter.ReadVal(&val)
if val != 123 {
@ -43,7 +43,7 @@ func Test_reflect_int8(t *testing.T) {
}
func Test_reflect_int16(t *testing.T) {
iter := ParseString(`123`)
iter := ParseString(ConfigOfDefault, `123`)
val := int16(0)
iter.ReadVal(&val)
if val != 123 {
@ -52,7 +52,7 @@ func Test_reflect_int16(t *testing.T) {
}
func Test_reflect_int32(t *testing.T) {
iter := ParseString(`123`)
iter := ParseString(ConfigOfDefault, `123`)
val := int32(0)
iter.ReadVal(&val)
if val != 123 {
@ -61,7 +61,7 @@ func Test_reflect_int32(t *testing.T) {
}
func Test_reflect_int64(t *testing.T) {
iter := ParseString(`123`)
iter := ParseString(ConfigOfDefault, `123`)
val := int64(0)
iter.ReadVal(&val)
if val != 123 {
@ -70,7 +70,7 @@ func Test_reflect_int64(t *testing.T) {
}
func Test_reflect_uint(t *testing.T) {
iter := ParseString(`123`)
iter := ParseString(ConfigOfDefault, `123`)
val := uint(0)
iter.ReadVal(&val)
if val != 123 {
@ -79,7 +79,7 @@ func Test_reflect_uint(t *testing.T) {
}
func Test_reflect_uint8(t *testing.T) {
iter := ParseString(`123`)
iter := ParseString(ConfigOfDefault, `123`)
val := uint8(0)
iter.ReadVal(&val)
if val != 123 {
@ -88,7 +88,7 @@ func Test_reflect_uint8(t *testing.T) {
}
func Test_reflect_uint16(t *testing.T) {
iter := ParseString(`123`)
iter := ParseString(ConfigOfDefault, `123`)
val := uint16(0)
iter.ReadVal(&val)
if val != 123 {
@ -97,7 +97,7 @@ func Test_reflect_uint16(t *testing.T) {
}
func Test_reflect_uint32(t *testing.T) {
iter := ParseString(`123`)
iter := ParseString(ConfigOfDefault, `123`)
val := uint32(0)
iter.ReadVal(&val)
if val != 123 {
@ -106,7 +106,7 @@ func Test_reflect_uint32(t *testing.T) {
}
func Test_reflect_uint64(t *testing.T) {
iter := ParseString(`123`)
iter := ParseString(ConfigOfDefault, `123`)
val := uint64(0)
iter.ReadVal(&val)
if val != 123 {
@ -115,7 +115,7 @@ func Test_reflect_uint64(t *testing.T) {
}
func Test_reflect_byte(t *testing.T) {
iter := ParseString(`123`)
iter := ParseString(ConfigOfDefault, `123`)
val := byte(0)
iter.ReadVal(&val)
if val != 123 {
@ -124,7 +124,7 @@ func Test_reflect_byte(t *testing.T) {
}
func Test_reflect_float32(t *testing.T) {
iter := ParseString(`1.23`)
iter := ParseString(ConfigOfDefault, `1.23`)
val := float32(0)
iter.ReadVal(&val)
if val != 1.23 {
@ -134,7 +134,7 @@ func Test_reflect_float32(t *testing.T) {
}
func Test_reflect_float64(t *testing.T) {
iter := ParseString(`1.23`)
iter := ParseString(ConfigOfDefault, `1.23`)
val := float64(0)
iter.ReadVal(&val)
if val != 1.23 {
@ -144,11 +144,11 @@ func Test_reflect_float64(t *testing.T) {
}
func Test_reflect_bool(t *testing.T) {
iter := ParseString(`true`)
iter := ParseString(ConfigOfDefault, `true`)
val := false
iter.ReadVal(&val)
if val != true {
fmt.Println(iter.Error)
t.Fatal(val)
}
}
}

View File

@ -1,9 +1,9 @@
package jsoniter
import (
"testing"
"github.com/json-iterator/go/require"
"bytes"
"github.com/json-iterator/go/require"
"testing"
)
func Test_decode_one_field_struct(t *testing.T) {
@ -149,9 +149,9 @@ func Test_write_val_one_field_struct(t *testing.T) {
func Test_mixed(t *testing.T) {
should := require.New(t)
type AA struct {
ID int `json:"id"`
ID int `json:"id"`
Payload map[string]interface{} `json:"payload"`
buf *bytes.Buffer `json:"-"`
buf *bytes.Buffer `json:"-"`
}
aa := AA{}
err := UnmarshalFromString(` {"id":1, "payload":{"account":"123","password":"456"}}`, &aa)
@ -230,7 +230,7 @@ func Test_anonymous_struct_marshal(t *testing.T) {
type TestObject struct {
Field string
}
str, err := MarshalToString(struct{
str, err := MarshalToString(struct {
TestObject
Field int
}{
@ -238,4 +238,4 @@ func Test_anonymous_struct_marshal(t *testing.T) {
})
should.Nil(err)
should.Equal(`{"Field":100}`, str)
}
}

View File

@ -3,9 +3,9 @@ package jsoniter
import (
"encoding/json"
"fmt"
"github.com/json-iterator/go/require"
"testing"
"unsafe"
"github.com/json-iterator/go/require"
)
func Test_decode_slice(t *testing.T) {
@ -24,17 +24,17 @@ func Test_decode_large_slice(t *testing.T) {
func Test_decode_nested(t *testing.T) {
type StructOfString struct {
field1 string
field2 string
Field1 string
Field2 string
}
iter := ParseString(`[{"field1": "hello"}, null, {"field2": "world"}]`)
iter := ParseString(ConfigOfDefault, `[{"field1": "hello"}, null, {"field2": "world"}]`)
slice := []*StructOfString{}
iter.ReadVal(&slice)
if len(slice) != 3 {
fmt.Println(iter.Error)
t.Fatal(len(slice))
}
if slice[0].field1 != "hello" {
if slice[0].Field1 != "hello" {
fmt.Println(iter.Error)
t.Fatal(slice[0])
}
@ -42,19 +42,19 @@ func Test_decode_nested(t *testing.T) {
fmt.Println(iter.Error)
t.Fatal(slice[1])
}
if slice[2].field2 != "world" {
if slice[2].Field2 != "world" {
fmt.Println(iter.Error)
t.Fatal(slice[2])
}
}
func Test_decode_base64(t *testing.T) {
iter := ParseString(`"YWJj"`)
iter := ParseString(ConfigOfDefault, `"YWJj"`)
val := []byte{}
RegisterTypeDecoder("[]uint8", func(ptr unsafe.Pointer, iter *Iterator) {
*((*[]byte)(ptr)) = iter.ReadBase64()
})
defer CleanDecoders()
defer ConfigOfDefault.CleanDecoders()
iter.ReadVal(&val)
if "abc" != string(val) {
t.Fatal(string(val))
@ -70,7 +70,7 @@ type StructOfTagOne struct {
func Benchmark_jsoniter_reflect(b *testing.B) {
b.ReportAllocs()
iter := NewIterator()
iter := NewIterator(ConfigOfDefault)
Struct := &StructOfTagOne{}
//var Struct *StructOfTagOne
input := []byte(`{"field3": "100", "field4": "100"}`)
@ -96,7 +96,7 @@ func Benchmark_jsoniter_direct(b *testing.B) {
// iter.Skip()
// }
//}
iter := ParseString(`["hello", "world"]`)
iter := ParseString(ConfigOfDefault, `["hello", "world"]`)
array := make([]string, 0, 2)
for iter.ReadArray() {
array = append(array, iter.ReadString())

View File

@ -6,7 +6,7 @@ import (
)
func Test_skip_number(t *testing.T) {
iter := ParseString(`[-0.12, "b"]`)
iter := ParseString(ConfigOfDefault, `[-0.12, "b"]`)
iter.ReadArray()
iter.Skip()
iter.ReadArray()
@ -16,7 +16,7 @@ func Test_skip_number(t *testing.T) {
}
func Test_skip_null(t *testing.T) {
iter := ParseString(`[null , "b"]`)
iter := ParseString(ConfigOfDefault, `[null , "b"]`)
iter.ReadArray()
iter.Skip()
iter.ReadArray()
@ -26,7 +26,7 @@ func Test_skip_null(t *testing.T) {
}
func Test_skip_true(t *testing.T) {
iter := ParseString(`[true , "b"]`)
iter := ParseString(ConfigOfDefault, `[true , "b"]`)
iter.ReadArray()
iter.Skip()
iter.ReadArray()
@ -36,7 +36,7 @@ func Test_skip_true(t *testing.T) {
}
func Test_skip_false(t *testing.T) {
iter := ParseString(`[false , "b"]`)
iter := ParseString(ConfigOfDefault, `[false , "b"]`)
iter.ReadArray()
iter.Skip()
iter.ReadArray()
@ -46,7 +46,7 @@ func Test_skip_false(t *testing.T) {
}
func Test_skip_array(t *testing.T) {
iter := ParseString(`[[1, [2, [3], 4]], "b"]`)
iter := ParseString(ConfigOfDefault, `[[1, [2, [3], 4]], "b"]`)
iter.ReadArray()
iter.Skip()
iter.ReadArray()
@ -56,7 +56,7 @@ func Test_skip_array(t *testing.T) {
}
func Test_skip_empty_array(t *testing.T) {
iter := ParseString(`[ [ ], "b"]`)
iter := ParseString(ConfigOfDefault, `[ [ ], "b"]`)
iter.ReadArray()
iter.Skip()
iter.ReadArray()
@ -66,7 +66,7 @@ func Test_skip_empty_array(t *testing.T) {
}
func Test_skip_nested(t *testing.T) {
iter := ParseString(`[ {"a" : [{"b": "c"}], "d": 102 }, "b"]`)
iter := ParseString(ConfigOfDefault, `[ {"a" : [{"b": "c"}], "d": 102 }, "b"]`)
iter.ReadArray()
iter.Skip()
iter.ReadArray()
@ -106,7 +106,7 @@ func Benchmark_jsoniter_skip(b *testing.B) {
}`)
for n := 0; n < b.N; n++ {
result := TestResp{}
iter := ParseBytes(input)
iter := ParseBytes(ConfigOfDefault, input)
for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
switch field {
case "code":

53
jsoniter_stream_test.go Normal file
View File

@ -0,0 +1,53 @@
package jsoniter
import (
"github.com/json-iterator/go/require"
"testing"
)
func Test_writeByte_should_grow_buffer(t *testing.T) {
should := require.New(t)
stream := NewStream(ConfigOfDefault, nil, 1)
stream.writeByte('1')
should.Equal("1", string(stream.Buffer()))
should.Equal(1, len(stream.buf))
stream.writeByte('2')
should.Equal("12", string(stream.Buffer()))
should.Equal(2, len(stream.buf))
stream.writeThreeBytes('3', '4', '5')
should.Equal("12345", string(stream.Buffer()))
}
func Test_writeBytes_should_grow_buffer(t *testing.T) {
should := require.New(t)
stream := NewStream(ConfigOfDefault, nil, 1)
stream.Write([]byte{'1', '2'})
should.Equal("12", string(stream.Buffer()))
should.Equal(3, len(stream.buf))
stream.Write([]byte{'3', '4', '5', '6', '7'})
should.Equal("1234567", string(stream.Buffer()))
should.Equal(8, len(stream.buf))
}
func Test_writeIndention_should_grow_buffer(t *testing.T) {
should := require.New(t)
stream := NewStream(Config{IndentionStep: 2}.Froze(), nil, 1)
stream.WriteVal([]int{1, 2, 3})
should.Equal("[\n 1,\n 2,\n 3\n]", string(stream.Buffer()))
}
func Test_writeRaw_should_grow_buffer(t *testing.T) {
should := require.New(t)
stream := NewStream(ConfigOfDefault, nil, 1)
stream.WriteRaw("123")
should.Nil(stream.Error)
should.Equal("123", string(stream.Buffer()))
}
func Test_writeString_should_grow_buffer(t *testing.T) {
should := require.New(t)
stream := NewStream(ConfigOfDefault, nil, 0)
stream.WriteString("123")
should.Nil(stream.Error)
should.Equal(`"123"`, string(stream.Buffer()))
}

View File

@ -3,36 +3,37 @@ package jsoniter
import (
"bytes"
"encoding/json"
"testing"
"github.com/json-iterator/go/require"
"fmt"
"github.com/json-iterator/go/require"
"testing"
"unicode/utf8"
)
func Test_read_normal_string(t *testing.T) {
cases := map[string]string{
`"0123456789012345678901234567890123456789"`: `0123456789012345678901234567890123456789`,
`""`: ``,
`""`: ``,
`"hello"`: `hello`,
}
for input, output := range cases {
t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
should := require.New(t)
iter := ParseString(input)
iter := ParseString(ConfigOfDefault, input)
should.Equal(output, iter.ReadString())
})
t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
should := require.New(t)
iter := Parse(bytes.NewBufferString(input), 2)
iter := Parse(ConfigOfDefault, bytes.NewBufferString(input), 2)
should.Equal(output, iter.ReadString())
})
t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
should := require.New(t)
iter := ParseString(input)
iter := ParseString(ConfigOfDefault, input)
should.Equal(output, string(iter.ReadStringAsSlice()))
})
t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
should := require.New(t)
iter := Parse(bytes.NewBufferString(input), 2)
iter := Parse(ConfigOfDefault, bytes.NewBufferString(input), 2)
should.Equal(output, string(iter.ReadStringAsSlice()))
})
}
@ -40,20 +41,20 @@ func Test_read_normal_string(t *testing.T) {
func Test_read_exotic_string(t *testing.T) {
cases := map[string]string{
`"hel\"lo"`: `hel"lo`,
`"hel\nlo"`: "hel\nlo",
`"hel\"lo"`: `hel"lo`,
`"hel\nlo"`: "hel\nlo",
`"\u4e2d\u6587"`: "中文",
`"\ud83d\udc4a"`: "\xf0\x9f\x91\x8a", // surrogate
}
for input, output := range cases {
t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
should := require.New(t)
iter := ParseString(input)
iter := ParseString(ConfigOfDefault, input)
should.Equal(output, iter.ReadString())
})
t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
should := require.New(t)
iter := Parse(bytes.NewBufferString(input), 2)
iter := Parse(ConfigOfDefault, bytes.NewBufferString(input), 2)
should.Equal(output, iter.ReadString())
})
}
@ -61,7 +62,7 @@ func Test_read_exotic_string(t *testing.T) {
func Test_read_string_as_interface(t *testing.T) {
should := require.New(t)
iter := ParseString(`"hello"`)
iter := ParseString(ConfigOfDefault, `"hello"`)
should.Equal("hello", iter.Read())
}
@ -98,22 +99,71 @@ func Test_write_string(t *testing.T) {
func Test_write_val_string(t *testing.T) {
should := require.New(t)
buf := &bytes.Buffer{}
stream := NewStream(buf, 4096)
stream := NewStream(ConfigOfDefault, buf, 4096)
stream.WriteVal("hello")
stream.Flush()
should.Nil(stream.Error)
should.Equal(`"hello"`, buf.String())
}
func Test_decode_slash(t *testing.T) {
should := require.New(t)
var obj interface{}
should.NotNil(json.Unmarshal([]byte("\\"), &obj))
should.NotNil(UnmarshalFromString("\\", &obj))
}
func Test_html_escape(t *testing.T) {
should := require.New(t)
output, err := json.Marshal(`>`)
should.Nil(err)
should.Equal(`"\u003e"`, string(output))
output, err = ConfigCompatibleWithStandardLibrary.Marshal(`>`)
should.Nil(err)
should.Equal(`"\u003e"`, string(output))
}
func Test_string_encode_with_std(t *testing.T) {
should := require.New(t)
for i := 0; i < utf8.RuneSelf; i++ {
input := string([]byte{byte(i)})
stdOutputBytes, err := json.Marshal(input)
should.Nil(err)
stdOutput := string(stdOutputBytes)
jsoniterOutputBytes, err := ConfigCompatibleWithStandardLibrary.Marshal(input)
should.Nil(err)
jsoniterOutput := string(jsoniterOutputBytes)
should.Equal(stdOutput, jsoniterOutput)
}
}
func Test_string_encode_with_std_without_html_escape(t *testing.T) {
should := require.New(t)
for i := 0; i < utf8.RuneSelf; i++ {
input := string([]byte{byte(i)})
buf := &bytes.Buffer{}
encoder := json.NewEncoder(buf)
encoder.SetEscapeHTML(false)
err := encoder.Encode(input)
should.Nil(err)
stdOutput := buf.String()
stdOutput = stdOutput[:len(stdOutput) - 1]
jsoniterOutputBytes, err := Marshal(input)
should.Nil(err)
jsoniterOutput := string(jsoniterOutputBytes)
should.Equal(stdOutput, jsoniterOutput)
}
}
func Benchmark_jsoniter_unicode(b *testing.B) {
for n := 0; n < b.N; n++ {
iter := ParseString(`"\ud83d\udc4a"`)
iter := ParseString(ConfigOfDefault, `"\ud83d\udc4a"`)
iter.ReadString()
}
}
func Benchmark_jsoniter_ascii(b *testing.B) {
iter := NewIterator()
iter := NewIterator(ConfigOfDefault)
input := []byte(`"hello, world! hello, world!"`)
b.ResetTimer()
for n := 0; n < b.N; n++ {
@ -123,7 +173,7 @@ func Benchmark_jsoniter_ascii(b *testing.B) {
}
func Benchmark_jsoniter_string_as_bytes(b *testing.B) {
iter := ParseString(`"hello, world!"`)
iter := ParseString(ConfigOfDefault, `"hello, world!"`)
b.ResetTimer()
for n := 0; n < b.N; n++ {
iter.ResetBytes(iter.buf)

View File

@ -0,0 +1,144 @@
package test
import (
"bytes"
"encoding/json"
"testing"
"github.com/davecgh/go-spew/spew"
fuzz "github.com/google/gofuzz"
jsoniter "github.com/json-iterator/go"
)
func Test_Roundtrip(t *testing.T) {
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
for i := 0; i < 1000; i++ {
var before T
fz.Fuzz(&before)
jbStd, err := json.Marshal(before)
if err != nil {
t.Errorf("failed to marshal with stdlib: %v", err)
}
jbIter, err := jsoniter.Marshal(before)
if err != nil {
t.Errorf("failed to marshal with jsoniter: %v", err)
}
if string(jbStd) != string(jbIter) {
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
indent(jbStd, " "), indent(jbIter, " "), dump(before))
}
var afterStd T
err = json.Unmarshal(jbIter, &afterStd)
if err != nil {
t.Errorf("failed to unmarshal with stdlib: %v", err)
}
var afterIter T
err = jsoniter.Unmarshal(jbIter, &afterIter)
if err != nil {
t.Errorf("failed to unmarshal with jsoniter: %v", err)
}
if fingerprint(afterStd) != fingerprint(afterIter) {
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
dump(afterStd), dump(afterIter), indent(jbIter, " "))
}
}
}
const indentStr = "> "
func fingerprint(obj interface{}) string {
c := spew.ConfigState{
SortKeys: true,
SpewKeys: true,
}
return c.Sprintf("%v", obj)
}
func dump(obj interface{}) string {
cfg := spew.ConfigState{
Indent: indentStr,
}
return cfg.Sdump(obj)
}
func indent(src []byte, prefix string) string {
var buf bytes.Buffer
json.Indent(&buf, src, prefix, indentStr)
return buf.String()
}
func BenchmarkStandardMarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var obj T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&obj)
for i := 0; i < t.N; i++ {
jb, err := json.Marshal(obj)
if err != nil {
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
}
_ = jb
}
}
func BenchmarkStandardUnmarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var before T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&before)
jb, err := json.Marshal(before)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
for i := 0; i < t.N; i++ {
var after T
err = json.Unmarshal(jb, &after)
if err != nil {
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
}
}
}
func BenchmarkJSONIterMarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var obj T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&obj)
for i := 0; i < t.N; i++ {
jb, err := jsoniter.Marshal(obj)
if err != nil {
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
}
_ = jb
}
}
func BenchmarkJSONIterUnmarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var before T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&before)
jb, err := json.Marshal(before)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
for i := 0; i < t.N; i++ {
var after T
err = jsoniter.Unmarshal(jb, &after)
if err != nil {
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
}
}
}

View File

@ -0,0 +1,3 @@
package test
type T bool

View File

@ -0,0 +1,144 @@
package test
import (
"bytes"
"encoding/json"
"testing"
"github.com/davecgh/go-spew/spew"
fuzz "github.com/google/gofuzz"
jsoniter "github.com/json-iterator/go"
)
func Test_Roundtrip(t *testing.T) {
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
for i := 0; i < 1000; i++ {
var before T
fz.Fuzz(&before)
jbStd, err := json.Marshal(before)
if err != nil {
t.Errorf("failed to marshal with stdlib: %v", err)
}
jbIter, err := jsoniter.Marshal(before)
if err != nil {
t.Errorf("failed to marshal with jsoniter: %v", err)
}
if string(jbStd) != string(jbIter) {
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
indent(jbStd, " "), indent(jbIter, " "), dump(before))
}
var afterStd T
err = json.Unmarshal(jbIter, &afterStd)
if err != nil {
t.Errorf("failed to unmarshal with stdlib: %v", err)
}
var afterIter T
err = jsoniter.Unmarshal(jbIter, &afterIter)
if err != nil {
t.Errorf("failed to unmarshal with jsoniter: %v", err)
}
if fingerprint(afterStd) != fingerprint(afterIter) {
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
dump(afterStd), dump(afterIter), indent(jbIter, " "))
}
}
}
const indentStr = "> "
func fingerprint(obj interface{}) string {
c := spew.ConfigState{
SortKeys: true,
SpewKeys: true,
}
return c.Sprintf("%v", obj)
}
func dump(obj interface{}) string {
cfg := spew.ConfigState{
Indent: indentStr,
}
return cfg.Sdump(obj)
}
func indent(src []byte, prefix string) string {
var buf bytes.Buffer
json.Indent(&buf, src, prefix, indentStr)
return buf.String()
}
func BenchmarkStandardMarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var obj T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&obj)
for i := 0; i < t.N; i++ {
jb, err := json.Marshal(obj)
if err != nil {
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
}
_ = jb
}
}
func BenchmarkStandardUnmarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var before T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&before)
jb, err := json.Marshal(before)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
for i := 0; i < t.N; i++ {
var after T
err = json.Unmarshal(jb, &after)
if err != nil {
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
}
}
}
func BenchmarkJSONIterMarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var obj T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&obj)
for i := 0; i < t.N; i++ {
jb, err := jsoniter.Marshal(obj)
if err != nil {
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
}
_ = jb
}
}
func BenchmarkJSONIterUnmarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var before T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&before)
jb, err := json.Marshal(before)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
for i := 0; i < t.N; i++ {
var after T
err = jsoniter.Unmarshal(jb, &after)
if err != nil {
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
}
}
}

View File

@ -0,0 +1,3 @@
package test
type T bool

View File

@ -0,0 +1,144 @@
package test
import (
"bytes"
"encoding/json"
"testing"
"github.com/davecgh/go-spew/spew"
fuzz "github.com/google/gofuzz"
jsoniter "github.com/json-iterator/go"
)
func Test_Roundtrip(t *testing.T) {
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
for i := 0; i < 1000; i++ {
var before T
fz.Fuzz(&before)
jbStd, err := json.Marshal(before)
if err != nil {
t.Errorf("failed to marshal with stdlib: %v", err)
}
jbIter, err := jsoniter.Marshal(before)
if err != nil {
t.Errorf("failed to marshal with jsoniter: %v", err)
}
if string(jbStd) != string(jbIter) {
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
indent(jbStd, " "), indent(jbIter, " "), dump(before))
}
var afterStd T
err = json.Unmarshal(jbIter, &afterStd)
if err != nil {
t.Errorf("failed to unmarshal with stdlib: %v", err)
}
var afterIter T
err = jsoniter.Unmarshal(jbIter, &afterIter)
if err != nil {
t.Errorf("failed to unmarshal with jsoniter: %v", err)
}
if fingerprint(afterStd) != fingerprint(afterIter) {
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
dump(afterStd), dump(afterIter), indent(jbIter, " "))
}
}
}
const indentStr = "> "
func fingerprint(obj interface{}) string {
c := spew.ConfigState{
SortKeys: true,
SpewKeys: true,
}
return c.Sprintf("%v", obj)
}
func dump(obj interface{}) string {
cfg := spew.ConfigState{
Indent: indentStr,
}
return cfg.Sdump(obj)
}
func indent(src []byte, prefix string) string {
var buf bytes.Buffer
json.Indent(&buf, src, prefix, indentStr)
return buf.String()
}
func BenchmarkStandardMarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var obj T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&obj)
for i := 0; i < t.N; i++ {
jb, err := json.Marshal(obj)
if err != nil {
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
}
_ = jb
}
}
func BenchmarkStandardUnmarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var before T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&before)
jb, err := json.Marshal(before)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
for i := 0; i < t.N; i++ {
var after T
err = json.Unmarshal(jb, &after)
if err != nil {
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
}
}
}
func BenchmarkJSONIterMarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var obj T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&obj)
for i := 0; i < t.N; i++ {
jb, err := jsoniter.Marshal(obj)
if err != nil {
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
}
_ = jb
}
}
func BenchmarkJSONIterUnmarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var before T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&before)
jb, err := json.Marshal(before)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
for i := 0; i < t.N; i++ {
var after T
err = jsoniter.Unmarshal(jb, &after)
if err != nil {
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
}
}
}

View File

@ -0,0 +1,3 @@
package test
type T byte

View File

@ -0,0 +1,144 @@
package test
import (
"bytes"
"encoding/json"
"testing"
"github.com/davecgh/go-spew/spew"
fuzz "github.com/google/gofuzz"
jsoniter "github.com/json-iterator/go"
)
func Test_Roundtrip(t *testing.T) {
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
for i := 0; i < 1000; i++ {
var before T
fz.Fuzz(&before)
jbStd, err := json.Marshal(before)
if err != nil {
t.Errorf("failed to marshal with stdlib: %v", err)
}
jbIter, err := jsoniter.Marshal(before)
if err != nil {
t.Errorf("failed to marshal with jsoniter: %v", err)
}
if string(jbStd) != string(jbIter) {
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
indent(jbStd, " "), indent(jbIter, " "), dump(before))
}
var afterStd T
err = json.Unmarshal(jbIter, &afterStd)
if err != nil {
t.Errorf("failed to unmarshal with stdlib: %v", err)
}
var afterIter T
err = jsoniter.Unmarshal(jbIter, &afterIter)
if err != nil {
t.Errorf("failed to unmarshal with jsoniter: %v", err)
}
if fingerprint(afterStd) != fingerprint(afterIter) {
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
dump(afterStd), dump(afterIter), indent(jbIter, " "))
}
}
}
const indentStr = "> "
func fingerprint(obj interface{}) string {
c := spew.ConfigState{
SortKeys: true,
SpewKeys: true,
}
return c.Sprintf("%v", obj)
}
func dump(obj interface{}) string {
cfg := spew.ConfigState{
Indent: indentStr,
}
return cfg.Sdump(obj)
}
func indent(src []byte, prefix string) string {
var buf bytes.Buffer
json.Indent(&buf, src, prefix, indentStr)
return buf.String()
}
func BenchmarkStandardMarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var obj T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&obj)
for i := 0; i < t.N; i++ {
jb, err := json.Marshal(obj)
if err != nil {
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
}
_ = jb
}
}
func BenchmarkStandardUnmarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var before T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&before)
jb, err := json.Marshal(before)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
for i := 0; i < t.N; i++ {
var after T
err = json.Unmarshal(jb, &after)
if err != nil {
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
}
}
}
func BenchmarkJSONIterMarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var obj T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&obj)
for i := 0; i < t.N; i++ {
jb, err := jsoniter.Marshal(obj)
if err != nil {
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
}
_ = jb
}
}
func BenchmarkJSONIterUnmarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var before T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&before)
jb, err := json.Marshal(before)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
for i := 0; i < t.N; i++ {
var after T
err = jsoniter.Unmarshal(jb, &after)
if err != nil {
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
}
}
}

View File

@ -0,0 +1,3 @@
package test
type T byte

View File

@ -0,0 +1,144 @@
package test
import (
"bytes"
"encoding/json"
"testing"
"github.com/davecgh/go-spew/spew"
fuzz "github.com/google/gofuzz"
jsoniter "github.com/json-iterator/go"
)
func Test_Roundtrip(t *testing.T) {
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
for i := 0; i < 1000; i++ {
var before T
fz.Fuzz(&before)
jbStd, err := json.Marshal(before)
if err != nil {
t.Errorf("failed to marshal with stdlib: %v", err)
}
jbIter, err := jsoniter.Marshal(before)
if err != nil {
t.Errorf("failed to marshal with jsoniter: %v", err)
}
if string(jbStd) != string(jbIter) {
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
indent(jbStd, " "), indent(jbIter, " "), dump(before))
}
var afterStd T
err = json.Unmarshal(jbIter, &afterStd)
if err != nil {
t.Errorf("failed to unmarshal with stdlib: %v", err)
}
var afterIter T
err = jsoniter.Unmarshal(jbIter, &afterIter)
if err != nil {
t.Errorf("failed to unmarshal with jsoniter: %v", err)
}
if fingerprint(afterStd) != fingerprint(afterIter) {
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
dump(afterStd), dump(afterIter), indent(jbIter, " "))
}
}
}
const indentStr = "> "
func fingerprint(obj interface{}) string {
c := spew.ConfigState{
SortKeys: true,
SpewKeys: true,
}
return c.Sprintf("%v", obj)
}
func dump(obj interface{}) string {
cfg := spew.ConfigState{
Indent: indentStr,
}
return cfg.Sdump(obj)
}
func indent(src []byte, prefix string) string {
var buf bytes.Buffer
json.Indent(&buf, src, prefix, indentStr)
return buf.String()
}
func BenchmarkStandardMarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var obj T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&obj)
for i := 0; i < t.N; i++ {
jb, err := json.Marshal(obj)
if err != nil {
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
}
_ = jb
}
}
func BenchmarkStandardUnmarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var before T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&before)
jb, err := json.Marshal(before)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
for i := 0; i < t.N; i++ {
var after T
err = json.Unmarshal(jb, &after)
if err != nil {
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
}
}
}
func BenchmarkJSONIterMarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var obj T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&obj)
for i := 0; i < t.N; i++ {
jb, err := jsoniter.Marshal(obj)
if err != nil {
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
}
_ = jb
}
}
func BenchmarkJSONIterUnmarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var before T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&before)
jb, err := json.Marshal(before)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
for i := 0; i < t.N; i++ {
var after T
err = jsoniter.Unmarshal(jb, &after)
if err != nil {
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
}
}
}

View File

@ -0,0 +1,3 @@
package test
type T float32

View File

@ -0,0 +1,144 @@
package test
import (
"bytes"
"encoding/json"
"testing"
"github.com/davecgh/go-spew/spew"
fuzz "github.com/google/gofuzz"
jsoniter "github.com/json-iterator/go"
)
func Test_Roundtrip(t *testing.T) {
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
for i := 0; i < 1000; i++ {
var before T
fz.Fuzz(&before)
jbStd, err := json.Marshal(before)
if err != nil {
t.Errorf("failed to marshal with stdlib: %v", err)
}
jbIter, err := jsoniter.Marshal(before)
if err != nil {
t.Errorf("failed to marshal with jsoniter: %v", err)
}
if string(jbStd) != string(jbIter) {
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
indent(jbStd, " "), indent(jbIter, " "), dump(before))
}
var afterStd T
err = json.Unmarshal(jbIter, &afterStd)
if err != nil {
t.Errorf("failed to unmarshal with stdlib: %v", err)
}
var afterIter T
err = jsoniter.Unmarshal(jbIter, &afterIter)
if err != nil {
t.Errorf("failed to unmarshal with jsoniter: %v", err)
}
if fingerprint(afterStd) != fingerprint(afterIter) {
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
dump(afterStd), dump(afterIter), indent(jbIter, " "))
}
}
}
const indentStr = "> "
func fingerprint(obj interface{}) string {
c := spew.ConfigState{
SortKeys: true,
SpewKeys: true,
}
return c.Sprintf("%v", obj)
}
func dump(obj interface{}) string {
cfg := spew.ConfigState{
Indent: indentStr,
}
return cfg.Sdump(obj)
}
func indent(src []byte, prefix string) string {
var buf bytes.Buffer
json.Indent(&buf, src, prefix, indentStr)
return buf.String()
}
func BenchmarkStandardMarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var obj T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&obj)
for i := 0; i < t.N; i++ {
jb, err := json.Marshal(obj)
if err != nil {
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
}
_ = jb
}
}
func BenchmarkStandardUnmarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var before T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&before)
jb, err := json.Marshal(before)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
for i := 0; i < t.N; i++ {
var after T
err = json.Unmarshal(jb, &after)
if err != nil {
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
}
}
}
func BenchmarkJSONIterMarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var obj T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&obj)
for i := 0; i < t.N; i++ {
jb, err := jsoniter.Marshal(obj)
if err != nil {
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
}
_ = jb
}
}
func BenchmarkJSONIterUnmarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var before T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&before)
jb, err := json.Marshal(before)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
for i := 0; i < t.N; i++ {
var after T
err = jsoniter.Unmarshal(jb, &after)
if err != nil {
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
}
}
}

View File

@ -0,0 +1,3 @@
package test
type T float32

View File

@ -0,0 +1,144 @@
package test
import (
"bytes"
"encoding/json"
"testing"
"github.com/davecgh/go-spew/spew"
fuzz "github.com/google/gofuzz"
jsoniter "github.com/json-iterator/go"
)
func Test_Roundtrip(t *testing.T) {
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
for i := 0; i < 1000; i++ {
var before T
fz.Fuzz(&before)
jbStd, err := json.Marshal(before)
if err != nil {
t.Errorf("failed to marshal with stdlib: %v", err)
}
jbIter, err := jsoniter.Marshal(before)
if err != nil {
t.Errorf("failed to marshal with jsoniter: %v", err)
}
if string(jbStd) != string(jbIter) {
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
indent(jbStd, " "), indent(jbIter, " "), dump(before))
}
var afterStd T
err = json.Unmarshal(jbIter, &afterStd)
if err != nil {
t.Errorf("failed to unmarshal with stdlib: %v", err)
}
var afterIter T
err = jsoniter.Unmarshal(jbIter, &afterIter)
if err != nil {
t.Errorf("failed to unmarshal with jsoniter: %v", err)
}
if fingerprint(afterStd) != fingerprint(afterIter) {
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
dump(afterStd), dump(afterIter), indent(jbIter, " "))
}
}
}
const indentStr = "> "
func fingerprint(obj interface{}) string {
c := spew.ConfigState{
SortKeys: true,
SpewKeys: true,
}
return c.Sprintf("%v", obj)
}
func dump(obj interface{}) string {
cfg := spew.ConfigState{
Indent: indentStr,
}
return cfg.Sdump(obj)
}
func indent(src []byte, prefix string) string {
var buf bytes.Buffer
json.Indent(&buf, src, prefix, indentStr)
return buf.String()
}
func BenchmarkStandardMarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var obj T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&obj)
for i := 0; i < t.N; i++ {
jb, err := json.Marshal(obj)
if err != nil {
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
}
_ = jb
}
}
func BenchmarkStandardUnmarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var before T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&before)
jb, err := json.Marshal(before)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
for i := 0; i < t.N; i++ {
var after T
err = json.Unmarshal(jb, &after)
if err != nil {
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
}
}
}
func BenchmarkJSONIterMarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var obj T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&obj)
for i := 0; i < t.N; i++ {
jb, err := jsoniter.Marshal(obj)
if err != nil {
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
}
_ = jb
}
}
func BenchmarkJSONIterUnmarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var before T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&before)
jb, err := json.Marshal(before)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
for i := 0; i < t.N; i++ {
var after T
err = jsoniter.Unmarshal(jb, &after)
if err != nil {
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
}
}
}

View File

@ -0,0 +1,3 @@
package test
type T float64

View File

@ -0,0 +1,144 @@
package test
import (
"bytes"
"encoding/json"
"testing"
"github.com/davecgh/go-spew/spew"
fuzz "github.com/google/gofuzz"
jsoniter "github.com/json-iterator/go"
)
func Test_Roundtrip(t *testing.T) {
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
for i := 0; i < 1000; i++ {
var before T
fz.Fuzz(&before)
jbStd, err := json.Marshal(before)
if err != nil {
t.Errorf("failed to marshal with stdlib: %v", err)
}
jbIter, err := jsoniter.Marshal(before)
if err != nil {
t.Errorf("failed to marshal with jsoniter: %v", err)
}
if string(jbStd) != string(jbIter) {
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
indent(jbStd, " "), indent(jbIter, " "), dump(before))
}
var afterStd T
err = json.Unmarshal(jbIter, &afterStd)
if err != nil {
t.Errorf("failed to unmarshal with stdlib: %v", err)
}
var afterIter T
err = jsoniter.Unmarshal(jbIter, &afterIter)
if err != nil {
t.Errorf("failed to unmarshal with jsoniter: %v", err)
}
if fingerprint(afterStd) != fingerprint(afterIter) {
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
dump(afterStd), dump(afterIter), indent(jbIter, " "))
}
}
}
const indentStr = "> "
func fingerprint(obj interface{}) string {
c := spew.ConfigState{
SortKeys: true,
SpewKeys: true,
}
return c.Sprintf("%v", obj)
}
func dump(obj interface{}) string {
cfg := spew.ConfigState{
Indent: indentStr,
}
return cfg.Sdump(obj)
}
func indent(src []byte, prefix string) string {
var buf bytes.Buffer
json.Indent(&buf, src, prefix, indentStr)
return buf.String()
}
func BenchmarkStandardMarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var obj T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&obj)
for i := 0; i < t.N; i++ {
jb, err := json.Marshal(obj)
if err != nil {
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
}
_ = jb
}
}
func BenchmarkStandardUnmarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var before T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&before)
jb, err := json.Marshal(before)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
for i := 0; i < t.N; i++ {
var after T
err = json.Unmarshal(jb, &after)
if err != nil {
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
}
}
}
func BenchmarkJSONIterMarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var obj T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&obj)
for i := 0; i < t.N; i++ {
jb, err := jsoniter.Marshal(obj)
if err != nil {
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
}
_ = jb
}
}
func BenchmarkJSONIterUnmarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var before T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&before)
jb, err := json.Marshal(before)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
for i := 0; i < t.N; i++ {
var after T
err = jsoniter.Unmarshal(jb, &after)
if err != nil {
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
}
}
}

View File

@ -0,0 +1,3 @@
package test
type T float64

View File

@ -0,0 +1,144 @@
package test
import (
"bytes"
"encoding/json"
"testing"
"github.com/davecgh/go-spew/spew"
fuzz "github.com/google/gofuzz"
jsoniter "github.com/json-iterator/go"
)
func Test_Roundtrip(t *testing.T) {
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
for i := 0; i < 1000; i++ {
var before T
fz.Fuzz(&before)
jbStd, err := json.Marshal(before)
if err != nil {
t.Errorf("failed to marshal with stdlib: %v", err)
}
jbIter, err := jsoniter.Marshal(before)
if err != nil {
t.Errorf("failed to marshal with jsoniter: %v", err)
}
if string(jbStd) != string(jbIter) {
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
indent(jbStd, " "), indent(jbIter, " "), dump(before))
}
var afterStd T
err = json.Unmarshal(jbIter, &afterStd)
if err != nil {
t.Errorf("failed to unmarshal with stdlib: %v", err)
}
var afterIter T
err = jsoniter.Unmarshal(jbIter, &afterIter)
if err != nil {
t.Errorf("failed to unmarshal with jsoniter: %v", err)
}
if fingerprint(afterStd) != fingerprint(afterIter) {
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
dump(afterStd), dump(afterIter), indent(jbIter, " "))
}
}
}
const indentStr = "> "
func fingerprint(obj interface{}) string {
c := spew.ConfigState{
SortKeys: true,
SpewKeys: true,
}
return c.Sprintf("%v", obj)
}
func dump(obj interface{}) string {
cfg := spew.ConfigState{
Indent: indentStr,
}
return cfg.Sdump(obj)
}
func indent(src []byte, prefix string) string {
var buf bytes.Buffer
json.Indent(&buf, src, prefix, indentStr)
return buf.String()
}
func BenchmarkStandardMarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var obj T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&obj)
for i := 0; i < t.N; i++ {
jb, err := json.Marshal(obj)
if err != nil {
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
}
_ = jb
}
}
func BenchmarkStandardUnmarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var before T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&before)
jb, err := json.Marshal(before)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
for i := 0; i < t.N; i++ {
var after T
err = json.Unmarshal(jb, &after)
if err != nil {
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
}
}
}
func BenchmarkJSONIterMarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var obj T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&obj)
for i := 0; i < t.N; i++ {
jb, err := jsoniter.Marshal(obj)
if err != nil {
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
}
_ = jb
}
}
func BenchmarkJSONIterUnmarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var before T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&before)
jb, err := json.Marshal(before)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
for i := 0; i < t.N; i++ {
var after T
err = jsoniter.Unmarshal(jb, &after)
if err != nil {
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
}
}
}

View File

@ -0,0 +1,3 @@
package test
type T int16

View File

@ -0,0 +1,144 @@
package test
import (
"bytes"
"encoding/json"
"testing"
"github.com/davecgh/go-spew/spew"
fuzz "github.com/google/gofuzz"
jsoniter "github.com/json-iterator/go"
)
func Test_Roundtrip(t *testing.T) {
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
for i := 0; i < 1000; i++ {
var before T
fz.Fuzz(&before)
jbStd, err := json.Marshal(before)
if err != nil {
t.Errorf("failed to marshal with stdlib: %v", err)
}
jbIter, err := jsoniter.Marshal(before)
if err != nil {
t.Errorf("failed to marshal with jsoniter: %v", err)
}
if string(jbStd) != string(jbIter) {
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
indent(jbStd, " "), indent(jbIter, " "), dump(before))
}
var afterStd T
err = json.Unmarshal(jbIter, &afterStd)
if err != nil {
t.Errorf("failed to unmarshal with stdlib: %v", err)
}
var afterIter T
err = jsoniter.Unmarshal(jbIter, &afterIter)
if err != nil {
t.Errorf("failed to unmarshal with jsoniter: %v", err)
}
if fingerprint(afterStd) != fingerprint(afterIter) {
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
dump(afterStd), dump(afterIter), indent(jbIter, " "))
}
}
}
const indentStr = "> "
func fingerprint(obj interface{}) string {
c := spew.ConfigState{
SortKeys: true,
SpewKeys: true,
}
return c.Sprintf("%v", obj)
}
func dump(obj interface{}) string {
cfg := spew.ConfigState{
Indent: indentStr,
}
return cfg.Sdump(obj)
}
func indent(src []byte, prefix string) string {
var buf bytes.Buffer
json.Indent(&buf, src, prefix, indentStr)
return buf.String()
}
func BenchmarkStandardMarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var obj T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&obj)
for i := 0; i < t.N; i++ {
jb, err := json.Marshal(obj)
if err != nil {
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
}
_ = jb
}
}
func BenchmarkStandardUnmarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var before T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&before)
jb, err := json.Marshal(before)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
for i := 0; i < t.N; i++ {
var after T
err = json.Unmarshal(jb, &after)
if err != nil {
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
}
}
}
func BenchmarkJSONIterMarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var obj T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&obj)
for i := 0; i < t.N; i++ {
jb, err := jsoniter.Marshal(obj)
if err != nil {
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
}
_ = jb
}
}
func BenchmarkJSONIterUnmarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var before T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&before)
jb, err := json.Marshal(before)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
for i := 0; i < t.N; i++ {
var after T
err = jsoniter.Unmarshal(jb, &after)
if err != nil {
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
}
}
}

View File

@ -0,0 +1,3 @@
package test
type T int16

View File

@ -0,0 +1,144 @@
package test
import (
"bytes"
"encoding/json"
"testing"
"github.com/davecgh/go-spew/spew"
fuzz "github.com/google/gofuzz"
jsoniter "github.com/json-iterator/go"
)
func Test_Roundtrip(t *testing.T) {
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
for i := 0; i < 1000; i++ {
var before T
fz.Fuzz(&before)
jbStd, err := json.Marshal(before)
if err != nil {
t.Errorf("failed to marshal with stdlib: %v", err)
}
jbIter, err := jsoniter.Marshal(before)
if err != nil {
t.Errorf("failed to marshal with jsoniter: %v", err)
}
if string(jbStd) != string(jbIter) {
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
indent(jbStd, " "), indent(jbIter, " "), dump(before))
}
var afterStd T
err = json.Unmarshal(jbIter, &afterStd)
if err != nil {
t.Errorf("failed to unmarshal with stdlib: %v", err)
}
var afterIter T
err = jsoniter.Unmarshal(jbIter, &afterIter)
if err != nil {
t.Errorf("failed to unmarshal with jsoniter: %v", err)
}
if fingerprint(afterStd) != fingerprint(afterIter) {
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
dump(afterStd), dump(afterIter), indent(jbIter, " "))
}
}
}
const indentStr = "> "
func fingerprint(obj interface{}) string {
c := spew.ConfigState{
SortKeys: true,
SpewKeys: true,
}
return c.Sprintf("%v", obj)
}
func dump(obj interface{}) string {
cfg := spew.ConfigState{
Indent: indentStr,
}
return cfg.Sdump(obj)
}
func indent(src []byte, prefix string) string {
var buf bytes.Buffer
json.Indent(&buf, src, prefix, indentStr)
return buf.String()
}
func BenchmarkStandardMarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var obj T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&obj)
for i := 0; i < t.N; i++ {
jb, err := json.Marshal(obj)
if err != nil {
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
}
_ = jb
}
}
func BenchmarkStandardUnmarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var before T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&before)
jb, err := json.Marshal(before)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
for i := 0; i < t.N; i++ {
var after T
err = json.Unmarshal(jb, &after)
if err != nil {
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
}
}
}
func BenchmarkJSONIterMarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var obj T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&obj)
for i := 0; i < t.N; i++ {
jb, err := jsoniter.Marshal(obj)
if err != nil {
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
}
_ = jb
}
}
func BenchmarkJSONIterUnmarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var before T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&before)
jb, err := json.Marshal(before)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
for i := 0; i < t.N; i++ {
var after T
err = jsoniter.Unmarshal(jb, &after)
if err != nil {
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
}
}
}

View File

@ -0,0 +1,3 @@
package test
type T int32

View File

@ -0,0 +1,144 @@
package test
import (
"bytes"
"encoding/json"
"testing"
"github.com/davecgh/go-spew/spew"
fuzz "github.com/google/gofuzz"
jsoniter "github.com/json-iterator/go"
)
func Test_Roundtrip(t *testing.T) {
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
for i := 0; i < 1000; i++ {
var before T
fz.Fuzz(&before)
jbStd, err := json.Marshal(before)
if err != nil {
t.Errorf("failed to marshal with stdlib: %v", err)
}
jbIter, err := jsoniter.Marshal(before)
if err != nil {
t.Errorf("failed to marshal with jsoniter: %v", err)
}
if string(jbStd) != string(jbIter) {
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
indent(jbStd, " "), indent(jbIter, " "), dump(before))
}
var afterStd T
err = json.Unmarshal(jbIter, &afterStd)
if err != nil {
t.Errorf("failed to unmarshal with stdlib: %v", err)
}
var afterIter T
err = jsoniter.Unmarshal(jbIter, &afterIter)
if err != nil {
t.Errorf("failed to unmarshal with jsoniter: %v", err)
}
if fingerprint(afterStd) != fingerprint(afterIter) {
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
dump(afterStd), dump(afterIter), indent(jbIter, " "))
}
}
}
const indentStr = "> "
func fingerprint(obj interface{}) string {
c := spew.ConfigState{
SortKeys: true,
SpewKeys: true,
}
return c.Sprintf("%v", obj)
}
func dump(obj interface{}) string {
cfg := spew.ConfigState{
Indent: indentStr,
}
return cfg.Sdump(obj)
}
func indent(src []byte, prefix string) string {
var buf bytes.Buffer
json.Indent(&buf, src, prefix, indentStr)
return buf.String()
}
func BenchmarkStandardMarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var obj T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&obj)
for i := 0; i < t.N; i++ {
jb, err := json.Marshal(obj)
if err != nil {
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
}
_ = jb
}
}
func BenchmarkStandardUnmarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var before T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&before)
jb, err := json.Marshal(before)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
for i := 0; i < t.N; i++ {
var after T
err = json.Unmarshal(jb, &after)
if err != nil {
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
}
}
}
func BenchmarkJSONIterMarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var obj T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&obj)
for i := 0; i < t.N; i++ {
jb, err := jsoniter.Marshal(obj)
if err != nil {
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
}
_ = jb
}
}
func BenchmarkJSONIterUnmarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var before T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&before)
jb, err := json.Marshal(before)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
for i := 0; i < t.N; i++ {
var after T
err = jsoniter.Unmarshal(jb, &after)
if err != nil {
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
}
}
}

View File

@ -0,0 +1,3 @@
package test
type T int32

View File

@ -0,0 +1,144 @@
package test
import (
"bytes"
"encoding/json"
"testing"
"github.com/davecgh/go-spew/spew"
fuzz "github.com/google/gofuzz"
jsoniter "github.com/json-iterator/go"
)
func Test_Roundtrip(t *testing.T) {
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
for i := 0; i < 1000; i++ {
var before T
fz.Fuzz(&before)
jbStd, err := json.Marshal(before)
if err != nil {
t.Errorf("failed to marshal with stdlib: %v", err)
}
jbIter, err := jsoniter.Marshal(before)
if err != nil {
t.Errorf("failed to marshal with jsoniter: %v", err)
}
if string(jbStd) != string(jbIter) {
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
indent(jbStd, " "), indent(jbIter, " "), dump(before))
}
var afterStd T
err = json.Unmarshal(jbIter, &afterStd)
if err != nil {
t.Errorf("failed to unmarshal with stdlib: %v", err)
}
var afterIter T
err = jsoniter.Unmarshal(jbIter, &afterIter)
if err != nil {
t.Errorf("failed to unmarshal with jsoniter: %v", err)
}
if fingerprint(afterStd) != fingerprint(afterIter) {
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
dump(afterStd), dump(afterIter), indent(jbIter, " "))
}
}
}
const indentStr = "> "
func fingerprint(obj interface{}) string {
c := spew.ConfigState{
SortKeys: true,
SpewKeys: true,
}
return c.Sprintf("%v", obj)
}
func dump(obj interface{}) string {
cfg := spew.ConfigState{
Indent: indentStr,
}
return cfg.Sdump(obj)
}
func indent(src []byte, prefix string) string {
var buf bytes.Buffer
json.Indent(&buf, src, prefix, indentStr)
return buf.String()
}
func BenchmarkStandardMarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var obj T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&obj)
for i := 0; i < t.N; i++ {
jb, err := json.Marshal(obj)
if err != nil {
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
}
_ = jb
}
}
func BenchmarkStandardUnmarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var before T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&before)
jb, err := json.Marshal(before)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
for i := 0; i < t.N; i++ {
var after T
err = json.Unmarshal(jb, &after)
if err != nil {
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
}
}
}
func BenchmarkJSONIterMarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var obj T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&obj)
for i := 0; i < t.N; i++ {
jb, err := jsoniter.Marshal(obj)
if err != nil {
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
}
_ = jb
}
}
func BenchmarkJSONIterUnmarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var before T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&before)
jb, err := json.Marshal(before)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
for i := 0; i < t.N; i++ {
var after T
err = jsoniter.Unmarshal(jb, &after)
if err != nil {
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
}
}
}

View File

@ -0,0 +1,3 @@
package test
type T int8

View File

@ -0,0 +1,144 @@
package test
import (
"bytes"
"encoding/json"
"testing"
"github.com/davecgh/go-spew/spew"
fuzz "github.com/google/gofuzz"
jsoniter "github.com/json-iterator/go"
)
func Test_Roundtrip(t *testing.T) {
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
for i := 0; i < 1000; i++ {
var before T
fz.Fuzz(&before)
jbStd, err := json.Marshal(before)
if err != nil {
t.Errorf("failed to marshal with stdlib: %v", err)
}
jbIter, err := jsoniter.Marshal(before)
if err != nil {
t.Errorf("failed to marshal with jsoniter: %v", err)
}
if string(jbStd) != string(jbIter) {
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
indent(jbStd, " "), indent(jbIter, " "), dump(before))
}
var afterStd T
err = json.Unmarshal(jbIter, &afterStd)
if err != nil {
t.Errorf("failed to unmarshal with stdlib: %v", err)
}
var afterIter T
err = jsoniter.Unmarshal(jbIter, &afterIter)
if err != nil {
t.Errorf("failed to unmarshal with jsoniter: %v", err)
}
if fingerprint(afterStd) != fingerprint(afterIter) {
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
dump(afterStd), dump(afterIter), indent(jbIter, " "))
}
}
}
const indentStr = "> "
func fingerprint(obj interface{}) string {
c := spew.ConfigState{
SortKeys: true,
SpewKeys: true,
}
return c.Sprintf("%v", obj)
}
func dump(obj interface{}) string {
cfg := spew.ConfigState{
Indent: indentStr,
}
return cfg.Sdump(obj)
}
func indent(src []byte, prefix string) string {
var buf bytes.Buffer
json.Indent(&buf, src, prefix, indentStr)
return buf.String()
}
func BenchmarkStandardMarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var obj T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&obj)
for i := 0; i < t.N; i++ {
jb, err := json.Marshal(obj)
if err != nil {
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
}
_ = jb
}
}
func BenchmarkStandardUnmarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var before T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&before)
jb, err := json.Marshal(before)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
for i := 0; i < t.N; i++ {
var after T
err = json.Unmarshal(jb, &after)
if err != nil {
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
}
}
}
func BenchmarkJSONIterMarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var obj T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&obj)
for i := 0; i < t.N; i++ {
jb, err := jsoniter.Marshal(obj)
if err != nil {
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
}
_ = jb
}
}
func BenchmarkJSONIterUnmarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var before T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&before)
jb, err := json.Marshal(before)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
for i := 0; i < t.N; i++ {
var after T
err = jsoniter.Unmarshal(jb, &after)
if err != nil {
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
}
}
}

View File

@ -0,0 +1,3 @@
package test
type T int8

View File

@ -0,0 +1,144 @@
package test
import (
"bytes"
"encoding/json"
"testing"
"github.com/davecgh/go-spew/spew"
fuzz "github.com/google/gofuzz"
jsoniter "github.com/json-iterator/go"
)
func Test_Roundtrip(t *testing.T) {
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
for i := 0; i < 1000; i++ {
var before T
fz.Fuzz(&before)
jbStd, err := json.Marshal(before)
if err != nil {
t.Errorf("failed to marshal with stdlib: %v", err)
}
jbIter, err := jsoniter.Marshal(before)
if err != nil {
t.Errorf("failed to marshal with jsoniter: %v", err)
}
if string(jbStd) != string(jbIter) {
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
indent(jbStd, " "), indent(jbIter, " "), dump(before))
}
var afterStd T
err = json.Unmarshal(jbIter, &afterStd)
if err != nil {
t.Errorf("failed to unmarshal with stdlib: %v", err)
}
var afterIter T
err = jsoniter.Unmarshal(jbIter, &afterIter)
if err != nil {
t.Errorf("failed to unmarshal with jsoniter: %v", err)
}
if fingerprint(afterStd) != fingerprint(afterIter) {
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
dump(afterStd), dump(afterIter), indent(jbIter, " "))
}
}
}
const indentStr = "> "
func fingerprint(obj interface{}) string {
c := spew.ConfigState{
SortKeys: true,
SpewKeys: true,
}
return c.Sprintf("%v", obj)
}
func dump(obj interface{}) string {
cfg := spew.ConfigState{
Indent: indentStr,
}
return cfg.Sdump(obj)
}
func indent(src []byte, prefix string) string {
var buf bytes.Buffer
json.Indent(&buf, src, prefix, indentStr)
return buf.String()
}
func BenchmarkStandardMarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var obj T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&obj)
for i := 0; i < t.N; i++ {
jb, err := json.Marshal(obj)
if err != nil {
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
}
_ = jb
}
}
func BenchmarkStandardUnmarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var before T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&before)
jb, err := json.Marshal(before)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
for i := 0; i < t.N; i++ {
var after T
err = json.Unmarshal(jb, &after)
if err != nil {
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
}
}
}
func BenchmarkJSONIterMarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var obj T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&obj)
for i := 0; i < t.N; i++ {
jb, err := jsoniter.Marshal(obj)
if err != nil {
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
}
_ = jb
}
}
func BenchmarkJSONIterUnmarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var before T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&before)
jb, err := json.Marshal(before)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
for i := 0; i < t.N; i++ {
var after T
err = jsoniter.Unmarshal(jb, &after)
if err != nil {
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
}
}
}

View File

@ -0,0 +1,3 @@
package test
type T string

View File

@ -0,0 +1,144 @@
package test
import (
"bytes"
"encoding/json"
"testing"
"github.com/davecgh/go-spew/spew"
fuzz "github.com/google/gofuzz"
jsoniter "github.com/json-iterator/go"
)
func Test_Roundtrip(t *testing.T) {
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
for i := 0; i < 1000; i++ {
var before T
fz.Fuzz(&before)
jbStd, err := json.Marshal(before)
if err != nil {
t.Errorf("failed to marshal with stdlib: %v", err)
}
jbIter, err := jsoniter.Marshal(before)
if err != nil {
t.Errorf("failed to marshal with jsoniter: %v", err)
}
if string(jbStd) != string(jbIter) {
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
indent(jbStd, " "), indent(jbIter, " "), dump(before))
}
var afterStd T
err = json.Unmarshal(jbIter, &afterStd)
if err != nil {
t.Errorf("failed to unmarshal with stdlib: %v", err)
}
var afterIter T
err = jsoniter.Unmarshal(jbIter, &afterIter)
if err != nil {
t.Errorf("failed to unmarshal with jsoniter: %v", err)
}
if fingerprint(afterStd) != fingerprint(afterIter) {
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
dump(afterStd), dump(afterIter), indent(jbIter, " "))
}
}
}
const indentStr = "> "
func fingerprint(obj interface{}) string {
c := spew.ConfigState{
SortKeys: true,
SpewKeys: true,
}
return c.Sprintf("%v", obj)
}
func dump(obj interface{}) string {
cfg := spew.ConfigState{
Indent: indentStr,
}
return cfg.Sdump(obj)
}
func indent(src []byte, prefix string) string {
var buf bytes.Buffer
json.Indent(&buf, src, prefix, indentStr)
return buf.String()
}
func BenchmarkStandardMarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var obj T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&obj)
for i := 0; i < t.N; i++ {
jb, err := json.Marshal(obj)
if err != nil {
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
}
_ = jb
}
}
func BenchmarkStandardUnmarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var before T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&before)
jb, err := json.Marshal(before)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
for i := 0; i < t.N; i++ {
var after T
err = json.Unmarshal(jb, &after)
if err != nil {
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
}
}
}
func BenchmarkJSONIterMarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var obj T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&obj)
for i := 0; i < t.N; i++ {
jb, err := jsoniter.Marshal(obj)
if err != nil {
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
}
_ = jb
}
}
func BenchmarkJSONIterUnmarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var before T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&before)
jb, err := json.Marshal(before)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
for i := 0; i < t.N; i++ {
var after T
err = jsoniter.Unmarshal(jb, &after)
if err != nil {
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
}
}
}

View File

@ -0,0 +1,3 @@
package test
type T string

View File

@ -0,0 +1,144 @@
package test
import (
"bytes"
"encoding/json"
"testing"
"github.com/davecgh/go-spew/spew"
fuzz "github.com/google/gofuzz"
jsoniter "github.com/json-iterator/go"
)
func Test_Roundtrip(t *testing.T) {
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
for i := 0; i < 1000; i++ {
var before T
fz.Fuzz(&before)
jbStd, err := json.Marshal(before)
if err != nil {
t.Errorf("failed to marshal with stdlib: %v", err)
}
jbIter, err := jsoniter.Marshal(before)
if err != nil {
t.Errorf("failed to marshal with jsoniter: %v", err)
}
if string(jbStd) != string(jbIter) {
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
indent(jbStd, " "), indent(jbIter, " "), dump(before))
}
var afterStd T
err = json.Unmarshal(jbIter, &afterStd)
if err != nil {
t.Errorf("failed to unmarshal with stdlib: %v", err)
}
var afterIter T
err = jsoniter.Unmarshal(jbIter, &afterIter)
if err != nil {
t.Errorf("failed to unmarshal with jsoniter: %v", err)
}
if fingerprint(afterStd) != fingerprint(afterIter) {
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
dump(afterStd), dump(afterIter), indent(jbIter, " "))
}
}
}
const indentStr = "> "
func fingerprint(obj interface{}) string {
c := spew.ConfigState{
SortKeys: true,
SpewKeys: true,
}
return c.Sprintf("%v", obj)
}
func dump(obj interface{}) string {
cfg := spew.ConfigState{
Indent: indentStr,
}
return cfg.Sdump(obj)
}
func indent(src []byte, prefix string) string {
var buf bytes.Buffer
json.Indent(&buf, src, prefix, indentStr)
return buf.String()
}
func BenchmarkStandardMarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var obj T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&obj)
for i := 0; i < t.N; i++ {
jb, err := json.Marshal(obj)
if err != nil {
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
}
_ = jb
}
}
func BenchmarkStandardUnmarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var before T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&before)
jb, err := json.Marshal(before)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
for i := 0; i < t.N; i++ {
var after T
err = json.Unmarshal(jb, &after)
if err != nil {
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
}
}
}
func BenchmarkJSONIterMarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var obj T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&obj)
for i := 0; i < t.N; i++ {
jb, err := jsoniter.Marshal(obj)
if err != nil {
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
}
_ = jb
}
}
func BenchmarkJSONIterUnmarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var before T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&before)
jb, err := json.Marshal(before)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
for i := 0; i < t.N; i++ {
var after T
err = jsoniter.Unmarshal(jb, &after)
if err != nil {
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
}
}
}

View File

@ -0,0 +1,3 @@
package test
type T uint16

View File

@ -0,0 +1,144 @@
package test
import (
"bytes"
"encoding/json"
"testing"
"github.com/davecgh/go-spew/spew"
fuzz "github.com/google/gofuzz"
jsoniter "github.com/json-iterator/go"
)
func Test_Roundtrip(t *testing.T) {
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
for i := 0; i < 1000; i++ {
var before T
fz.Fuzz(&before)
jbStd, err := json.Marshal(before)
if err != nil {
t.Errorf("failed to marshal with stdlib: %v", err)
}
jbIter, err := jsoniter.Marshal(before)
if err != nil {
t.Errorf("failed to marshal with jsoniter: %v", err)
}
if string(jbStd) != string(jbIter) {
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
indent(jbStd, " "), indent(jbIter, " "), dump(before))
}
var afterStd T
err = json.Unmarshal(jbIter, &afterStd)
if err != nil {
t.Errorf("failed to unmarshal with stdlib: %v", err)
}
var afterIter T
err = jsoniter.Unmarshal(jbIter, &afterIter)
if err != nil {
t.Errorf("failed to unmarshal with jsoniter: %v", err)
}
if fingerprint(afterStd) != fingerprint(afterIter) {
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
dump(afterStd), dump(afterIter), indent(jbIter, " "))
}
}
}
const indentStr = "> "
func fingerprint(obj interface{}) string {
c := spew.ConfigState{
SortKeys: true,
SpewKeys: true,
}
return c.Sprintf("%v", obj)
}
func dump(obj interface{}) string {
cfg := spew.ConfigState{
Indent: indentStr,
}
return cfg.Sdump(obj)
}
func indent(src []byte, prefix string) string {
var buf bytes.Buffer
json.Indent(&buf, src, prefix, indentStr)
return buf.String()
}
func BenchmarkStandardMarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var obj T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&obj)
for i := 0; i < t.N; i++ {
jb, err := json.Marshal(obj)
if err != nil {
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
}
_ = jb
}
}
func BenchmarkStandardUnmarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var before T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&before)
jb, err := json.Marshal(before)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
for i := 0; i < t.N; i++ {
var after T
err = json.Unmarshal(jb, &after)
if err != nil {
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
}
}
}
func BenchmarkJSONIterMarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var obj T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&obj)
for i := 0; i < t.N; i++ {
jb, err := jsoniter.Marshal(obj)
if err != nil {
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
}
_ = jb
}
}
func BenchmarkJSONIterUnmarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var before T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&before)
jb, err := json.Marshal(before)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
for i := 0; i < t.N; i++ {
var after T
err = jsoniter.Unmarshal(jb, &after)
if err != nil {
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
}
}
}

View File

@ -0,0 +1,3 @@
package test
type T uint16

View File

@ -0,0 +1,144 @@
package test
import (
"bytes"
"encoding/json"
"testing"
"github.com/davecgh/go-spew/spew"
fuzz "github.com/google/gofuzz"
jsoniter "github.com/json-iterator/go"
)
func Test_Roundtrip(t *testing.T) {
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
for i := 0; i < 1000; i++ {
var before T
fz.Fuzz(&before)
jbStd, err := json.Marshal(before)
if err != nil {
t.Errorf("failed to marshal with stdlib: %v", err)
}
jbIter, err := jsoniter.Marshal(before)
if err != nil {
t.Errorf("failed to marshal with jsoniter: %v", err)
}
if string(jbStd) != string(jbIter) {
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
indent(jbStd, " "), indent(jbIter, " "), dump(before))
}
var afterStd T
err = json.Unmarshal(jbIter, &afterStd)
if err != nil {
t.Errorf("failed to unmarshal with stdlib: %v", err)
}
var afterIter T
err = jsoniter.Unmarshal(jbIter, &afterIter)
if err != nil {
t.Errorf("failed to unmarshal with jsoniter: %v", err)
}
if fingerprint(afterStd) != fingerprint(afterIter) {
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
dump(afterStd), dump(afterIter), indent(jbIter, " "))
}
}
}
const indentStr = "> "
func fingerprint(obj interface{}) string {
c := spew.ConfigState{
SortKeys: true,
SpewKeys: true,
}
return c.Sprintf("%v", obj)
}
func dump(obj interface{}) string {
cfg := spew.ConfigState{
Indent: indentStr,
}
return cfg.Sdump(obj)
}
func indent(src []byte, prefix string) string {
var buf bytes.Buffer
json.Indent(&buf, src, prefix, indentStr)
return buf.String()
}
func BenchmarkStandardMarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var obj T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&obj)
for i := 0; i < t.N; i++ {
jb, err := json.Marshal(obj)
if err != nil {
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
}
_ = jb
}
}
func BenchmarkStandardUnmarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var before T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&before)
jb, err := json.Marshal(before)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
for i := 0; i < t.N; i++ {
var after T
err = json.Unmarshal(jb, &after)
if err != nil {
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
}
}
}
func BenchmarkJSONIterMarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var obj T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&obj)
for i := 0; i < t.N; i++ {
jb, err := jsoniter.Marshal(obj)
if err != nil {
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
}
_ = jb
}
}
func BenchmarkJSONIterUnmarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var before T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&before)
jb, err := json.Marshal(before)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
for i := 0; i < t.N; i++ {
var after T
err = jsoniter.Unmarshal(jb, &after)
if err != nil {
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
}
}
}

View File

@ -0,0 +1,3 @@
package test
type T uint32

View File

@ -0,0 +1,144 @@
package test
import (
"bytes"
"encoding/json"
"testing"
"github.com/davecgh/go-spew/spew"
fuzz "github.com/google/gofuzz"
jsoniter "github.com/json-iterator/go"
)
func Test_Roundtrip(t *testing.T) {
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
for i := 0; i < 1000; i++ {
var before T
fz.Fuzz(&before)
jbStd, err := json.Marshal(before)
if err != nil {
t.Errorf("failed to marshal with stdlib: %v", err)
}
jbIter, err := jsoniter.Marshal(before)
if err != nil {
t.Errorf("failed to marshal with jsoniter: %v", err)
}
if string(jbStd) != string(jbIter) {
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
indent(jbStd, " "), indent(jbIter, " "), dump(before))
}
var afterStd T
err = json.Unmarshal(jbIter, &afterStd)
if err != nil {
t.Errorf("failed to unmarshal with stdlib: %v", err)
}
var afterIter T
err = jsoniter.Unmarshal(jbIter, &afterIter)
if err != nil {
t.Errorf("failed to unmarshal with jsoniter: %v", err)
}
if fingerprint(afterStd) != fingerprint(afterIter) {
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
dump(afterStd), dump(afterIter), indent(jbIter, " "))
}
}
}
const indentStr = "> "
func fingerprint(obj interface{}) string {
c := spew.ConfigState{
SortKeys: true,
SpewKeys: true,
}
return c.Sprintf("%v", obj)
}
func dump(obj interface{}) string {
cfg := spew.ConfigState{
Indent: indentStr,
}
return cfg.Sdump(obj)
}
func indent(src []byte, prefix string) string {
var buf bytes.Buffer
json.Indent(&buf, src, prefix, indentStr)
return buf.String()
}
func BenchmarkStandardMarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var obj T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&obj)
for i := 0; i < t.N; i++ {
jb, err := json.Marshal(obj)
if err != nil {
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
}
_ = jb
}
}
func BenchmarkStandardUnmarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var before T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&before)
jb, err := json.Marshal(before)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
for i := 0; i < t.N; i++ {
var after T
err = json.Unmarshal(jb, &after)
if err != nil {
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
}
}
}
func BenchmarkJSONIterMarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var obj T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&obj)
for i := 0; i < t.N; i++ {
jb, err := jsoniter.Marshal(obj)
if err != nil {
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
}
_ = jb
}
}
func BenchmarkJSONIterUnmarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var before T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&before)
jb, err := json.Marshal(before)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
for i := 0; i < t.N; i++ {
var after T
err = jsoniter.Unmarshal(jb, &after)
if err != nil {
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
}
}
}

View File

@ -0,0 +1,3 @@
package test
type T uint32

View File

@ -0,0 +1,144 @@
package test
import (
"bytes"
"encoding/json"
"testing"
"github.com/davecgh/go-spew/spew"
fuzz "github.com/google/gofuzz"
jsoniter "github.com/json-iterator/go"
)
func Test_Roundtrip(t *testing.T) {
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
for i := 0; i < 1000; i++ {
var before T
fz.Fuzz(&before)
jbStd, err := json.Marshal(before)
if err != nil {
t.Errorf("failed to marshal with stdlib: %v", err)
}
jbIter, err := jsoniter.Marshal(before)
if err != nil {
t.Errorf("failed to marshal with jsoniter: %v", err)
}
if string(jbStd) != string(jbIter) {
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
indent(jbStd, " "), indent(jbIter, " "), dump(before))
}
var afterStd T
err = json.Unmarshal(jbIter, &afterStd)
if err != nil {
t.Errorf("failed to unmarshal with stdlib: %v", err)
}
var afterIter T
err = jsoniter.Unmarshal(jbIter, &afterIter)
if err != nil {
t.Errorf("failed to unmarshal with jsoniter: %v", err)
}
if fingerprint(afterStd) != fingerprint(afterIter) {
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
dump(afterStd), dump(afterIter), indent(jbIter, " "))
}
}
}
const indentStr = "> "
func fingerprint(obj interface{}) string {
c := spew.ConfigState{
SortKeys: true,
SpewKeys: true,
}
return c.Sprintf("%v", obj)
}
func dump(obj interface{}) string {
cfg := spew.ConfigState{
Indent: indentStr,
}
return cfg.Sdump(obj)
}
func indent(src []byte, prefix string) string {
var buf bytes.Buffer
json.Indent(&buf, src, prefix, indentStr)
return buf.String()
}
func BenchmarkStandardMarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var obj T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&obj)
for i := 0; i < t.N; i++ {
jb, err := json.Marshal(obj)
if err != nil {
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
}
_ = jb
}
}
func BenchmarkStandardUnmarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var before T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&before)
jb, err := json.Marshal(before)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
for i := 0; i < t.N; i++ {
var after T
err = json.Unmarshal(jb, &after)
if err != nil {
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
}
}
}
func BenchmarkJSONIterMarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var obj T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&obj)
for i := 0; i < t.N; i++ {
jb, err := jsoniter.Marshal(obj)
if err != nil {
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
}
_ = jb
}
}
func BenchmarkJSONIterUnmarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var before T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&before)
jb, err := json.Marshal(before)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
for i := 0; i < t.N; i++ {
var after T
err = jsoniter.Unmarshal(jb, &after)
if err != nil {
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
}
}
}

View File

@ -0,0 +1,3 @@
package test
type T uint8

View File

@ -0,0 +1,144 @@
package test
import (
"bytes"
"encoding/json"
"testing"
"github.com/davecgh/go-spew/spew"
fuzz "github.com/google/gofuzz"
jsoniter "github.com/json-iterator/go"
)
func Test_Roundtrip(t *testing.T) {
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
for i := 0; i < 1000; i++ {
var before T
fz.Fuzz(&before)
jbStd, err := json.Marshal(before)
if err != nil {
t.Errorf("failed to marshal with stdlib: %v", err)
}
jbIter, err := jsoniter.Marshal(before)
if err != nil {
t.Errorf("failed to marshal with jsoniter: %v", err)
}
if string(jbStd) != string(jbIter) {
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
indent(jbStd, " "), indent(jbIter, " "), dump(before))
}
var afterStd T
err = json.Unmarshal(jbIter, &afterStd)
if err != nil {
t.Errorf("failed to unmarshal with stdlib: %v", err)
}
var afterIter T
err = jsoniter.Unmarshal(jbIter, &afterIter)
if err != nil {
t.Errorf("failed to unmarshal with jsoniter: %v", err)
}
if fingerprint(afterStd) != fingerprint(afterIter) {
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
dump(afterStd), dump(afterIter), indent(jbIter, " "))
}
}
}
const indentStr = "> "
func fingerprint(obj interface{}) string {
c := spew.ConfigState{
SortKeys: true,
SpewKeys: true,
}
return c.Sprintf("%v", obj)
}
func dump(obj interface{}) string {
cfg := spew.ConfigState{
Indent: indentStr,
}
return cfg.Sdump(obj)
}
func indent(src []byte, prefix string) string {
var buf bytes.Buffer
json.Indent(&buf, src, prefix, indentStr)
return buf.String()
}
func BenchmarkStandardMarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var obj T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&obj)
for i := 0; i < t.N; i++ {
jb, err := json.Marshal(obj)
if err != nil {
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
}
_ = jb
}
}
func BenchmarkStandardUnmarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var before T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&before)
jb, err := json.Marshal(before)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
for i := 0; i < t.N; i++ {
var after T
err = json.Unmarshal(jb, &after)
if err != nil {
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
}
}
}
func BenchmarkJSONIterMarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var obj T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&obj)
for i := 0; i < t.N; i++ {
jb, err := jsoniter.Marshal(obj)
if err != nil {
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
}
_ = jb
}
}
func BenchmarkJSONIterUnmarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var before T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&before)
jb, err := json.Marshal(before)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
for i := 0; i < t.N; i++ {
var after T
err = jsoniter.Unmarshal(jb, &after)
if err != nil {
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
}
}
}

View File

@ -0,0 +1,3 @@
package test
type T uint8

View File

@ -0,0 +1,144 @@
package test
import (
"bytes"
"encoding/json"
"testing"
"github.com/davecgh/go-spew/spew"
fuzz "github.com/google/gofuzz"
jsoniter "github.com/json-iterator/go"
)
func Test_Roundtrip(t *testing.T) {
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
for i := 0; i < 1000; i++ {
var before T
fz.Fuzz(&before)
jbStd, err := json.Marshal(before)
if err != nil {
t.Errorf("failed to marshal with stdlib: %v", err)
}
jbIter, err := jsoniter.Marshal(before)
if err != nil {
t.Errorf("failed to marshal with jsoniter: %v", err)
}
if string(jbStd) != string(jbIter) {
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
indent(jbStd, " "), indent(jbIter, " "), dump(before))
}
var afterStd T
err = json.Unmarshal(jbIter, &afterStd)
if err != nil {
t.Errorf("failed to unmarshal with stdlib: %v", err)
}
var afterIter T
err = jsoniter.Unmarshal(jbIter, &afterIter)
if err != nil {
t.Errorf("failed to unmarshal with jsoniter: %v", err)
}
if fingerprint(afterStd) != fingerprint(afterIter) {
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
dump(afterStd), dump(afterIter), indent(jbIter, " "))
}
}
}
const indentStr = "> "
func fingerprint(obj interface{}) string {
c := spew.ConfigState{
SortKeys: true,
SpewKeys: true,
}
return c.Sprintf("%v", obj)
}
func dump(obj interface{}) string {
cfg := spew.ConfigState{
Indent: indentStr,
}
return cfg.Sdump(obj)
}
func indent(src []byte, prefix string) string {
var buf bytes.Buffer
json.Indent(&buf, src, prefix, indentStr)
return buf.String()
}
func BenchmarkStandardMarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var obj T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&obj)
for i := 0; i < t.N; i++ {
jb, err := json.Marshal(obj)
if err != nil {
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
}
_ = jb
}
}
func BenchmarkStandardUnmarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var before T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&before)
jb, err := json.Marshal(before)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
for i := 0; i < t.N; i++ {
var after T
err = json.Unmarshal(jb, &after)
if err != nil {
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
}
}
}
func BenchmarkJSONIterMarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var obj T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&obj)
for i := 0; i < t.N; i++ {
jb, err := jsoniter.Marshal(obj)
if err != nil {
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
}
_ = jb
}
}
func BenchmarkJSONIterUnmarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var before T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&before)
jb, err := json.Marshal(before)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
for i := 0; i < t.N; i++ {
var after T
err = jsoniter.Unmarshal(jb, &after)
if err != nil {
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
}
}
}

View File

@ -0,0 +1,3 @@
package test
type T map[int32]bool

View File

@ -0,0 +1,144 @@
package test
import (
"bytes"
"encoding/json"
"testing"
"github.com/davecgh/go-spew/spew"
fuzz "github.com/google/gofuzz"
jsoniter "github.com/json-iterator/go"
)
func Test_Roundtrip(t *testing.T) {
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
for i := 0; i < 1000; i++ {
var before T
fz.Fuzz(&before)
jbStd, err := json.Marshal(before)
if err != nil {
t.Errorf("failed to marshal with stdlib: %v", err)
}
jbIter, err := jsoniter.Marshal(before)
if err != nil {
t.Errorf("failed to marshal with jsoniter: %v", err)
}
if string(jbStd) != string(jbIter) {
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
indent(jbStd, " "), indent(jbIter, " "), dump(before))
}
var afterStd T
err = json.Unmarshal(jbIter, &afterStd)
if err != nil {
t.Errorf("failed to unmarshal with stdlib: %v", err)
}
var afterIter T
err = jsoniter.Unmarshal(jbIter, &afterIter)
if err != nil {
t.Errorf("failed to unmarshal with jsoniter: %v", err)
}
if fingerprint(afterStd) != fingerprint(afterIter) {
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
dump(afterStd), dump(afterIter), indent(jbIter, " "))
}
}
}
const indentStr = "> "
func fingerprint(obj interface{}) string {
c := spew.ConfigState{
SortKeys: true,
SpewKeys: true,
}
return c.Sprintf("%v", obj)
}
func dump(obj interface{}) string {
cfg := spew.ConfigState{
Indent: indentStr,
}
return cfg.Sdump(obj)
}
func indent(src []byte, prefix string) string {
var buf bytes.Buffer
json.Indent(&buf, src, prefix, indentStr)
return buf.String()
}
func BenchmarkStandardMarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var obj T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&obj)
for i := 0; i < t.N; i++ {
jb, err := json.Marshal(obj)
if err != nil {
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
}
_ = jb
}
}
func BenchmarkStandardUnmarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var before T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&before)
jb, err := json.Marshal(before)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
for i := 0; i < t.N; i++ {
var after T
err = json.Unmarshal(jb, &after)
if err != nil {
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
}
}
}
func BenchmarkJSONIterMarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var obj T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&obj)
for i := 0; i < t.N; i++ {
jb, err := jsoniter.Marshal(obj)
if err != nil {
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
}
_ = jb
}
}
func BenchmarkJSONIterUnmarshal(t *testing.B) {
t.ReportAllocs()
t.ResetTimer()
var before T
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
fz.Fuzz(&before)
jb, err := json.Marshal(before)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
for i := 0; i < t.N; i++ {
var after T
err = jsoniter.Unmarshal(jb, &after)
if err != nil {
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
}
}
}

Some files were not shown because too many files have changed in this diff Show More