You've already forked json-iterator
mirror of
https://github.com/json-iterator/go.git
synced 2025-06-15 22:50:24 +02:00
Compare commits
240 Commits
jsoniter-g
...
1.0.6
Author | SHA1 | Date | |
---|---|---|---|
e7c7f3b337 | |||
75810179f6 | |||
6a8f9fa342 | |||
24bb2eee9f | |||
64cc784089 | |||
477be43d00 | |||
a8708bca85 | |||
658ff9ef15 | |||
64c1c67885 | |||
e3bc511e5a | |||
8fa357ab7b | |||
761ce8cce2 | |||
c3b6c1e845 | |||
0ed9de94f2 | |||
6fded6eb5f | |||
dc3395f770 | |||
bd4e013f98 | |||
48a4a1e4db | |||
9bc223734a | |||
eb9aeccee2 | |||
28adca2a14 | |||
a9b3f36b2f | |||
1e8e785321 | |||
002b5ae342 | |||
07f99a1124 | |||
71f74dc71e | |||
7990317be5 | |||
9edd73f752 | |||
3d5ee1098a | |||
ee8cfb7547 | |||
bca911dae0 | |||
28452fcdec | |||
ea8c33040f | |||
358cfc3929 | |||
c39a632e65 | |||
e31252f2e2 | |||
807e4a8b20 | |||
e78b7e89b6 | |||
945d1aaa19 | |||
ba3857729b | |||
c3ed5e85e0 | |||
c27f6f9350 | |||
0ab880662f | |||
6dad2de6cc | |||
11c1cce0d8 | |||
96fcb84835 | |||
e7a8aea845 | |||
60a9df5ebc | |||
7b060ec866 | |||
25f147f530 | |||
a9b9c73b4d | |||
e0df39fda2 | |||
13f86432b8 | |||
d2a7335211 | |||
b2a706d14b | |||
23078876c5 | |||
051434fab7 | |||
be6688fc1a | |||
ff2b70c1db | |||
f7279a603e | |||
9f088cbcc4 | |||
3c0e5762c4 | |||
d394a135a1 | |||
9fddff05f0 | |||
b1b003864e | |||
aed5a81f09 | |||
f1258b01aa | |||
fbd210edfc | |||
640251ab91 | |||
06b2a7cf1d | |||
5fffb9b8f7 | |||
7e3b776024 | |||
6240e1e798 | |||
0149a5cf4a | |||
5068c8baaf | |||
16f78601b5 | |||
8f50a91be2 | |||
73c7bc881e | |||
4de15a3a87 | |||
14b28b2226 | |||
abe3c4016b | |||
dbb1ef3f63 | |||
46b20bbbec | |||
fdfe0b9a69 | |||
faa3dcf46a | |||
1f58120d43 | |||
6ed27152e0 | |||
3c298d8a76 | |||
9f6e5962a9 | |||
c463aa12c4 | |||
b5d2607a6d | |||
48cc4d965a | |||
c59c42fda0 | |||
8324374402 | |||
2017f3866b | |||
ddc5af4512 | |||
2f7e5c8dd7 | |||
92772579dd | |||
ae57d167e8 | |||
eef35e549b | |||
005d86dc44 | |||
e658f6597a | |||
f8eb43eda3 | |||
18a241d40b | |||
0fdf883ac0 | |||
34fbec74ad | |||
90574c5ca3 | |||
6a4ba7bfa9 | |||
0828e559d0 | |||
2c67d0f68a | |||
f29a0391bc | |||
374e68a144 | |||
b134d86290 | |||
bc3221879d | |||
8c7fc7584a | |||
db32ee8c2d | |||
d80309af3b | |||
36b14963da | |||
f706335302 | |||
2dc0031b26 | |||
cdbd2ed810 | |||
39e9d67807 | |||
2066b01acb | |||
ac3b3cd160 | |||
887789156a | |||
7df5a67d0d | |||
9c358632dc | |||
1cfa233923 | |||
d249b05a85 | |||
abbd16da6c | |||
b67201557a | |||
5124683f24 | |||
4892de725b | |||
34a2174be3 | |||
24ecaff2a1 | |||
c15b4d116c | |||
12cd299fa8 | |||
60ba332980 | |||
f705934fbf | |||
17a26a6e20 | |||
156284b028 | |||
6b6938829d | |||
e066e54964 | |||
18d6ae2668 | |||
c966eaa031 | |||
f6da8e62c3 | |||
5eded4f6ae | |||
9b3ec40fd9 | |||
0d604da7d7 | |||
b6ace7d51b | |||
6a4fbb9892 | |||
4ae426c4b7 | |||
b46d0a2324 | |||
8b03604184 | |||
93ce14316d | |||
779c3e2164 | |||
4b33139ad0 | |||
dc388588a3 | |||
bd4364ab7c | |||
b9dc3ebda7 | |||
90137b4a60 | |||
be9d4ded4f | |||
7b1fd129cf | |||
b91b7ac682 | |||
845d8438db | |||
d37197e176 | |||
45c22b130b | |||
4a84b0b30e | |||
0187038bad | |||
c38e47d169 | |||
b27718d16b | |||
a447a8f797 | |||
0d6dae80e1 | |||
d336ee6da6 | |||
3606750b83 | |||
db3f5046d7 | |||
f0487718f6 | |||
46574e7d09 | |||
3a6ecf051c | |||
5862c51768 | |||
ce479f3476 | |||
bede1d7f40 | |||
d3448d3dbd | |||
8fbed91768 | |||
3b6853d209 | |||
4351a2e6e9 | |||
891d33b415 | |||
ad20f12c34 | |||
9ecb1fd36d | |||
6d0e6f3733 | |||
711f836582 | |||
37ba1b32b5 | |||
2c10d8e6bb | |||
aaf6160146 | |||
b1afefe058 | |||
3bb49c1e47 | |||
eb68fff85c | |||
e07a4ca5ec | |||
dfa4bdf888 | |||
b74ffb2e03 | |||
a46060dedc | |||
5eadecbb66 | |||
5bc013d6a3 | |||
f7df62f1b5 | |||
07f423d248 | |||
d4c0cb2986 | |||
84ed6b3caf | |||
ee6536c50a | |||
b6eb62e96b | |||
8675af13bf | |||
21ca11f96a | |||
dd88d25090 | |||
6a289f32c2 | |||
4907dc00f6 | |||
2350982504 | |||
27725b7139 | |||
ca6a524d4f | |||
1de44419ea | |||
550531a046 | |||
1745078ab7 | |||
6129e85d53 | |||
ee3313111c | |||
4e65952c09 | |||
3829a470ae | |||
3f35bed884 | |||
8d7efe886c | |||
f245011c7d | |||
4ea96ac7c3 | |||
50beb4f15d | |||
e5d7a65616 | |||
d7b6b4e0bb | |||
712ddb1942 | |||
ca8dd93d0b | |||
ac8dd56dfb | |||
d8dbf14af4 | |||
402c6c79e2 | |||
2e10d5fdad | |||
f0b07a2313 | |||
919a2eff5c | |||
a743df1b8a |
3
.codecov.yml
Normal file
3
.codecov.yml
Normal file
@ -0,0 +1,3 @@
|
||||
ignore:
|
||||
- "output_tests/.*"
|
||||
|
5
.gitignore
vendored
5
.gitignore
vendored
@ -1,3 +1,4 @@
|
||||
.idea
|
||||
/vendor
|
||||
/bug_test.go
|
||||
/coverage.txt
|
||||
/profile.out
|
||||
/.idea
|
||||
|
@ -2,6 +2,7 @@ language: go
|
||||
|
||||
go:
|
||||
- 1.8.x
|
||||
- 1.x
|
||||
|
||||
before_install:
|
||||
- go get -t -v ./...
|
||||
|
33
Gopkg.lock
generated
Normal file
33
Gopkg.lock
generated
Normal file
@ -0,0 +1,33 @@
|
||||
# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
|
||||
|
||||
|
||||
[[projects]]
|
||||
name = "github.com/davecgh/go-spew"
|
||||
packages = ["spew"]
|
||||
revision = "346938d642f2ec3594ed81d874461961cd0faa76"
|
||||
version = "v1.1.0"
|
||||
|
||||
[[projects]]
|
||||
branch = "master"
|
||||
name = "github.com/google/gofuzz"
|
||||
packages = ["."]
|
||||
revision = "24818f796faf91cd76ec7bddd72458fbced7a6c1"
|
||||
|
||||
[[projects]]
|
||||
name = "github.com/pmezard/go-difflib"
|
||||
packages = ["difflib"]
|
||||
revision = "792786c7400a136282c1664665ae0a8db921c6c2"
|
||||
version = "v1.0.0"
|
||||
|
||||
[[projects]]
|
||||
name = "github.com/stretchr/testify"
|
||||
packages = ["assert","require"]
|
||||
revision = "69483b4bd14f5845b5a1e55bca19e954e827f1d0"
|
||||
version = "v1.1.4"
|
||||
|
||||
[solve-meta]
|
||||
analyzer-name = "dep"
|
||||
analyzer-version = 1
|
||||
inputs-digest = "f8b7cf3941d3792cbbd570bb53c093adaf774334d1162c651565c97a58dc9d09"
|
||||
solver-name = "gps-cdcl"
|
||||
solver-version = 1
|
33
Gopkg.toml
Normal file
33
Gopkg.toml
Normal file
@ -0,0 +1,33 @@
|
||||
# Gopkg.toml example
|
||||
#
|
||||
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
|
||||
# for detailed Gopkg.toml documentation.
|
||||
#
|
||||
# required = ["github.com/user/thing/cmd/thing"]
|
||||
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
|
||||
#
|
||||
# [[constraint]]
|
||||
# name = "github.com/user/project"
|
||||
# version = "1.0.0"
|
||||
#
|
||||
# [[constraint]]
|
||||
# name = "github.com/user/project2"
|
||||
# branch = "dev"
|
||||
# source = "github.com/myfork/project2"
|
||||
#
|
||||
# [[override]]
|
||||
# name = "github.com/x/y"
|
||||
# version = "2.4.0"
|
||||
|
||||
|
||||
[[constraint]]
|
||||
name = "github.com/davecgh/go-spew"
|
||||
version = "1.1.0"
|
||||
|
||||
[[constraint]]
|
||||
branch = "master"
|
||||
name = "github.com/google/gofuzz"
|
||||
|
||||
[[constraint]]
|
||||
name = "github.com/stretchr/testify"
|
||||
version = "1.1.4"
|
17
README.md
17
README.md
@ -8,6 +8,12 @@
|
||||
|
||||
A high-performance 100% compatible drop-in replacement of "encoding/json"
|
||||
|
||||
You can also use thrift like JSON using [thrift-iterator](https://github.com/thrift-iterator/go)
|
||||
|
||||
```
|
||||
Go开发者们请加入我们,滴滴出行平台技术部 taowen@didichuxing.com
|
||||
```
|
||||
|
||||
# Benchmark
|
||||
|
||||

|
||||
@ -40,7 +46,9 @@ with
|
||||
|
||||
```go
|
||||
import "github.com/json-iterator/go"
|
||||
jsoniter.Marshal(&data)
|
||||
|
||||
var json = jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
json.Marshal(&data)
|
||||
```
|
||||
|
||||
Replace
|
||||
@ -54,7 +62,9 @@ with
|
||||
|
||||
```go
|
||||
import "github.com/json-iterator/go"
|
||||
jsoniter.Unmarshal(input, &data)
|
||||
|
||||
var json = jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
json.Unmarshal(input, &data)
|
||||
```
|
||||
|
||||
[More documentation](http://jsoniter.com/migrate-from-go-std.html)
|
||||
@ -70,6 +80,9 @@ go get github.com/json-iterator/go
|
||||
Contributors
|
||||
|
||||
* [thockin](https://github.com/thockin)
|
||||
* [mattn](https://github.com/mattn)
|
||||
* [cch123](https://github.com/cch123)
|
||||
* [Oleg Shaldybin](https://github.com/olegshaldybin)
|
||||
* [Jason Toffaletti](https://github.com/toffaletti)
|
||||
|
||||
Report issue or pull request, or email taowen@gmail.com, or [](https://gitter.im/json-iterator/Lobby)
|
||||
|
123
any_tests/jsoniter_any_array_test.go
Normal file
123
any_tests/jsoniter_any_array_test.go
Normal file
@ -0,0 +1,123 @@
|
||||
package any_tests
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_read_empty_array_as_any(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := jsoniter.Get([]byte("[]"))
|
||||
should.Equal(jsoniter.ArrayValue, any.Get().ValueType())
|
||||
should.Equal(jsoniter.InvalidValue, any.Get(0.3).ValueType())
|
||||
should.Equal(0, any.Size())
|
||||
should.Equal(jsoniter.ArrayValue, any.ValueType())
|
||||
should.Nil(any.LastError())
|
||||
should.Equal(0, any.ToInt())
|
||||
should.Equal(int32(0), any.ToInt32())
|
||||
should.Equal(int64(0), any.ToInt64())
|
||||
should.Equal(uint(0), any.ToUint())
|
||||
should.Equal(uint32(0), any.ToUint32())
|
||||
should.Equal(uint64(0), any.ToUint64())
|
||||
should.Equal(float32(0), any.ToFloat32())
|
||||
should.Equal(float64(0), any.ToFloat64())
|
||||
}
|
||||
|
||||
func Test_read_one_element_array_as_any(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := jsoniter.Get([]byte("[1]"))
|
||||
should.Equal(1, any.Size())
|
||||
}
|
||||
|
||||
func Test_read_two_element_array_as_any(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := jsoniter.Get([]byte("[1,2]"))
|
||||
should.Equal(1, any.Get(0).ToInt())
|
||||
should.Equal(2, any.Size())
|
||||
should.True(any.ToBool())
|
||||
should.Equal(1, any.ToInt())
|
||||
should.Equal([]interface{}{float64(1), float64(2)}, any.GetInterface())
|
||||
stream := jsoniter.NewStream(jsoniter.ConfigDefault, nil, 32)
|
||||
any.WriteTo(stream)
|
||||
should.Equal("[1,2]", string(stream.Buffer()))
|
||||
arr := []int{}
|
||||
any.ToVal(&arr)
|
||||
should.Equal([]int{1, 2}, arr)
|
||||
}
|
||||
|
||||
func Test_wrap_array_and_convert_to_any(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := jsoniter.Wrap([]int{1, 2, 3})
|
||||
any2 := jsoniter.Wrap([]int{})
|
||||
|
||||
should.Equal("[1,2,3]", any.ToString())
|
||||
should.True(any.ToBool())
|
||||
should.False(any2.ToBool())
|
||||
|
||||
should.Equal(1, any.ToInt())
|
||||
should.Equal(0, any2.ToInt())
|
||||
should.Equal(int32(1), any.ToInt32())
|
||||
should.Equal(int32(0), any2.ToInt32())
|
||||
should.Equal(int64(1), any.ToInt64())
|
||||
should.Equal(int64(0), any2.ToInt64())
|
||||
should.Equal(uint(1), any.ToUint())
|
||||
should.Equal(uint(0), any2.ToUint())
|
||||
should.Equal(uint32(1), any.ToUint32())
|
||||
should.Equal(uint32(0), any2.ToUint32())
|
||||
should.Equal(uint64(1), any.ToUint64())
|
||||
should.Equal(uint64(0), any2.ToUint64())
|
||||
should.Equal(float32(1), any.ToFloat32())
|
||||
should.Equal(float32(0), any2.ToFloat32())
|
||||
should.Equal(float64(1), any.ToFloat64())
|
||||
should.Equal(float64(0), any2.ToFloat64())
|
||||
should.Equal(3, any.Size())
|
||||
should.Equal(0, any2.Size())
|
||||
|
||||
var i interface{} = []int{1, 2, 3}
|
||||
should.Equal(i, any.GetInterface())
|
||||
}
|
||||
|
||||
func Test_array_lazy_any_get(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := jsoniter.Get([]byte("[1,[2,3],4]"))
|
||||
should.Equal(3, any.Get(1, 1).ToInt())
|
||||
should.Equal("[1,[2,3],4]", any.ToString())
|
||||
}
|
||||
|
||||
func Test_array_lazy_any_get_all(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := jsoniter.Get([]byte("[[1],[2],[3,4]]"))
|
||||
should.Equal("[1,2,3]", any.Get('*', 0).ToString())
|
||||
any = jsoniter.Get([]byte("[[[1],[2],[3,4]]]"), 0, '*', 0)
|
||||
should.Equal("[1,2,3]", any.ToString())
|
||||
}
|
||||
|
||||
func Test_array_wrapper_any_get_all(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := jsoniter.Wrap([][]int{
|
||||
{1, 2},
|
||||
{3, 4},
|
||||
{5, 6},
|
||||
})
|
||||
should.Equal("[1,3,5]", any.Get('*', 0).ToString())
|
||||
should.Equal(jsoniter.ArrayValue, any.ValueType())
|
||||
should.True(any.ToBool())
|
||||
should.Equal(1, any.Get(0, 0).ToInt())
|
||||
}
|
||||
|
||||
func Test_array_lazy_any_get_invalid(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := jsoniter.Get([]byte("[]"))
|
||||
should.Equal(jsoniter.InvalidValue, any.Get(1, 1).ValueType())
|
||||
should.NotNil(any.Get(1, 1).LastError())
|
||||
should.Equal(jsoniter.InvalidValue, any.Get("1").ValueType())
|
||||
should.NotNil(any.Get("1").LastError())
|
||||
}
|
||||
|
||||
func Test_invalid_array(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := jsoniter.Get([]byte("["), 0)
|
||||
should.Equal(jsoniter.InvalidValue, any.ValueType())
|
||||
}
|
65
any_tests/jsoniter_any_bool_test.go
Normal file
65
any_tests/jsoniter_any_bool_test.go
Normal file
@ -0,0 +1,65 @@
|
||||
package any_tests
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
var boolConvertMap = map[string]bool{
|
||||
"null": false,
|
||||
"true": true,
|
||||
"false": false,
|
||||
|
||||
`"true"`: true,
|
||||
`"false"`: true,
|
||||
|
||||
"123": true,
|
||||
`"123"`: true,
|
||||
"0": false,
|
||||
`"0"`: false,
|
||||
"-1": true,
|
||||
`"-1"`: true,
|
||||
|
||||
"1.1": true,
|
||||
"0.0": false,
|
||||
"-1.1": true,
|
||||
`""`: false,
|
||||
"[1,2]": true,
|
||||
"[]": false,
|
||||
"{}": true,
|
||||
`{"abc":1}`: true,
|
||||
}
|
||||
|
||||
func Test_read_bool_as_any(t *testing.T) {
|
||||
should := require.New(t)
|
||||
|
||||
var any jsoniter.Any
|
||||
for k, v := range boolConvertMap {
|
||||
any = jsoniter.Get([]byte(k))
|
||||
if v {
|
||||
should.True(any.ToBool(), fmt.Sprintf("origin val is %v", k))
|
||||
} else {
|
||||
should.False(any.ToBool(), fmt.Sprintf("origin val is %v", k))
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func Test_write_bool_to_stream(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := jsoniter.Get([]byte("true"))
|
||||
stream := jsoniter.NewStream(jsoniter.ConfigDefault, nil, 32)
|
||||
any.WriteTo(stream)
|
||||
should.Equal("true", string(stream.Buffer()))
|
||||
should.Equal(any.ValueType(), jsoniter.BoolValue)
|
||||
|
||||
any = jsoniter.Get([]byte("false"))
|
||||
stream = jsoniter.NewStream(jsoniter.ConfigDefault, nil, 32)
|
||||
any.WriteTo(stream)
|
||||
should.Equal("false", string(stream.Buffer()))
|
||||
|
||||
should.Equal(any.ValueType(), jsoniter.BoolValue)
|
||||
}
|
103
any_tests/jsoniter_any_float_test.go
Normal file
103
any_tests/jsoniter_any_float_test.go
Normal file
@ -0,0 +1,103 @@
|
||||
package any_tests
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
var floatConvertMap = map[string]float64{
|
||||
"null": 0,
|
||||
"true": 1,
|
||||
"false": 0,
|
||||
|
||||
`"true"`: 0,
|
||||
`"false"`: 0,
|
||||
|
||||
"1e1": 10,
|
||||
"1e+1": 10,
|
||||
"1e-1": .1,
|
||||
"1E1": 10,
|
||||
"1E+1": 10,
|
||||
"1E-1": .1,
|
||||
|
||||
"-1e1": -10,
|
||||
"-1e+1": -10,
|
||||
"-1e-1": -.1,
|
||||
"-1E1": -10,
|
||||
"-1E+1": -10,
|
||||
"-1E-1": -.1,
|
||||
|
||||
`"1e1"`: 10,
|
||||
`"1e+1"`: 10,
|
||||
`"1e-1"`: .1,
|
||||
`"1E1"`: 10,
|
||||
`"1E+1"`: 10,
|
||||
`"1E-1"`: .1,
|
||||
|
||||
`"-1e1"`: -10,
|
||||
`"-1e+1"`: -10,
|
||||
`"-1e-1"`: -.1,
|
||||
`"-1E1"`: -10,
|
||||
`"-1E+1"`: -10,
|
||||
`"-1E-1"`: -.1,
|
||||
|
||||
"123": 123,
|
||||
`"123true"`: 123,
|
||||
`"+"`: 0,
|
||||
`"-"`: 0,
|
||||
|
||||
`"-123true"`: -123,
|
||||
`"-99.9true"`: -99.9,
|
||||
"0": 0,
|
||||
`"0"`: 0,
|
||||
"-1": -1,
|
||||
|
||||
"1.1": 1.1,
|
||||
"0.0": 0,
|
||||
"-1.1": -1.1,
|
||||
`"+1.1"`: 1.1,
|
||||
`""`: 0,
|
||||
"[1,2]": 1,
|
||||
"[]": 0,
|
||||
"{}": 0,
|
||||
`{"abc":1}`: 0,
|
||||
}
|
||||
|
||||
func Test_read_any_to_float(t *testing.T) {
|
||||
should := require.New(t)
|
||||
for k, v := range floatConvertMap {
|
||||
any := jsoniter.Get([]byte(k))
|
||||
should.Equal(float64(v), any.ToFloat64(), "the original val is "+k)
|
||||
}
|
||||
|
||||
for k, v := range floatConvertMap {
|
||||
any := jsoniter.Get([]byte(k))
|
||||
should.Equal(float32(v), any.ToFloat32(), "the original val is "+k)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_read_float_to_any(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := jsoniter.WrapFloat64(12.3)
|
||||
anyFloat64 := float64(12.3)
|
||||
//negaAnyFloat64 := float64(-1.1)
|
||||
any2 := jsoniter.WrapFloat64(-1.1)
|
||||
should.Equal(float64(12.3), any.ToFloat64())
|
||||
//should.Equal("12.3", any.ToString())
|
||||
should.True(any.ToBool())
|
||||
should.Equal(float32(anyFloat64), any.ToFloat32())
|
||||
should.Equal(int(anyFloat64), any.ToInt())
|
||||
should.Equal(int32(anyFloat64), any.ToInt32())
|
||||
should.Equal(int64(anyFloat64), any.ToInt64())
|
||||
should.Equal(uint(anyFloat64), any.ToUint())
|
||||
should.Equal(uint32(anyFloat64), any.ToUint32())
|
||||
should.Equal(uint64(anyFloat64), any.ToUint64())
|
||||
should.Equal(uint(0), any2.ToUint())
|
||||
should.Equal(uint32(0), any2.ToUint32())
|
||||
should.Equal(uint64(0), any2.ToUint64())
|
||||
should.Equal(any.ValueType(), jsoniter.NumberValue)
|
||||
|
||||
should.Equal("1.23E+01", any.ToString())
|
||||
}
|
198
any_tests/jsoniter_any_int_test.go
Normal file
198
any_tests/jsoniter_any_int_test.go
Normal file
@ -0,0 +1,198 @@
|
||||
package any_tests
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
var intConvertMap = map[string]int{
|
||||
"null": 0,
|
||||
"321.1": 321,
|
||||
"-321.1": -321,
|
||||
`"1.1"`: 1,
|
||||
`"-321.1"`: -321,
|
||||
"0.0": 0,
|
||||
"0": 0,
|
||||
`"0"`: 0,
|
||||
`"0.0"`: 0,
|
||||
"-1.1": -1,
|
||||
"true": 1,
|
||||
"false": 0,
|
||||
`"true"`: 0,
|
||||
`"false"`: 0,
|
||||
`"true123"`: 0,
|
||||
`"123true"`: 123,
|
||||
`"-123true"`: -123,
|
||||
`"1.2332e6"`: 1,
|
||||
`""`: 0,
|
||||
"+": 0,
|
||||
"-": 0,
|
||||
"[]": 0,
|
||||
"[1,2]": 1,
|
||||
`["1","2"]`: 1,
|
||||
// object in php cannot convert to int
|
||||
"{}": 0,
|
||||
}
|
||||
|
||||
func Test_read_any_to_int(t *testing.T) {
|
||||
should := require.New(t)
|
||||
|
||||
// int
|
||||
for k, v := range intConvertMap {
|
||||
any := jsoniter.Get([]byte(k))
|
||||
should.Equal(v, any.ToInt(), fmt.Sprintf("origin val %v", k))
|
||||
}
|
||||
|
||||
// int32
|
||||
for k, v := range intConvertMap {
|
||||
any := jsoniter.Get([]byte(k))
|
||||
should.Equal(int32(v), any.ToInt32(), fmt.Sprintf("original val is %v", k))
|
||||
}
|
||||
|
||||
// int64
|
||||
for k, v := range intConvertMap {
|
||||
any := jsoniter.Get([]byte(k))
|
||||
should.Equal(int64(v), any.ToInt64(), fmt.Sprintf("original val is %v", k))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
var uintConvertMap = map[string]int{
|
||||
"null": 0,
|
||||
"321.1": 321,
|
||||
`"1.1"`: 1,
|
||||
`"-123.1"`: 0,
|
||||
"0.0": 0,
|
||||
"0": 0,
|
||||
`"0"`: 0,
|
||||
`"0.0"`: 0,
|
||||
`"00.0"`: 0,
|
||||
"true": 1,
|
||||
"false": 0,
|
||||
`"true"`: 0,
|
||||
`"false"`: 0,
|
||||
`"true123"`: 0,
|
||||
`"+1"`: 1,
|
||||
`"123true"`: 123,
|
||||
`"-123true"`: 0,
|
||||
`"1.2332e6"`: 1,
|
||||
`""`: 0,
|
||||
"+": 0,
|
||||
"-": 0,
|
||||
".": 0,
|
||||
"[]": 0,
|
||||
"[1,2]": 1,
|
||||
"{}": 0,
|
||||
"{1,2}": 0,
|
||||
"-1.1": 0,
|
||||
"-321.1": 0,
|
||||
}
|
||||
|
||||
func Test_read_any_to_uint(t *testing.T) {
|
||||
should := require.New(t)
|
||||
|
||||
for k, v := range uintConvertMap {
|
||||
any := jsoniter.Get([]byte(k))
|
||||
should.Equal(uint64(v), any.ToUint64(), fmt.Sprintf("origin val %v", k))
|
||||
}
|
||||
|
||||
for k, v := range uintConvertMap {
|
||||
any := jsoniter.Get([]byte(k))
|
||||
should.Equal(uint32(v), any.ToUint32(), fmt.Sprintf("origin val %v", k))
|
||||
}
|
||||
|
||||
for k, v := range uintConvertMap {
|
||||
any := jsoniter.Get([]byte(k))
|
||||
should.Equal(uint(v), any.ToUint(), fmt.Sprintf("origin val %v", k))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func Test_read_int64_to_any(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := jsoniter.WrapInt64(12345)
|
||||
should.Equal(12345, any.ToInt())
|
||||
should.Equal(int32(12345), any.ToInt32())
|
||||
should.Equal(int64(12345), any.ToInt64())
|
||||
should.Equal(uint(12345), any.ToUint())
|
||||
should.Equal(uint32(12345), any.ToUint32())
|
||||
should.Equal(uint64(12345), any.ToUint64())
|
||||
should.Equal(float32(12345), any.ToFloat32())
|
||||
should.Equal(float64(12345), any.ToFloat64())
|
||||
should.Equal("12345", any.ToString())
|
||||
should.Equal(true, any.ToBool())
|
||||
should.Equal(any.ValueType(), jsoniter.NumberValue)
|
||||
stream := jsoniter.NewStream(jsoniter.ConfigDefault, nil, 32)
|
||||
any.WriteTo(stream)
|
||||
should.Equal("12345", string(stream.Buffer()))
|
||||
}
|
||||
func Test_read_int32_to_any(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := jsoniter.WrapInt32(12345)
|
||||
should.Equal(12345, any.ToInt())
|
||||
should.Equal(int32(12345), any.ToInt32())
|
||||
should.Equal(int64(12345), any.ToInt64())
|
||||
should.Equal(uint(12345), any.ToUint())
|
||||
should.Equal(uint32(12345), any.ToUint32())
|
||||
should.Equal(uint64(12345), any.ToUint64())
|
||||
should.Equal(float32(12345), any.ToFloat32())
|
||||
should.Equal(float64(12345), any.ToFloat64())
|
||||
should.Equal("12345", any.ToString())
|
||||
should.Equal(true, any.ToBool())
|
||||
should.Equal(any.ValueType(), jsoniter.NumberValue)
|
||||
stream := jsoniter.NewStream(jsoniter.ConfigDefault, nil, 32)
|
||||
any.WriteTo(stream)
|
||||
should.Equal("12345", string(stream.Buffer()))
|
||||
}
|
||||
|
||||
func Test_read_uint32_to_any(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := jsoniter.WrapUint32(12345)
|
||||
should.Equal(12345, any.ToInt())
|
||||
should.Equal(int32(12345), any.ToInt32())
|
||||
should.Equal(int64(12345), any.ToInt64())
|
||||
should.Equal(uint(12345), any.ToUint())
|
||||
should.Equal(uint32(12345), any.ToUint32())
|
||||
should.Equal(uint64(12345), any.ToUint64())
|
||||
should.Equal(float32(12345), any.ToFloat32())
|
||||
should.Equal(float64(12345), any.ToFloat64())
|
||||
should.Equal("12345", any.ToString())
|
||||
should.Equal(true, any.ToBool())
|
||||
should.Equal(any.ValueType(), jsoniter.NumberValue)
|
||||
stream := jsoniter.NewStream(jsoniter.ConfigDefault, nil, 32)
|
||||
any.WriteTo(stream)
|
||||
should.Equal("12345", string(stream.Buffer()))
|
||||
}
|
||||
|
||||
func Test_read_uint64_to_any(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := jsoniter.WrapUint64(12345)
|
||||
should.Equal(12345, any.ToInt())
|
||||
should.Equal(int32(12345), any.ToInt32())
|
||||
should.Equal(int64(12345), any.ToInt64())
|
||||
should.Equal(uint(12345), any.ToUint())
|
||||
should.Equal(uint32(12345), any.ToUint32())
|
||||
should.Equal(uint64(12345), any.ToUint64())
|
||||
should.Equal(float32(12345), any.ToFloat32())
|
||||
should.Equal(float64(12345), any.ToFloat64())
|
||||
should.Equal("12345", any.ToString())
|
||||
should.Equal(true, any.ToBool())
|
||||
should.Equal(any.ValueType(), jsoniter.NumberValue)
|
||||
stream := jsoniter.NewStream(jsoniter.ConfigDefault, nil, 32)
|
||||
any.WriteTo(stream)
|
||||
should.Equal("12345", string(stream.Buffer()))
|
||||
stream = jsoniter.NewStream(jsoniter.ConfigDefault, nil, 32)
|
||||
stream.WriteUint(uint(123))
|
||||
should.Equal("123", string(stream.Buffer()))
|
||||
}
|
||||
|
||||
func Test_int_lazy_any_get(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := jsoniter.Get([]byte("1234"))
|
||||
// panic!!
|
||||
//should.Equal(any.LastError(), io.EOF)
|
||||
should.Equal(jsoniter.InvalidValue, any.Get(1, "2").ValueType())
|
||||
}
|
28
any_tests/jsoniter_any_map_test.go
Normal file
28
any_tests/jsoniter_any_map_test.go
Normal file
@ -0,0 +1,28 @@
|
||||
package any_tests
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/require"
|
||||
"testing"
|
||||
"github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_wrap_map(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := jsoniter.Wrap(map[string]string{"Field1": "hello"})
|
||||
should.Equal("hello", any.Get("Field1").ToString())
|
||||
any = jsoniter.Wrap(map[string]string{"Field1": "hello"})
|
||||
should.Equal(1, any.Size())
|
||||
}
|
||||
|
||||
func Test_map_wrapper_any_get_all(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := jsoniter.Wrap(map[string][]int{"Field1": {1, 2}})
|
||||
should.Equal(`{"Field1":1}`, any.Get('*', 0).ToString())
|
||||
should.Contains(any.Keys(), "Field1")
|
||||
|
||||
// map write to
|
||||
stream := jsoniter.NewStream(jsoniter.ConfigDefault, nil, 0)
|
||||
any.WriteTo(stream)
|
||||
// TODO cannot pass
|
||||
//should.Equal(string(stream.buf), "")
|
||||
}
|
@ -1,13 +1,14 @@
|
||||
package jsoniter
|
||||
package any_tests
|
||||
|
||||
import (
|
||||
"github.com/json-iterator/go/require"
|
||||
"github.com/stretchr/testify/require"
|
||||
"testing"
|
||||
"github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_read_null_as_any(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := Get([]byte(`null`))
|
||||
any := jsoniter.Get([]byte(`null`))
|
||||
should.Equal(0, any.ToInt())
|
||||
should.Equal(float64(0), any.ToFloat64())
|
||||
should.Equal("", any.ToString())
|
123
any_tests/jsoniter_any_object_test.go
Normal file
123
any_tests/jsoniter_any_object_test.go
Normal file
@ -0,0 +1,123 @@
|
||||
package any_tests
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_read_object_as_any(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := jsoniter.Get([]byte(`{"a":"stream","c":"d"}`))
|
||||
should.Equal(`{"a":"stream","c":"d"}`, any.ToString())
|
||||
// partial parse
|
||||
should.Equal("stream", any.Get("a").ToString())
|
||||
should.Equal("d", any.Get("c").ToString())
|
||||
should.Equal(2, len(any.Keys()))
|
||||
any = jsoniter.Get([]byte(`{"a":"stream","c":"d"}`))
|
||||
// full parse
|
||||
should.Equal(2, len(any.Keys()))
|
||||
should.Equal(2, any.Size())
|
||||
should.True(any.ToBool())
|
||||
should.Equal(0, any.ToInt())
|
||||
should.Equal(jsoniter.ObjectValue, any.ValueType())
|
||||
should.Nil(any.LastError())
|
||||
obj := struct {
|
||||
A string
|
||||
}{}
|
||||
any.ToVal(&obj)
|
||||
should.Equal("stream", obj.A)
|
||||
}
|
||||
|
||||
func Test_object_lazy_any_get(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := jsoniter.Get([]byte(`{"a":{"stream":{"c":"d"}}}`))
|
||||
should.Equal("d", any.Get("a", "stream", "c").ToString())
|
||||
}
|
||||
|
||||
func Test_object_lazy_any_get_all(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := jsoniter.Get([]byte(`{"a":[0],"stream":[1]}`))
|
||||
should.Contains(any.Get('*', 0).ToString(), `"a":0`)
|
||||
}
|
||||
|
||||
func Test_object_lazy_any_get_invalid(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := jsoniter.Get([]byte(`{}`))
|
||||
should.Equal(jsoniter.InvalidValue, any.Get("a", "stream", "c").ValueType())
|
||||
should.Equal(jsoniter.InvalidValue, any.Get(1).ValueType())
|
||||
}
|
||||
|
||||
func Test_wrap_map_and_convert_to_any(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := jsoniter.Wrap(map[string]interface{}{"a": 1})
|
||||
should.True(any.ToBool())
|
||||
should.Equal(0, any.ToInt())
|
||||
should.Equal(int32(0), any.ToInt32())
|
||||
should.Equal(int64(0), any.ToInt64())
|
||||
should.Equal(float32(0), any.ToFloat32())
|
||||
should.Equal(float64(0), any.ToFloat64())
|
||||
should.Equal(uint(0), any.ToUint())
|
||||
should.Equal(uint32(0), any.ToUint32())
|
||||
should.Equal(uint64(0), any.ToUint64())
|
||||
}
|
||||
|
||||
func Test_wrap_object_and_convert_to_any(t *testing.T) {
|
||||
should := require.New(t)
|
||||
type TestObject struct {
|
||||
Field1 string
|
||||
field2 string
|
||||
}
|
||||
any := jsoniter.Wrap(TestObject{"hello", "world"})
|
||||
should.Equal("hello", any.Get("Field1").ToString())
|
||||
any = jsoniter.Wrap(TestObject{"hello", "world"})
|
||||
should.Equal(2, any.Size())
|
||||
should.Equal(`{"Field1":"hello"}`, any.Get('*').ToString())
|
||||
|
||||
should.Equal(0, any.ToInt())
|
||||
should.Equal(int32(0), any.ToInt32())
|
||||
should.Equal(int64(0), any.ToInt64())
|
||||
should.Equal(float32(0), any.ToFloat32())
|
||||
should.Equal(float64(0), any.ToFloat64())
|
||||
should.Equal(uint(0), any.ToUint())
|
||||
should.Equal(uint32(0), any.ToUint32())
|
||||
should.Equal(uint64(0), any.ToUint64())
|
||||
should.True(any.ToBool())
|
||||
should.Equal(`{"Field1":"hello"}`, any.ToString())
|
||||
|
||||
// cannot pass!
|
||||
//stream := NewStream(ConfigDefault, nil, 32)
|
||||
//any.WriteTo(stream)
|
||||
//should.Equal(`{"Field1":"hello"}`, string(stream.Buffer()))
|
||||
// cannot pass!
|
||||
|
||||
}
|
||||
|
||||
func Test_any_within_struct(t *testing.T) {
|
||||
should := require.New(t)
|
||||
type TestObject struct {
|
||||
Field1 jsoniter.Any
|
||||
Field2 jsoniter.Any
|
||||
}
|
||||
obj := TestObject{}
|
||||
err := jsoniter.UnmarshalFromString(`{"Field1": "hello", "Field2": [1,2,3]}`, &obj)
|
||||
should.Nil(err)
|
||||
should.Equal("hello", obj.Field1.ToString())
|
||||
should.Equal("[1,2,3]", obj.Field2.ToString())
|
||||
}
|
||||
|
||||
func Test_object_wrapper_any_get_all(t *testing.T) {
|
||||
should := require.New(t)
|
||||
type TestObject struct {
|
||||
Field1 []int
|
||||
Field2 []int
|
||||
}
|
||||
any := jsoniter.Wrap(TestObject{[]int{1, 2}, []int{3, 4}})
|
||||
should.Contains(any.Get('*', 0).ToString(), `"Field2":3`)
|
||||
should.Contains(any.Keys(), "Field1")
|
||||
should.Contains(any.Keys(), "Field2")
|
||||
should.NotContains(any.Keys(), "Field3")
|
||||
|
||||
//should.Contains(any.GetObject()["Field1"].GetArray()[0], 1)
|
||||
}
|
58
any_tests/jsoniter_any_string_test.go
Normal file
58
any_tests/jsoniter_any_string_test.go
Normal file
@ -0,0 +1,58 @@
|
||||
package any_tests
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
var stringConvertMap = map[string]string{
|
||||
"null": "",
|
||||
"321.1": "321.1",
|
||||
`"1.1"`: "1.1",
|
||||
`"-123.1"`: "-123.1",
|
||||
"0.0": "0.0",
|
||||
"0": "0",
|
||||
`"0"`: "0",
|
||||
`"0.0"`: "0.0",
|
||||
`"00.0"`: "00.0",
|
||||
"true": "true",
|
||||
"false": "false",
|
||||
`"true"`: "true",
|
||||
`"false"`: "false",
|
||||
`"true123"`: "true123",
|
||||
`"+1"`: "+1",
|
||||
"[]": "[]",
|
||||
"[1,2]": "[1,2]",
|
||||
"{}": "{}",
|
||||
`{"a":1, "stream":true}`: `{"a":1, "stream":true}`,
|
||||
}
|
||||
|
||||
func Test_read_any_to_string(t *testing.T) {
|
||||
should := require.New(t)
|
||||
for k, v := range stringConvertMap {
|
||||
any := jsoniter.Get([]byte(k))
|
||||
should.Equal(v, any.ToString(), "original val "+k)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_read_string_as_any(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := jsoniter.Get([]byte(`"hello"`))
|
||||
should.Equal("hello", any.ToString())
|
||||
should.True(any.ToBool())
|
||||
any = jsoniter.Get([]byte(`" "`))
|
||||
should.False(any.ToBool())
|
||||
any = jsoniter.Get([]byte(`"false"`))
|
||||
should.True(any.ToBool())
|
||||
any = jsoniter.Get([]byte(`"123"`))
|
||||
should.Equal(123, any.ToInt())
|
||||
}
|
||||
|
||||
func Test_wrap_string(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := jsoniter.Get([]byte("-32000")).MustBeValid()
|
||||
should.Equal(-32000, any.ToInt())
|
||||
should.NoError(any.LastError())
|
||||
}
|
72
any_tests/jsoniter_must_be_valid_test.go
Normal file
72
any_tests/jsoniter_must_be_valid_test.go
Normal file
@ -0,0 +1,72 @@
|
||||
package any_tests
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
// if must be valid is useless, just drop this test
|
||||
func Test_must_be_valid(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := jsoniter.Get([]byte("123"))
|
||||
should.Equal(any.MustBeValid().ToInt(), 123)
|
||||
|
||||
any = jsoniter.Wrap(int8(10))
|
||||
should.Equal(any.MustBeValid().ToInt(), 10)
|
||||
|
||||
any = jsoniter.Wrap(int16(10))
|
||||
should.Equal(any.MustBeValid().ToInt(), 10)
|
||||
|
||||
any = jsoniter.Wrap(int32(10))
|
||||
should.Equal(any.MustBeValid().ToInt(), 10)
|
||||
|
||||
any = jsoniter.Wrap(int64(10))
|
||||
should.Equal(any.MustBeValid().ToInt(), 10)
|
||||
|
||||
any = jsoniter.Wrap(uint(10))
|
||||
should.Equal(any.MustBeValid().ToInt(), 10)
|
||||
|
||||
any = jsoniter.Wrap(uint8(10))
|
||||
should.Equal(any.MustBeValid().ToInt(), 10)
|
||||
|
||||
any = jsoniter.Wrap(uint16(10))
|
||||
should.Equal(any.MustBeValid().ToInt(), 10)
|
||||
|
||||
any = jsoniter.Wrap(uint32(10))
|
||||
should.Equal(any.MustBeValid().ToInt(), 10)
|
||||
|
||||
any = jsoniter.Wrap(uint64(10))
|
||||
should.Equal(any.MustBeValid().ToInt(), 10)
|
||||
|
||||
any = jsoniter.Wrap(float32(10))
|
||||
should.Equal(any.MustBeValid().ToFloat64(), float64(10))
|
||||
|
||||
any = jsoniter.Wrap(float64(10))
|
||||
should.Equal(any.MustBeValid().ToFloat64(), float64(10))
|
||||
|
||||
any = jsoniter.Wrap(true)
|
||||
should.Equal(any.MustBeValid().ToFloat64(), float64(1))
|
||||
|
||||
any = jsoniter.Wrap(false)
|
||||
should.Equal(any.MustBeValid().ToFloat64(), float64(0))
|
||||
|
||||
any = jsoniter.Wrap(nil)
|
||||
should.Equal(any.MustBeValid().ToFloat64(), float64(0))
|
||||
|
||||
any = jsoniter.Wrap(struct{ age int }{age: 1})
|
||||
should.Equal(any.MustBeValid().ToFloat64(), float64(0))
|
||||
|
||||
any = jsoniter.Wrap(map[string]interface{}{"abc": 1})
|
||||
should.Equal(any.MustBeValid().ToFloat64(), float64(0))
|
||||
|
||||
any = jsoniter.Wrap("abc")
|
||||
should.Equal(any.MustBeValid().ToFloat64(), float64(0))
|
||||
|
||||
any = jsoniter.Wrap([]int{})
|
||||
should.Equal(any.MustBeValid().ToFloat64(), float64(0))
|
||||
|
||||
any = jsoniter.Wrap([]int{1, 2})
|
||||
should.Equal(any.MustBeValid().ToFloat64(), float64(1))
|
||||
}
|
119
any_tests/jsoniter_wrap_test.go
Normal file
119
any_tests/jsoniter_wrap_test.go
Normal file
@ -0,0 +1,119 @@
|
||||
package any_tests
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_wrap_and_valuetype_everything(t *testing.T) {
|
||||
should := require.New(t)
|
||||
var i interface{}
|
||||
any := jsoniter.Get([]byte("123"))
|
||||
// default of number type is float64
|
||||
i = float64(123)
|
||||
should.Equal(i, any.GetInterface())
|
||||
|
||||
any = jsoniter.Wrap(int8(10))
|
||||
should.Equal(any.ValueType(), jsoniter.NumberValue)
|
||||
should.Equal(any.LastError(), nil)
|
||||
// get interface is not int8 interface
|
||||
// i = int8(10)
|
||||
// should.Equal(i, any.GetInterface())
|
||||
|
||||
any = jsoniter.Wrap(int16(10))
|
||||
should.Equal(any.ValueType(), jsoniter.NumberValue)
|
||||
should.Equal(any.LastError(), nil)
|
||||
//i = int16(10)
|
||||
//should.Equal(i, any.GetInterface())
|
||||
|
||||
any = jsoniter.Wrap(int32(10))
|
||||
should.Equal(any.ValueType(), jsoniter.NumberValue)
|
||||
should.Equal(any.LastError(), nil)
|
||||
i = int32(10)
|
||||
should.Equal(i, any.GetInterface())
|
||||
any = jsoniter.Wrap(int64(10))
|
||||
should.Equal(any.ValueType(), jsoniter.NumberValue)
|
||||
should.Equal(any.LastError(), nil)
|
||||
i = int64(10)
|
||||
should.Equal(i, any.GetInterface())
|
||||
|
||||
any = jsoniter.Wrap(uint(10))
|
||||
should.Equal(any.ValueType(), jsoniter.NumberValue)
|
||||
should.Equal(any.LastError(), nil)
|
||||
// not equal
|
||||
//i = uint(10)
|
||||
//should.Equal(i, any.GetInterface())
|
||||
any = jsoniter.Wrap(uint8(10))
|
||||
should.Equal(any.ValueType(), jsoniter.NumberValue)
|
||||
should.Equal(any.LastError(), nil)
|
||||
// not equal
|
||||
// i = uint8(10)
|
||||
// should.Equal(i, any.GetInterface())
|
||||
any = jsoniter.Wrap(uint16(10))
|
||||
should.Equal(any.ValueType(), jsoniter.NumberValue)
|
||||
should.Equal(any.LastError(), nil)
|
||||
any = jsoniter.Wrap(uint32(10))
|
||||
should.Equal(any.ValueType(), jsoniter.NumberValue)
|
||||
should.Equal(any.LastError(), nil)
|
||||
i = uint32(10)
|
||||
should.Equal(i, any.GetInterface())
|
||||
any = jsoniter.Wrap(uint64(10))
|
||||
should.Equal(any.ValueType(), jsoniter.NumberValue)
|
||||
should.Equal(any.LastError(), nil)
|
||||
i = uint64(10)
|
||||
should.Equal(i, any.GetInterface())
|
||||
|
||||
any = jsoniter.Wrap(float32(10))
|
||||
should.Equal(any.ValueType(), jsoniter.NumberValue)
|
||||
should.Equal(any.LastError(), nil)
|
||||
// not equal
|
||||
//i = float32(10)
|
||||
//should.Equal(i, any.GetInterface())
|
||||
any = jsoniter.Wrap(float64(10))
|
||||
should.Equal(any.ValueType(), jsoniter.NumberValue)
|
||||
should.Equal(any.LastError(), nil)
|
||||
i = float64(10)
|
||||
should.Equal(i, any.GetInterface())
|
||||
|
||||
any = jsoniter.Wrap(true)
|
||||
should.Equal(any.ValueType(), jsoniter.BoolValue)
|
||||
should.Equal(any.LastError(), nil)
|
||||
i = true
|
||||
should.Equal(i, any.GetInterface())
|
||||
any = jsoniter.Wrap(false)
|
||||
should.Equal(any.ValueType(), jsoniter.BoolValue)
|
||||
should.Equal(any.LastError(), nil)
|
||||
i = false
|
||||
should.Equal(i, any.GetInterface())
|
||||
|
||||
any = jsoniter.Wrap(nil)
|
||||
should.Equal(any.ValueType(), jsoniter.NilValue)
|
||||
should.Equal(any.LastError(), nil)
|
||||
i = nil
|
||||
should.Equal(i, any.GetInterface())
|
||||
|
||||
stream := jsoniter.NewStream(jsoniter.ConfigDefault, nil, 32)
|
||||
any.WriteTo(stream)
|
||||
should.Equal("null", string(stream.Buffer()))
|
||||
should.Equal(any.LastError(), nil)
|
||||
|
||||
any = jsoniter.Wrap(struct{ age int }{age: 1})
|
||||
should.Equal(any.ValueType(), jsoniter.ObjectValue)
|
||||
should.Equal(any.LastError(), nil)
|
||||
i = struct{ age int }{age: 1}
|
||||
should.Equal(i, any.GetInterface())
|
||||
|
||||
any = jsoniter.Wrap(map[string]interface{}{"abc": 1})
|
||||
should.Equal(any.ValueType(), jsoniter.ObjectValue)
|
||||
should.Equal(any.LastError(), nil)
|
||||
i = map[string]interface{}{"abc": 1}
|
||||
should.Equal(i, any.GetInterface())
|
||||
|
||||
any = jsoniter.Wrap("abc")
|
||||
i = "abc"
|
||||
should.Equal(i, any.GetInterface())
|
||||
should.Equal(nil, any.LastError())
|
||||
|
||||
}
|
38
api_tests/config_test.go
Normal file
38
api_tests/config_test.go
Normal file
@ -0,0 +1,38 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/json-iterator/go"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
func Test_use_number_for_unmarshal(t *testing.T) {
|
||||
should := require.New(t)
|
||||
api := jsoniter.Config{UseNumber: true}.Froze()
|
||||
var obj interface{}
|
||||
should.Nil(api.UnmarshalFromString("123", &obj))
|
||||
should.Equal(json.Number("123"), obj)
|
||||
}
|
||||
|
||||
func Test_customize_float_marshal(t *testing.T) {
|
||||
should := require.New(t)
|
||||
json := jsoniter.Config{MarshalFloatWith6Digits: true}.Froze()
|
||||
str, err := json.MarshalToString(float32(1.23456789))
|
||||
should.Nil(err)
|
||||
should.Equal("1.234568", str)
|
||||
}
|
||||
|
||||
|
||||
func Test_customize_tag_key(t *testing.T) {
|
||||
|
||||
type TestObject struct {
|
||||
Field string `orm:"field"`
|
||||
}
|
||||
|
||||
should := require.New(t)
|
||||
json := jsoniter.Config{TagKey: "orm"}.Froze()
|
||||
str, err := json.MarshalToString(TestObject{"hello"})
|
||||
should.Nil(err)
|
||||
should.Equal(`{"field":"hello"}`, str)
|
||||
}
|
@ -1,17 +1,28 @@
|
||||
package jsoniter
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"github.com/json-iterator/go/require"
|
||||
"io/ioutil"
|
||||
"github.com/stretchr/testify/require"
|
||||
"testing"
|
||||
"github.com/json-iterator/go"
|
||||
"io/ioutil"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
func Test_disallowUnknownFields(t *testing.T) {
|
||||
should := require.New(t)
|
||||
type TestObject struct{}
|
||||
var obj TestObject
|
||||
decoder := jsoniter.NewDecoder(bytes.NewBufferString(`{"field1":100}`))
|
||||
decoder.DisallowUnknownFields()
|
||||
should.Error(decoder.Decode(&obj))
|
||||
}
|
||||
|
||||
|
||||
func Test_new_decoder(t *testing.T) {
|
||||
should := require.New(t)
|
||||
decoder1 := json.NewDecoder(bytes.NewBufferString(`[1][2]`))
|
||||
decoder2 := NewDecoder(bytes.NewBufferString(`[1][2]`))
|
||||
decoder2 := jsoniter.NewDecoder(bytes.NewBufferString(`[1][2]`))
|
||||
arr1 := []int{}
|
||||
should.Nil(decoder1.Decode(&arr1))
|
||||
should.Equal([]int{1}, arr1)
|
||||
@ -37,7 +48,7 @@ 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 := jsoniter.NewDecoder(bytes.NewBufferString(`123`))
|
||||
decoder2.UseNumber()
|
||||
var obj1 interface{}
|
||||
should.Nil(decoder1.Decode(&obj1))
|
||||
@ -46,25 +57,3 @@ func Test_use_number(t *testing.T) {
|
||||
should.Nil(decoder2.Decode(&obj2))
|
||||
should.Equal(json.Number("123"), obj2)
|
||||
}
|
||||
|
||||
func Test_use_number_for_unmarshal(t *testing.T) {
|
||||
should := require.New(t)
|
||||
api := Config{UseNumber: true}.Froze()
|
||||
var obj interface{}
|
||||
should.Nil(api.UnmarshalFromString("123", &obj))
|
||||
should.Equal(json.Number("123"), obj)
|
||||
}
|
||||
|
||||
func Test_marshal_indent(t *testing.T) {
|
||||
should := require.New(t)
|
||||
obj := struct {
|
||||
F1 int
|
||||
F2 []int
|
||||
}{1, []int{2, 3, 4}}
|
||||
output, err := json.MarshalIndent(obj, "", " ")
|
||||
should.Nil(err)
|
||||
should.Equal("{\n \"F1\": 1,\n \"F2\": [\n 2,\n 3,\n 4\n ]\n}", string(output))
|
||||
output, err = MarshalIndent(obj, "", " ")
|
||||
should.Nil(err)
|
||||
should.Equal("{\n \"F1\": 1,\n \"F2\": [\n 2,\n 3,\n 4\n ]\n}", string(output))
|
||||
}
|
@ -1,13 +1,15 @@
|
||||
// +build go1.8
|
||||
//+build go1.8
|
||||
|
||||
package jsoniter
|
||||
package test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"encoding/json"
|
||||
"github.com/json-iterator/go/require"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_new_encoder(t *testing.T) {
|
||||
@ -18,14 +20,14 @@ func Test_new_encoder(t *testing.T) {
|
||||
encoder1.Encode([]int{1})
|
||||
should.Equal("[1]\n", buf1.String())
|
||||
buf2 := &bytes.Buffer{}
|
||||
encoder2 := NewEncoder(buf2)
|
||||
encoder2 := jsoniter.NewEncoder(buf2)
|
||||
encoder2.SetEscapeHTML(false)
|
||||
encoder2.Encode([]int{1})
|
||||
should.Equal("[1]", buf2.String())
|
||||
should.Equal("[1]\n", buf2.String())
|
||||
}
|
||||
|
||||
func Test_string_encode_with_std_without_html_escape(t *testing.T) {
|
||||
api := Config{EscapeHtml: false}.Froze()
|
||||
api := jsoniter.Config{EscapeHTML: false}.Froze()
|
||||
should := require.New(t)
|
||||
for i := 0; i < utf8.RuneSelf; i++ {
|
||||
input := string([]byte{byte(i)})
|
||||
@ -41,4 +43,4 @@ func Test_string_encode_with_std_without_html_escape(t *testing.T) {
|
||||
jsoniterOutput := string(jsoniterOutputBytes)
|
||||
should.Equal(stdOutput, jsoniterOutput)
|
||||
}
|
||||
}
|
||||
}
|
20
api_tests/encoder_test.go
Normal file
20
api_tests/encoder_test.go
Normal file
@ -0,0 +1,20 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"github.com/stretchr/testify/require"
|
||||
"bytes"
|
||||
"github.com/json-iterator/go"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
// Standard Encoder has trailing newline.
|
||||
func TestEncoderHasTrailingNewline(t *testing.T) {
|
||||
should := require.New(t)
|
||||
var buf, stdbuf bytes.Buffer
|
||||
enc := jsoniter.ConfigCompatibleWithStandardLibrary.NewEncoder(&buf)
|
||||
enc.Encode(1)
|
||||
stdenc := json.NewEncoder(&stdbuf)
|
||||
stdenc.Encode(1)
|
||||
should.Equal(stdbuf.Bytes(), buf.Bytes())
|
||||
}
|
36
api_tests/marshal_indent_test.go
Normal file
36
api_tests/marshal_indent_test.go
Normal file
@ -0,0 +1,36 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/stretchr/testify/require"
|
||||
"testing"
|
||||
"github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_marshal_indent(t *testing.T) {
|
||||
should := require.New(t)
|
||||
obj := struct {
|
||||
F1 int
|
||||
F2 []int
|
||||
}{1, []int{2, 3, 4}}
|
||||
output, err := json.MarshalIndent(obj, "", " ")
|
||||
should.Nil(err)
|
||||
should.Equal("{\n \"F1\": 1,\n \"F2\": [\n 2,\n 3,\n 4\n ]\n}", string(output))
|
||||
output, err = jsoniter.MarshalIndent(obj, "", " ")
|
||||
should.Nil(err)
|
||||
should.Equal("{\n \"F1\": 1,\n \"F2\": [\n 2,\n 3,\n 4\n ]\n}", string(output))
|
||||
}
|
||||
|
||||
func Test_marshal_indent_map(t *testing.T) {
|
||||
should := require.New(t)
|
||||
obj := map[int]int{1: 2}
|
||||
output, err := json.MarshalIndent(obj, "", " ")
|
||||
should.Nil(err)
|
||||
should.Equal("{\n \"1\": 2\n}", string(output))
|
||||
output, err = jsoniter.MarshalIndent(obj, "", " ")
|
||||
should.Nil(err)
|
||||
should.Equal("{\n \"1\": 2\n}", string(output))
|
||||
output, err = jsoniter.ConfigCompatibleWithStandardLibrary.MarshalIndent(obj, "", " ")
|
||||
should.Nil(err)
|
||||
should.Equal("{\n \"1\": 2\n}", string(output))
|
||||
}
|
1017
assert/assertions.go
1017
assert/assertions.go
File diff suppressed because it is too large
Load Diff
25
benchmarks/encode_string_test.go
Normal file
25
benchmarks/encode_string_test.go
Normal file
@ -0,0 +1,25 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/json-iterator/go"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Benchmark_encode_string_with_SetEscapeHTML(b *testing.B) {
|
||||
type V struct {
|
||||
S string
|
||||
B bool
|
||||
I int
|
||||
}
|
||||
var json = jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
b.ReportAllocs()
|
||||
for i := 0; i < b.N; i++ {
|
||||
buf := &bytes.Buffer{}
|
||||
enc := json.NewEncoder(buf)
|
||||
enc.SetEscapeHTML(true)
|
||||
if err := enc.Encode(V{S: "s", B: true, I: 233}); err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
158
benchmarks/jsoniter_large_file_test.go
Normal file
158
benchmarks/jsoniter_large_file_test.go
Normal file
@ -0,0 +1,158 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
"github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
//func Test_large_file(t *testing.T) {
|
||||
// file, err := os.Open("/tmp/large-file.json")
|
||||
// if err != nil {
|
||||
// t.Fatal(err)
|
||||
// }
|
||||
// iter := Parse(file, 4096)
|
||||
// count := 0
|
||||
// for iter.ReadArray() {
|
||||
// iter.Skip()
|
||||
// count++
|
||||
// }
|
||||
// if count != 11351 {
|
||||
// t.Fatal(count)
|
||||
// }
|
||||
//}
|
||||
|
||||
func init() {
|
||||
ioutil.WriteFile("/tmp/large-file.json", []byte(`[{
|
||||
"person": {
|
||||
"id": "d50887ca-a6ce-4e59-b89f-14f0b5d03b03",
|
||||
"name": {
|
||||
"fullName": "Leonid Bugaev",
|
||||
"givenName": "Leonid",
|
||||
"familyName": "Bugaev"
|
||||
},
|
||||
"email": "leonsbox@gmail.com",
|
||||
"gender": "male",
|
||||
"location": "Saint Petersburg, Saint Petersburg, RU",
|
||||
"geo": {
|
||||
"city": "Saint Petersburg",
|
||||
"state": "Saint Petersburg",
|
||||
"country": "Russia",
|
||||
"lat": 59.9342802,
|
||||
"lng": 30.3350986
|
||||
},
|
||||
"bio": "Senior engineer at Granify.com",
|
||||
"site": "http://flickfaver.com",
|
||||
"avatar": "https://d1ts43dypk8bqh.cloudfront.net/v1/avatars/d50887ca-a6ce-4e59-b89f-14f0b5d03b03",
|
||||
"employment": {
|
||||
"name": "www.latera.ru",
|
||||
"title": "Software Engineer",
|
||||
"domain": "gmail.com"
|
||||
},
|
||||
"facebook": {
|
||||
"handle": "leonid.bugaev"
|
||||
},
|
||||
"github": {
|
||||
"handle": "buger",
|
||||
"id": 14009,
|
||||
"avatar": "https://avatars.githubusercontent.com/u/14009?v=3",
|
||||
"company": "Granify",
|
||||
"blog": "http://leonsbox.com",
|
||||
"followers": 95,
|
||||
"following": 10
|
||||
},
|
||||
"twitter": {
|
||||
"handle": "flickfaver",
|
||||
"id": 77004410,
|
||||
"bio": null,
|
||||
"followers": 2,
|
||||
"following": 1,
|
||||
"statuses": 5,
|
||||
"favorites": 0,
|
||||
"location": "",
|
||||
"site": "http://flickfaver.com",
|
||||
"avatar": null
|
||||
},
|
||||
"linkedin": {
|
||||
"handle": "in/leonidbugaev"
|
||||
},
|
||||
"googleplus": {
|
||||
"handle": null
|
||||
},
|
||||
"angellist": {
|
||||
"handle": "leonid-bugaev",
|
||||
"id": 61541,
|
||||
"bio": "Senior engineer at Granify.com",
|
||||
"blog": "http://buger.github.com",
|
||||
"site": "http://buger.github.com",
|
||||
"followers": 41,
|
||||
"avatar": "https://d1qb2nb5cznatu.cloudfront.net/users/61541-medium_jpg?1405474390"
|
||||
},
|
||||
"klout": {
|
||||
"handle": null,
|
||||
"score": null
|
||||
},
|
||||
"foursquare": {
|
||||
"handle": null
|
||||
},
|
||||
"aboutme": {
|
||||
"handle": "leonid.bugaev",
|
||||
"bio": null,
|
||||
"avatar": null
|
||||
},
|
||||
"gravatar": {
|
||||
"handle": "buger",
|
||||
"urls": [
|
||||
],
|
||||
"avatar": "http://1.gravatar.com/avatar/f7c8edd577d13b8930d5522f28123510",
|
||||
"avatars": [
|
||||
{
|
||||
"url": "http://1.gravatar.com/avatar/f7c8edd577d13b8930d5522f28123510",
|
||||
"type": "thumbnail"
|
||||
}
|
||||
]
|
||||
},
|
||||
"fuzzy": false
|
||||
},
|
||||
"company": "hello"
|
||||
}]`), 0666)
|
||||
}
|
||||
|
||||
/*
|
||||
200000 8886 ns/op 4336 B/op 6 allocs/op
|
||||
50000 34244 ns/op 6744 B/op 14 allocs/op
|
||||
*/
|
||||
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 := jsoniter.Parse(jsoniter.ConfigDefault, file, 4096)
|
||||
count := 0
|
||||
iter.ReadArrayCB(func(iter *jsoniter.Iterator) bool {
|
||||
// Skip() is strict by default, use --tags jsoniter-sloppy to skip without validation
|
||||
iter.Skip()
|
||||
count++
|
||||
return true
|
||||
})
|
||||
file.Close()
|
||||
if iter.Error != nil {
|
||||
b.Error(iter.Error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func Benchmark_json_large_file(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
for n := 0; n < b.N; n++ {
|
||||
file, _ := os.Open("/tmp/large-file.json")
|
||||
bytes, _ := ioutil.ReadAll(file)
|
||||
file.Close()
|
||||
result := []struct{}{}
|
||||
err := json.Unmarshal(bytes, &result)
|
||||
if err != nil {
|
||||
b.Error(err)
|
||||
}
|
||||
}
|
||||
}
|
12
build.sh
Executable file
12
build.sh
Executable file
@ -0,0 +1,12 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
set -x
|
||||
|
||||
if [ ! -d /tmp/build-golang/src/github.com/json-iterator ]; then
|
||||
mkdir -p /tmp/build-golang/src/github.com/json-iterator
|
||||
ln -s $PWD /tmp/build-golang/src/github.com/json-iterator/go
|
||||
fi
|
||||
export GOPATH=/tmp/build-golang
|
||||
go get -u github.com/golang/dep/cmd/dep
|
||||
cd /tmp/build-golang/src/github.com/json-iterator/go
|
||||
exec $GOPATH/bin/dep ensure -update
|
@ -1,10 +1,8 @@
|
||||
package jsoniter_test
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func ExampleMarshal() {
|
||||
@ -18,7 +16,7 @@ func ExampleMarshal() {
|
||||
Name: "Reds",
|
||||
Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
|
||||
}
|
||||
b, err := jsoniter.Marshal(group)
|
||||
b, err := Marshal(group)
|
||||
if err != nil {
|
||||
fmt.Println("error:", err)
|
||||
}
|
||||
@ -37,7 +35,7 @@ func ExampleUnmarshal() {
|
||||
Order string
|
||||
}
|
||||
var animals []Animal
|
||||
err := jsoniter.Unmarshal(jsonBlob, &animals)
|
||||
err := Unmarshal(jsonBlob, &animals)
|
||||
if err != nil {
|
||||
fmt.Println("error:", err)
|
||||
}
|
||||
@ -46,7 +44,7 @@ func ExampleUnmarshal() {
|
||||
// [{Name:Platypus Order:Monotremata} {Name:Quoll Order:Dasyuromorphia}]
|
||||
}
|
||||
|
||||
func ExampleMarshalWithBestPerformance() {
|
||||
func ExampleConfigFastest_Marshal() {
|
||||
type ColorGroup struct {
|
||||
ID int
|
||||
Name string
|
||||
@ -57,8 +55,8 @@ func ExampleMarshalWithBestPerformance() {
|
||||
Name: "Reds",
|
||||
Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
|
||||
}
|
||||
stream := jsoniter.ConfigFastest.BorrowStream(nil)
|
||||
defer jsoniter.ConfigFastest.ReturnStream(stream)
|
||||
stream := ConfigFastest.BorrowStream(nil)
|
||||
defer ConfigFastest.ReturnStream(stream)
|
||||
stream.WriteVal(group)
|
||||
if stream.Error != nil {
|
||||
fmt.Println("error:", stream.Error)
|
||||
@ -68,7 +66,7 @@ func ExampleMarshalWithBestPerformance() {
|
||||
// {"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]}
|
||||
}
|
||||
|
||||
func ExampleUnmarshalWithBestPerformance() {
|
||||
func ExampleConfigFastest_Unmarshal() {
|
||||
var jsonBlob = []byte(`[
|
||||
{"Name": "Platypus", "Order": "Monotremata"},
|
||||
{"Name": "Quoll", "Order": "Dasyuromorphia"}
|
||||
@ -78,8 +76,8 @@ func ExampleUnmarshalWithBestPerformance() {
|
||||
Order string
|
||||
}
|
||||
var animals []Animal
|
||||
iter := jsoniter.ConfigFastest.BorrowIterator(jsonBlob)
|
||||
defer jsoniter.ConfigFastest.ReturnIterator(iter)
|
||||
iter := ConfigFastest.BorrowIterator(jsonBlob)
|
||||
defer ConfigFastest.ReturnIterator(iter)
|
||||
iter.ReadVal(&animals)
|
||||
if iter.Error != nil {
|
||||
fmt.Println("error:", iter.Error)
|
||||
@ -89,9 +87,9 @@ func ExampleUnmarshalWithBestPerformance() {
|
||||
// [{Name:Platypus Order:Monotremata} {Name:Quoll Order:Dasyuromorphia}]
|
||||
}
|
||||
|
||||
func ExampleOneLine() {
|
||||
func ExampleGet() {
|
||||
val := []byte(`{"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]}`)
|
||||
fmt.Printf(jsoniter.Get(val, "Colors", 0).ToString())
|
||||
fmt.Printf(Get(val, "Colors", 0).ToString())
|
||||
// Output:
|
||||
// Crimson
|
||||
}
|
||||
|
101
extension_tests/decoder_test.go
Normal file
101
extension_tests/decoder_test.go
Normal file
@ -0,0 +1,101 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"unsafe"
|
||||
"time"
|
||||
"github.com/json-iterator/go"
|
||||
"github.com/stretchr/testify/require"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func Test_customize_type_decoder(t *testing.T) {
|
||||
t.Skip()
|
||||
jsoniter.RegisterTypeDecoderFunc("time.Time", func(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
t, err := time.ParseInLocation("2006-01-02 15:04:05", iter.ReadString(), time.UTC)
|
||||
if err != nil {
|
||||
iter.Error = err
|
||||
return
|
||||
}
|
||||
*((*time.Time)(ptr)) = t
|
||||
})
|
||||
//defer jsoniter.ConfigDefault.(*frozenConfig).cleanDecoders()
|
||||
val := time.Time{}
|
||||
err := jsoniter.Unmarshal([]byte(`"2016-12-05 08:43:28"`), &val)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
year, month, day := val.Date()
|
||||
if year != 2016 || month != 12 || day != 5 {
|
||||
t.Fatal(val)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_customize_byte_array_encoder(t *testing.T) {
|
||||
t.Skip()
|
||||
//jsoniter.ConfigDefault.(*frozenConfig).cleanEncoders()
|
||||
should := require.New(t)
|
||||
jsoniter.RegisterTypeEncoderFunc("[]uint8", func(ptr unsafe.Pointer, stream *jsoniter.Stream) {
|
||||
t := *((*[]byte)(ptr))
|
||||
stream.WriteString(string(t))
|
||||
}, nil)
|
||||
//defer jsoniter.ConfigDefault.(*frozenConfig).cleanEncoders()
|
||||
val := []byte("abc")
|
||||
str, err := jsoniter.MarshalToString(val)
|
||||
should.Nil(err)
|
||||
should.Equal(`"abc"`, str)
|
||||
}
|
||||
|
||||
func Test_customize_field_decoder(t *testing.T) {
|
||||
type Tom struct {
|
||||
field1 string
|
||||
}
|
||||
jsoniter.RegisterFieldDecoderFunc("jsoniter.Tom", "field1", func(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
*((*string)(ptr)) = strconv.Itoa(iter.ReadInt())
|
||||
})
|
||||
//defer jsoniter.ConfigDefault.(*frozenConfig).cleanDecoders()
|
||||
tom := Tom{}
|
||||
err := jsoniter.Unmarshal([]byte(`{"field1": 100}`), &tom)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func Test_recursive_empty_interface_customization(t *testing.T) {
|
||||
t.Skip()
|
||||
var obj interface{}
|
||||
jsoniter.RegisterTypeDecoderFunc("interface {}", func(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
switch iter.WhatIsNext() {
|
||||
case jsoniter.NumberValue:
|
||||
*(*interface{})(ptr) = iter.ReadInt64()
|
||||
default:
|
||||
*(*interface{})(ptr) = iter.Read()
|
||||
}
|
||||
})
|
||||
should := require.New(t)
|
||||
jsoniter.Unmarshal([]byte("[100]"), &obj)
|
||||
should.Equal([]interface{}{int64(100)}, obj)
|
||||
}
|
||||
|
||||
type MyInterface interface {
|
||||
Hello() string
|
||||
}
|
||||
|
||||
type MyString string
|
||||
|
||||
func (ms MyString) Hello() string {
|
||||
return string(ms)
|
||||
}
|
||||
|
||||
func Test_read_custom_interface(t *testing.T) {
|
||||
t.Skip()
|
||||
should := require.New(t)
|
||||
var val MyInterface
|
||||
jsoniter.RegisterTypeDecoderFunc("jsoniter.MyInterface", func(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
*((*MyInterface)(ptr)) = MyString(iter.ReadString())
|
||||
})
|
||||
err := jsoniter.UnmarshalFromString(`"hello"`, &val)
|
||||
should.Nil(err)
|
||||
should.Equal("hello", val.Hello())
|
||||
}
|
75
extension_tests/extension_test.go
Normal file
75
extension_tests/extension_test.go
Normal file
@ -0,0 +1,75 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"unsafe"
|
||||
"strconv"
|
||||
"testing"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
type TestObject1 struct {
|
||||
Field1 string
|
||||
}
|
||||
|
||||
type testExtension struct {
|
||||
jsoniter.DummyExtension
|
||||
}
|
||||
|
||||
func (extension *testExtension) UpdateStructDescriptor(structDescriptor *jsoniter.StructDescriptor) {
|
||||
if structDescriptor.Type.String() != "test.TestObject1" {
|
||||
return
|
||||
}
|
||||
binding := structDescriptor.GetField("Field1")
|
||||
binding.Encoder = &funcEncoder{fun: func(ptr unsafe.Pointer, stream *jsoniter.Stream) {
|
||||
str := *((*string)(ptr))
|
||||
val, _ := strconv.Atoi(str)
|
||||
stream.WriteInt(val)
|
||||
}}
|
||||
binding.Decoder = &funcDecoder{func(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
*((*string)(ptr)) = strconv.Itoa(iter.ReadInt())
|
||||
}}
|
||||
binding.ToNames = []string{"field-1"}
|
||||
binding.FromNames = []string{"field-1"}
|
||||
}
|
||||
|
||||
func Test_customize_field_by_extension(t *testing.T) {
|
||||
should := require.New(t)
|
||||
cfg := jsoniter.Config{}.Froze()
|
||||
cfg.RegisterExtension(&testExtension{})
|
||||
obj := TestObject1{}
|
||||
err := cfg.UnmarshalFromString(`{"field-1": 100}`, &obj)
|
||||
should.Nil(err)
|
||||
should.Equal("100", obj.Field1)
|
||||
str, err := cfg.MarshalToString(obj)
|
||||
should.Nil(err)
|
||||
should.Equal(`{"field-1":100}`, str)
|
||||
}
|
||||
|
||||
type funcDecoder struct {
|
||||
fun jsoniter.DecoderFunc
|
||||
}
|
||||
|
||||
func (decoder *funcDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
decoder.fun(ptr, iter)
|
||||
}
|
||||
|
||||
type funcEncoder struct {
|
||||
fun jsoniter.EncoderFunc
|
||||
isEmptyFunc func(ptr unsafe.Pointer) bool
|
||||
}
|
||||
|
||||
func (encoder *funcEncoder) Encode(ptr unsafe.Pointer, stream *jsoniter.Stream) {
|
||||
encoder.fun(ptr, stream)
|
||||
}
|
||||
|
||||
func (encoder *funcEncoder) EncodeInterface(val interface{}, stream *jsoniter.Stream) {
|
||||
jsoniter.WriteToStream(val, stream, encoder)
|
||||
}
|
||||
|
||||
func (encoder *funcEncoder) IsEmpty(ptr unsafe.Pointer) bool {
|
||||
if encoder.isEmptyFunc == nil {
|
||||
return false
|
||||
}
|
||||
return encoder.isEmptyFunc(ptr)
|
||||
}
|
@ -2,26 +2,30 @@ package extra
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/json-iterator/go"
|
||||
"io"
|
||||
"math"
|
||||
"reflect"
|
||||
"strings"
|
||||
"unsafe"
|
||||
|
||||
"github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
const MaxUint = ^uint(0)
|
||||
const MaxInt = int(MaxUint >> 1)
|
||||
const MinInt = -MaxInt - 1
|
||||
const maxUint = ^uint(0)
|
||||
const maxInt = int(maxUint >> 1)
|
||||
const minInt = -maxInt - 1
|
||||
|
||||
// RegisterFuzzyDecoders decode input from PHP with tolerance.
|
||||
// It will handle string/number auto conversation, and treat empty [] as empty struct.
|
||||
func RegisterFuzzyDecoders() {
|
||||
jsoniter.RegisterExtension(&tolerateEmptyArrayExtension{})
|
||||
jsoniter.RegisterTypeDecoder("string", &FuzzyStringDecoder{})
|
||||
jsoniter.RegisterTypeDecoder("float32", &FuzzyFloat32Decoder{})
|
||||
jsoniter.RegisterTypeDecoder("float64", &FuzzyFloat64Decoder{})
|
||||
jsoniter.RegisterTypeDecoder("int", &FuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
jsoniter.RegisterTypeDecoder("string", &fuzzyStringDecoder{})
|
||||
jsoniter.RegisterTypeDecoder("float32", &fuzzyFloat32Decoder{})
|
||||
jsoniter.RegisterTypeDecoder("float64", &fuzzyFloat64Decoder{})
|
||||
jsoniter.RegisterTypeDecoder("int", &fuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
if isFloat {
|
||||
val := iter.ReadFloat64()
|
||||
if val > float64(MaxInt) || val < float64(MinInt) {
|
||||
if val > float64(maxInt) || val < float64(minInt) {
|
||||
iter.ReportError("fuzzy decode int", "exceed range")
|
||||
return
|
||||
}
|
||||
@ -30,10 +34,10 @@ func RegisterFuzzyDecoders() {
|
||||
*((*int)(ptr)) = iter.ReadInt()
|
||||
}
|
||||
}})
|
||||
jsoniter.RegisterTypeDecoder("uint", &FuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
jsoniter.RegisterTypeDecoder("uint", &fuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
if isFloat {
|
||||
val := iter.ReadFloat64()
|
||||
if val > float64(MaxUint) || val < 0 {
|
||||
if val > float64(maxUint) || val < 0 {
|
||||
iter.ReportError("fuzzy decode uint", "exceed range")
|
||||
return
|
||||
}
|
||||
@ -42,7 +46,7 @@ func RegisterFuzzyDecoders() {
|
||||
*((*uint)(ptr)) = iter.ReadUint()
|
||||
}
|
||||
}})
|
||||
jsoniter.RegisterTypeDecoder("int8", &FuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
jsoniter.RegisterTypeDecoder("int8", &fuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
if isFloat {
|
||||
val := iter.ReadFloat64()
|
||||
if val > float64(math.MaxInt8) || val < float64(math.MinInt8) {
|
||||
@ -54,7 +58,7 @@ func RegisterFuzzyDecoders() {
|
||||
*((*int8)(ptr)) = iter.ReadInt8()
|
||||
}
|
||||
}})
|
||||
jsoniter.RegisterTypeDecoder("uint8", &FuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
jsoniter.RegisterTypeDecoder("uint8", &fuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
if isFloat {
|
||||
val := iter.ReadFloat64()
|
||||
if val > float64(math.MaxUint8) || val < 0 {
|
||||
@ -66,7 +70,7 @@ func RegisterFuzzyDecoders() {
|
||||
*((*uint8)(ptr)) = iter.ReadUint8()
|
||||
}
|
||||
}})
|
||||
jsoniter.RegisterTypeDecoder("int16", &FuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
jsoniter.RegisterTypeDecoder("int16", &fuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
if isFloat {
|
||||
val := iter.ReadFloat64()
|
||||
if val > float64(math.MaxInt16) || val < float64(math.MinInt16) {
|
||||
@ -78,7 +82,7 @@ func RegisterFuzzyDecoders() {
|
||||
*((*int16)(ptr)) = iter.ReadInt16()
|
||||
}
|
||||
}})
|
||||
jsoniter.RegisterTypeDecoder("uint16", &FuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
jsoniter.RegisterTypeDecoder("uint16", &fuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
if isFloat {
|
||||
val := iter.ReadFloat64()
|
||||
if val > float64(math.MaxUint16) || val < 0 {
|
||||
@ -90,7 +94,7 @@ func RegisterFuzzyDecoders() {
|
||||
*((*uint16)(ptr)) = iter.ReadUint16()
|
||||
}
|
||||
}})
|
||||
jsoniter.RegisterTypeDecoder("int32", &FuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
jsoniter.RegisterTypeDecoder("int32", &fuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
if isFloat {
|
||||
val := iter.ReadFloat64()
|
||||
if val > float64(math.MaxInt32) || val < float64(math.MinInt32) {
|
||||
@ -102,7 +106,7 @@ func RegisterFuzzyDecoders() {
|
||||
*((*int32)(ptr)) = iter.ReadInt32()
|
||||
}
|
||||
}})
|
||||
jsoniter.RegisterTypeDecoder("uint32", &FuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
jsoniter.RegisterTypeDecoder("uint32", &fuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
if isFloat {
|
||||
val := iter.ReadFloat64()
|
||||
if val > float64(math.MaxUint32) || val < 0 {
|
||||
@ -114,7 +118,7 @@ func RegisterFuzzyDecoders() {
|
||||
*((*uint32)(ptr)) = iter.ReadUint32()
|
||||
}
|
||||
}})
|
||||
jsoniter.RegisterTypeDecoder("int64", &FuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
jsoniter.RegisterTypeDecoder("int64", &fuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
if isFloat {
|
||||
val := iter.ReadFloat64()
|
||||
if val > float64(math.MaxInt64) || val < float64(math.MinInt64) {
|
||||
@ -126,7 +130,7 @@ func RegisterFuzzyDecoders() {
|
||||
*((*int64)(ptr)) = iter.ReadInt64()
|
||||
}
|
||||
}})
|
||||
jsoniter.RegisterTypeDecoder("uint64", &FuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
jsoniter.RegisterTypeDecoder("uint64", &fuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
if isFloat {
|
||||
val := iter.ReadFloat64()
|
||||
if val > float64(math.MaxUint64) || val < 0 {
|
||||
@ -156,99 +160,119 @@ type tolerateEmptyArrayDecoder struct {
|
||||
}
|
||||
|
||||
func (decoder *tolerateEmptyArrayDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
if iter.WhatIsNext() == jsoniter.Array {
|
||||
if iter.WhatIsNext() == jsoniter.ArrayValue {
|
||||
iter.Skip()
|
||||
newIter := iter.Config().BorrowIterator([]byte("{}"))
|
||||
defer iter.Config().ReturnIterator(newIter)
|
||||
newIter := iter.Pool().BorrowIterator([]byte("{}"))
|
||||
defer iter.Pool().ReturnIterator(newIter)
|
||||
decoder.valDecoder.Decode(ptr, newIter)
|
||||
} else {
|
||||
decoder.valDecoder.Decode(ptr, iter)
|
||||
}
|
||||
}
|
||||
|
||||
type FuzzyStringDecoder struct {
|
||||
type fuzzyStringDecoder struct {
|
||||
}
|
||||
|
||||
func (decoder *FuzzyStringDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
func (decoder *fuzzyStringDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
valueType := iter.WhatIsNext()
|
||||
switch valueType {
|
||||
case jsoniter.Number:
|
||||
case jsoniter.NumberValue:
|
||||
var number json.Number
|
||||
iter.ReadVal(&number)
|
||||
*((*string)(ptr)) = string(number)
|
||||
case jsoniter.String:
|
||||
case jsoniter.StringValue:
|
||||
*((*string)(ptr)) = iter.ReadString()
|
||||
default:
|
||||
iter.ReportError("FuzzyStringDecoder", "not number or string")
|
||||
iter.ReportError("fuzzyStringDecoder", "not number or string")
|
||||
}
|
||||
}
|
||||
|
||||
type FuzzyIntegerDecoder struct {
|
||||
type fuzzyIntegerDecoder struct {
|
||||
fun func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator)
|
||||
}
|
||||
|
||||
func (decoder *FuzzyIntegerDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
func (decoder *fuzzyIntegerDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
valueType := iter.WhatIsNext()
|
||||
var str string
|
||||
switch valueType {
|
||||
case jsoniter.Number:
|
||||
case jsoniter.NumberValue:
|
||||
var number json.Number
|
||||
iter.ReadVal(&number)
|
||||
str = string(number)
|
||||
case jsoniter.String:
|
||||
case jsoniter.StringValue:
|
||||
str = iter.ReadString()
|
||||
case jsoniter.BoolValue:
|
||||
if iter.ReadBool() {
|
||||
str = "1"
|
||||
} else {
|
||||
str = "0"
|
||||
}
|
||||
default:
|
||||
iter.ReportError("FuzzyIntegerDecoder", "not number or string")
|
||||
iter.ReportError("fuzzyIntegerDecoder", "not number or string")
|
||||
}
|
||||
newIter := iter.Config().BorrowIterator([]byte(str))
|
||||
defer iter.Config().ReturnIterator(newIter)
|
||||
newIter := iter.Pool().BorrowIterator([]byte(str))
|
||||
defer iter.Pool().ReturnIterator(newIter)
|
||||
isFloat := strings.IndexByte(str, '.') != -1
|
||||
decoder.fun(isFloat, ptr, newIter)
|
||||
if newIter.Error != nil {
|
||||
if newIter.Error != nil && newIter.Error != io.EOF {
|
||||
iter.Error = newIter.Error
|
||||
}
|
||||
}
|
||||
|
||||
type FuzzyFloat32Decoder struct {
|
||||
type fuzzyFloat32Decoder struct {
|
||||
}
|
||||
|
||||
func (decoder *FuzzyFloat32Decoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
func (decoder *fuzzyFloat32Decoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
valueType := iter.WhatIsNext()
|
||||
var str string
|
||||
switch valueType {
|
||||
case jsoniter.Number:
|
||||
case jsoniter.NumberValue:
|
||||
*((*float32)(ptr)) = iter.ReadFloat32()
|
||||
case jsoniter.String:
|
||||
case jsoniter.StringValue:
|
||||
str = iter.ReadString()
|
||||
newIter := iter.Config().BorrowIterator([]byte(str))
|
||||
defer iter.Config().ReturnIterator(newIter)
|
||||
newIter := iter.Pool().BorrowIterator([]byte(str))
|
||||
defer iter.Pool().ReturnIterator(newIter)
|
||||
*((*float32)(ptr)) = newIter.ReadFloat32()
|
||||
if newIter.Error != nil {
|
||||
if newIter.Error != nil && newIter.Error != io.EOF {
|
||||
iter.Error = newIter.Error
|
||||
}
|
||||
case jsoniter.BoolValue:
|
||||
// support bool to float32
|
||||
if iter.ReadBool() {
|
||||
*((*float32)(ptr)) = 1
|
||||
} else {
|
||||
*((*float32)(ptr)) = 0
|
||||
}
|
||||
default:
|
||||
iter.ReportError("FuzzyFloat32Decoder", "not number or string")
|
||||
iter.ReportError("fuzzyFloat32Decoder", "not number or string")
|
||||
}
|
||||
}
|
||||
|
||||
type FuzzyFloat64Decoder struct {
|
||||
type fuzzyFloat64Decoder struct {
|
||||
}
|
||||
|
||||
func (decoder *FuzzyFloat64Decoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
func (decoder *fuzzyFloat64Decoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
valueType := iter.WhatIsNext()
|
||||
var str string
|
||||
switch valueType {
|
||||
case jsoniter.Number:
|
||||
case jsoniter.NumberValue:
|
||||
*((*float64)(ptr)) = iter.ReadFloat64()
|
||||
case jsoniter.String:
|
||||
case jsoniter.StringValue:
|
||||
str = iter.ReadString()
|
||||
newIter := iter.Config().BorrowIterator([]byte(str))
|
||||
defer iter.Config().ReturnIterator(newIter)
|
||||
newIter := iter.Pool().BorrowIterator([]byte(str))
|
||||
defer iter.Pool().ReturnIterator(newIter)
|
||||
*((*float64)(ptr)) = newIter.ReadFloat64()
|
||||
if newIter.Error != nil {
|
||||
if newIter.Error != nil && newIter.Error != io.EOF {
|
||||
iter.Error = newIter.Error
|
||||
}
|
||||
case jsoniter.BoolValue:
|
||||
// support bool to float64
|
||||
if iter.ReadBool() {
|
||||
*((*float64)(ptr)) = 1
|
||||
} else {
|
||||
*((*float64)(ptr)) = 0
|
||||
}
|
||||
default:
|
||||
iter.ReportError("FuzzyFloat32Decoder", "not number or string")
|
||||
iter.ReportError("fuzzyFloat32Decoder", "not number or string")
|
||||
}
|
||||
}
|
||||
|
@ -1,89 +1,326 @@
|
||||
package extra
|
||||
|
||||
import (
|
||||
"github.com/json-iterator/go"
|
||||
"github.com/json-iterator/go/require"
|
||||
"testing"
|
||||
|
||||
"github.com/json-iterator/go"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func init() {
|
||||
RegisterFuzzyDecoders()
|
||||
}
|
||||
|
||||
func Test_string_to_string(t *testing.T) {
|
||||
func Test_any_to_string(t *testing.T) {
|
||||
should := require.New(t)
|
||||
var val string
|
||||
should.Nil(jsoniter.UnmarshalFromString(`"100"`, &val))
|
||||
should.Equal("100", val)
|
||||
should.Nil(jsoniter.UnmarshalFromString("10", &val))
|
||||
should.Equal("10", val)
|
||||
should.Nil(jsoniter.UnmarshalFromString("10.1", &val))
|
||||
should.Equal("10.1", val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`"10.1"`, &val))
|
||||
should.Equal("10.1", val)
|
||||
should.NotNil(jsoniter.UnmarshalFromString("{}", &val))
|
||||
should.NotNil(jsoniter.UnmarshalFromString("[]", &val))
|
||||
}
|
||||
|
||||
func Test_int_to_string(t *testing.T) {
|
||||
func Test_any_to_int64(t *testing.T) {
|
||||
should := require.New(t)
|
||||
var val string
|
||||
should.Nil(jsoniter.UnmarshalFromString(`100`, &val))
|
||||
should.Equal("100", val)
|
||||
}
|
||||
var val int64
|
||||
|
||||
func Test_float_to_string(t *testing.T) {
|
||||
should := require.New(t)
|
||||
var val string
|
||||
should.Nil(jsoniter.UnmarshalFromString(`12.0`, &val))
|
||||
should.Equal("12.0", val)
|
||||
}
|
||||
|
||||
func Test_string_to_int(t *testing.T) {
|
||||
should := require.New(t)
|
||||
var val int
|
||||
should.Nil(jsoniter.UnmarshalFromString(`"100"`, &val))
|
||||
should.Equal(100, val)
|
||||
}
|
||||
should.Equal(int64(100), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`"10.1"`, &val))
|
||||
should.Equal(int64(10), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`10.1`, &val))
|
||||
should.Equal(int64(10), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`10`, &val))
|
||||
should.Equal(int64(10), val)
|
||||
|
||||
func Test_int_to_int(t *testing.T) {
|
||||
should := require.New(t)
|
||||
var val int
|
||||
should.Nil(jsoniter.UnmarshalFromString(`100`, &val))
|
||||
should.Equal(100, val)
|
||||
}
|
||||
// bool part
|
||||
should.Nil(jsoniter.UnmarshalFromString(`false`, &val))
|
||||
should.Equal(int64(0), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`true`, &val))
|
||||
should.Equal(int64(1), val)
|
||||
|
||||
func Test_float_to_int(t *testing.T) {
|
||||
should := require.New(t)
|
||||
var val int
|
||||
should.Nil(jsoniter.UnmarshalFromString(`1.23`, &val))
|
||||
should.Equal(1, val)
|
||||
}
|
||||
|
||||
func Test_large_float_to_int(t *testing.T) {
|
||||
should := require.New(t)
|
||||
var val int
|
||||
should.Nil(jsoniter.UnmarshalFromString(`-10`, &val))
|
||||
should.Equal(int64(-10), val)
|
||||
should.NotNil(jsoniter.UnmarshalFromString("{}", &val))
|
||||
should.NotNil(jsoniter.UnmarshalFromString("[]", &val))
|
||||
// large float to int
|
||||
should.NotNil(jsoniter.UnmarshalFromString(`1234512345123451234512345.0`, &val))
|
||||
}
|
||||
|
||||
func Test_string_to_float32(t *testing.T) {
|
||||
func Test_any_to_int(t *testing.T) {
|
||||
should := require.New(t)
|
||||
var val int
|
||||
should.Nil(jsoniter.UnmarshalFromString(`"100"`, &val))
|
||||
should.Equal(100, val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`"10.1"`, &val))
|
||||
should.Equal(10, val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`10.1`, &val))
|
||||
should.Equal(10, val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`10`, &val))
|
||||
should.Equal(10, val)
|
||||
|
||||
// bool part
|
||||
should.Nil(jsoniter.UnmarshalFromString(`false`, &val))
|
||||
should.Equal(0, val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`true`, &val))
|
||||
should.Equal(1, val)
|
||||
|
||||
should.NotNil(jsoniter.UnmarshalFromString("{}", &val))
|
||||
should.NotNil(jsoniter.UnmarshalFromString("[]", &val))
|
||||
// large float to int
|
||||
should.NotNil(jsoniter.UnmarshalFromString(`1234512345123451234512345.0`, &val))
|
||||
}
|
||||
|
||||
func Test_any_to_int16(t *testing.T) {
|
||||
should := require.New(t)
|
||||
var val int16
|
||||
should.Nil(jsoniter.UnmarshalFromString(`"100"`, &val))
|
||||
should.Equal(int16(100), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`"10.1"`, &val))
|
||||
should.Equal(int16(10), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`10.1`, &val))
|
||||
should.Equal(int16(10), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`10`, &val))
|
||||
should.Equal(int16(10), val)
|
||||
|
||||
// bool part
|
||||
should.Nil(jsoniter.UnmarshalFromString(`false`, &val))
|
||||
should.Equal(int16(0), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`true`, &val))
|
||||
should.Equal(int16(1), val)
|
||||
|
||||
should.NotNil(jsoniter.UnmarshalFromString("{}", &val))
|
||||
should.NotNil(jsoniter.UnmarshalFromString("[]", &val))
|
||||
// large float to int
|
||||
should.NotNil(jsoniter.UnmarshalFromString(`1234512345123451234512345.0`, &val))
|
||||
}
|
||||
|
||||
func Test_any_to_int32(t *testing.T) {
|
||||
should := require.New(t)
|
||||
var val int32
|
||||
should.Nil(jsoniter.UnmarshalFromString(`"100"`, &val))
|
||||
should.Equal(int32(100), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`"10.1"`, &val))
|
||||
should.Equal(int32(10), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`10.1`, &val))
|
||||
should.Equal(int32(10), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`10`, &val))
|
||||
should.Equal(int32(10), val)
|
||||
|
||||
// bool part
|
||||
should.Nil(jsoniter.UnmarshalFromString(`false`, &val))
|
||||
should.Equal(int32(0), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`true`, &val))
|
||||
should.Equal(int32(1), val)
|
||||
|
||||
should.NotNil(jsoniter.UnmarshalFromString("{}", &val))
|
||||
should.NotNil(jsoniter.UnmarshalFromString("[]", &val))
|
||||
// large float to int
|
||||
should.NotNil(jsoniter.UnmarshalFromString(`1234512345123451234512345.0`, &val))
|
||||
}
|
||||
|
||||
func Test_any_to_int8(t *testing.T) {
|
||||
should := require.New(t)
|
||||
var val int8
|
||||
should.Nil(jsoniter.UnmarshalFromString(`"100"`, &val))
|
||||
should.Equal(int8(100), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`"10.1"`, &val))
|
||||
should.Equal(int8(10), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`10.1`, &val))
|
||||
should.Equal(int8(10), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`10`, &val))
|
||||
should.Equal(int8(10), val)
|
||||
|
||||
// bool part
|
||||
should.Nil(jsoniter.UnmarshalFromString(`false`, &val))
|
||||
should.Equal(int8(0), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`true`, &val))
|
||||
should.Equal(int8(1), val)
|
||||
|
||||
should.NotNil(jsoniter.UnmarshalFromString("{}", &val))
|
||||
should.NotNil(jsoniter.UnmarshalFromString("[]", &val))
|
||||
// large float to int
|
||||
should.NotNil(jsoniter.UnmarshalFromString(`1234512345123451234512345.0`, &val))
|
||||
}
|
||||
|
||||
func Test_any_to_uint8(t *testing.T) {
|
||||
should := require.New(t)
|
||||
var val uint8
|
||||
should.Nil(jsoniter.UnmarshalFromString(`"100"`, &val))
|
||||
should.Equal(uint8(100), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`"10.1"`, &val))
|
||||
should.Equal(uint8(10), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`10.1`, &val))
|
||||
should.Equal(uint8(10), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`10`, &val))
|
||||
should.Equal(uint8(10), val)
|
||||
|
||||
// bool part
|
||||
should.Nil(jsoniter.UnmarshalFromString(`false`, &val))
|
||||
should.Equal(uint8(0), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`true`, &val))
|
||||
should.Equal(uint8(1), val)
|
||||
|
||||
should.NotNil(jsoniter.UnmarshalFromString("{}", &val))
|
||||
should.NotNil(jsoniter.UnmarshalFromString("[]", &val))
|
||||
// large float to int
|
||||
should.NotNil(jsoniter.UnmarshalFromString(`1234512345123451234512345.0`, &val))
|
||||
}
|
||||
|
||||
func Test_any_to_uint64(t *testing.T) {
|
||||
should := require.New(t)
|
||||
var val uint64
|
||||
|
||||
should.Nil(jsoniter.UnmarshalFromString(`"100"`, &val))
|
||||
should.Equal(uint64(100), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`"10.1"`, &val))
|
||||
should.Equal(uint64(10), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`10.1`, &val))
|
||||
should.Equal(uint64(10), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`10`, &val))
|
||||
should.Equal(uint64(10), val)
|
||||
|
||||
// bool part
|
||||
should.Nil(jsoniter.UnmarshalFromString(`false`, &val))
|
||||
should.Equal(uint64(0), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`true`, &val))
|
||||
should.Equal(uint64(1), val)
|
||||
|
||||
// TODO fix?
|
||||
should.NotNil(jsoniter.UnmarshalFromString(`-10`, &val))
|
||||
should.Equal(uint64(0), val)
|
||||
should.NotNil(jsoniter.UnmarshalFromString("{}", &val))
|
||||
should.NotNil(jsoniter.UnmarshalFromString("[]", &val))
|
||||
// large float to int
|
||||
should.NotNil(jsoniter.UnmarshalFromString(`1234512345123451234512345.0`, &val))
|
||||
}
|
||||
func Test_any_to_uint32(t *testing.T) {
|
||||
should := require.New(t)
|
||||
var val uint32
|
||||
|
||||
should.Nil(jsoniter.UnmarshalFromString(`"100"`, &val))
|
||||
should.Equal(uint32(100), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`"10.1"`, &val))
|
||||
should.Equal(uint32(10), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`10.1`, &val))
|
||||
should.Equal(uint32(10), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`10`, &val))
|
||||
should.Equal(uint32(10), val)
|
||||
|
||||
// bool part
|
||||
should.Nil(jsoniter.UnmarshalFromString(`false`, &val))
|
||||
should.Equal(uint32(0), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`true`, &val))
|
||||
should.Equal(uint32(1), val)
|
||||
|
||||
// TODO fix?
|
||||
should.NotNil(jsoniter.UnmarshalFromString(`-10`, &val))
|
||||
should.Equal(uint32(0), val)
|
||||
should.NotNil(jsoniter.UnmarshalFromString("{}", &val))
|
||||
should.NotNil(jsoniter.UnmarshalFromString("[]", &val))
|
||||
// large float to int
|
||||
should.NotNil(jsoniter.UnmarshalFromString(`1234512345123451234512345.0`, &val))
|
||||
}
|
||||
func Test_any_to_uint16(t *testing.T) {
|
||||
should := require.New(t)
|
||||
var val uint16
|
||||
|
||||
should.Nil(jsoniter.UnmarshalFromString(`"100"`, &val))
|
||||
should.Equal(uint16(100), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`"10.1"`, &val))
|
||||
should.Equal(uint16(10), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`10.1`, &val))
|
||||
should.Equal(uint16(10), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`10`, &val))
|
||||
should.Equal(uint16(10), val)
|
||||
|
||||
// bool part
|
||||
should.Nil(jsoniter.UnmarshalFromString(`false`, &val))
|
||||
should.Equal(uint16(0), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`true`, &val))
|
||||
should.Equal(uint16(1), val)
|
||||
|
||||
// TODO fix?
|
||||
should.NotNil(jsoniter.UnmarshalFromString(`-10`, &val))
|
||||
should.Equal(uint16(0), val)
|
||||
should.NotNil(jsoniter.UnmarshalFromString("{}", &val))
|
||||
should.NotNil(jsoniter.UnmarshalFromString("[]", &val))
|
||||
// large float to int
|
||||
should.NotNil(jsoniter.UnmarshalFromString(`1234512345123451234512345.0`, &val))
|
||||
}
|
||||
func Test_any_to_uint(t *testing.T) {
|
||||
should := require.New(t)
|
||||
var val uint
|
||||
should.Nil(jsoniter.UnmarshalFromString(`"100"`, &val))
|
||||
should.Equal(uint(100), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`"10.1"`, &val))
|
||||
should.Equal(uint(10), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`10.1`, &val))
|
||||
should.Equal(uint(10), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`10`, &val))
|
||||
should.Equal(uint(10), val)
|
||||
|
||||
should.Nil(jsoniter.UnmarshalFromString(`false`, &val))
|
||||
should.Equal(uint(0), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`true`, &val))
|
||||
should.Equal(uint(1), val)
|
||||
|
||||
should.NotNil(jsoniter.UnmarshalFromString("{}", &val))
|
||||
should.NotNil(jsoniter.UnmarshalFromString("[]", &val))
|
||||
// large float to int
|
||||
should.NotNil(jsoniter.UnmarshalFromString(`1234512345123451234512345.0`, &val))
|
||||
}
|
||||
|
||||
func Test_any_to_float32(t *testing.T) {
|
||||
should := require.New(t)
|
||||
var val float32
|
||||
should.Nil(jsoniter.UnmarshalFromString(`"100"`, &val))
|
||||
should.Equal(float32(100), val)
|
||||
|
||||
should.Nil(jsoniter.UnmarshalFromString(`"10.1"`, &val))
|
||||
should.Equal(float32(10.1), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`10.1`, &val))
|
||||
should.Equal(float32(10.1), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`10`, &val))
|
||||
should.Equal(float32(10), val)
|
||||
|
||||
// bool part
|
||||
should.Nil(jsoniter.UnmarshalFromString(`false`, &val))
|
||||
should.Equal(float32(0), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`true`, &val))
|
||||
should.Equal(float32(1), val)
|
||||
|
||||
should.NotNil(jsoniter.UnmarshalFromString("{}", &val))
|
||||
should.NotNil(jsoniter.UnmarshalFromString("[]", &val))
|
||||
}
|
||||
|
||||
func Test_float_to_float32(t *testing.T) {
|
||||
should := require.New(t)
|
||||
var val float32
|
||||
should.Nil(jsoniter.UnmarshalFromString(`1.23`, &val))
|
||||
should.Equal(float32(1.23), val)
|
||||
}
|
||||
|
||||
func Test_string_to_float64(t *testing.T) {
|
||||
func Test_any_to_float64(t *testing.T) {
|
||||
should := require.New(t)
|
||||
var val float64
|
||||
|
||||
should.Nil(jsoniter.UnmarshalFromString(`"100"`, &val))
|
||||
should.Equal(float64(100), val)
|
||||
}
|
||||
|
||||
func Test_float_to_float64(t *testing.T) {
|
||||
should := require.New(t)
|
||||
var val float64
|
||||
should.Nil(jsoniter.UnmarshalFromString(`1.23`, &val))
|
||||
should.Equal(float64(1.23), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`"10.1"`, &val))
|
||||
should.Equal(float64(10.1), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`10.1`, &val))
|
||||
should.Equal(float64(10.1), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`10`, &val))
|
||||
should.Equal(float64(10), val)
|
||||
|
||||
// bool part
|
||||
should.Nil(jsoniter.UnmarshalFromString(`false`, &val))
|
||||
should.Equal(float64(0), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`true`, &val))
|
||||
should.Equal(float64(1), val)
|
||||
|
||||
should.NotNil(jsoniter.UnmarshalFromString("{}", &val))
|
||||
should.NotNil(jsoniter.UnmarshalFromString("[]", &val))
|
||||
}
|
||||
|
||||
func Test_empty_array_as_map(t *testing.T) {
|
||||
@ -99,3 +336,24 @@ func Test_empty_array_as_object(t *testing.T) {
|
||||
should.Nil(jsoniter.UnmarshalFromString(`[]`, &val))
|
||||
should.Equal(struct{}{}, val)
|
||||
}
|
||||
|
||||
func Test_bad_case(t *testing.T) {
|
||||
var jsonstr = `
|
||||
{
|
||||
"extra_type": 181760,
|
||||
"combo_type": 0,
|
||||
"trigger_time_ms": 1498800398000,
|
||||
"_create_time": "2017-06-16 11:21:39",
|
||||
"_msg_type": 41000
|
||||
}
|
||||
`
|
||||
|
||||
type OrderEventRequestParams struct {
|
||||
ExtraType uint64 `json:"extra_type"`
|
||||
}
|
||||
|
||||
var a OrderEventRequestParams
|
||||
err := jsoniter.UnmarshalFromString(jsonstr, &a)
|
||||
should := require.New(t)
|
||||
should.Nil(err)
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"unicode"
|
||||
)
|
||||
|
||||
// SetNamingStrategy rename struct fields uniformly
|
||||
func SetNamingStrategy(translate func(string) string) {
|
||||
jsoniter.RegisterExtension(&namingStrategyExtension{jsoniter.DummyExtension{}, translate})
|
||||
}
|
||||
@ -21,6 +22,7 @@ func (extension *namingStrategyExtension) UpdateStructDescriptor(structDescripto
|
||||
}
|
||||
}
|
||||
|
||||
// LowerCaseWithUnderscores one strategy to SetNamingStrategy for. It will change HelloWorld to hello_world.
|
||||
func LowerCaseWithUnderscores(name string) string {
|
||||
newName := []rune{}
|
||||
for i, c := range name {
|
||||
|
@ -2,7 +2,7 @@ package extra
|
||||
|
||||
import (
|
||||
"github.com/json-iterator/go"
|
||||
"github.com/json-iterator/go/require"
|
||||
"github.com/stretchr/testify/require"
|
||||
"testing"
|
||||
)
|
||||
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"unicode"
|
||||
)
|
||||
|
||||
// SupportPrivateFields include private fields when encoding/decoding
|
||||
func SupportPrivateFields() {
|
||||
jsoniter.RegisterExtension(&privateFieldsExtension{})
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ package extra
|
||||
|
||||
import (
|
||||
"github.com/json-iterator/go"
|
||||
"github.com/json-iterator/go/require"
|
||||
"github.com/stretchr/testify/require"
|
||||
"testing"
|
||||
)
|
||||
|
||||
|
@ -6,7 +6,7 @@ import (
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// keep epoch milliseconds
|
||||
// RegisterTimeAsInt64Codec encode/decode time since number of unit since epoch. the precision is the unit.
|
||||
func RegisterTimeAsInt64Codec(precision time.Duration) {
|
||||
jsoniter.RegisterTypeEncoder("time.Time", &timeAsInt64Codec{precision})
|
||||
jsoniter.RegisterTypeDecoder("time.Time", &timeAsInt64Codec{precision})
|
||||
|
@ -2,7 +2,7 @@ package extra
|
||||
|
||||
import (
|
||||
"github.com/json-iterator/go"
|
||||
"github.com/json-iterator/go/require"
|
||||
"github.com/stretchr/testify/require"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
@ -1,14 +1,3 @@
|
||||
// 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 (
|
||||
@ -16,6 +5,7 @@ import (
|
||||
"io"
|
||||
)
|
||||
|
||||
// RawMessage to make replace json with jsoniter
|
||||
type RawMessage []byte
|
||||
|
||||
// Unmarshal adapts to json/encoding Unmarshal API
|
||||
@ -35,10 +25,12 @@ func lastNotSpacePos(data []byte) int {
|
||||
return 0
|
||||
}
|
||||
|
||||
// UnmarshalFromString convenient method to read from string instead of []byte
|
||||
func UnmarshalFromString(str string, v interface{}) error {
|
||||
return ConfigDefault.UnmarshalFromString(str, v)
|
||||
}
|
||||
|
||||
// Get quick method to get value from deeply nested JSON structure
|
||||
func Get(data []byte, path ...interface{}) Any {
|
||||
return ConfigDefault.Get(data, path...)
|
||||
}
|
||||
@ -51,10 +43,12 @@ func Marshal(v interface{}) ([]byte, error) {
|
||||
return ConfigDefault.Marshal(v)
|
||||
}
|
||||
|
||||
// MarshalIndent same as json.MarshalIndent. Prefix is not supported.
|
||||
func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) {
|
||||
return ConfigDefault.MarshalIndent(v, prefix, indent)
|
||||
}
|
||||
|
||||
// MarshalToString convenient method to write as string instead of []byte
|
||||
func MarshalToString(v interface{}) (string, error) {
|
||||
return ConfigDefault.MarshalToString(v)
|
||||
}
|
||||
@ -75,7 +69,13 @@ type Decoder struct {
|
||||
iter *Iterator
|
||||
}
|
||||
|
||||
// Decode decode JSON into interface{}
|
||||
func (adapter *Decoder) Decode(obj interface{}) error {
|
||||
if adapter.iter.head == adapter.iter.tail && adapter.iter.reader != nil {
|
||||
if !adapter.iter.loadMore() {
|
||||
return io.EOF
|
||||
}
|
||||
}
|
||||
adapter.iter.ReadVal(obj)
|
||||
err := adapter.iter.Error
|
||||
if err == io.EOF {
|
||||
@ -84,41 +84,67 @@ func (adapter *Decoder) Decode(obj interface{}) error {
|
||||
return adapter.iter.Error
|
||||
}
|
||||
|
||||
// More is there more?
|
||||
func (adapter *Decoder) More() bool {
|
||||
return adapter.iter.head != adapter.iter.tail
|
||||
}
|
||||
|
||||
// Buffered remaining buffer
|
||||
func (adapter *Decoder) Buffered() io.Reader {
|
||||
remaining := adapter.iter.buf[adapter.iter.head:adapter.iter.tail]
|
||||
return bytes.NewReader(remaining)
|
||||
}
|
||||
|
||||
func (decoder *Decoder) UseNumber() {
|
||||
origCfg := decoder.iter.cfg.configBeforeFrozen
|
||||
origCfg.UseNumber = true
|
||||
decoder.iter.cfg = origCfg.Froze()
|
||||
// UseNumber causes the Decoder to unmarshal a number into an interface{} as a
|
||||
// Number instead of as a float64.
|
||||
func (adapter *Decoder) UseNumber() {
|
||||
cfg := adapter.iter.cfg.configBeforeFrozen
|
||||
cfg.UseNumber = true
|
||||
adapter.iter.cfg = cfg.frozeWithCacheReuse()
|
||||
}
|
||||
|
||||
// DisallowUnknownFields causes the Decoder to return an error when the destination
|
||||
// is a struct and the input contains object keys which do not match any
|
||||
// non-ignored, exported fields in the destination.
|
||||
func (adapter *Decoder) DisallowUnknownFields() {
|
||||
cfg := adapter.iter.cfg.configBeforeFrozen
|
||||
cfg.DisallowUnknownFields = true
|
||||
adapter.iter.cfg = cfg.frozeWithCacheReuse()
|
||||
}
|
||||
|
||||
// NewEncoder same as json.NewEncoder
|
||||
func NewEncoder(writer io.Writer) *Encoder {
|
||||
return ConfigDefault.NewEncoder(writer)
|
||||
}
|
||||
|
||||
// Encoder same as json.Encoder
|
||||
type Encoder struct {
|
||||
stream *Stream
|
||||
}
|
||||
|
||||
// Encode encode interface{} as JSON to io.Writer
|
||||
func (adapter *Encoder) Encode(val interface{}) error {
|
||||
adapter.stream.WriteVal(val)
|
||||
adapter.stream.WriteRaw("\n")
|
||||
adapter.stream.Flush()
|
||||
return adapter.stream.Error
|
||||
}
|
||||
|
||||
// SetIndent set the indention. Prefix is not supported
|
||||
func (adapter *Encoder) SetIndent(prefix, indent string) {
|
||||
adapter.stream.cfg.indentionStep = len(indent)
|
||||
config := adapter.stream.cfg.configBeforeFrozen
|
||||
config.IndentionStep = len(indent)
|
||||
adapter.stream.cfg = config.frozeWithCacheReuse()
|
||||
}
|
||||
|
||||
func (adapter *Encoder) SetEscapeHTML(escapeHtml bool) {
|
||||
// SetEscapeHTML escape html by default, set to false to disable
|
||||
func (adapter *Encoder) SetEscapeHTML(escapeHTML bool) {
|
||||
config := adapter.stream.cfg.configBeforeFrozen
|
||||
config.EscapeHtml = escapeHtml
|
||||
adapter.stream.cfg = config.Froze()
|
||||
config.EscapeHTML = escapeHTML
|
||||
adapter.stream.cfg = config.frozeWithCacheReuse()
|
||||
}
|
||||
|
||||
// Valid reports whether data is a valid JSON encoding.
|
||||
func Valid(data []byte) bool {
|
||||
return ConfigDefault.Valid(data)
|
||||
}
|
||||
|
@ -1,11 +1,14 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// Any generic object representation.
|
||||
// The lazy json implementation holds []byte and parse lazily.
|
||||
type Any interface {
|
||||
LastError() error
|
||||
ValueType() ValueType
|
||||
@ -25,10 +28,6 @@ type Any interface {
|
||||
// TODO: add Set
|
||||
Size() int
|
||||
Keys() []string
|
||||
// TODO: remove me
|
||||
GetArray() []Any
|
||||
// TODO: remove me
|
||||
GetObject() map[string]Any
|
||||
GetInterface() interface{}
|
||||
WriteTo(stream *Stream)
|
||||
}
|
||||
@ -47,42 +46,41 @@ func (any *baseAny) Keys() []string {
|
||||
return []string{}
|
||||
}
|
||||
|
||||
func (any *baseAny) GetArray() []Any {
|
||||
return []Any{}
|
||||
}
|
||||
|
||||
func (any *baseAny) GetObject() map[string]Any {
|
||||
return map[string]Any{}
|
||||
}
|
||||
|
||||
func (any *baseAny) ToVal(obj interface{}) {
|
||||
panic("not implemented")
|
||||
}
|
||||
|
||||
// WrapInt32 turn int32 into Any interface
|
||||
func WrapInt32(val int32) Any {
|
||||
return &int32Any{baseAny{}, val}
|
||||
}
|
||||
|
||||
// WrapInt64 turn int64 into Any interface
|
||||
func WrapInt64(val int64) Any {
|
||||
return &int64Any{baseAny{}, val}
|
||||
}
|
||||
|
||||
// WrapUint32 turn uint32 into Any interface
|
||||
func WrapUint32(val uint32) Any {
|
||||
return &uint32Any{baseAny{}, val}
|
||||
}
|
||||
|
||||
// WrapUint64 turn uint64 into Any interface
|
||||
func WrapUint64(val uint64) Any {
|
||||
return &uint64Any{baseAny{}, val}
|
||||
}
|
||||
|
||||
// WrapFloat64 turn float64 into Any interface
|
||||
func WrapFloat64(val float64) Any {
|
||||
return &floatAny{baseAny{}, val}
|
||||
}
|
||||
|
||||
// WrapString turn string into Any interface
|
||||
func WrapString(val string) Any {
|
||||
return &stringAny{baseAny{}, val}
|
||||
}
|
||||
|
||||
// Wrap turn a go object into Any interface
|
||||
func Wrap(val interface{}) Any {
|
||||
if val == nil {
|
||||
return &nilAny{}
|
||||
@ -91,8 +89,8 @@ func Wrap(val interface{}) Any {
|
||||
if isAny {
|
||||
return asAny
|
||||
}
|
||||
type_ := reflect.TypeOf(val)
|
||||
switch type_.Kind() {
|
||||
typ := reflect.TypeOf(val)
|
||||
switch typ.Kind() {
|
||||
case reflect.Slice:
|
||||
return wrapArray(val)
|
||||
case reflect.Struct:
|
||||
@ -128,13 +126,13 @@ func Wrap(val interface{}) Any {
|
||||
case reflect.Bool:
|
||||
if val.(bool) == true {
|
||||
return &trueAny{}
|
||||
} else {
|
||||
return &falseAny{}
|
||||
}
|
||||
return &falseAny{}
|
||||
}
|
||||
return &invalidAny{baseAny{}, fmt.Errorf("unsupported type: %v", type_)}
|
||||
return &invalidAny{baseAny{}, fmt.Errorf("unsupported type: %v", typ)}
|
||||
}
|
||||
|
||||
// ReadAny read next JSON element as an Any object. It is a better json.RawMessage.
|
||||
func (iter *Iterator) ReadAny() Any {
|
||||
return iter.readAny()
|
||||
}
|
||||
@ -146,13 +144,13 @@ func (iter *Iterator) readAny() Any {
|
||||
iter.unreadByte()
|
||||
return &stringAny{baseAny{}, iter.ReadString()}
|
||||
case 'n':
|
||||
iter.skipFixedBytes(3) // null
|
||||
iter.skipThreeBytes('u', 'l', 'l') // null
|
||||
return &nilAny{}
|
||||
case 't':
|
||||
iter.skipFixedBytes(3) // true
|
||||
iter.skipThreeBytes('r', 'u', 'e') // true
|
||||
return &trueAny{}
|
||||
case 'f':
|
||||
iter.skipFixedBytes(4) // false
|
||||
iter.skipFourBytes('a', 'l', 's', 'e') // false
|
||||
return &falseAny{}
|
||||
case '{':
|
||||
return iter.readObjectAny()
|
||||
@ -160,6 +158,8 @@ func (iter *Iterator) readAny() Any {
|
||||
return iter.readArrayAny()
|
||||
case '-':
|
||||
return iter.readNumberAny(false)
|
||||
case 0:
|
||||
return &invalidAny{baseAny{}, errors.New("input is empty")}
|
||||
default:
|
||||
return iter.readNumberAny(true)
|
||||
}
|
||||
@ -232,9 +232,8 @@ func locatePath(iter *Iterator, path []interface{}) Any {
|
||||
case int32:
|
||||
if '*' == pathKey {
|
||||
return iter.readAny().Get(path[i:]...)
|
||||
} else {
|
||||
return newInvalidAny(path[i:])
|
||||
}
|
||||
return newInvalidAny(path[i:])
|
||||
default:
|
||||
return newInvalidAny(path[i:])
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ type arrayLazyAny struct {
|
||||
}
|
||||
|
||||
func (any *arrayLazyAny) ValueType() ValueType {
|
||||
return Array
|
||||
return ArrayValue
|
||||
}
|
||||
|
||||
func (any *arrayLazyAny) MustBeValid() Any {
|
||||
@ -33,65 +33,57 @@ func (any *arrayLazyAny) ToBool() bool {
|
||||
func (any *arrayLazyAny) ToInt() int {
|
||||
if any.ToBool() {
|
||||
return 1
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (any *arrayLazyAny) ToInt32() int32 {
|
||||
if any.ToBool() {
|
||||
return 1
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (any *arrayLazyAny) ToInt64() int64 {
|
||||
if any.ToBool() {
|
||||
return 1
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (any *arrayLazyAny) ToUint() uint {
|
||||
if any.ToBool() {
|
||||
return 1
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (any *arrayLazyAny) ToUint32() uint32 {
|
||||
if any.ToBool() {
|
||||
return 1
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (any *arrayLazyAny) ToUint64() uint64 {
|
||||
if any.ToBool() {
|
||||
return 1
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (any *arrayLazyAny) ToFloat32() float32 {
|
||||
if any.ToBool() {
|
||||
return 1
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (any *arrayLazyAny) ToFloat64() float64 {
|
||||
if any.ToBool() {
|
||||
return 1
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (any *arrayLazyAny) ToString() string {
|
||||
@ -115,10 +107,9 @@ func (any *arrayLazyAny) Get(path ...interface{}) Any {
|
||||
valueBytes := locateArrayElement(iter, firstPath)
|
||||
if valueBytes == nil {
|
||||
return newInvalidAny(path)
|
||||
} else {
|
||||
iter.ResetBytes(valueBytes)
|
||||
return locatePath(iter, path[1:])
|
||||
}
|
||||
iter.ResetBytes(valueBytes)
|
||||
return locatePath(iter, path[1:])
|
||||
case int32:
|
||||
if '*' == firstPath {
|
||||
iter := any.cfg.BorrowIterator(any.buf)
|
||||
@ -126,15 +117,14 @@ func (any *arrayLazyAny) Get(path ...interface{}) Any {
|
||||
arr := make([]Any, 0)
|
||||
iter.ReadArrayCB(func(iter *Iterator) bool {
|
||||
found := iter.readAny().Get(path[1:]...)
|
||||
if found.ValueType() != Invalid {
|
||||
if found.ValueType() != InvalidValue {
|
||||
arr = append(arr, found)
|
||||
}
|
||||
return true
|
||||
})
|
||||
return wrapArray(arr)
|
||||
} else {
|
||||
return newInvalidAny(path)
|
||||
}
|
||||
return newInvalidAny(path)
|
||||
default:
|
||||
return newInvalidAny(path)
|
||||
}
|
||||
@ -152,17 +142,6 @@ func (any *arrayLazyAny) Size() int {
|
||||
return size
|
||||
}
|
||||
|
||||
func (any *arrayLazyAny) GetArray() []Any {
|
||||
elements := make([]Any, 0)
|
||||
iter := any.cfg.BorrowIterator(any.buf)
|
||||
defer any.cfg.ReturnIterator(iter)
|
||||
iter.ReadArrayCB(func(iter *Iterator) bool {
|
||||
elements = append(elements, iter.ReadAny())
|
||||
return true
|
||||
})
|
||||
return elements
|
||||
}
|
||||
|
||||
func (any *arrayLazyAny) WriteTo(stream *Stream) {
|
||||
stream.Write(any.buf)
|
||||
}
|
||||
@ -183,7 +162,7 @@ func wrapArray(val interface{}) *arrayAny {
|
||||
}
|
||||
|
||||
func (any *arrayAny) ValueType() ValueType {
|
||||
return Array
|
||||
return ArrayValue
|
||||
}
|
||||
|
||||
func (any *arrayAny) MustBeValid() Any {
|
||||
@ -274,14 +253,13 @@ func (any *arrayAny) Get(path ...interface{}) Any {
|
||||
mappedAll := make([]Any, 0)
|
||||
for i := 0; i < any.val.Len(); i++ {
|
||||
mapped := Wrap(any.val.Index(i).Interface()).Get(path[1:]...)
|
||||
if mapped.ValueType() != Invalid {
|
||||
if mapped.ValueType() != InvalidValue {
|
||||
mappedAll = append(mappedAll, mapped)
|
||||
}
|
||||
}
|
||||
return wrapArray(mappedAll)
|
||||
} else {
|
||||
return newInvalidAny(path)
|
||||
}
|
||||
return newInvalidAny(path)
|
||||
default:
|
||||
return newInvalidAny(path)
|
||||
}
|
||||
@ -291,14 +269,6 @@ func (any *arrayAny) Size() int {
|
||||
return any.val.Len()
|
||||
}
|
||||
|
||||
func (any *arrayAny) GetArray() []Any {
|
||||
elements := make([]Any, any.val.Len())
|
||||
for i := 0; i < any.val.Len(); i++ {
|
||||
elements[i] = Wrap(any.val.Index(i).Interface())
|
||||
}
|
||||
return elements
|
||||
}
|
||||
|
||||
func (any *arrayAny) WriteTo(stream *Stream) {
|
||||
stream.WriteVal(any.val)
|
||||
}
|
||||
|
@ -61,7 +61,7 @@ func (any *trueAny) GetInterface() interface{} {
|
||||
}
|
||||
|
||||
func (any *trueAny) ValueType() ValueType {
|
||||
return Bool
|
||||
return BoolValue
|
||||
}
|
||||
|
||||
func (any *trueAny) MustBeValid() Any {
|
||||
@ -129,7 +129,7 @@ func (any *falseAny) GetInterface() interface{} {
|
||||
}
|
||||
|
||||
func (any *falseAny) ValueType() ValueType {
|
||||
return Bool
|
||||
return BoolValue
|
||||
}
|
||||
|
||||
func (any *falseAny) MustBeValid() Any {
|
||||
|
@ -14,7 +14,7 @@ func (any *floatAny) Parse() *Iterator {
|
||||
}
|
||||
|
||||
func (any *floatAny) ValueType() ValueType {
|
||||
return Number
|
||||
return NumberValue
|
||||
}
|
||||
|
||||
func (any *floatAny) MustBeValid() Any {
|
||||
@ -42,15 +42,24 @@ func (any *floatAny) ToInt64() int64 {
|
||||
}
|
||||
|
||||
func (any *floatAny) ToUint() uint {
|
||||
return uint(any.val)
|
||||
if any.val > 0 {
|
||||
return uint(any.val)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (any *floatAny) ToUint32() uint32 {
|
||||
return uint32(any.val)
|
||||
if any.val > 0 {
|
||||
return uint32(any.val)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (any *floatAny) ToUint64() uint64 {
|
||||
return uint64(any.val)
|
||||
if any.val > 0 {
|
||||
return uint64(any.val)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (any *floatAny) ToFloat32() float32 {
|
||||
|
@ -14,7 +14,7 @@ func (any *int32Any) LastError() error {
|
||||
}
|
||||
|
||||
func (any *int32Any) ValueType() ValueType {
|
||||
return Number
|
||||
return NumberValue
|
||||
}
|
||||
|
||||
func (any *int32Any) MustBeValid() Any {
|
||||
|
@ -14,7 +14,7 @@ func (any *int64Any) LastError() error {
|
||||
}
|
||||
|
||||
func (any *int64Any) ValueType() ValueType {
|
||||
return Number
|
||||
return NumberValue
|
||||
}
|
||||
|
||||
func (any *int64Any) MustBeValid() Any {
|
||||
|
@ -16,12 +16,11 @@ func (any *invalidAny) LastError() error {
|
||||
}
|
||||
|
||||
func (any *invalidAny) ValueType() ValueType {
|
||||
return Invalid
|
||||
return InvalidValue
|
||||
}
|
||||
|
||||
func (any *invalidAny) MustBeValid() Any {
|
||||
panic(any.err)
|
||||
return any
|
||||
}
|
||||
|
||||
func (any *invalidAny) ToBool() bool {
|
||||
@ -70,9 +69,8 @@ func (any *invalidAny) WriteTo(stream *Stream) {
|
||||
func (any *invalidAny) Get(path ...interface{}) Any {
|
||||
if any.err == nil {
|
||||
return &invalidAny{baseAny{}, fmt.Errorf("get %v from invalid", path)}
|
||||
} else {
|
||||
return &invalidAny{baseAny{}, fmt.Errorf("%v, get %v from invalid", any.err, path)}
|
||||
}
|
||||
return &invalidAny{baseAny{}, fmt.Errorf("%v, get %v from invalid", any.err, path)}
|
||||
}
|
||||
|
||||
func (any *invalidAny) Parse() *Iterator {
|
||||
|
@ -9,7 +9,7 @@ func (any *nilAny) LastError() error {
|
||||
}
|
||||
|
||||
func (any *nilAny) ValueType() ValueType {
|
||||
return Nil
|
||||
return NilValue
|
||||
}
|
||||
|
||||
func (any *nilAny) MustBeValid() Any {
|
||||
|
@ -1,6 +1,7 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"io"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
@ -12,7 +13,7 @@ type numberLazyAny struct {
|
||||
}
|
||||
|
||||
func (any *numberLazyAny) ValueType() ValueType {
|
||||
return Number
|
||||
return NumberValue
|
||||
}
|
||||
|
||||
func (any *numberLazyAny) MustBeValid() Any {
|
||||
@ -31,7 +32,9 @@ func (any *numberLazyAny) ToInt() int {
|
||||
iter := any.cfg.BorrowIterator(any.buf)
|
||||
defer any.cfg.ReturnIterator(iter)
|
||||
val := iter.ReadInt()
|
||||
any.err = iter.Error
|
||||
if iter.Error != nil && iter.Error != io.EOF {
|
||||
any.err = iter.Error
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
@ -39,7 +42,9 @@ func (any *numberLazyAny) ToInt32() int32 {
|
||||
iter := any.cfg.BorrowIterator(any.buf)
|
||||
defer any.cfg.ReturnIterator(iter)
|
||||
val := iter.ReadInt32()
|
||||
any.err = iter.Error
|
||||
if iter.Error != nil && iter.Error != io.EOF {
|
||||
any.err = iter.Error
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
@ -47,7 +52,9 @@ func (any *numberLazyAny) ToInt64() int64 {
|
||||
iter := any.cfg.BorrowIterator(any.buf)
|
||||
defer any.cfg.ReturnIterator(iter)
|
||||
val := iter.ReadInt64()
|
||||
any.err = iter.Error
|
||||
if iter.Error != nil && iter.Error != io.EOF {
|
||||
any.err = iter.Error
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
@ -55,7 +62,9 @@ func (any *numberLazyAny) ToUint() uint {
|
||||
iter := any.cfg.BorrowIterator(any.buf)
|
||||
defer any.cfg.ReturnIterator(iter)
|
||||
val := iter.ReadUint()
|
||||
any.err = iter.Error
|
||||
if iter.Error != nil && iter.Error != io.EOF {
|
||||
any.err = iter.Error
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
@ -63,7 +72,9 @@ func (any *numberLazyAny) ToUint32() uint32 {
|
||||
iter := any.cfg.BorrowIterator(any.buf)
|
||||
defer any.cfg.ReturnIterator(iter)
|
||||
val := iter.ReadUint32()
|
||||
any.err = iter.Error
|
||||
if iter.Error != nil && iter.Error != io.EOF {
|
||||
any.err = iter.Error
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
@ -71,7 +82,9 @@ func (any *numberLazyAny) ToUint64() uint64 {
|
||||
iter := any.cfg.BorrowIterator(any.buf)
|
||||
defer any.cfg.ReturnIterator(iter)
|
||||
val := iter.ReadUint64()
|
||||
any.err = iter.Error
|
||||
if iter.Error != nil && iter.Error != io.EOF {
|
||||
any.err = iter.Error
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
@ -79,7 +92,9 @@ func (any *numberLazyAny) ToFloat32() float32 {
|
||||
iter := any.cfg.BorrowIterator(any.buf)
|
||||
defer any.cfg.ReturnIterator(iter)
|
||||
val := iter.ReadFloat32()
|
||||
any.err = iter.Error
|
||||
if iter.Error != nil && iter.Error != io.EOF {
|
||||
any.err = iter.Error
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
@ -87,7 +102,9 @@ func (any *numberLazyAny) ToFloat64() float64 {
|
||||
iter := any.cfg.BorrowIterator(any.buf)
|
||||
defer any.cfg.ReturnIterator(iter)
|
||||
val := iter.ReadFloat64()
|
||||
any.err = iter.Error
|
||||
if iter.Error != nil && iter.Error != io.EOF {
|
||||
any.err = iter.Error
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
|
@ -13,7 +13,7 @@ type objectLazyAny struct {
|
||||
}
|
||||
|
||||
func (any *objectLazyAny) ValueType() ValueType {
|
||||
return Object
|
||||
return ObjectValue
|
||||
}
|
||||
|
||||
func (any *objectLazyAny) MustBeValid() Any {
|
||||
@ -25,73 +25,39 @@ func (any *objectLazyAny) LastError() error {
|
||||
}
|
||||
|
||||
func (any *objectLazyAny) ToBool() bool {
|
||||
iter := any.cfg.BorrowIterator(any.buf)
|
||||
defer any.cfg.ReturnIterator(iter)
|
||||
return iter.ReadObject() != ""
|
||||
return true
|
||||
}
|
||||
|
||||
func (any *objectLazyAny) ToInt() int {
|
||||
if any.ToBool() {
|
||||
return 1
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (any *objectLazyAny) ToInt32() int32 {
|
||||
if any.ToBool() {
|
||||
return 1
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (any *objectLazyAny) ToInt64() int64 {
|
||||
if any.ToBool() {
|
||||
return 1
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (any *objectLazyAny) ToUint() uint {
|
||||
if any.ToBool() {
|
||||
return 1
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (any *objectLazyAny) ToUint32() uint32 {
|
||||
if any.ToBool() {
|
||||
return 1
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (any *objectLazyAny) ToUint64() uint64 {
|
||||
if any.ToBool() {
|
||||
return 1
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (any *objectLazyAny) ToFloat32() float32 {
|
||||
if any.ToBool() {
|
||||
return 1
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (any *objectLazyAny) ToFloat64() float64 {
|
||||
if any.ToBool() {
|
||||
return 1
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (any *objectLazyAny) ToString() string {
|
||||
@ -115,26 +81,24 @@ func (any *objectLazyAny) Get(path ...interface{}) Any {
|
||||
valueBytes := locateObjectField(iter, firstPath)
|
||||
if valueBytes == nil {
|
||||
return newInvalidAny(path)
|
||||
} else {
|
||||
iter.ResetBytes(valueBytes)
|
||||
return locatePath(iter, path[1:])
|
||||
}
|
||||
iter.ResetBytes(valueBytes)
|
||||
return locatePath(iter, path[1:])
|
||||
case int32:
|
||||
if '*' == firstPath {
|
||||
mappedAll := map[string]Any{}
|
||||
iter := any.cfg.BorrowIterator(any.buf)
|
||||
defer any.cfg.ReturnIterator(iter)
|
||||
iter.ReadObjectCB(func(iter *Iterator, field string) bool {
|
||||
iter.ReadMapCB(func(iter *Iterator, field string) bool {
|
||||
mapped := locatePath(iter, path[1:])
|
||||
if mapped.ValueType() != Invalid {
|
||||
if mapped.ValueType() != InvalidValue {
|
||||
mappedAll[field] = mapped
|
||||
}
|
||||
return true
|
||||
})
|
||||
return wrapMap(mappedAll)
|
||||
} else {
|
||||
return newInvalidAny(path)
|
||||
}
|
||||
return newInvalidAny(path)
|
||||
default:
|
||||
return newInvalidAny(path)
|
||||
}
|
||||
@ -144,7 +108,7 @@ func (any *objectLazyAny) Keys() []string {
|
||||
keys := []string{}
|
||||
iter := any.cfg.BorrowIterator(any.buf)
|
||||
defer any.cfg.ReturnIterator(iter)
|
||||
iter.ReadObjectCB(func(iter *Iterator, field string) bool {
|
||||
iter.ReadMapCB(func(iter *Iterator, field string) bool {
|
||||
iter.Skip()
|
||||
keys = append(keys, field)
|
||||
return true
|
||||
@ -164,17 +128,6 @@ func (any *objectLazyAny) Size() int {
|
||||
return size
|
||||
}
|
||||
|
||||
func (any *objectLazyAny) GetObject() map[string]Any {
|
||||
asMap := map[string]Any{}
|
||||
iter := any.cfg.BorrowIterator(any.buf)
|
||||
defer any.cfg.ReturnIterator(iter)
|
||||
iter.ReadObjectCB(func(iter *Iterator, field string) bool {
|
||||
asMap[field] = iter.ReadAny()
|
||||
return true
|
||||
})
|
||||
return asMap
|
||||
}
|
||||
|
||||
func (any *objectLazyAny) WriteTo(stream *Stream) {
|
||||
stream.Write(any.buf)
|
||||
}
|
||||
@ -196,7 +149,7 @@ func wrapStruct(val interface{}) *objectAny {
|
||||
}
|
||||
|
||||
func (any *objectAny) ValueType() ValueType {
|
||||
return Object
|
||||
return ObjectValue
|
||||
}
|
||||
|
||||
func (any *objectAny) MustBeValid() Any {
|
||||
@ -216,59 +169,35 @@ func (any *objectAny) ToBool() bool {
|
||||
}
|
||||
|
||||
func (any *objectAny) ToInt() int {
|
||||
if any.val.NumField() == 0 {
|
||||
return 0
|
||||
}
|
||||
return 1
|
||||
return 0
|
||||
}
|
||||
|
||||
func (any *objectAny) ToInt32() int32 {
|
||||
if any.val.NumField() == 0 {
|
||||
return 0
|
||||
}
|
||||
return 1
|
||||
return 0
|
||||
}
|
||||
|
||||
func (any *objectAny) ToInt64() int64 {
|
||||
if any.val.NumField() == 0 {
|
||||
return 0
|
||||
}
|
||||
return 1
|
||||
return 0
|
||||
}
|
||||
|
||||
func (any *objectAny) ToUint() uint {
|
||||
if any.val.NumField() == 0 {
|
||||
return 0
|
||||
}
|
||||
return 1
|
||||
return 0
|
||||
}
|
||||
|
||||
func (any *objectAny) ToUint32() uint32 {
|
||||
if any.val.NumField() == 0 {
|
||||
return 0
|
||||
}
|
||||
return 1
|
||||
return 0
|
||||
}
|
||||
|
||||
func (any *objectAny) ToUint64() uint64 {
|
||||
if any.val.NumField() == 0 {
|
||||
return 0
|
||||
}
|
||||
return 1
|
||||
return 0
|
||||
}
|
||||
|
||||
func (any *objectAny) ToFloat32() float32 {
|
||||
if any.val.NumField() == 0 {
|
||||
return 0
|
||||
}
|
||||
return 1
|
||||
return 0
|
||||
}
|
||||
|
||||
func (any *objectAny) ToFloat64() float64 {
|
||||
if any.val.NumField() == 0 {
|
||||
return 0
|
||||
}
|
||||
return 1
|
||||
return 0
|
||||
}
|
||||
|
||||
func (any *objectAny) ToString() string {
|
||||
@ -295,15 +224,14 @@ func (any *objectAny) Get(path ...interface{}) Any {
|
||||
field := any.val.Field(i)
|
||||
if field.CanInterface() {
|
||||
mapped := Wrap(field.Interface()).Get(path[1:]...)
|
||||
if mapped.ValueType() != Invalid {
|
||||
if mapped.ValueType() != InvalidValue {
|
||||
mappedAll[any.val.Type().Field(i).Name] = mapped
|
||||
}
|
||||
}
|
||||
}
|
||||
return wrapMap(mappedAll)
|
||||
} else {
|
||||
return newInvalidAny(path)
|
||||
}
|
||||
return newInvalidAny(path)
|
||||
default:
|
||||
return newInvalidAny(path)
|
||||
}
|
||||
@ -321,17 +249,6 @@ func (any *objectAny) Size() int {
|
||||
return any.val.NumField()
|
||||
}
|
||||
|
||||
func (any *objectAny) GetObject() map[string]Any {
|
||||
object := map[string]Any{}
|
||||
for i := 0; i < any.val.NumField(); i++ {
|
||||
field := any.val.Field(i)
|
||||
if field.CanInterface() {
|
||||
object[any.val.Type().Field(i).Name] = Wrap(field.Interface())
|
||||
}
|
||||
}
|
||||
return object
|
||||
}
|
||||
|
||||
func (any *objectAny) WriteTo(stream *Stream) {
|
||||
stream.WriteVal(any.val)
|
||||
}
|
||||
@ -351,7 +268,7 @@ func wrapMap(val interface{}) *mapAny {
|
||||
}
|
||||
|
||||
func (any *mapAny) ValueType() ValueType {
|
||||
return Object
|
||||
return ObjectValue
|
||||
}
|
||||
|
||||
func (any *mapAny) MustBeValid() Any {
|
||||
@ -367,63 +284,39 @@ func (any *mapAny) LastError() error {
|
||||
}
|
||||
|
||||
func (any *mapAny) ToBool() bool {
|
||||
return any.val.Len() != 0
|
||||
return true
|
||||
}
|
||||
|
||||
func (any *mapAny) ToInt() int {
|
||||
if any.val.Len() == 0 {
|
||||
return 0
|
||||
}
|
||||
return 1
|
||||
return 0
|
||||
}
|
||||
|
||||
func (any *mapAny) ToInt32() int32 {
|
||||
if any.val.Len() == 0 {
|
||||
return 0
|
||||
}
|
||||
return 1
|
||||
return 0
|
||||
}
|
||||
|
||||
func (any *mapAny) ToInt64() int64 {
|
||||
if any.val.Len() == 0 {
|
||||
return 0
|
||||
}
|
||||
return 1
|
||||
return 0
|
||||
}
|
||||
|
||||
func (any *mapAny) ToUint() uint {
|
||||
if any.val.Len() == 0 {
|
||||
return 0
|
||||
}
|
||||
return 1
|
||||
return 0
|
||||
}
|
||||
|
||||
func (any *mapAny) ToUint32() uint32 {
|
||||
if any.val.Len() == 0 {
|
||||
return 0
|
||||
}
|
||||
return 1
|
||||
return 0
|
||||
}
|
||||
|
||||
func (any *mapAny) ToUint64() uint64 {
|
||||
if any.val.Len() == 0 {
|
||||
return 0
|
||||
}
|
||||
return 1
|
||||
return 0
|
||||
}
|
||||
|
||||
func (any *mapAny) ToFloat32() float32 {
|
||||
if any.val.Len() == 0 {
|
||||
return 0
|
||||
}
|
||||
return 1
|
||||
return 0
|
||||
}
|
||||
|
||||
func (any *mapAny) ToFloat64() float64 {
|
||||
if any.val.Len() == 0 {
|
||||
return 0
|
||||
}
|
||||
return 1
|
||||
return 0
|
||||
}
|
||||
|
||||
func (any *mapAny) ToString() string {
|
||||
@ -444,14 +337,13 @@ func (any *mapAny) Get(path ...interface{}) Any {
|
||||
keyAsStr := key.String()
|
||||
element := Wrap(any.val.MapIndex(key).Interface())
|
||||
mapped := element.Get(path[1:]...)
|
||||
if mapped.ValueType() != Invalid {
|
||||
if mapped.ValueType() != InvalidValue {
|
||||
mappedAll[keyAsStr] = mapped
|
||||
}
|
||||
}
|
||||
return wrapMap(mappedAll)
|
||||
} else {
|
||||
return newInvalidAny(path)
|
||||
}
|
||||
return newInvalidAny(path)
|
||||
default:
|
||||
value := any.val.MapIndex(reflect.ValueOf(firstPath))
|
||||
if !value.IsValid() {
|
||||
@ -473,16 +365,6 @@ func (any *mapAny) Size() int {
|
||||
return any.val.Len()
|
||||
}
|
||||
|
||||
func (any *mapAny) GetObject() map[string]Any {
|
||||
object := map[string]Any{}
|
||||
for _, key := range any.val.MapKeys() {
|
||||
keyAsStr := key.String()
|
||||
element := Wrap(any.val.MapIndex(key).Interface())
|
||||
object[keyAsStr] = element
|
||||
}
|
||||
return object
|
||||
}
|
||||
|
||||
func (any *mapAny) WriteTo(stream *Stream) {
|
||||
stream.WriteVal(any.val)
|
||||
}
|
||||
|
@ -22,7 +22,7 @@ func (any *stringAny) Parse() *Iterator {
|
||||
}
|
||||
|
||||
func (any *stringAny) ValueType() ValueType {
|
||||
return String
|
||||
return StringValue
|
||||
}
|
||||
|
||||
func (any *stringAny) MustBeValid() Any {
|
||||
@ -35,7 +35,7 @@ func (any *stringAny) LastError() error {
|
||||
|
||||
func (any *stringAny) ToBool() bool {
|
||||
str := any.ToString()
|
||||
if str == "false" {
|
||||
if str == "0" {
|
||||
return false
|
||||
}
|
||||
for _, c := range str {
|
||||
@ -49,42 +49,107 @@ func (any *stringAny) ToBool() bool {
|
||||
}
|
||||
|
||||
func (any *stringAny) ToInt() int {
|
||||
parsed, _ := strconv.ParseInt(any.val, 10, 64)
|
||||
return int(parsed)
|
||||
return int(any.ToInt64())
|
||||
|
||||
}
|
||||
|
||||
func (any *stringAny) ToInt32() int32 {
|
||||
parsed, _ := strconv.ParseInt(any.val, 10, 32)
|
||||
return int32(parsed)
|
||||
return int32(any.ToInt64())
|
||||
}
|
||||
|
||||
func (any *stringAny) ToInt64() int64 {
|
||||
parsed, _ := strconv.ParseInt(any.val, 10, 64)
|
||||
return parsed
|
||||
if any.val == "" {
|
||||
return 0
|
||||
}
|
||||
|
||||
flag := 1
|
||||
startPos := 0
|
||||
endPos := 0
|
||||
if any.val[0] == '+' || any.val[0] == '-' {
|
||||
startPos = 1
|
||||
}
|
||||
|
||||
if any.val[0] == '-' {
|
||||
flag = -1
|
||||
}
|
||||
|
||||
for i := startPos; i < len(any.val); i++ {
|
||||
if any.val[i] >= '0' && any.val[i] <= '9' {
|
||||
endPos = i + 1
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
parsed, _ := strconv.ParseInt(any.val[startPos:endPos], 10, 64)
|
||||
return int64(flag) * parsed
|
||||
}
|
||||
|
||||
func (any *stringAny) ToUint() uint {
|
||||
parsed, _ := strconv.ParseUint(any.val, 10, 64)
|
||||
return uint(parsed)
|
||||
return uint(any.ToUint64())
|
||||
}
|
||||
|
||||
func (any *stringAny) ToUint32() uint32 {
|
||||
parsed, _ := strconv.ParseUint(any.val, 10, 32)
|
||||
return uint32(parsed)
|
||||
return uint32(any.ToUint64())
|
||||
}
|
||||
|
||||
func (any *stringAny) ToUint64() uint64 {
|
||||
parsed, _ := strconv.ParseUint(any.val, 10, 64)
|
||||
if any.val == "" {
|
||||
return 0
|
||||
}
|
||||
|
||||
startPos := 0
|
||||
endPos := 0
|
||||
|
||||
if any.val[0] == '-' {
|
||||
return 0
|
||||
}
|
||||
if any.val[0] == '+' {
|
||||
startPos = 1
|
||||
}
|
||||
|
||||
for i := startPos; i < len(any.val); i++ {
|
||||
if any.val[i] >= '0' && any.val[i] <= '9' {
|
||||
endPos = i + 1
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
parsed, _ := strconv.ParseUint(any.val[startPos:endPos], 10, 64)
|
||||
return parsed
|
||||
}
|
||||
|
||||
func (any *stringAny) ToFloat32() float32 {
|
||||
parsed, _ := strconv.ParseFloat(any.val, 32)
|
||||
return float32(parsed)
|
||||
return float32(any.ToFloat64())
|
||||
}
|
||||
|
||||
func (any *stringAny) ToFloat64() float64 {
|
||||
parsed, _ := strconv.ParseFloat(any.val, 64)
|
||||
if len(any.val) == 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
// first char invalid
|
||||
if any.val[0] != '+' && any.val[0] != '-' && (any.val[0] > '9' || any.val[0] < '0') {
|
||||
return 0
|
||||
}
|
||||
|
||||
// extract valid num expression from string
|
||||
// eg 123true => 123, -12.12xxa => -12.12
|
||||
endPos := 1
|
||||
for i := 1; i < len(any.val); i++ {
|
||||
if any.val[i] == '.' || any.val[i] == 'e' || any.val[i] == 'E' || any.val[i] == '+' || any.val[i] == '-' {
|
||||
endPos = i + 1
|
||||
continue
|
||||
}
|
||||
|
||||
// end position is the first char which is not digit
|
||||
if any.val[i] >= '0' && any.val[i] <= '9' {
|
||||
endPos = i + 1
|
||||
} else {
|
||||
endPos = i
|
||||
break
|
||||
}
|
||||
}
|
||||
parsed, _ := strconv.ParseFloat(any.val[:endPos], 64)
|
||||
return parsed
|
||||
}
|
||||
|
||||
|
@ -14,7 +14,7 @@ func (any *uint32Any) LastError() error {
|
||||
}
|
||||
|
||||
func (any *uint32Any) ValueType() ValueType {
|
||||
return Number
|
||||
return NumberValue
|
||||
}
|
||||
|
||||
func (any *uint32Any) MustBeValid() Any {
|
||||
|
@ -14,7 +14,7 @@ func (any *uint64Any) LastError() error {
|
||||
}
|
||||
|
||||
func (any *uint64Any) ValueType() ValueType {
|
||||
return Number
|
||||
return NumberValue
|
||||
}
|
||||
|
||||
func (any *uint64Any) MustBeValid() Any {
|
||||
|
@ -5,30 +5,29 @@ import (
|
||||
"errors"
|
||||
"io"
|
||||
"reflect"
|
||||
"sync/atomic"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// Config customize how the API should behave.
|
||||
// The API is created from Config by Froze.
|
||||
type Config struct {
|
||||
IndentionStep int
|
||||
MarshalFloatWith6Digits bool
|
||||
EscapeHtml bool
|
||||
SortMapKeys bool
|
||||
UseNumber bool
|
||||
IndentionStep int
|
||||
MarshalFloatWith6Digits bool
|
||||
EscapeHTML bool
|
||||
SortMapKeys bool
|
||||
UseNumber bool
|
||||
DisallowUnknownFields bool
|
||||
TagKey string
|
||||
OnlyTaggedField bool
|
||||
ValidateJsonRawMessage bool
|
||||
ObjectFieldMustBeSimpleString bool
|
||||
}
|
||||
|
||||
type frozenConfig struct {
|
||||
configBeforeFrozen Config
|
||||
sortMapKeys bool
|
||||
indentionStep int
|
||||
decoderCache unsafe.Pointer
|
||||
encoderCache unsafe.Pointer
|
||||
extensions []Extension
|
||||
streamPool chan *Stream
|
||||
iteratorPool chan *Iterator
|
||||
}
|
||||
|
||||
type Api interface {
|
||||
// API the public interface of this package.
|
||||
// Primary Marshal and Unmarshal.
|
||||
type API interface {
|
||||
IteratorPool
|
||||
StreamPool
|
||||
MarshalToString(v interface{}) (string, error)
|
||||
Marshal(v interface{}) ([]byte, error)
|
||||
MarshalIndent(v interface{}, prefix, indent string) ([]byte, error)
|
||||
@ -37,57 +36,103 @@ type Api interface {
|
||||
Get(data []byte, path ...interface{}) Any
|
||||
NewEncoder(writer io.Writer) *Encoder
|
||||
NewDecoder(reader io.Reader) *Decoder
|
||||
Valid(data []byte) bool
|
||||
RegisterExtension(extension Extension)
|
||||
}
|
||||
|
||||
// ConfigDefault the default API
|
||||
var ConfigDefault = Config{
|
||||
EscapeHtml: true,
|
||||
EscapeHTML: true,
|
||||
}.Froze()
|
||||
|
||||
// Trying to be 100% compatible with standard library behavior
|
||||
// ConfigCompatibleWithStandardLibrary tries to be 100% compatible with standard library behavior
|
||||
var ConfigCompatibleWithStandardLibrary = Config{
|
||||
EscapeHtml: true,
|
||||
SortMapKeys: true,
|
||||
EscapeHTML: true,
|
||||
SortMapKeys: true,
|
||||
ValidateJsonRawMessage: true,
|
||||
}.Froze()
|
||||
|
||||
// ConfigFastest marshals float with only 6 digits precision
|
||||
var ConfigFastest = Config{
|
||||
EscapeHtml: false,
|
||||
MarshalFloatWith6Digits: true,
|
||||
EscapeHTML: false,
|
||||
MarshalFloatWith6Digits: true, // will lose precession
|
||||
ObjectFieldMustBeSimpleString: true, // do not unescape object field
|
||||
}.Froze()
|
||||
|
||||
func (cfg Config) Froze() *frozenConfig {
|
||||
// TODO: cache frozen config
|
||||
frozenConfig := &frozenConfig{
|
||||
sortMapKeys: cfg.SortMapKeys,
|
||||
indentionStep: cfg.IndentionStep,
|
||||
streamPool: make(chan *Stream, 16),
|
||||
iteratorPool: make(chan *Iterator, 16),
|
||||
// Froze forge API from config
|
||||
func (cfg Config) Froze() API {
|
||||
api := &frozenConfig{
|
||||
sortMapKeys: cfg.SortMapKeys,
|
||||
indentionStep: cfg.IndentionStep,
|
||||
objectFieldMustBeSimpleString: cfg.ObjectFieldMustBeSimpleString,
|
||||
onlyTaggedField: cfg.OnlyTaggedField,
|
||||
disallowUnknownFields: cfg.DisallowUnknownFields,
|
||||
streamPool: make(chan *Stream, 16),
|
||||
iteratorPool: make(chan *Iterator, 16),
|
||||
}
|
||||
atomic.StorePointer(&frozenConfig.decoderCache, unsafe.Pointer(&map[string]ValDecoder{}))
|
||||
atomic.StorePointer(&frozenConfig.encoderCache, unsafe.Pointer(&map[string]ValEncoder{}))
|
||||
api.initCache()
|
||||
if cfg.MarshalFloatWith6Digits {
|
||||
frozenConfig.marshalFloatWith6Digits()
|
||||
api.marshalFloatWith6Digits()
|
||||
}
|
||||
if cfg.EscapeHtml {
|
||||
frozenConfig.escapeHtml()
|
||||
if cfg.EscapeHTML {
|
||||
api.escapeHTML()
|
||||
}
|
||||
if cfg.UseNumber {
|
||||
frozenConfig.useNumber()
|
||||
api.useNumber()
|
||||
}
|
||||
frozenConfig.configBeforeFrozen = cfg
|
||||
return frozenConfig
|
||||
if cfg.ValidateJsonRawMessage {
|
||||
api.validateJsonRawMessage()
|
||||
}
|
||||
api.configBeforeFrozen = cfg
|
||||
return api
|
||||
}
|
||||
|
||||
func (cfg Config) frozeWithCacheReuse() *frozenConfig {
|
||||
api := getFrozenConfigFromCache(cfg)
|
||||
if api != nil {
|
||||
return api
|
||||
}
|
||||
api = cfg.Froze().(*frozenConfig)
|
||||
addFrozenConfigToCache(cfg, api)
|
||||
return api
|
||||
}
|
||||
|
||||
func (cfg *frozenConfig) validateJsonRawMessage() {
|
||||
encoder := &funcEncoder{func(ptr unsafe.Pointer, stream *Stream) {
|
||||
rawMessage := *(*json.RawMessage)(ptr)
|
||||
iter := cfg.BorrowIterator([]byte(rawMessage))
|
||||
iter.Read()
|
||||
if iter.Error != nil {
|
||||
stream.WriteRaw("null")
|
||||
} else {
|
||||
cfg.ReturnIterator(iter)
|
||||
stream.WriteRaw(string(rawMessage))
|
||||
}
|
||||
}, func(ptr unsafe.Pointer) bool {
|
||||
return false
|
||||
}}
|
||||
cfg.addEncoderToCache(reflect.TypeOf((*json.RawMessage)(nil)).Elem(), encoder)
|
||||
cfg.addEncoderToCache(reflect.TypeOf((*RawMessage)(nil)).Elem(), encoder)
|
||||
}
|
||||
|
||||
func (cfg *frozenConfig) useNumber() {
|
||||
cfg.addDecoderToCache(reflect.TypeOf((*interface{})(nil)).Elem(), &funcDecoder{func(ptr unsafe.Pointer, iter *Iterator) {
|
||||
if iter.WhatIsNext() == Number {
|
||||
if iter.WhatIsNext() == NumberValue {
|
||||
*((*interface{})(ptr)) = json.Number(iter.readNumberAsString())
|
||||
} else {
|
||||
*((*interface{})(ptr)) = iter.Read()
|
||||
}
|
||||
}})
|
||||
}
|
||||
func (cfg *frozenConfig) getTagKey() string {
|
||||
tagKey := cfg.configBeforeFrozen.TagKey
|
||||
if tagKey == "" {
|
||||
return "json"
|
||||
}
|
||||
return tagKey
|
||||
}
|
||||
|
||||
func (cfg *frozenConfig) registerExtension(extension Extension) {
|
||||
func (cfg *frozenConfig) RegisterExtension(extension Extension) {
|
||||
cfg.extensions = append(cfg.extensions, extension)
|
||||
}
|
||||
|
||||
@ -134,7 +179,7 @@ type htmlEscapedStringEncoder struct {
|
||||
|
||||
func (encoder *htmlEscapedStringEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||
str := *((*string)(ptr))
|
||||
stream.WriteStringWithHtmlEscaped(str)
|
||||
stream.WriteStringWithHTMLEscaped(str)
|
||||
}
|
||||
|
||||
func (encoder *htmlEscapedStringEncoder) EncodeInterface(val interface{}, stream *Stream) {
|
||||
@ -145,62 +190,20 @@ func (encoder *htmlEscapedStringEncoder) IsEmpty(ptr unsafe.Pointer) bool {
|
||||
return *((*string)(ptr)) == ""
|
||||
}
|
||||
|
||||
func (cfg *frozenConfig) escapeHtml() {
|
||||
func (cfg *frozenConfig) escapeHTML() {
|
||||
cfg.addEncoderToCache(reflect.TypeOf((*string)(nil)).Elem(), &htmlEscapedStringEncoder{})
|
||||
}
|
||||
|
||||
func (cfg *frozenConfig) addDecoderToCache(cacheKey reflect.Type, decoder ValDecoder) {
|
||||
done := false
|
||||
for !done {
|
||||
ptr := atomic.LoadPointer(&cfg.decoderCache)
|
||||
cache := *(*map[reflect.Type]ValDecoder)(ptr)
|
||||
copied := map[reflect.Type]ValDecoder{}
|
||||
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 ValEncoder) {
|
||||
done := false
|
||||
for !done {
|
||||
ptr := atomic.LoadPointer(&cfg.encoderCache)
|
||||
cache := *(*map[reflect.Type]ValEncoder)(ptr)
|
||||
copied := map[reflect.Type]ValEncoder{}
|
||||
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) ValDecoder {
|
||||
ptr := atomic.LoadPointer(&cfg.decoderCache)
|
||||
cache := *(*map[reflect.Type]ValDecoder)(ptr)
|
||||
return cache[cacheKey]
|
||||
}
|
||||
|
||||
func (cfg *frozenConfig) getEncoderFromCache(cacheKey reflect.Type) ValEncoder {
|
||||
ptr := atomic.LoadPointer(&cfg.encoderCache)
|
||||
cache := *(*map[reflect.Type]ValEncoder)(ptr)
|
||||
return cache[cacheKey]
|
||||
}
|
||||
|
||||
// cleanDecoders cleans decoders registered or cached
|
||||
func (cfg *frozenConfig) cleanDecoders() {
|
||||
typeDecoders = map[string]ValDecoder{}
|
||||
fieldDecoders = map[string]ValDecoder{}
|
||||
*cfg = *cfg.configBeforeFrozen.Froze()
|
||||
*cfg = *(cfg.configBeforeFrozen.Froze().(*frozenConfig))
|
||||
}
|
||||
|
||||
// cleanEncoders cleans encoders registered or cached
|
||||
func (cfg *frozenConfig) cleanEncoders() {
|
||||
typeEncoders = map[string]ValEncoder{}
|
||||
fieldEncoders = map[string]ValEncoder{}
|
||||
*cfg = *cfg.configBeforeFrozen.Froze()
|
||||
*cfg = *(cfg.configBeforeFrozen.Froze().(*frozenConfig))
|
||||
}
|
||||
|
||||
func (cfg *frozenConfig) MarshalToString(v interface{}) (string, error) {
|
||||
@ -237,7 +240,7 @@ func (cfg *frozenConfig) MarshalIndent(v interface{}, prefix, indent string) ([]
|
||||
}
|
||||
newCfg := cfg.configBeforeFrozen
|
||||
newCfg.IndentionStep = len(indent)
|
||||
return newCfg.Froze().Marshal(v)
|
||||
return newCfg.frozeWithCacheReuse().Marshal(v)
|
||||
}
|
||||
|
||||
func (cfg *frozenConfig) UnmarshalFromString(str string, v interface{}) error {
|
||||
@ -295,3 +298,10 @@ func (cfg *frozenConfig) NewDecoder(reader io.Reader) *Decoder {
|
||||
iter := Parse(cfg, reader, 512)
|
||||
return &Decoder{iter}
|
||||
}
|
||||
|
||||
func (cfg *frozenConfig) Valid(data []byte) bool {
|
||||
iter := cfg.BorrowIterator(data)
|
||||
defer cfg.ReturnIterator(iter)
|
||||
iter.Skip()
|
||||
return iter.Error == nil
|
||||
}
|
||||
|
65
feature_config_with_sync_map.go
Normal file
65
feature_config_with_sync_map.go
Normal file
@ -0,0 +1,65 @@
|
||||
//+build go1.9
|
||||
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type frozenConfig struct {
|
||||
configBeforeFrozen Config
|
||||
sortMapKeys bool
|
||||
indentionStep int
|
||||
objectFieldMustBeSimpleString bool
|
||||
onlyTaggedField bool
|
||||
disallowUnknownFields bool
|
||||
decoderCache sync.Map
|
||||
encoderCache sync.Map
|
||||
extensions []Extension
|
||||
streamPool chan *Stream
|
||||
iteratorPool chan *Iterator
|
||||
}
|
||||
|
||||
func (cfg *frozenConfig) initCache() {
|
||||
cfg.decoderCache = sync.Map{}
|
||||
cfg.encoderCache = sync.Map{}
|
||||
}
|
||||
|
||||
func (cfg *frozenConfig) addDecoderToCache(cacheKey reflect.Type, decoder ValDecoder) {
|
||||
cfg.decoderCache.Store(cacheKey, decoder)
|
||||
}
|
||||
|
||||
func (cfg *frozenConfig) addEncoderToCache(cacheKey reflect.Type, encoder ValEncoder) {
|
||||
cfg.encoderCache.Store(cacheKey, encoder)
|
||||
}
|
||||
|
||||
func (cfg *frozenConfig) getDecoderFromCache(cacheKey reflect.Type) ValDecoder {
|
||||
decoder, found := cfg.decoderCache.Load(cacheKey)
|
||||
if found {
|
||||
return decoder.(ValDecoder)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cfg *frozenConfig) getEncoderFromCache(cacheKey reflect.Type) ValEncoder {
|
||||
encoder, found := cfg.encoderCache.Load(cacheKey)
|
||||
if found {
|
||||
return encoder.(ValEncoder)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var cfgCache = &sync.Map{}
|
||||
|
||||
func getFrozenConfigFromCache(cfg Config) *frozenConfig {
|
||||
obj, found := cfgCache.Load(cfg)
|
||||
if found {
|
||||
return obj.(*frozenConfig)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func addFrozenConfigToCache(cfg Config, frozenConfig *frozenConfig) {
|
||||
cfgCache.Store(cfg, frozenConfig)
|
||||
}
|
71
feature_config_without_sync_map.go
Normal file
71
feature_config_without_sync_map.go
Normal file
@ -0,0 +1,71 @@
|
||||
//+build !go1.9
|
||||
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type frozenConfig struct {
|
||||
configBeforeFrozen Config
|
||||
sortMapKeys bool
|
||||
indentionStep int
|
||||
objectFieldMustBeSimpleString bool
|
||||
onlyTaggedField bool
|
||||
disallowUnknownFields bool
|
||||
cacheLock *sync.RWMutex
|
||||
decoderCache map[reflect.Type]ValDecoder
|
||||
encoderCache map[reflect.Type]ValEncoder
|
||||
extensions []Extension
|
||||
streamPool chan *Stream
|
||||
iteratorPool chan *Iterator
|
||||
}
|
||||
|
||||
func (cfg *frozenConfig) initCache() {
|
||||
cfg.cacheLock = &sync.RWMutex{}
|
||||
cfg.decoderCache = map[reflect.Type]ValDecoder{}
|
||||
cfg.encoderCache = map[reflect.Type]ValEncoder{}
|
||||
}
|
||||
|
||||
func (cfg *frozenConfig) addDecoderToCache(cacheKey reflect.Type, decoder ValDecoder) {
|
||||
cfg.cacheLock.Lock()
|
||||
cfg.decoderCache[cacheKey] = decoder
|
||||
cfg.cacheLock.Unlock()
|
||||
}
|
||||
|
||||
func (cfg *frozenConfig) addEncoderToCache(cacheKey reflect.Type, encoder ValEncoder) {
|
||||
cfg.cacheLock.Lock()
|
||||
cfg.encoderCache[cacheKey] = encoder
|
||||
cfg.cacheLock.Unlock()
|
||||
}
|
||||
|
||||
func (cfg *frozenConfig) getDecoderFromCache(cacheKey reflect.Type) ValDecoder {
|
||||
cfg.cacheLock.RLock()
|
||||
decoder, _ := cfg.decoderCache[cacheKey].(ValDecoder)
|
||||
cfg.cacheLock.RUnlock()
|
||||
return decoder
|
||||
}
|
||||
|
||||
func (cfg *frozenConfig) getEncoderFromCache(cacheKey reflect.Type) ValEncoder {
|
||||
cfg.cacheLock.RLock()
|
||||
encoder, _ := cfg.encoderCache[cacheKey].(ValEncoder)
|
||||
cfg.cacheLock.RUnlock()
|
||||
return encoder
|
||||
}
|
||||
|
||||
var cfgCacheLock = &sync.RWMutex{}
|
||||
var cfgCache = map[Config]*frozenConfig{}
|
||||
|
||||
func getFrozenConfigFromCache(cfg Config) *frozenConfig {
|
||||
cfgCacheLock.RLock()
|
||||
frozenConfig := cfgCache[cfg]
|
||||
cfgCacheLock.RUnlock()
|
||||
return frozenConfig
|
||||
}
|
||||
|
||||
func addFrozenConfigToCache(cfg Config, frozenConfig *frozenConfig) {
|
||||
cfgCacheLock.Lock()
|
||||
cfgCache[cfg] = frozenConfig
|
||||
cfgCacheLock.Unlock()
|
||||
}
|
168
feature_iter.go
168
feature_iter.go
@ -1,26 +1,29 @@
|
||||
//
|
||||
// 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 (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
|
||||
// ValueType the type for JSON element
|
||||
type ValueType int
|
||||
|
||||
const (
|
||||
Invalid ValueType = iota
|
||||
String
|
||||
Number
|
||||
Nil
|
||||
Bool
|
||||
Array
|
||||
Object
|
||||
// InvalidValue invalid JSON element
|
||||
InvalidValue ValueType = iota
|
||||
// StringValue JSON element "string"
|
||||
StringValue
|
||||
// NumberValue JSON element 100 or 0.10
|
||||
NumberValue
|
||||
// NilValue JSON element null
|
||||
NilValue
|
||||
// BoolValue JSON element true or false
|
||||
BoolValue
|
||||
// ArrayValue JSON element []
|
||||
ArrayValue
|
||||
// ObjectValue JSON element {}
|
||||
ObjectValue
|
||||
)
|
||||
|
||||
var hexDigits []byte
|
||||
@ -42,28 +45,29 @@ func init() {
|
||||
}
|
||||
valueTypes = make([]ValueType, 256)
|
||||
for i := 0; i < len(valueTypes); i++ {
|
||||
valueTypes[i] = Invalid
|
||||
valueTypes[i] = InvalidValue
|
||||
}
|
||||
valueTypes['"'] = String
|
||||
valueTypes['-'] = Number
|
||||
valueTypes['0'] = Number
|
||||
valueTypes['1'] = Number
|
||||
valueTypes['2'] = Number
|
||||
valueTypes['3'] = Number
|
||||
valueTypes['4'] = Number
|
||||
valueTypes['5'] = Number
|
||||
valueTypes['6'] = Number
|
||||
valueTypes['7'] = Number
|
||||
valueTypes['8'] = Number
|
||||
valueTypes['9'] = Number
|
||||
valueTypes['t'] = Bool
|
||||
valueTypes['f'] = Bool
|
||||
valueTypes['n'] = Nil
|
||||
valueTypes['['] = Array
|
||||
valueTypes['{'] = Object
|
||||
valueTypes['"'] = StringValue
|
||||
valueTypes['-'] = NumberValue
|
||||
valueTypes['0'] = NumberValue
|
||||
valueTypes['1'] = NumberValue
|
||||
valueTypes['2'] = NumberValue
|
||||
valueTypes['3'] = NumberValue
|
||||
valueTypes['4'] = NumberValue
|
||||
valueTypes['5'] = NumberValue
|
||||
valueTypes['6'] = NumberValue
|
||||
valueTypes['7'] = NumberValue
|
||||
valueTypes['8'] = NumberValue
|
||||
valueTypes['9'] = NumberValue
|
||||
valueTypes['t'] = BoolValue
|
||||
valueTypes['f'] = BoolValue
|
||||
valueTypes['n'] = NilValue
|
||||
valueTypes['['] = ArrayValue
|
||||
valueTypes['{'] = ObjectValue
|
||||
}
|
||||
|
||||
// Iterator is a fast and flexible JSON parser
|
||||
// Iterator is a io.Reader like object, with JSON specific read functions.
|
||||
// Error is not returned as return value, but stored as Error member on this iterator instance.
|
||||
type Iterator struct {
|
||||
cfg *frozenConfig
|
||||
reader io.Reader
|
||||
@ -73,12 +77,13 @@ type Iterator struct {
|
||||
captureStartedAt int
|
||||
captured []byte
|
||||
Error error
|
||||
Attachment interface{} // open for customized decoder
|
||||
}
|
||||
|
||||
// Create creates an empty Iterator instance
|
||||
func NewIterator(cfg *frozenConfig) *Iterator {
|
||||
// NewIterator creates an empty Iterator instance
|
||||
func NewIterator(cfg API) *Iterator {
|
||||
return &Iterator{
|
||||
cfg: cfg,
|
||||
cfg: cfg.(*frozenConfig),
|
||||
reader: nil,
|
||||
buf: nil,
|
||||
head: 0,
|
||||
@ -86,10 +91,10 @@ func NewIterator(cfg *frozenConfig) *Iterator {
|
||||
}
|
||||
}
|
||||
|
||||
// Parse parses a json buffer in io.Reader into an Iterator instance
|
||||
func Parse(cfg *frozenConfig, reader io.Reader, bufSize int) *Iterator {
|
||||
// Parse creates an Iterator instance from io.Reader
|
||||
func Parse(cfg API, reader io.Reader, bufSize int) *Iterator {
|
||||
return &Iterator{
|
||||
cfg: cfg,
|
||||
cfg: cfg.(*frozenConfig),
|
||||
reader: reader,
|
||||
buf: make([]byte, bufSize),
|
||||
head: 0,
|
||||
@ -97,10 +102,10 @@ func Parse(cfg *frozenConfig, reader io.Reader, bufSize int) *Iterator {
|
||||
}
|
||||
}
|
||||
|
||||
// ParseBytes parses a json byte slice into an Iterator instance
|
||||
func ParseBytes(cfg *frozenConfig, input []byte) *Iterator {
|
||||
// ParseBytes creates an Iterator instance from byte array
|
||||
func ParseBytes(cfg API, input []byte) *Iterator {
|
||||
return &Iterator{
|
||||
cfg: cfg,
|
||||
cfg: cfg.(*frozenConfig),
|
||||
reader: nil,
|
||||
buf: input,
|
||||
head: 0,
|
||||
@ -108,16 +113,17 @@ func ParseBytes(cfg *frozenConfig, input []byte) *Iterator {
|
||||
}
|
||||
}
|
||||
|
||||
// ParseString parses a json string into an Iterator instance
|
||||
func ParseString(cfg *frozenConfig, input string) *Iterator {
|
||||
// ParseString creates an Iterator instance from string
|
||||
func ParseString(cfg API, input string) *Iterator {
|
||||
return ParseBytes(cfg, []byte(input))
|
||||
}
|
||||
|
||||
func (iter *Iterator) Config() *frozenConfig {
|
||||
// Pool returns a pool can provide more iterator with same configuration
|
||||
func (iter *Iterator) Pool() IteratorPool {
|
||||
return iter.cfg
|
||||
}
|
||||
|
||||
// Reset can reset an Iterator instance for another json buffer in io.Reader
|
||||
// Reset reuse iterator instance by specifying another reader
|
||||
func (iter *Iterator) Reset(reader io.Reader) *Iterator {
|
||||
iter.reader = reader
|
||||
iter.head = 0
|
||||
@ -125,7 +131,7 @@ func (iter *Iterator) Reset(reader io.Reader) *Iterator {
|
||||
return iter
|
||||
}
|
||||
|
||||
// ResetBytes can reset an Iterator instance for another json byte slice
|
||||
// ResetBytes reuse iterator instance by specifying another byte array as input
|
||||
func (iter *Iterator) ResetBytes(input []byte) *Iterator {
|
||||
iter.reader = nil
|
||||
iter.buf = input
|
||||
@ -134,7 +140,7 @@ func (iter *Iterator) ResetBytes(input []byte) *Iterator {
|
||||
return iter
|
||||
}
|
||||
|
||||
// WhatIsNext gets ValueType of relatively next json object
|
||||
// WhatIsNext gets ValueType of relatively next json element
|
||||
func (iter *Iterator) WhatIsNext() ValueType {
|
||||
valueType := valueTypes[iter.nextToken()]
|
||||
iter.unreadByte()
|
||||
@ -154,6 +160,18 @@ func (iter *Iterator) skipWhitespacesWithoutLoadMore() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (iter *Iterator) isObjectEnd() bool {
|
||||
c := iter.nextToken()
|
||||
if c == ',' {
|
||||
return false
|
||||
}
|
||||
if c == '}' {
|
||||
return true
|
||||
}
|
||||
iter.ReportError("isObjectEnd", "object ended prematurely, unexpected char "+string([]byte{c}))
|
||||
return true
|
||||
}
|
||||
|
||||
func (iter *Iterator) nextToken() byte {
|
||||
// a variation of skip whitespaces, returning the next non-whitespace token
|
||||
for {
|
||||
@ -172,6 +190,7 @@ func (iter *Iterator) nextToken() byte {
|
||||
}
|
||||
}
|
||||
|
||||
// ReportError record a error in iterator instance with current position.
|
||||
func (iter *Iterator) ReportError(operation string, msg string) {
|
||||
if iter.Error != nil {
|
||||
if iter.Error != io.EOF {
|
||||
@ -182,17 +201,31 @@ func (iter *Iterator) ReportError(operation string, msg string) {
|
||||
if peekStart < 0 {
|
||||
peekStart = 0
|
||||
}
|
||||
iter.Error = fmt.Errorf("%s: %s, parsing %v ...%s... at %s", operation, msg, iter.head,
|
||||
string(iter.buf[peekStart:iter.head]), string(iter.buf[0:iter.tail]))
|
||||
peekEnd := iter.head + 10
|
||||
if peekEnd > iter.tail {
|
||||
peekEnd = iter.tail
|
||||
}
|
||||
parsing := string(iter.buf[peekStart:peekEnd])
|
||||
contextStart := iter.head - 50
|
||||
if contextStart < 0 {
|
||||
contextStart = 0
|
||||
}
|
||||
contextEnd := iter.head + 50
|
||||
if contextEnd > iter.tail {
|
||||
contextEnd = iter.tail
|
||||
}
|
||||
context := string(iter.buf[contextStart:contextEnd])
|
||||
iter.Error = fmt.Errorf("%s: %s, error found in #%v byte of ...|%s|..., bigger context ...|%s|...",
|
||||
operation, msg, iter.head-peekStart, parsing, context)
|
||||
}
|
||||
|
||||
// CurrentBuffer gets current buffer as string
|
||||
// CurrentBuffer gets current buffer as string for debugging purpose
|
||||
func (iter *Iterator) CurrentBuffer() string {
|
||||
peekStart := iter.head - 10
|
||||
if peekStart < 0 {
|
||||
peekStart = 0
|
||||
}
|
||||
return fmt.Sprintf("parsing %v ...|%s|... at %s", iter.head,
|
||||
return fmt.Sprintf("parsing #%v byte, around ...|%s|..., whole buffer ...|%s|...", iter.head,
|
||||
string(iter.buf[peekStart:iter.head]), string(iter.buf[0:iter.tail]))
|
||||
}
|
||||
|
||||
@ -218,7 +251,7 @@ func (iter *Iterator) loadMore() bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
if iter.captureStartedAt != -1 {
|
||||
if iter.captured != nil {
|
||||
iter.captured = append(iter.captured,
|
||||
iter.buf[iter.captureStartedAt:iter.tail]...)
|
||||
iter.captureStartedAt = 0
|
||||
@ -241,37 +274,44 @@ func (iter *Iterator) loadMore() bool {
|
||||
}
|
||||
|
||||
func (iter *Iterator) unreadByte() {
|
||||
if iter.head == 0 {
|
||||
iter.ReportError("unreadByte", "unread too many bytes")
|
||||
if iter.Error != nil {
|
||||
return
|
||||
}
|
||||
iter.head--
|
||||
return
|
||||
}
|
||||
|
||||
// Read read the next JSON element as generic interface{}.
|
||||
func (iter *Iterator) Read() interface{} {
|
||||
valueType := iter.WhatIsNext()
|
||||
switch valueType {
|
||||
case String:
|
||||
case StringValue:
|
||||
return iter.ReadString()
|
||||
case Number:
|
||||
case NumberValue:
|
||||
if iter.cfg.configBeforeFrozen.UseNumber {
|
||||
return json.Number(iter.readNumberAsString())
|
||||
}
|
||||
return iter.ReadFloat64()
|
||||
case Nil:
|
||||
iter.skipFixedBytes(4) // null
|
||||
case NilValue:
|
||||
iter.skipFourBytes('n', 'u', 'l', 'l')
|
||||
return nil
|
||||
case Bool:
|
||||
case BoolValue:
|
||||
return iter.ReadBool()
|
||||
case Array:
|
||||
case ArrayValue:
|
||||
arr := []interface{}{}
|
||||
iter.ReadArrayCB(func(iter *Iterator) bool {
|
||||
arr = append(arr, iter.Read())
|
||||
var elem interface{}
|
||||
iter.ReadVal(&elem)
|
||||
arr = append(arr, elem)
|
||||
return true
|
||||
})
|
||||
return arr
|
||||
case Object:
|
||||
case ObjectValue:
|
||||
obj := map[string]interface{}{}
|
||||
iter.ReadObjectCB(func(Iter *Iterator, field string) bool {
|
||||
obj[field] = iter.Read()
|
||||
iter.ReadMapCB(func(Iter *Iterator, field string) bool {
|
||||
var elem interface{}
|
||||
iter.ReadVal(&elem)
|
||||
obj[field] = elem
|
||||
return true
|
||||
})
|
||||
return obj
|
||||
|
@ -1,10 +1,11 @@
|
||||
package jsoniter
|
||||
|
||||
// ReadArray read array element, tells if the array has more element to read.
|
||||
func (iter *Iterator) ReadArray() (ret bool) {
|
||||
c := iter.nextToken()
|
||||
switch c {
|
||||
case 'n':
|
||||
iter.skipFixedBytes(3)
|
||||
iter.skipThreeBytes('u', 'l', 'l')
|
||||
return false // null
|
||||
case '[':
|
||||
c = iter.nextToken()
|
||||
@ -18,11 +19,12 @@ 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
|
||||
}
|
||||
}
|
||||
|
||||
// ReadArrayCB read array with callback
|
||||
func (iter *Iterator) ReadArrayCB(callback func(*Iterator) bool) (ret bool) {
|
||||
c := iter.nextToken()
|
||||
if c == '[' {
|
||||
@ -32,19 +34,25 @@ func (iter *Iterator) ReadArrayCB(callback func(*Iterator) bool) (ret bool) {
|
||||
if !callback(iter) {
|
||||
return false
|
||||
}
|
||||
for iter.nextToken() == ',' {
|
||||
c = iter.nextToken()
|
||||
for c == ',' {
|
||||
if !callback(iter) {
|
||||
return false
|
||||
}
|
||||
c = iter.nextToken()
|
||||
}
|
||||
if c != ']' {
|
||||
iter.ReportError("ReadArrayCB", "expect ] in the end, but found "+string([]byte{c}))
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
return true
|
||||
}
|
||||
if c == 'n' {
|
||||
iter.skipFixedBytes(3)
|
||||
iter.skipThreeBytes('u', 'l', 'l')
|
||||
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
|
||||
}
|
||||
|
@ -1,9 +1,11 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"math/big"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
@ -30,6 +32,7 @@ func init() {
|
||||
floatDigits['.'] = dotInNumber
|
||||
}
|
||||
|
||||
// ReadBigFloat read big.Float
|
||||
func (iter *Iterator) ReadBigFloat() (ret *big.Float) {
|
||||
str := iter.readNumberAsString()
|
||||
if iter.Error != nil && iter.Error != io.EOF {
|
||||
@ -47,6 +50,7 @@ func (iter *Iterator) ReadBigFloat() (ret *big.Float) {
|
||||
return val
|
||||
}
|
||||
|
||||
// ReadBigInt read big.Int
|
||||
func (iter *Iterator) ReadBigInt() (ret *big.Int) {
|
||||
str := iter.readNumberAsString()
|
||||
if iter.Error != nil && iter.Error != io.EOF {
|
||||
@ -62,20 +66,49 @@ func (iter *Iterator) ReadBigInt() (ret *big.Int) {
|
||||
return ret
|
||||
}
|
||||
|
||||
//ReadFloat32 read float32
|
||||
func (iter *Iterator) ReadFloat32() (ret float32) {
|
||||
c := iter.nextToken()
|
||||
if c == '-' {
|
||||
return -iter.readPositiveFloat32()
|
||||
} else {
|
||||
iter.unreadByte()
|
||||
return iter.readPositiveFloat32()
|
||||
}
|
||||
iter.unreadByte()
|
||||
return iter.readPositiveFloat32()
|
||||
}
|
||||
|
||||
func (iter *Iterator) readPositiveFloat32() (ret float32) {
|
||||
value := uint64(0)
|
||||
c := byte(' ')
|
||||
i := iter.head
|
||||
// first char
|
||||
if i == iter.tail {
|
||||
return iter.readFloat32SlowPath()
|
||||
}
|
||||
c = iter.buf[i]
|
||||
i++
|
||||
ind := floatDigits[c]
|
||||
switch ind {
|
||||
case invalidCharForNumber:
|
||||
return iter.readFloat32SlowPath()
|
||||
case endOfNumber:
|
||||
iter.ReportError("readFloat32", "empty number")
|
||||
return
|
||||
case dotInNumber:
|
||||
iter.ReportError("readFloat32", "leading dot is invalid")
|
||||
return
|
||||
case 0:
|
||||
if i == iter.tail {
|
||||
return iter.readFloat32SlowPath()
|
||||
}
|
||||
c = iter.buf[i]
|
||||
switch c {
|
||||
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
|
||||
iter.ReportError("readFloat32", "leading zero is invalid")
|
||||
return
|
||||
}
|
||||
}
|
||||
value = uint64(ind)
|
||||
// chars before dot
|
||||
non_decimal_loop:
|
||||
for ; i < iter.tail; i++ {
|
||||
c = iter.buf[i]
|
||||
@ -94,17 +127,21 @@ non_decimal_loop:
|
||||
}
|
||||
value = (value << 3) + (value << 1) + uint64(ind) // value = value * 10 + ind;
|
||||
}
|
||||
// chars after dot
|
||||
if c == '.' {
|
||||
i++
|
||||
decimalPlaces := 0
|
||||
if i == iter.tail {
|
||||
return iter.readFloat32SlowPath()
|
||||
}
|
||||
for ; i < iter.tail; i++ {
|
||||
c = iter.buf[i]
|
||||
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()
|
||||
@ -131,7 +168,7 @@ load_loop:
|
||||
for i := iter.head; i < iter.tail; i++ {
|
||||
c := iter.buf[i]
|
||||
switch c {
|
||||
case '-', '.', 'e', 'E', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
|
||||
case '+', '-', '.', 'e', 'E', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
|
||||
str = append(str, c)
|
||||
continue
|
||||
default:
|
||||
@ -157,6 +194,11 @@ func (iter *Iterator) readFloat32SlowPath() (ret float32) {
|
||||
if iter.Error != nil && iter.Error != io.EOF {
|
||||
return
|
||||
}
|
||||
errMsg := validateFloat(str)
|
||||
if errMsg != "" {
|
||||
iter.ReportError("readFloat32SlowPath", errMsg)
|
||||
return
|
||||
}
|
||||
val, err := strconv.ParseFloat(str, 32)
|
||||
if err != nil {
|
||||
iter.Error = err
|
||||
@ -165,20 +207,49 @@ func (iter *Iterator) readFloat32SlowPath() (ret float32) {
|
||||
return float32(val)
|
||||
}
|
||||
|
||||
// ReadFloat64 read float64
|
||||
func (iter *Iterator) ReadFloat64() (ret float64) {
|
||||
c := iter.nextToken()
|
||||
if c == '-' {
|
||||
return -iter.readPositiveFloat64()
|
||||
} else {
|
||||
iter.unreadByte()
|
||||
return iter.readPositiveFloat64()
|
||||
}
|
||||
iter.unreadByte()
|
||||
return iter.readPositiveFloat64()
|
||||
}
|
||||
|
||||
func (iter *Iterator) readPositiveFloat64() (ret float64) {
|
||||
value := uint64(0)
|
||||
c := byte(' ')
|
||||
i := iter.head
|
||||
// first char
|
||||
if i == iter.tail {
|
||||
return iter.readFloat64SlowPath()
|
||||
}
|
||||
c = iter.buf[i]
|
||||
i++
|
||||
ind := floatDigits[c]
|
||||
switch ind {
|
||||
case invalidCharForNumber:
|
||||
return iter.readFloat64SlowPath()
|
||||
case endOfNumber:
|
||||
iter.ReportError("readFloat64", "empty number")
|
||||
return
|
||||
case dotInNumber:
|
||||
iter.ReportError("readFloat64", "leading dot is invalid")
|
||||
return
|
||||
case 0:
|
||||
if i == iter.tail {
|
||||
return iter.readFloat64SlowPath()
|
||||
}
|
||||
c = iter.buf[i]
|
||||
switch c {
|
||||
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
|
||||
iter.ReportError("readFloat64", "leading zero is invalid")
|
||||
return
|
||||
}
|
||||
}
|
||||
value = uint64(ind)
|
||||
// chars before dot
|
||||
non_decimal_loop:
|
||||
for ; i < iter.tail; i++ {
|
||||
c = iter.buf[i]
|
||||
@ -197,17 +268,21 @@ non_decimal_loop:
|
||||
}
|
||||
value = (value << 3) + (value << 1) + uint64(ind) // value = value * 10 + ind;
|
||||
}
|
||||
// chars after dot
|
||||
if c == '.' {
|
||||
i++
|
||||
decimalPlaces := 0
|
||||
if i == iter.tail {
|
||||
return iter.readFloat64SlowPath()
|
||||
}
|
||||
for ; i < iter.tail; i++ {
|
||||
c = iter.buf[i]
|
||||
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()
|
||||
@ -231,6 +306,11 @@ func (iter *Iterator) readFloat64SlowPath() (ret float64) {
|
||||
if iter.Error != nil && iter.Error != io.EOF {
|
||||
return
|
||||
}
|
||||
errMsg := validateFloat(str)
|
||||
if errMsg != "" {
|
||||
iter.ReportError("readFloat64SlowPath", errMsg)
|
||||
return
|
||||
}
|
||||
val, err := strconv.ParseFloat(str, 64)
|
||||
if err != nil {
|
||||
iter.Error = err
|
||||
@ -238,3 +318,30 @@ func (iter *Iterator) readFloat64SlowPath() (ret float64) {
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
func validateFloat(str string) string {
|
||||
// strconv.ParseFloat is not validating `1.` or `1.e1`
|
||||
if len(str) == 0 {
|
||||
return "empty number"
|
||||
}
|
||||
if str[0] == '-' {
|
||||
return "-- is not valid"
|
||||
}
|
||||
dotPos := strings.IndexByte(str, '.')
|
||||
if dotPos != -1 {
|
||||
if dotPos == len(str)-1 {
|
||||
return "dot can not be last character"
|
||||
}
|
||||
switch str[dotPos+1] {
|
||||
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
|
||||
default:
|
||||
return "missing digit after dot"
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// ReadNumber read json.Number
|
||||
func (iter *Iterator) ReadNumber() (ret json.Number) {
|
||||
return json.Number(iter.readNumberAsString())
|
||||
}
|
||||
|
@ -20,14 +20,17 @@ func init() {
|
||||
}
|
||||
}
|
||||
|
||||
// ReadUint read uint
|
||||
func (iter *Iterator) ReadUint() uint {
|
||||
return uint(iter.ReadUint64())
|
||||
}
|
||||
|
||||
// ReadInt read int
|
||||
func (iter *Iterator) ReadInt() int {
|
||||
return int(iter.ReadInt64())
|
||||
}
|
||||
|
||||
// ReadInt8 read int8
|
||||
func (iter *Iterator) ReadInt8() (ret int8) {
|
||||
c := iter.nextToken()
|
||||
if c == '-' {
|
||||
@ -37,16 +40,16 @@ func (iter *Iterator) ReadInt8() (ret int8) {
|
||||
return
|
||||
}
|
||||
return -int8(val)
|
||||
} else {
|
||||
val := iter.readUint32(c)
|
||||
if val > math.MaxInt8 {
|
||||
iter.ReportError("ReadInt8", "overflow: "+strconv.FormatInt(int64(val), 10))
|
||||
return
|
||||
}
|
||||
return int8(val)
|
||||
}
|
||||
val := iter.readUint32(c)
|
||||
if val > math.MaxInt8 {
|
||||
iter.ReportError("ReadInt8", "overflow: "+strconv.FormatInt(int64(val), 10))
|
||||
return
|
||||
}
|
||||
return int8(val)
|
||||
}
|
||||
|
||||
// ReadUint8 read uint8
|
||||
func (iter *Iterator) ReadUint8() (ret uint8) {
|
||||
val := iter.readUint32(iter.nextToken())
|
||||
if val > math.MaxUint8 {
|
||||
@ -56,6 +59,7 @@ func (iter *Iterator) ReadUint8() (ret uint8) {
|
||||
return uint8(val)
|
||||
}
|
||||
|
||||
// ReadInt16 read int16
|
||||
func (iter *Iterator) ReadInt16() (ret int16) {
|
||||
c := iter.nextToken()
|
||||
if c == '-' {
|
||||
@ -65,16 +69,16 @@ func (iter *Iterator) ReadInt16() (ret int16) {
|
||||
return
|
||||
}
|
||||
return -int16(val)
|
||||
} else {
|
||||
val := iter.readUint32(c)
|
||||
if val > math.MaxInt16 {
|
||||
iter.ReportError("ReadInt16", "overflow: "+strconv.FormatInt(int64(val), 10))
|
||||
return
|
||||
}
|
||||
return int16(val)
|
||||
}
|
||||
val := iter.readUint32(c)
|
||||
if val > math.MaxInt16 {
|
||||
iter.ReportError("ReadInt16", "overflow: "+strconv.FormatInt(int64(val), 10))
|
||||
return
|
||||
}
|
||||
return int16(val)
|
||||
}
|
||||
|
||||
// ReadUint16 read uint16
|
||||
func (iter *Iterator) ReadUint16() (ret uint16) {
|
||||
val := iter.readUint32(iter.nextToken())
|
||||
if val > math.MaxUint16 {
|
||||
@ -84,6 +88,7 @@ func (iter *Iterator) ReadUint16() (ret uint16) {
|
||||
return uint16(val)
|
||||
}
|
||||
|
||||
// ReadInt32 read int32
|
||||
func (iter *Iterator) ReadInt32() (ret int32) {
|
||||
c := iter.nextToken()
|
||||
if c == '-' {
|
||||
@ -93,16 +98,16 @@ func (iter *Iterator) ReadInt32() (ret int32) {
|
||||
return
|
||||
}
|
||||
return -int32(val)
|
||||
} else {
|
||||
val := iter.readUint32(c)
|
||||
if val > math.MaxInt32 {
|
||||
iter.ReportError("ReadInt32", "overflow: "+strconv.FormatInt(int64(val), 10))
|
||||
return
|
||||
}
|
||||
return int32(val)
|
||||
}
|
||||
val := iter.readUint32(c)
|
||||
if val > math.MaxInt32 {
|
||||
iter.ReportError("ReadInt32", "overflow: "+strconv.FormatInt(int64(val), 10))
|
||||
return
|
||||
}
|
||||
return int32(val)
|
||||
}
|
||||
|
||||
// ReadUint32 read uint32
|
||||
func (iter *Iterator) ReadUint32() (ret uint32) {
|
||||
return iter.readUint32(iter.nextToken())
|
||||
}
|
||||
@ -110,6 +115,7 @@ func (iter *Iterator) ReadUint32() (ret uint32) {
|
||||
func (iter *Iterator) readUint32(c byte) (ret uint32) {
|
||||
ind := intDigits[c]
|
||||
if ind == 0 {
|
||||
iter.assertInteger()
|
||||
return 0 // single zero
|
||||
}
|
||||
if ind == invalidCharForNumber {
|
||||
@ -122,12 +128,14 @@ func (iter *Iterator) readUint32(c byte) (ret uint32) {
|
||||
ind2 := intDigits[iter.buf[i]]
|
||||
if ind2 == invalidCharForNumber {
|
||||
iter.head = i
|
||||
iter.assertInteger()
|
||||
return value
|
||||
}
|
||||
i++
|
||||
ind3 := intDigits[iter.buf[i]]
|
||||
if ind3 == invalidCharForNumber {
|
||||
iter.head = i
|
||||
iter.assertInteger()
|
||||
return value*10 + uint32(ind2)
|
||||
}
|
||||
//iter.head = i + 1
|
||||
@ -136,30 +144,35 @@ func (iter *Iterator) readUint32(c byte) (ret uint32) {
|
||||
ind4 := intDigits[iter.buf[i]]
|
||||
if ind4 == invalidCharForNumber {
|
||||
iter.head = i
|
||||
iter.assertInteger()
|
||||
return value*100 + uint32(ind2)*10 + uint32(ind3)
|
||||
}
|
||||
i++
|
||||
ind5 := intDigits[iter.buf[i]]
|
||||
if ind5 == invalidCharForNumber {
|
||||
iter.head = i
|
||||
iter.assertInteger()
|
||||
return value*1000 + uint32(ind2)*100 + uint32(ind3)*10 + uint32(ind4)
|
||||
}
|
||||
i++
|
||||
ind6 := intDigits[iter.buf[i]]
|
||||
if ind6 == invalidCharForNumber {
|
||||
iter.head = i
|
||||
iter.assertInteger()
|
||||
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
|
||||
iter.assertInteger()
|
||||
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
|
||||
iter.assertInteger()
|
||||
return value*1000000 + uint32(ind2)*100000 + uint32(ind3)*10000 + uint32(ind4)*1000 + uint32(ind5)*100 + uint32(ind6)*10 + uint32(ind7)
|
||||
}
|
||||
i++
|
||||
@ -167,6 +180,7 @@ func (iter *Iterator) readUint32(c byte) (ret uint32) {
|
||||
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 {
|
||||
iter.assertInteger()
|
||||
return value
|
||||
}
|
||||
}
|
||||
@ -175,6 +189,7 @@ func (iter *Iterator) readUint32(c byte) (ret uint32) {
|
||||
ind = intDigits[iter.buf[i]]
|
||||
if ind == invalidCharForNumber {
|
||||
iter.head = i
|
||||
iter.assertInteger()
|
||||
return value
|
||||
}
|
||||
if value > uint32SafeToMultiply10 {
|
||||
@ -182,19 +197,20 @@ func (iter *Iterator) readUint32(c byte) (ret uint32) {
|
||||
if value2 < value {
|
||||
iter.ReportError("readUint32", "overflow")
|
||||
return
|
||||
} else {
|
||||
value = value2
|
||||
continue
|
||||
}
|
||||
value = value2
|
||||
continue
|
||||
}
|
||||
value = (value << 3) + (value << 1) + uint32(ind)
|
||||
}
|
||||
if !iter.loadMore() {
|
||||
iter.assertInteger()
|
||||
return value
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ReadInt64 read int64
|
||||
func (iter *Iterator) ReadInt64() (ret int64) {
|
||||
c := iter.nextToken()
|
||||
if c == '-' {
|
||||
@ -204,16 +220,16 @@ func (iter *Iterator) ReadInt64() (ret int64) {
|
||||
return
|
||||
}
|
||||
return -int64(val)
|
||||
} else {
|
||||
val := iter.readUint64(c)
|
||||
if val > math.MaxInt64 {
|
||||
iter.ReportError("ReadInt64", "overflow: "+strconv.FormatUint(uint64(val), 10))
|
||||
return
|
||||
}
|
||||
return int64(val)
|
||||
}
|
||||
val := iter.readUint64(c)
|
||||
if val > math.MaxInt64 {
|
||||
iter.ReportError("ReadInt64", "overflow: "+strconv.FormatUint(uint64(val), 10))
|
||||
return
|
||||
}
|
||||
return int64(val)
|
||||
}
|
||||
|
||||
// ReadUint64 read uint64
|
||||
func (iter *Iterator) ReadUint64() uint64 {
|
||||
return iter.readUint64(iter.nextToken())
|
||||
}
|
||||
@ -221,6 +237,7 @@ func (iter *Iterator) ReadUint64() uint64 {
|
||||
func (iter *Iterator) readUint64(c byte) (ret uint64) {
|
||||
ind := intDigits[c]
|
||||
if ind == 0 {
|
||||
iter.assertInteger()
|
||||
return 0 // single zero
|
||||
}
|
||||
if ind == invalidCharForNumber {
|
||||
@ -228,11 +245,73 @@ func (iter *Iterator) readUint64(c byte) (ret uint64) {
|
||||
return
|
||||
}
|
||||
value := uint64(ind)
|
||||
if iter.tail-iter.head > 10 {
|
||||
i := iter.head
|
||||
ind2 := intDigits[iter.buf[i]]
|
||||
if ind2 == invalidCharForNumber {
|
||||
iter.head = i
|
||||
iter.assertInteger()
|
||||
return value
|
||||
}
|
||||
i++
|
||||
ind3 := intDigits[iter.buf[i]]
|
||||
if ind3 == invalidCharForNumber {
|
||||
iter.head = i
|
||||
iter.assertInteger()
|
||||
return value*10 + uint64(ind2)
|
||||
}
|
||||
//iter.head = i + 1
|
||||
//value = value * 100 + uint32(ind2) * 10 + uint32(ind3)
|
||||
i++
|
||||
ind4 := intDigits[iter.buf[i]]
|
||||
if ind4 == invalidCharForNumber {
|
||||
iter.head = i
|
||||
iter.assertInteger()
|
||||
return value*100 + uint64(ind2)*10 + uint64(ind3)
|
||||
}
|
||||
i++
|
||||
ind5 := intDigits[iter.buf[i]]
|
||||
if ind5 == invalidCharForNumber {
|
||||
iter.head = i
|
||||
iter.assertInteger()
|
||||
return value*1000 + uint64(ind2)*100 + uint64(ind3)*10 + uint64(ind4)
|
||||
}
|
||||
i++
|
||||
ind6 := intDigits[iter.buf[i]]
|
||||
if ind6 == invalidCharForNumber {
|
||||
iter.head = i
|
||||
iter.assertInteger()
|
||||
return value*10000 + uint64(ind2)*1000 + uint64(ind3)*100 + uint64(ind4)*10 + uint64(ind5)
|
||||
}
|
||||
i++
|
||||
ind7 := intDigits[iter.buf[i]]
|
||||
if ind7 == invalidCharForNumber {
|
||||
iter.head = i
|
||||
iter.assertInteger()
|
||||
return value*100000 + uint64(ind2)*10000 + uint64(ind3)*1000 + uint64(ind4)*100 + uint64(ind5)*10 + uint64(ind6)
|
||||
}
|
||||
i++
|
||||
ind8 := intDigits[iter.buf[i]]
|
||||
if ind8 == invalidCharForNumber {
|
||||
iter.head = i
|
||||
iter.assertInteger()
|
||||
return value*1000000 + uint64(ind2)*100000 + uint64(ind3)*10000 + uint64(ind4)*1000 + uint64(ind5)*100 + uint64(ind6)*10 + uint64(ind7)
|
||||
}
|
||||
i++
|
||||
ind9 := intDigits[iter.buf[i]]
|
||||
value = value*10000000 + uint64(ind2)*1000000 + uint64(ind3)*100000 + uint64(ind4)*10000 + uint64(ind5)*1000 + uint64(ind6)*100 + uint64(ind7)*10 + uint64(ind8)
|
||||
iter.head = i
|
||||
if ind9 == invalidCharForNumber {
|
||||
iter.assertInteger()
|
||||
return value
|
||||
}
|
||||
}
|
||||
for {
|
||||
for i := iter.head; i < iter.tail; i++ {
|
||||
ind = intDigits[iter.buf[i]]
|
||||
if ind == invalidCharForNumber {
|
||||
iter.head = i
|
||||
iter.assertInteger()
|
||||
return value
|
||||
}
|
||||
if value > uint64SafeToMultiple10 {
|
||||
@ -240,15 +319,21 @@ func (iter *Iterator) readUint64(c byte) (ret uint64) {
|
||||
if value2 < value {
|
||||
iter.ReportError("readUint64", "overflow")
|
||||
return
|
||||
} else {
|
||||
value = value2
|
||||
continue
|
||||
}
|
||||
value = value2
|
||||
continue
|
||||
}
|
||||
value = (value << 3) + (value << 1) + uint64(ind)
|
||||
}
|
||||
if !iter.loadMore() {
|
||||
iter.assertInteger()
|
||||
return value
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (iter *Iterator) assertInteger() {
|
||||
if iter.head < len(iter.buf) && iter.buf[iter.head] == '.' {
|
||||
iter.ReportError("assertInteger", "can not decode float as int")
|
||||
}
|
||||
}
|
||||
|
@ -3,28 +3,40 @@ package jsoniter
|
||||
import (
|
||||
"fmt"
|
||||
"unicode"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// ReadObject read one field from object.
|
||||
// If object ended, returns empty string.
|
||||
// Otherwise, returns the field name.
|
||||
func (iter *Iterator) ReadObject() (ret string) {
|
||||
c := iter.nextToken()
|
||||
switch c {
|
||||
case 'n':
|
||||
iter.skipFixedBytes(3)
|
||||
iter.skipThreeBytes('u', 'l', 'l')
|
||||
return "" // null
|
||||
case '{':
|
||||
c = iter.nextToken()
|
||||
if c == '"' {
|
||||
iter.unreadByte()
|
||||
return string(iter.readObjectFieldAsBytes())
|
||||
field := iter.ReadString()
|
||||
c = iter.nextToken()
|
||||
if c != ':' {
|
||||
iter.ReportError("ReadObject", "expect : after object field, but found "+string([]byte{c}))
|
||||
}
|
||||
return field
|
||||
}
|
||||
if c == '}' {
|
||||
return "" // end of object
|
||||
}
|
||||
iter.ReportError("ReadObject", `expect " after {`)
|
||||
iter.ReportError("ReadObject", `expect " after {, but found `+string([]byte{c}))
|
||||
return
|
||||
case ',':
|
||||
return string(iter.readObjectFieldAsBytes())
|
||||
field := iter.ReadString()
|
||||
c = iter.nextToken()
|
||||
if c != ':' {
|
||||
iter.ReportError("ReadObject", "expect : after object field, but found "+string([]byte{c}))
|
||||
}
|
||||
return field
|
||||
case '}':
|
||||
return "" // end of object
|
||||
default:
|
||||
@ -33,79 +45,114 @@ func (iter *Iterator) ReadObject() (ret string) {
|
||||
}
|
||||
}
|
||||
|
||||
func (iter *Iterator) readFieldHash() int32 {
|
||||
// CaseInsensitive
|
||||
func (iter *Iterator) readFieldHash() int64 {
|
||||
hash := int64(0x811c9dc5)
|
||||
c := iter.nextToken()
|
||||
if c == '"' {
|
||||
for {
|
||||
for i := iter.head; i < iter.tail; i++ {
|
||||
// require ascii string and no escape
|
||||
b := iter.buf[i]
|
||||
if 'A' <= b && b <= 'Z' {
|
||||
b += 'a' - 'A'
|
||||
}
|
||||
if b == '"' {
|
||||
iter.head = i + 1
|
||||
c = iter.nextToken()
|
||||
if c != ':' {
|
||||
iter.ReportError("readFieldHash", `expect :, but found `+string([]byte{c}))
|
||||
if c != '"' {
|
||||
iter.ReportError("readFieldHash", `expect ", but found `+string([]byte{c}))
|
||||
return 0
|
||||
}
|
||||
for {
|
||||
for i := iter.head; i < iter.tail; i++ {
|
||||
// require ascii string and no escape
|
||||
b := iter.buf[i]
|
||||
if b == '\\' {
|
||||
iter.head = i
|
||||
for _, b := range iter.readStringSlowPath() {
|
||||
if 'A' <= b && b <= 'Z' {
|
||||
b += 'a' - 'A'
|
||||
}
|
||||
return int32(hash)
|
||||
hash ^= int64(b)
|
||||
hash *= 0x1000193
|
||||
}
|
||||
hash ^= int64(b)
|
||||
hash *= 0x1000193
|
||||
c = iter.nextToken()
|
||||
if c != ':' {
|
||||
iter.ReportError("readFieldHash", `expect :, but found `+string([]byte{c}))
|
||||
return 0
|
||||
}
|
||||
return hash
|
||||
}
|
||||
if !iter.loadMore() {
|
||||
iter.ReportError("readFieldHash", `incomplete field name`)
|
||||
return 0
|
||||
if b == '"' {
|
||||
iter.head = i + 1
|
||||
c = iter.nextToken()
|
||||
if c != ':' {
|
||||
iter.ReportError("readFieldHash", `expect :, but found `+string([]byte{c}))
|
||||
return 0
|
||||
}
|
||||
return hash
|
||||
}
|
||||
if 'A' <= b && b <= 'Z' {
|
||||
b += 'a' - 'A'
|
||||
}
|
||||
hash ^= int64(b)
|
||||
hash *= 0x1000193
|
||||
}
|
||||
if !iter.loadMore() {
|
||||
iter.ReportError("readFieldHash", `incomplete field name`)
|
||||
return 0
|
||||
}
|
||||
}
|
||||
iter.ReportError("readFieldHash", `expect ", but found `+string([]byte{c}))
|
||||
return 0
|
||||
}
|
||||
|
||||
func calcHash(str string) int32 {
|
||||
func calcHash(str string) int64 {
|
||||
hash := int64(0x811c9dc5)
|
||||
for _, b := range str {
|
||||
hash ^= int64(unicode.ToLower(b))
|
||||
hash *= 0x1000193
|
||||
}
|
||||
return int32(hash)
|
||||
return int64(hash)
|
||||
}
|
||||
|
||||
// ReadObjectCB read object with callback, the key is ascii only and field name not copied
|
||||
func (iter *Iterator) ReadObjectCB(callback func(*Iterator, string) bool) bool {
|
||||
c := iter.nextToken()
|
||||
var field string
|
||||
if c == '{' {
|
||||
c = iter.nextToken()
|
||||
if c == '"' {
|
||||
iter.unreadByte()
|
||||
field := iter.readObjectFieldAsBytes()
|
||||
if !callback(iter, *(*string)(unsafe.Pointer(&field))) {
|
||||
field = iter.ReadString()
|
||||
c = iter.nextToken()
|
||||
if c != ':' {
|
||||
iter.ReportError("ReadObject", "expect : after object field, but found "+string([]byte{c}))
|
||||
}
|
||||
if !callback(iter, field) {
|
||||
return false
|
||||
}
|
||||
for iter.nextToken() == ',' {
|
||||
field = iter.readObjectFieldAsBytes()
|
||||
if !callback(iter, *(*string)(unsafe.Pointer(&field))) {
|
||||
c = iter.nextToken()
|
||||
for c == ',' {
|
||||
field = iter.ReadString()
|
||||
c = iter.nextToken()
|
||||
if c != ':' {
|
||||
iter.ReportError("ReadObject", "expect : after object field, but found "+string([]byte{c}))
|
||||
}
|
||||
if !callback(iter, field) {
|
||||
return false
|
||||
}
|
||||
c = iter.nextToken()
|
||||
}
|
||||
if c != '}' {
|
||||
iter.ReportError("ReadObjectCB", `object not ended with }`)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
if c == '}' {
|
||||
return true
|
||||
}
|
||||
iter.ReportError("ReadObjectCB", `expect " after }`)
|
||||
iter.ReportError("ReadObjectCB", `expect " after }, but found `+string([]byte{c}))
|
||||
return false
|
||||
}
|
||||
if c == 'n' {
|
||||
iter.skipFixedBytes(3)
|
||||
iter.skipThreeBytes('u', 'l', 'l')
|
||||
return true // null
|
||||
}
|
||||
iter.ReportError("ReadObjectCB", `expect { or n`)
|
||||
iter.ReportError("ReadObjectCB", `expect { or n, but found `+string([]byte{c}))
|
||||
return false
|
||||
}
|
||||
|
||||
// ReadMapCB read map with callback, the key can be any string
|
||||
func (iter *Iterator) ReadMapCB(callback func(*Iterator, string) bool) bool {
|
||||
c := iter.nextToken()
|
||||
if c == '{' {
|
||||
@ -114,35 +161,41 @@ func (iter *Iterator) ReadMapCB(callback func(*Iterator, string) bool) bool {
|
||||
iter.unreadByte()
|
||||
field := iter.ReadString()
|
||||
if iter.nextToken() != ':' {
|
||||
iter.ReportError("ReadMapCB", "expect : after object field")
|
||||
iter.ReportError("ReadMapCB", "expect : after object field, but found "+string([]byte{c}))
|
||||
return false
|
||||
}
|
||||
if !callback(iter, field) {
|
||||
return false
|
||||
}
|
||||
for iter.nextToken() == ',' {
|
||||
c = iter.nextToken()
|
||||
for c == ',' {
|
||||
field = iter.ReadString()
|
||||
if iter.nextToken() != ':' {
|
||||
iter.ReportError("ReadMapCB", "expect : after object field")
|
||||
iter.ReportError("ReadMapCB", "expect : after object field, but found "+string([]byte{c}))
|
||||
return false
|
||||
}
|
||||
if !callback(iter, field) {
|
||||
return false
|
||||
}
|
||||
c = iter.nextToken()
|
||||
}
|
||||
if c != '}' {
|
||||
iter.ReportError("ReadMapCB", `object not ended with }`)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
if c == '}' {
|
||||
return true
|
||||
}
|
||||
iter.ReportError("ReadMapCB", `expect " after }`)
|
||||
iter.ReportError("ReadMapCB", `expect " after }, but found `+string([]byte{c}))
|
||||
return false
|
||||
}
|
||||
if c == 'n' {
|
||||
iter.skipFixedBytes(3)
|
||||
iter.skipThreeBytes('u', 'l', 'l')
|
||||
return true // null
|
||||
}
|
||||
iter.ReportError("ReadMapCB", `expect { or n`)
|
||||
iter.ReportError("ReadMapCB", `expect { or n, but found `+string([]byte{c}))
|
||||
return false
|
||||
}
|
||||
|
||||
@ -156,10 +209,10 @@ func (iter *Iterator) readObjectStart() bool {
|
||||
iter.unreadByte()
|
||||
return true
|
||||
} else if c == 'n' {
|
||||
iter.skipFixedBytes(3)
|
||||
iter.skipThreeBytes('u', 'l', 'l')
|
||||
return false
|
||||
}
|
||||
iter.ReportError("readObjectStart", "expect { or n")
|
||||
iter.ReportError("readObjectStart", "expect { or n, but found "+string([]byte{c}))
|
||||
return false
|
||||
}
|
||||
|
||||
@ -175,7 +228,7 @@ func (iter *Iterator) readObjectFieldAsBytes() (ret []byte) {
|
||||
}
|
||||
}
|
||||
if iter.buf[iter.head] != ':' {
|
||||
iter.ReportError("readObjectFieldAsBytes", "expect : after object field")
|
||||
iter.ReportError("readObjectFieldAsBytes", "expect : after object field, but found "+string([]byte{iter.buf[iter.head]}))
|
||||
return
|
||||
}
|
||||
iter.head++
|
||||
|
@ -7,28 +7,30 @@ import "fmt"
|
||||
func (iter *Iterator) ReadNil() (ret bool) {
|
||||
c := iter.nextToken()
|
||||
if c == 'n' {
|
||||
iter.skipFixedBytes(3) // null
|
||||
iter.skipThreeBytes('u', 'l', 'l') // null
|
||||
return true
|
||||
}
|
||||
iter.unreadByte()
|
||||
return false
|
||||
}
|
||||
|
||||
// ReadBool reads a json object as Bool
|
||||
// ReadBool reads a json object as BoolValue
|
||||
func (iter *Iterator) ReadBool() (ret bool) {
|
||||
c := iter.nextToken()
|
||||
if c == 't' {
|
||||
iter.skipFixedBytes(3)
|
||||
iter.skipThreeBytes('r', 'u', 'e')
|
||||
return true
|
||||
}
|
||||
if c == 'f' {
|
||||
iter.skipFixedBytes(4)
|
||||
iter.skipFourBytes('a', 'l', 's', 'e')
|
||||
return false
|
||||
}
|
||||
iter.ReportError("ReadBool", "expect t or f")
|
||||
iter.ReportError("ReadBool", "expect t or f, but found "+string([]byte{c}))
|
||||
return
|
||||
}
|
||||
|
||||
// SkipAndReturnBytes skip next JSON element, and return its content as []byte.
|
||||
// The []byte can be kept, it is a copy of data.
|
||||
func (iter *Iterator) SkipAndReturnBytes() []byte {
|
||||
iter.startCapture(iter.head)
|
||||
iter.Skip()
|
||||
@ -57,11 +59,12 @@ func (iter *Iterator) stopCapture() []byte {
|
||||
iter.captureStartedAt = -1
|
||||
iter.captured = nil
|
||||
if len(captured) == 0 {
|
||||
return remaining
|
||||
} else {
|
||||
captured = append(captured, remaining...)
|
||||
return captured
|
||||
copied := make([]byte, len(remaining))
|
||||
copy(copied, remaining)
|
||||
return copied
|
||||
}
|
||||
captured = append(captured, remaining...)
|
||||
return captured
|
||||
}
|
||||
|
||||
// Skip skips a json object and positions to relatively the next json object
|
||||
@ -70,11 +73,16 @@ func (iter *Iterator) Skip() {
|
||||
switch c {
|
||||
case '"':
|
||||
iter.skipString()
|
||||
case 'n', 't':
|
||||
iter.skipFixedBytes(3) // null or true
|
||||
case 'n':
|
||||
iter.skipThreeBytes('u', 'l', 'l') // null
|
||||
case 't':
|
||||
iter.skipThreeBytes('r', 'u', 'e') // true
|
||||
case 'f':
|
||||
iter.skipFixedBytes(4) // false
|
||||
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
|
||||
iter.skipFourBytes('a', 'l', 's', 'e') // false
|
||||
case '0':
|
||||
iter.unreadByte()
|
||||
iter.ReadFloat32()
|
||||
case '-', '1', '2', '3', '4', '5', '6', '7', '8', '9':
|
||||
iter.skipNumber()
|
||||
case '[':
|
||||
iter.skipArray()
|
||||
@ -86,155 +94,36 @@ func (iter *Iterator) Skip() {
|
||||
}
|
||||
}
|
||||
|
||||
func (iter *Iterator) skipString() {
|
||||
for {
|
||||
end, escaped := iter.findStringEnd()
|
||||
if end == -1 {
|
||||
if !iter.loadMore() {
|
||||
iter.ReportError("skipString", "incomplete string")
|
||||
return
|
||||
}
|
||||
if escaped {
|
||||
iter.head = 1 // skip the first char as last char read is \
|
||||
}
|
||||
} else {
|
||||
iter.head = end
|
||||
return
|
||||
}
|
||||
func (iter *Iterator) skipFourBytes(b1, b2, b3, b4 byte) {
|
||||
if iter.readByte() != b1 {
|
||||
iter.ReportError("skipFourBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3, b4})))
|
||||
return
|
||||
}
|
||||
if iter.readByte() != b2 {
|
||||
iter.ReportError("skipFourBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3, b4})))
|
||||
return
|
||||
}
|
||||
if iter.readByte() != b3 {
|
||||
iter.ReportError("skipFourBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3, b4})))
|
||||
return
|
||||
}
|
||||
if iter.readByte() != b4 {
|
||||
iter.ReportError("skipFourBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3, b4})))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// adapted from: https://github.com/buger/jsonparser/blob/master/parser.go
|
||||
// Tries to find the end of string
|
||||
// Support if string contains escaped quote symbols.
|
||||
func (iter *Iterator) findStringEnd() (int, bool) {
|
||||
escaped := false
|
||||
for i := iter.head; i < iter.tail; i++ {
|
||||
c := iter.buf[i]
|
||||
if c == '"' {
|
||||
if !escaped {
|
||||
return i + 1, false
|
||||
}
|
||||
j := i - 1
|
||||
for {
|
||||
if j < iter.head || iter.buf[j] != '\\' {
|
||||
// even number of backslashes
|
||||
// either end of buffer, or " found
|
||||
return i + 1, true
|
||||
}
|
||||
j--
|
||||
if j < iter.head || iter.buf[j] != '\\' {
|
||||
// odd number of backslashes
|
||||
// it is \" or \\\"
|
||||
break
|
||||
}
|
||||
j--
|
||||
}
|
||||
} else if c == '\\' {
|
||||
escaped = true
|
||||
}
|
||||
func (iter *Iterator) skipThreeBytes(b1, b2, b3 byte) {
|
||||
if iter.readByte() != b1 {
|
||||
iter.ReportError("skipThreeBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3})))
|
||||
return
|
||||
}
|
||||
j := iter.tail - 1
|
||||
for {
|
||||
if j < iter.head || iter.buf[j] != '\\' {
|
||||
// even number of backslashes
|
||||
// either end of buffer, or " found
|
||||
return -1, false // do not end with \
|
||||
}
|
||||
j--
|
||||
if j < iter.head || iter.buf[j] != '\\' {
|
||||
// odd number of backslashes
|
||||
// it is \" or \\\"
|
||||
break
|
||||
}
|
||||
j--
|
||||
|
||||
if iter.readByte() != b2 {
|
||||
iter.ReportError("skipThreeBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3})))
|
||||
return
|
||||
}
|
||||
return -1, true // end with \
|
||||
}
|
||||
|
||||
func (iter *Iterator) skipArray() {
|
||||
level := 1
|
||||
for {
|
||||
for i := iter.head; i < iter.tail; i++ {
|
||||
switch iter.buf[i] {
|
||||
case '"': // If inside string, skip it
|
||||
iter.head = i + 1
|
||||
iter.skipString()
|
||||
i = iter.head - 1 // it will be i++ soon
|
||||
case '[': // If open symbol, increase level
|
||||
level++
|
||||
case ']': // If close symbol, increase level
|
||||
level--
|
||||
|
||||
// If we have returned to the original level, we're done
|
||||
if level == 0 {
|
||||
iter.head = i + 1
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
if !iter.loadMore() {
|
||||
iter.ReportError("skipObject", "incomplete array")
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (iter *Iterator) skipObject() {
|
||||
level := 1
|
||||
for {
|
||||
for i := iter.head; i < iter.tail; i++ {
|
||||
switch iter.buf[i] {
|
||||
case '"': // If inside string, skip it
|
||||
iter.head = i + 1
|
||||
iter.skipString()
|
||||
i = iter.head - 1 // it will be i++ soon
|
||||
case '{': // If open symbol, increase level
|
||||
level++
|
||||
case '}': // If close symbol, increase level
|
||||
level--
|
||||
|
||||
// If we have returned to the original level, we're done
|
||||
if level == 0 {
|
||||
iter.head = i + 1
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
if !iter.loadMore() {
|
||||
iter.ReportError("skipObject", "incomplete object")
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (iter *Iterator) skipNumber() {
|
||||
for {
|
||||
for i := iter.head; i < iter.tail; i++ {
|
||||
c := iter.buf[i]
|
||||
switch c {
|
||||
case ' ', '\n', '\r', '\t', ',', '}', ']':
|
||||
iter.head = i
|
||||
return
|
||||
}
|
||||
}
|
||||
if !iter.loadMore() {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (iter *Iterator) skipFixedBytes(n int) {
|
||||
iter.head += n
|
||||
if iter.head >= iter.tail {
|
||||
more := iter.head - iter.tail
|
||||
if !iter.loadMore() {
|
||||
if more > 0 {
|
||||
iter.ReportError("skipFixedBytes", "unexpected end")
|
||||
}
|
||||
return
|
||||
}
|
||||
iter.head += more
|
||||
if iter.readByte() != b3 {
|
||||
iter.ReportError("skipThreeBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3})))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
144
feature_iter_skip_sloppy.go
Normal file
144
feature_iter_skip_sloppy.go
Normal file
@ -0,0 +1,144 @@
|
||||
//+build jsoniter_sloppy
|
||||
|
||||
package jsoniter
|
||||
|
||||
// sloppy but faster implementation, do not validate the input json
|
||||
|
||||
func (iter *Iterator) skipNumber() {
|
||||
for {
|
||||
for i := iter.head; i < iter.tail; i++ {
|
||||
c := iter.buf[i]
|
||||
switch c {
|
||||
case ' ', '\n', '\r', '\t', ',', '}', ']':
|
||||
iter.head = i
|
||||
return
|
||||
}
|
||||
}
|
||||
if !iter.loadMore() {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (iter *Iterator) skipArray() {
|
||||
level := 1
|
||||
for {
|
||||
for i := iter.head; i < iter.tail; i++ {
|
||||
switch iter.buf[i] {
|
||||
case '"': // If inside string, skip it
|
||||
iter.head = i + 1
|
||||
iter.skipString()
|
||||
i = iter.head - 1 // it will be i++ soon
|
||||
case '[': // If open symbol, increase level
|
||||
level++
|
||||
case ']': // If close symbol, increase level
|
||||
level--
|
||||
|
||||
// If we have returned to the original level, we're done
|
||||
if level == 0 {
|
||||
iter.head = i + 1
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
if !iter.loadMore() {
|
||||
iter.ReportError("skipObject", "incomplete array")
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (iter *Iterator) skipObject() {
|
||||
level := 1
|
||||
for {
|
||||
for i := iter.head; i < iter.tail; i++ {
|
||||
switch iter.buf[i] {
|
||||
case '"': // If inside string, skip it
|
||||
iter.head = i + 1
|
||||
iter.skipString()
|
||||
i = iter.head - 1 // it will be i++ soon
|
||||
case '{': // If open symbol, increase level
|
||||
level++
|
||||
case '}': // If close symbol, increase level
|
||||
level--
|
||||
|
||||
// If we have returned to the original level, we're done
|
||||
if level == 0 {
|
||||
iter.head = i + 1
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
if !iter.loadMore() {
|
||||
iter.ReportError("skipObject", "incomplete object")
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (iter *Iterator) skipString() {
|
||||
for {
|
||||
end, escaped := iter.findStringEnd()
|
||||
if end == -1 {
|
||||
if !iter.loadMore() {
|
||||
iter.ReportError("skipString", "incomplete string")
|
||||
return
|
||||
}
|
||||
if escaped {
|
||||
iter.head = 1 // skip the first char as last char read is \
|
||||
}
|
||||
} else {
|
||||
iter.head = end
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// adapted from: https://github.com/buger/jsonparser/blob/master/parser.go
|
||||
// Tries to find the end of string
|
||||
// Support if string contains escaped quote symbols.
|
||||
func (iter *Iterator) findStringEnd() (int, bool) {
|
||||
escaped := false
|
||||
for i := iter.head; i < iter.tail; i++ {
|
||||
c := iter.buf[i]
|
||||
if c == '"' {
|
||||
if !escaped {
|
||||
return i + 1, false
|
||||
}
|
||||
j := i - 1
|
||||
for {
|
||||
if j < iter.head || iter.buf[j] != '\\' {
|
||||
// even number of backslashes
|
||||
// either end of buffer, or " found
|
||||
return i + 1, true
|
||||
}
|
||||
j--
|
||||
if j < iter.head || iter.buf[j] != '\\' {
|
||||
// odd number of backslashes
|
||||
// it is \" or \\\"
|
||||
break
|
||||
}
|
||||
j--
|
||||
}
|
||||
} else if c == '\\' {
|
||||
escaped = true
|
||||
}
|
||||
}
|
||||
j := iter.tail - 1
|
||||
for {
|
||||
if j < iter.head || iter.buf[j] != '\\' {
|
||||
// even number of backslashes
|
||||
// either end of buffer, or " found
|
||||
return -1, false // do not end with \
|
||||
}
|
||||
j--
|
||||
if j < iter.head || iter.buf[j] != '\\' {
|
||||
// odd number of backslashes
|
||||
// it is \" or \\\"
|
||||
break
|
||||
}
|
||||
j--
|
||||
|
||||
}
|
||||
return -1, true // end with \
|
||||
}
|
@ -1,7 +1,9 @@
|
||||
//+build jsoniter_sloppy
|
||||
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"github.com/json-iterator/go/require"
|
||||
"github.com/stretchr/testify/require"
|
||||
"io"
|
||||
"testing"
|
||||
)
|
89
feature_iter_skip_strict.go
Normal file
89
feature_iter_skip_strict.go
Normal file
@ -0,0 +1,89 @@
|
||||
//+build !jsoniter_sloppy
|
||||
|
||||
package jsoniter
|
||||
|
||||
import "fmt"
|
||||
|
||||
func (iter *Iterator) skipNumber() {
|
||||
if !iter.trySkipNumber() {
|
||||
iter.unreadByte()
|
||||
iter.ReadFloat32()
|
||||
}
|
||||
}
|
||||
|
||||
func (iter *Iterator) trySkipNumber() bool {
|
||||
dotFound := false
|
||||
for i := iter.head; i < iter.tail; i++ {
|
||||
c := iter.buf[i]
|
||||
switch c {
|
||||
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
|
||||
case '.':
|
||||
if dotFound {
|
||||
iter.ReportError("validateNumber", `more than one dot found in number`)
|
||||
return true // already failed
|
||||
}
|
||||
if i+1 == iter.tail {
|
||||
return false
|
||||
}
|
||||
c = iter.buf[i+1]
|
||||
switch c {
|
||||
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
|
||||
default:
|
||||
iter.ReportError("validateNumber", `missing digit after dot`)
|
||||
return true // already failed
|
||||
}
|
||||
dotFound = true
|
||||
default:
|
||||
switch c {
|
||||
case ',', ']', '}', ' ', '\t', '\n', '\r':
|
||||
if iter.head == i {
|
||||
return false // if - without following digits
|
||||
}
|
||||
iter.head = i
|
||||
return true // must be valid
|
||||
}
|
||||
return false // may be invalid
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (iter *Iterator) skipString() {
|
||||
if !iter.trySkipString() {
|
||||
iter.unreadByte()
|
||||
iter.ReadString()
|
||||
}
|
||||
}
|
||||
|
||||
func (iter *Iterator) trySkipString() bool {
|
||||
for i := iter.head; i < iter.tail; i++ {
|
||||
c := iter.buf[i]
|
||||
if c == '"' {
|
||||
iter.head = i + 1
|
||||
return true // valid
|
||||
} else if c == '\\' {
|
||||
return false
|
||||
} else if c < ' ' {
|
||||
iter.ReportError("trySkipString",
|
||||
fmt.Sprintf(`invalid control character found: %d`, c))
|
||||
return true // already failed
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (iter *Iterator) skipObject() {
|
||||
iter.unreadByte()
|
||||
iter.ReadObjectCB(func(iter *Iterator, field string) bool {
|
||||
iter.Skip()
|
||||
return true
|
||||
})
|
||||
}
|
||||
|
||||
func (iter *Iterator) skipArray() {
|
||||
iter.unreadByte()
|
||||
iter.ReadArrayCB(func(iter *Iterator) bool {
|
||||
iter.Skip()
|
||||
return true
|
||||
})
|
||||
}
|
@ -1,9 +1,11 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"unicode/utf16"
|
||||
)
|
||||
|
||||
// ReadString read string from iterator
|
||||
func (iter *Iterator) ReadString() (ret string) {
|
||||
c := iter.nextToken()
|
||||
if c == '"' {
|
||||
@ -15,14 +17,18 @@ func (iter *Iterator) ReadString() (ret string) {
|
||||
return ret
|
||||
} else if c == '\\' {
|
||||
break
|
||||
} else if c < ' ' {
|
||||
iter.ReportError("ReadString",
|
||||
fmt.Sprintf(`invalid control character found: %d`, c))
|
||||
return
|
||||
}
|
||||
}
|
||||
return iter.readStringSlowPath()
|
||||
} else if c == 'n' {
|
||||
iter.skipFixedBytes(3)
|
||||
iter.skipThreeBytes('u', 'l', 'l')
|
||||
return ""
|
||||
}
|
||||
iter.ReportError("ReadString", `expects " or n`)
|
||||
iter.ReportError("ReadString", `expects " or n, but found `+string([]byte{c}))
|
||||
return
|
||||
}
|
||||
|
||||
@ -36,66 +42,77 @@ func (iter *Iterator) readStringSlowPath() (ret string) {
|
||||
}
|
||||
if c == '\\' {
|
||||
c = iter.readByte()
|
||||
switch c {
|
||||
case 'u', 'U':
|
||||
r := iter.readU4()
|
||||
if utf16.IsSurrogate(r) {
|
||||
c = iter.readByte()
|
||||
if iter.Error != nil {
|
||||
return
|
||||
}
|
||||
if c != '\\' {
|
||||
iter.ReportError("ReadString",
|
||||
`expects \u after utf16 surrogate, but \ not found`)
|
||||
return
|
||||
}
|
||||
c = iter.readByte()
|
||||
if iter.Error != nil {
|
||||
return
|
||||
}
|
||||
if c != 'u' && c != 'U' {
|
||||
iter.ReportError("ReadString",
|
||||
`expects \u after utf16 surrogate, but \u not found`)
|
||||
return
|
||||
}
|
||||
r2 := iter.readU4()
|
||||
if iter.Error != nil {
|
||||
return
|
||||
}
|
||||
combined := utf16.DecodeRune(r, r2)
|
||||
str = appendRune(str, combined)
|
||||
} else {
|
||||
str = appendRune(str, r)
|
||||
}
|
||||
case '"':
|
||||
str = append(str, '"')
|
||||
case '\\':
|
||||
str = append(str, '\\')
|
||||
case '/':
|
||||
str = append(str, '/')
|
||||
case 'b':
|
||||
str = append(str, '\b')
|
||||
case 'f':
|
||||
str = append(str, '\f')
|
||||
case 'n':
|
||||
str = append(str, '\n')
|
||||
case 'r':
|
||||
str = append(str, '\r')
|
||||
case 't':
|
||||
str = append(str, '\t')
|
||||
default:
|
||||
iter.ReportError("ReadString",
|
||||
`invalid escape char after \`)
|
||||
return
|
||||
}
|
||||
str = iter.readEscapedChar(c, str)
|
||||
} else {
|
||||
str = append(str, c)
|
||||
}
|
||||
}
|
||||
iter.ReportError("ReadString", "unexpected end of input")
|
||||
iter.ReportError("readStringSlowPath", "unexpected end of input")
|
||||
return
|
||||
}
|
||||
|
||||
func (iter *Iterator) readEscapedChar(c byte, str []byte) []byte {
|
||||
switch c {
|
||||
case 'u':
|
||||
r := iter.readU4()
|
||||
if utf16.IsSurrogate(r) {
|
||||
c = iter.readByte()
|
||||
if iter.Error != nil {
|
||||
return nil
|
||||
}
|
||||
if c != '\\' {
|
||||
iter.unreadByte()
|
||||
str = appendRune(str, r)
|
||||
return str
|
||||
}
|
||||
c = iter.readByte()
|
||||
if iter.Error != nil {
|
||||
return nil
|
||||
}
|
||||
if c != 'u' {
|
||||
str = appendRune(str, r)
|
||||
return iter.readEscapedChar(c, str)
|
||||
}
|
||||
r2 := iter.readU4()
|
||||
if iter.Error != nil {
|
||||
return nil
|
||||
}
|
||||
combined := utf16.DecodeRune(r, r2)
|
||||
if combined == '\uFFFD' {
|
||||
str = appendRune(str, r)
|
||||
str = appendRune(str, r2)
|
||||
} else {
|
||||
str = appendRune(str, combined)
|
||||
}
|
||||
} else {
|
||||
str = appendRune(str, r)
|
||||
}
|
||||
case '"':
|
||||
str = append(str, '"')
|
||||
case '\\':
|
||||
str = append(str, '\\')
|
||||
case '/':
|
||||
str = append(str, '/')
|
||||
case 'b':
|
||||
str = append(str, '\b')
|
||||
case 'f':
|
||||
str = append(str, '\f')
|
||||
case 'n':
|
||||
str = append(str, '\n')
|
||||
case 'r':
|
||||
str = append(str, '\r')
|
||||
case 't':
|
||||
str = append(str, '\t')
|
||||
default:
|
||||
iter.ReportError("readEscapedChar",
|
||||
`invalid escape char after \`)
|
||||
return nil
|
||||
}
|
||||
return str
|
||||
}
|
||||
|
||||
// ReadStringAsSlice read string from iterator without copying into string form.
|
||||
// The []byte can not be kept, as it will change after next iterator call.
|
||||
func (iter *Iterator) ReadStringAsSlice() (ret []byte) {
|
||||
c := iter.nextToken()
|
||||
if c == '"' {
|
||||
@ -122,7 +139,7 @@ func (iter *Iterator) ReadStringAsSlice() (ret []byte) {
|
||||
}
|
||||
return copied
|
||||
}
|
||||
iter.ReportError("ReadString", `expects " or n`)
|
||||
iter.ReportError("ReadStringAsSlice", `expects " or n, but found `+string([]byte{c}))
|
||||
return
|
||||
}
|
||||
|
||||
@ -139,7 +156,7 @@ func (iter *Iterator) readU4() (ret rune) {
|
||||
} else if c >= 'A' && c <= 'F' {
|
||||
ret = ret*16 + rune(c-'A'+10)
|
||||
} else {
|
||||
iter.ReportError("readU4", "expects 0~9 or a~f")
|
||||
iter.ReportError("readU4", "expects 0~9 or a~f, but found "+string([]byte{c}))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
31
feature_json_number.go
Normal file
31
feature_json_number.go
Normal file
@ -0,0 +1,31 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type Number string
|
||||
|
||||
// String returns the literal text of the number.
|
||||
func (n Number) String() string { return string(n) }
|
||||
|
||||
// Float64 returns the number as a float64.
|
||||
func (n Number) Float64() (float64, error) {
|
||||
return strconv.ParseFloat(string(n), 64)
|
||||
}
|
||||
|
||||
// Int64 returns the number as an int64.
|
||||
func (n Number) Int64() (int64, error) {
|
||||
return strconv.ParseInt(string(n), 10, 64)
|
||||
}
|
||||
|
||||
func CastJsonNumber(val interface{}) (string, bool) {
|
||||
switch typedVal := val.(type) {
|
||||
case json.Number:
|
||||
return string(typedVal), true
|
||||
case Number:
|
||||
return string(typedVal), true
|
||||
}
|
||||
return "", false
|
||||
}
|
@ -4,6 +4,18 @@ import (
|
||||
"io"
|
||||
)
|
||||
|
||||
// IteratorPool a thread safe pool of iterators with same configuration
|
||||
type IteratorPool interface {
|
||||
BorrowIterator(data []byte) *Iterator
|
||||
ReturnIterator(iter *Iterator)
|
||||
}
|
||||
|
||||
// StreamPool a thread safe pool of streams with same configuration
|
||||
type StreamPool interface {
|
||||
BorrowStream(writer io.Writer) *Stream
|
||||
ReturnStream(stream *Stream)
|
||||
}
|
||||
|
||||
func (cfg *frozenConfig) BorrowStream(writer io.Writer) *Stream {
|
||||
select {
|
||||
case stream := <-cfg.streamPool:
|
||||
@ -16,6 +28,7 @@ func (cfg *frozenConfig) BorrowStream(writer io.Writer) *Stream {
|
||||
|
||||
func (cfg *frozenConfig) ReturnStream(stream *Stream) {
|
||||
stream.Error = nil
|
||||
stream.Attachment = nil
|
||||
select {
|
||||
case cfg.streamPool <- stream:
|
||||
return
|
||||
@ -36,6 +49,7 @@ func (cfg *frozenConfig) BorrowIterator(data []byte) *Iterator {
|
||||
|
||||
func (cfg *frozenConfig) ReturnIterator(iter *Iterator) {
|
||||
iter.Error = nil
|
||||
iter.Attachment = nil
|
||||
select {
|
||||
case cfg.iteratorPool <- iter:
|
||||
return
|
||||
|
1000
feature_reflect.go
1000
feature_reflect.go
File diff suppressed because it is too large
Load Diff
@ -7,23 +7,34 @@ import (
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
func decoderOfArray(cfg *frozenConfig, typ reflect.Type) (ValDecoder, error) {
|
||||
decoder, err := decoderOfType(cfg, typ.Elem())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &arrayDecoder{typ, typ.Elem(), decoder}, nil
|
||||
func decoderOfArray(cfg *frozenConfig, prefix string, typ reflect.Type) ValDecoder {
|
||||
decoder := decoderOfType(cfg, prefix+"[array]->", typ.Elem())
|
||||
return &arrayDecoder{typ, typ.Elem(), decoder}
|
||||
}
|
||||
|
||||
func encoderOfArray(cfg *frozenConfig, typ reflect.Type) (ValEncoder, error) {
|
||||
encoder, err := encoderOfType(cfg, typ.Elem())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
func encoderOfArray(cfg *frozenConfig, prefix string, typ reflect.Type) ValEncoder {
|
||||
if typ.Len() == 0 {
|
||||
return emptyArrayEncoder{}
|
||||
}
|
||||
encoder := encoderOfType(cfg, prefix+"[array]->", typ.Elem())
|
||||
if typ.Elem().Kind() == reflect.Map {
|
||||
encoder = &optionalEncoder{encoder}
|
||||
encoder = &OptionalEncoder{encoder}
|
||||
}
|
||||
return &arrayEncoder{typ, typ.Elem(), encoder}, nil
|
||||
return &arrayEncoder{typ, typ.Elem(), encoder}
|
||||
}
|
||||
|
||||
type emptyArrayEncoder struct{}
|
||||
|
||||
func (encoder emptyArrayEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||
stream.WriteEmptyArray()
|
||||
}
|
||||
|
||||
func (encoder emptyArrayEncoder) EncodeInterface(val interface{}, stream *Stream) {
|
||||
stream.WriteEmptyArray()
|
||||
}
|
||||
|
||||
func (encoder emptyArrayEncoder) IsEmpty(ptr unsafe.Pointer) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
type arrayEncoder struct {
|
||||
@ -34,11 +45,11 @@ type arrayEncoder struct {
|
||||
|
||||
func (encoder *arrayEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||
stream.WriteArrayStart()
|
||||
elemPtr := uintptr(ptr)
|
||||
encoder.elemEncoder.Encode(unsafe.Pointer(elemPtr), stream)
|
||||
elemPtr := unsafe.Pointer(ptr)
|
||||
encoder.elemEncoder.Encode(elemPtr, stream)
|
||||
for i := 1; i < encoder.arrayType.Len(); i++ {
|
||||
stream.WriteMore()
|
||||
elemPtr += encoder.elemType.Size()
|
||||
elemPtr = unsafe.Pointer(uintptr(elemPtr) + encoder.elemType.Size())
|
||||
encoder.elemEncoder.Encode(unsafe.Pointer(elemPtr), stream)
|
||||
}
|
||||
stream.WriteArrayEnd()
|
||||
@ -87,11 +98,13 @@ func (decoder *arrayDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
|
||||
func (decoder *arrayDecoder) doDecode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
offset := uintptr(0)
|
||||
for ; iter.ReadArray(); offset += decoder.elemType.Size() {
|
||||
iter.ReadArrayCB(func(iter *Iterator) bool {
|
||||
if offset < decoder.arrayType.Size() {
|
||||
decoder.elemDecoder.Decode(unsafe.Pointer(uintptr(ptr)+offset), iter)
|
||||
offset += decoder.elemType.Size()
|
||||
} else {
|
||||
iter.Skip()
|
||||
}
|
||||
}
|
||||
return true
|
||||
})
|
||||
}
|
||||
|
@ -15,6 +15,7 @@ var typeEncoders = map[string]ValEncoder{}
|
||||
var fieldEncoders = map[string]ValEncoder{}
|
||||
var extensions = []Extension{}
|
||||
|
||||
// StructDescriptor describe how should we encode/decode the struct
|
||||
type StructDescriptor struct {
|
||||
onePtrEmbedded bool
|
||||
onePtrOptimization bool
|
||||
@ -22,6 +23,8 @@ type StructDescriptor struct {
|
||||
Fields []*Binding
|
||||
}
|
||||
|
||||
// GetField get one field from the descriptor by its name.
|
||||
// Can not use map here to keep field orders.
|
||||
func (structDescriptor *StructDescriptor) GetField(fieldName string) *Binding {
|
||||
for _, binding := range structDescriptor.Fields {
|
||||
if binding.Field.Name == fieldName {
|
||||
@ -31,6 +34,7 @@ func (structDescriptor *StructDescriptor) GetField(fieldName string) *Binding {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Binding describe how should we encode/decode the struct field
|
||||
type Binding struct {
|
||||
levels []int
|
||||
Field *reflect.StructField
|
||||
@ -40,6 +44,8 @@ type Binding struct {
|
||||
Decoder ValDecoder
|
||||
}
|
||||
|
||||
// Extension the one for all SPI. Customize encoding/decoding by specifying alternate encoder/decoder.
|
||||
// Can also rename fields by UpdateStructDescriptor.
|
||||
type Extension interface {
|
||||
UpdateStructDescriptor(structDescriptor *StructDescriptor)
|
||||
CreateDecoder(typ reflect.Type) ValDecoder
|
||||
@ -48,24 +54,30 @@ type Extension interface {
|
||||
DecorateEncoder(typ reflect.Type, encoder ValEncoder) ValEncoder
|
||||
}
|
||||
|
||||
// DummyExtension embed this type get dummy implementation for all methods of Extension
|
||||
type DummyExtension struct {
|
||||
}
|
||||
|
||||
// UpdateStructDescriptor No-op
|
||||
func (extension *DummyExtension) UpdateStructDescriptor(structDescriptor *StructDescriptor) {
|
||||
}
|
||||
|
||||
// CreateDecoder No-op
|
||||
func (extension *DummyExtension) CreateDecoder(typ reflect.Type) ValDecoder {
|
||||
return nil
|
||||
}
|
||||
|
||||
// CreateEncoder No-op
|
||||
func (extension *DummyExtension) CreateEncoder(typ reflect.Type) ValEncoder {
|
||||
return nil
|
||||
}
|
||||
|
||||
// DecorateDecoder No-op
|
||||
func (extension *DummyExtension) DecorateDecoder(typ reflect.Type, decoder ValDecoder) ValDecoder {
|
||||
return decoder
|
||||
}
|
||||
|
||||
// DecorateEncoder No-op
|
||||
func (extension *DummyExtension) DecorateEncoder(typ reflect.Type, encoder ValEncoder) ValEncoder {
|
||||
return encoder
|
||||
}
|
||||
@ -98,58 +110,82 @@ func (encoder *funcEncoder) IsEmpty(ptr unsafe.Pointer) bool {
|
||||
return encoder.isEmptyFunc(ptr)
|
||||
}
|
||||
|
||||
// DecoderFunc the function form of TypeDecoder
|
||||
type DecoderFunc func(ptr unsafe.Pointer, iter *Iterator)
|
||||
|
||||
// EncoderFunc the function form of TypeEncoder
|
||||
type EncoderFunc func(ptr unsafe.Pointer, stream *Stream)
|
||||
|
||||
// RegisterTypeDecoderFunc register TypeDecoder for a type with function
|
||||
func RegisterTypeDecoderFunc(typ string, fun DecoderFunc) {
|
||||
typeDecoders[typ] = &funcDecoder{fun}
|
||||
}
|
||||
|
||||
// RegisterTypeDecoder register TypeDecoder for a typ
|
||||
func RegisterTypeDecoder(typ string, decoder ValDecoder) {
|
||||
typeDecoders[typ] = decoder
|
||||
}
|
||||
|
||||
// RegisterFieldDecoderFunc register TypeDecoder for a struct field with function
|
||||
func RegisterFieldDecoderFunc(typ string, field string, fun DecoderFunc) {
|
||||
RegisterFieldDecoder(typ, field, &funcDecoder{fun})
|
||||
}
|
||||
|
||||
// RegisterFieldDecoder register TypeDecoder for a struct field
|
||||
func RegisterFieldDecoder(typ string, field string, decoder ValDecoder) {
|
||||
fieldDecoders[fmt.Sprintf("%s/%s", typ, field)] = decoder
|
||||
}
|
||||
|
||||
// RegisterTypeEncoderFunc register TypeEncoder for a type with encode/isEmpty function
|
||||
func RegisterTypeEncoderFunc(typ string, fun EncoderFunc, isEmptyFunc func(unsafe.Pointer) bool) {
|
||||
typeEncoders[typ] = &funcEncoder{fun, isEmptyFunc}
|
||||
}
|
||||
|
||||
// RegisterTypeEncoder register TypeEncoder for a type
|
||||
func RegisterTypeEncoder(typ string, encoder ValEncoder) {
|
||||
typeEncoders[typ] = encoder
|
||||
}
|
||||
|
||||
// RegisterFieldEncoderFunc register TypeEncoder for a struct field with encode/isEmpty function
|
||||
func RegisterFieldEncoderFunc(typ string, field string, fun EncoderFunc, isEmptyFunc func(unsafe.Pointer) bool) {
|
||||
RegisterFieldEncoder(typ, field, &funcEncoder{fun, isEmptyFunc})
|
||||
}
|
||||
|
||||
// RegisterFieldEncoder register TypeEncoder for a struct field
|
||||
func RegisterFieldEncoder(typ string, field string, encoder ValEncoder) {
|
||||
fieldEncoders[fmt.Sprintf("%s/%s", typ, field)] = encoder
|
||||
}
|
||||
|
||||
// RegisterExtension register extension
|
||||
func RegisterExtension(extension Extension) {
|
||||
extensions = append(extensions, extension)
|
||||
}
|
||||
|
||||
func getTypeDecoderFromExtension(typ reflect.Type) ValDecoder {
|
||||
decoder := _getTypeDecoderFromExtension(typ)
|
||||
func getTypeDecoderFromExtension(cfg *frozenConfig, typ reflect.Type) ValDecoder {
|
||||
decoder := _getTypeDecoderFromExtension(cfg, typ)
|
||||
if decoder != nil {
|
||||
for _, extension := range extensions {
|
||||
decoder = extension.DecorateDecoder(typ, decoder)
|
||||
}
|
||||
for _, extension := range cfg.extensions {
|
||||
decoder = extension.DecorateDecoder(typ, decoder)
|
||||
}
|
||||
}
|
||||
return decoder
|
||||
}
|
||||
func _getTypeDecoderFromExtension(typ reflect.Type) ValDecoder {
|
||||
func _getTypeDecoderFromExtension(cfg *frozenConfig, typ reflect.Type) ValDecoder {
|
||||
for _, extension := range extensions {
|
||||
decoder := extension.CreateDecoder(typ)
|
||||
if decoder != nil {
|
||||
return decoder
|
||||
}
|
||||
}
|
||||
for _, extension := range cfg.extensions {
|
||||
decoder := extension.CreateDecoder(typ)
|
||||
if decoder != nil {
|
||||
return decoder
|
||||
}
|
||||
}
|
||||
typeName := typ.String()
|
||||
decoder := typeDecoders[typeName]
|
||||
if decoder != nil {
|
||||
@ -158,29 +194,38 @@ func _getTypeDecoderFromExtension(typ reflect.Type) ValDecoder {
|
||||
if typ.Kind() == reflect.Ptr {
|
||||
decoder := typeDecoders[typ.Elem().String()]
|
||||
if decoder != nil {
|
||||
return &optionalDecoder{typ.Elem(), decoder}
|
||||
return &OptionalDecoder{typ.Elem(), decoder}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func getTypeEncoderFromExtension(typ reflect.Type) ValEncoder {
|
||||
encoder := _getTypeEncoderFromExtension(typ)
|
||||
func getTypeEncoderFromExtension(cfg *frozenConfig, typ reflect.Type) ValEncoder {
|
||||
encoder := _getTypeEncoderFromExtension(cfg, typ)
|
||||
if encoder != nil {
|
||||
for _, extension := range extensions {
|
||||
encoder = extension.DecorateEncoder(typ, encoder)
|
||||
}
|
||||
for _, extension := range cfg.extensions {
|
||||
encoder = extension.DecorateEncoder(typ, encoder)
|
||||
}
|
||||
}
|
||||
return encoder
|
||||
}
|
||||
|
||||
func _getTypeEncoderFromExtension(typ reflect.Type) ValEncoder {
|
||||
func _getTypeEncoderFromExtension(cfg *frozenConfig, typ reflect.Type) ValEncoder {
|
||||
for _, extension := range extensions {
|
||||
encoder := extension.CreateEncoder(typ)
|
||||
if encoder != nil {
|
||||
return encoder
|
||||
}
|
||||
}
|
||||
for _, extension := range cfg.extensions {
|
||||
encoder := extension.CreateEncoder(typ)
|
||||
if encoder != nil {
|
||||
return encoder
|
||||
}
|
||||
}
|
||||
typeName := typ.String()
|
||||
encoder := typeEncoders[typeName]
|
||||
if encoder != nil {
|
||||
@ -189,23 +234,28 @@ func _getTypeEncoderFromExtension(typ reflect.Type) ValEncoder {
|
||||
if typ.Kind() == reflect.Ptr {
|
||||
encoder := typeEncoders[typ.Elem().String()]
|
||||
if encoder != nil {
|
||||
return &optionalEncoder{encoder}
|
||||
return &OptionalEncoder{encoder}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func describeStruct(cfg *frozenConfig, typ reflect.Type) (*StructDescriptor, error) {
|
||||
func describeStruct(cfg *frozenConfig, prefix string, typ reflect.Type) *StructDescriptor {
|
||||
embeddedBindings := []*Binding{}
|
||||
bindings := []*Binding{}
|
||||
for i := 0; i < typ.NumField(); i++ {
|
||||
field := typ.Field(i)
|
||||
if field.Anonymous && (field.Tag.Get("json") == "" || strings.Split(field.Tag.Get("json"), ",")[0] == "") {
|
||||
tag, hastag := field.Tag.Lookup(cfg.getTagKey())
|
||||
if cfg.onlyTaggedField && !hastag {
|
||||
continue
|
||||
}
|
||||
tagParts := strings.Split(tag, ",")
|
||||
if tag == "-" {
|
||||
continue
|
||||
}
|
||||
if field.Anonymous && (tag == "" || tagParts[0] == "") {
|
||||
if field.Type.Kind() == reflect.Struct {
|
||||
structDescriptor, err := describeStruct(cfg, field.Type)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
structDescriptor := describeStruct(cfg, prefix, field.Type)
|
||||
for _, binding := range structDescriptor.Fields {
|
||||
binding.levels = append([]int{i}, binding.levels...)
|
||||
omitempty := binding.Encoder.(*structFieldEncoder).omitempty
|
||||
@ -215,43 +265,32 @@ func describeStruct(cfg *frozenConfig, typ reflect.Type) (*StructDescriptor, err
|
||||
}
|
||||
continue
|
||||
} else if field.Type.Kind() == reflect.Ptr && field.Type.Elem().Kind() == reflect.Struct {
|
||||
structDescriptor, err := describeStruct(cfg, field.Type.Elem())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
structDescriptor := describeStruct(cfg, prefix, field.Type.Elem())
|
||||
for _, binding := range structDescriptor.Fields {
|
||||
binding.levels = append([]int{i}, binding.levels...)
|
||||
omitempty := binding.Encoder.(*structFieldEncoder).omitempty
|
||||
binding.Encoder = &optionalEncoder{binding.Encoder}
|
||||
binding.Encoder = &dereferenceEncoder{binding.Encoder}
|
||||
binding.Encoder = &structFieldEncoder{&field, binding.Encoder, omitempty}
|
||||
binding.Decoder = &deferenceDecoder{field.Type.Elem(), binding.Decoder}
|
||||
binding.Decoder = &dereferenceDecoder{field.Type.Elem(), binding.Decoder}
|
||||
binding.Decoder = &structFieldDecoder{&field, binding.Decoder}
|
||||
embeddedBindings = append(embeddedBindings, binding)
|
||||
}
|
||||
continue
|
||||
}
|
||||
}
|
||||
tagParts := strings.Split(field.Tag.Get("json"), ",")
|
||||
fieldNames := calcFieldNames(field.Name, tagParts[0], string(field.Tag.Get("json")))
|
||||
fieldNames := calcFieldNames(field.Name, tagParts[0], tag)
|
||||
fieldCacheKey := fmt.Sprintf("%s/%s", typ.String(), field.Name)
|
||||
decoder := fieldDecoders[fieldCacheKey]
|
||||
if decoder == nil {
|
||||
var err error
|
||||
decoder, err = decoderOfType(cfg, field.Type)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
decoder = decoderOfType(cfg, prefix+typ.String()+"."+field.Name+"->", field.Type)
|
||||
}
|
||||
encoder := fieldEncoders[fieldCacheKey]
|
||||
if encoder == nil {
|
||||
var err error
|
||||
encoder, err = encoderOfType(cfg, field.Type)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// map is stored as pointer in the struct
|
||||
if field.Type.Kind() == reflect.Map {
|
||||
encoder = &optionalEncoder{encoder}
|
||||
encoder = encoderOfType(cfg, prefix+typ.String()+"."+field.Name+"->", field.Type)
|
||||
// map is stored as pointer in the struct,
|
||||
// and treat nil or empty map as empty field
|
||||
if encoder != nil && field.Type.Kind() == reflect.Map {
|
||||
encoder = &optionalMapEncoder{encoder}
|
||||
}
|
||||
}
|
||||
binding := &Binding{
|
||||
@ -264,7 +303,7 @@ func describeStruct(cfg *frozenConfig, typ reflect.Type) (*StructDescriptor, err
|
||||
binding.levels = []int{i}
|
||||
bindings = append(bindings, binding)
|
||||
}
|
||||
return createStructDescriptor(cfg, typ, bindings, embeddedBindings), nil
|
||||
return createStructDescriptor(cfg, typ, bindings, embeddedBindings)
|
||||
}
|
||||
func createStructDescriptor(cfg *frozenConfig, typ reflect.Type, bindings []*Binding, embeddedBindings []*Binding) *StructDescriptor {
|
||||
onePtrEmbedded := false
|
||||
@ -292,6 +331,9 @@ func createStructDescriptor(cfg *frozenConfig, typ reflect.Type, bindings []*Bin
|
||||
for _, extension := range extensions {
|
||||
extension.UpdateStructDescriptor(structDescriptor)
|
||||
}
|
||||
for _, extension := range cfg.extensions {
|
||||
extension.UpdateStructDescriptor(structDescriptor)
|
||||
}
|
||||
processTags(structDescriptor, cfg)
|
||||
// merge normal & embedded bindings & sort with original order
|
||||
allBindings := sortableBindings(append(embeddedBindings, structDescriptor.Fields...))
|
||||
@ -342,7 +384,7 @@ func (bindings sortableBindings) Swap(i, j int) {
|
||||
func processTags(structDescriptor *StructDescriptor, cfg *frozenConfig) {
|
||||
for _, binding := range structDescriptor.Fields {
|
||||
shouldOmitEmpty := false
|
||||
tagParts := strings.Split(binding.Field.Tag.Get("json"), ",")
|
||||
tagParts := strings.Split(binding.Field.Tag.Get(cfg.getTagKey()), ",")
|
||||
for _, tagPart := range tagParts[1:] {
|
||||
if tagPart == "omitempty" {
|
||||
shouldOmitEmpty = true
|
||||
|
@ -9,6 +9,22 @@ import (
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
func decoderOfMap(cfg *frozenConfig, prefix string, typ reflect.Type) ValDecoder {
|
||||
decoder := decoderOfType(cfg, prefix+"[map]->", typ.Elem())
|
||||
mapInterface := reflect.New(typ).Interface()
|
||||
return &mapDecoder{typ, typ.Key(), typ.Elem(), decoder, extractInterface(mapInterface)}
|
||||
}
|
||||
|
||||
func encoderOfMap(cfg *frozenConfig, prefix string, typ reflect.Type) ValEncoder {
|
||||
elemType := typ.Elem()
|
||||
encoder := encoderOfType(cfg, prefix+"[map]->", elemType)
|
||||
mapInterface := reflect.New(typ).Elem().Interface()
|
||||
if cfg.sortMapKeys {
|
||||
return &sortKeysMapEncoder{typ, elemType, encoder, *((*emptyInterface)(unsafe.Pointer(&mapInterface)))}
|
||||
}
|
||||
return &mapEncoder{typ, elemType, encoder, *((*emptyInterface)(unsafe.Pointer(&mapInterface)))}
|
||||
}
|
||||
|
||||
type mapDecoder struct {
|
||||
mapType reflect.Type
|
||||
keyType reflect.Type
|
||||
@ -32,7 +48,7 @@ func (decoder *mapDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
}
|
||||
iter.ReadMapCB(func(iter *Iterator, keyStr string) bool {
|
||||
elem := reflect.New(decoder.elemType)
|
||||
decoder.elemDecoder.Decode(unsafe.Pointer(elem.Pointer()), iter)
|
||||
decoder.elemDecoder.Decode(extractInterface(elem.Interface()).word, iter)
|
||||
// to put into map, we have to use reflection
|
||||
keyType := decoder.keyType
|
||||
// TODO: remove this from loop
|
||||
@ -101,7 +117,11 @@ func (encoder *mapEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||
stream.WriteMore()
|
||||
}
|
||||
encodeMapKey(key, stream)
|
||||
stream.writeByte(':')
|
||||
if stream.indention > 0 {
|
||||
stream.writeTwoBytes(byte(':'), byte(' '))
|
||||
} else {
|
||||
stream.writeByte(':')
|
||||
}
|
||||
val := realVal.MapIndex(key).Interface()
|
||||
encoder.elemEncoder.EncodeInterface(val, stream)
|
||||
}
|
||||
@ -136,7 +156,7 @@ func encodeMapKey(key reflect.Value, stream *Stream) {
|
||||
stream.writeByte('"')
|
||||
return
|
||||
}
|
||||
stream.Error = &json.UnsupportedTypeError{key.Type()}
|
||||
stream.Error = &json.UnsupportedTypeError{Type: key.Type()}
|
||||
}
|
||||
|
||||
func (encoder *mapEncoder) EncodeInterface(val interface{}, stream *Stream) {
|
||||
@ -182,7 +202,11 @@ func (encoder *sortKeysMapEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||
stream.WriteMore()
|
||||
}
|
||||
stream.WriteVal(key.s) // might need html escape, so can not WriteString directly
|
||||
stream.writeByte(':')
|
||||
if stream.indention > 0 {
|
||||
stream.writeTwoBytes(byte(':'), byte(' '))
|
||||
} else {
|
||||
stream.writeByte(':')
|
||||
}
|
||||
val := realVal.MapIndex(key.v).Interface()
|
||||
encoder.elemEncoder.EncodeInterface(val, stream)
|
||||
}
|
||||
@ -216,7 +240,7 @@ func (w *reflectWithString) resolve() error {
|
||||
w.s = strconv.FormatUint(w.v.Uint(), 10)
|
||||
return nil
|
||||
}
|
||||
return &json.UnsupportedTypeError{w.v.Type()}
|
||||
return &json.UnsupportedTypeError{Type: w.v.Type()}
|
||||
}
|
||||
|
||||
func (sv stringValues) Len() int { return len(sv) }
|
||||
|
@ -32,15 +32,17 @@ type intCodec struct {
|
||||
}
|
||||
|
||||
func (codec *intCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
*((*int)(ptr)) = iter.ReadInt()
|
||||
if !iter.ReadNil() {
|
||||
*((*int)(ptr)) = iter.ReadInt()
|
||||
}
|
||||
}
|
||||
|
||||
func (codec *intCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||
stream.WriteInt(*((*int)(ptr)))
|
||||
}
|
||||
|
||||
func (encoder *intCodec) EncodeInterface(val interface{}, stream *Stream) {
|
||||
WriteToStream(val, stream, encoder)
|
||||
func (codec *intCodec) EncodeInterface(val interface{}, stream *Stream) {
|
||||
WriteToStream(val, stream, codec)
|
||||
}
|
||||
|
||||
func (codec *intCodec) IsEmpty(ptr unsafe.Pointer) bool {
|
||||
@ -51,15 +53,17 @@ type uintptrCodec struct {
|
||||
}
|
||||
|
||||
func (codec *uintptrCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
*((*uintptr)(ptr)) = uintptr(iter.ReadUint64())
|
||||
if !iter.ReadNil() {
|
||||
*((*uintptr)(ptr)) = uintptr(iter.ReadUint64())
|
||||
}
|
||||
}
|
||||
|
||||
func (codec *uintptrCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||
stream.WriteUint64(uint64(*((*uintptr)(ptr))))
|
||||
}
|
||||
|
||||
func (encoder *uintptrCodec) EncodeInterface(val interface{}, stream *Stream) {
|
||||
WriteToStream(val, stream, encoder)
|
||||
func (codec *uintptrCodec) EncodeInterface(val interface{}, stream *Stream) {
|
||||
WriteToStream(val, stream, codec)
|
||||
}
|
||||
|
||||
func (codec *uintptrCodec) IsEmpty(ptr unsafe.Pointer) bool {
|
||||
@ -70,15 +74,17 @@ type int8Codec struct {
|
||||
}
|
||||
|
||||
func (codec *int8Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
*((*int8)(ptr)) = iter.ReadInt8()
|
||||
if !iter.ReadNil() {
|
||||
*((*int8)(ptr)) = iter.ReadInt8()
|
||||
}
|
||||
}
|
||||
|
||||
func (codec *int8Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||
stream.WriteInt8(*((*int8)(ptr)))
|
||||
}
|
||||
|
||||
func (encoder *int8Codec) EncodeInterface(val interface{}, stream *Stream) {
|
||||
WriteToStream(val, stream, encoder)
|
||||
func (codec *int8Codec) EncodeInterface(val interface{}, stream *Stream) {
|
||||
WriteToStream(val, stream, codec)
|
||||
}
|
||||
|
||||
func (codec *int8Codec) IsEmpty(ptr unsafe.Pointer) bool {
|
||||
@ -89,15 +95,17 @@ type int16Codec struct {
|
||||
}
|
||||
|
||||
func (codec *int16Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
*((*int16)(ptr)) = iter.ReadInt16()
|
||||
if !iter.ReadNil() {
|
||||
*((*int16)(ptr)) = iter.ReadInt16()
|
||||
}
|
||||
}
|
||||
|
||||
func (codec *int16Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||
stream.WriteInt16(*((*int16)(ptr)))
|
||||
}
|
||||
|
||||
func (encoder *int16Codec) EncodeInterface(val interface{}, stream *Stream) {
|
||||
WriteToStream(val, stream, encoder)
|
||||
func (codec *int16Codec) EncodeInterface(val interface{}, stream *Stream) {
|
||||
WriteToStream(val, stream, codec)
|
||||
}
|
||||
|
||||
func (codec *int16Codec) IsEmpty(ptr unsafe.Pointer) bool {
|
||||
@ -108,15 +116,17 @@ type int32Codec struct {
|
||||
}
|
||||
|
||||
func (codec *int32Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
*((*int32)(ptr)) = iter.ReadInt32()
|
||||
if !iter.ReadNil() {
|
||||
*((*int32)(ptr)) = iter.ReadInt32()
|
||||
}
|
||||
}
|
||||
|
||||
func (codec *int32Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||
stream.WriteInt32(*((*int32)(ptr)))
|
||||
}
|
||||
|
||||
func (encoder *int32Codec) EncodeInterface(val interface{}, stream *Stream) {
|
||||
WriteToStream(val, stream, encoder)
|
||||
func (codec *int32Codec) EncodeInterface(val interface{}, stream *Stream) {
|
||||
WriteToStream(val, stream, codec)
|
||||
}
|
||||
|
||||
func (codec *int32Codec) IsEmpty(ptr unsafe.Pointer) bool {
|
||||
@ -127,15 +137,17 @@ type int64Codec struct {
|
||||
}
|
||||
|
||||
func (codec *int64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
*((*int64)(ptr)) = iter.ReadInt64()
|
||||
if !iter.ReadNil() {
|
||||
*((*int64)(ptr)) = iter.ReadInt64()
|
||||
}
|
||||
}
|
||||
|
||||
func (codec *int64Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||
stream.WriteInt64(*((*int64)(ptr)))
|
||||
}
|
||||
|
||||
func (encoder *int64Codec) EncodeInterface(val interface{}, stream *Stream) {
|
||||
WriteToStream(val, stream, encoder)
|
||||
func (codec *int64Codec) EncodeInterface(val interface{}, stream *Stream) {
|
||||
WriteToStream(val, stream, codec)
|
||||
}
|
||||
|
||||
func (codec *int64Codec) IsEmpty(ptr unsafe.Pointer) bool {
|
||||
@ -146,15 +158,18 @@ type uintCodec struct {
|
||||
}
|
||||
|
||||
func (codec *uintCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
*((*uint)(ptr)) = iter.ReadUint()
|
||||
if !iter.ReadNil() {
|
||||
*((*uint)(ptr)) = iter.ReadUint()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (codec *uintCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||
stream.WriteUint(*((*uint)(ptr)))
|
||||
}
|
||||
|
||||
func (encoder *uintCodec) EncodeInterface(val interface{}, stream *Stream) {
|
||||
WriteToStream(val, stream, encoder)
|
||||
func (codec *uintCodec) EncodeInterface(val interface{}, stream *Stream) {
|
||||
WriteToStream(val, stream, codec)
|
||||
}
|
||||
|
||||
func (codec *uintCodec) IsEmpty(ptr unsafe.Pointer) bool {
|
||||
@ -165,15 +180,17 @@ type uint8Codec struct {
|
||||
}
|
||||
|
||||
func (codec *uint8Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
*((*uint8)(ptr)) = iter.ReadUint8()
|
||||
if !iter.ReadNil() {
|
||||
*((*uint8)(ptr)) = iter.ReadUint8()
|
||||
}
|
||||
}
|
||||
|
||||
func (codec *uint8Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||
stream.WriteUint8(*((*uint8)(ptr)))
|
||||
}
|
||||
|
||||
func (encoder *uint8Codec) EncodeInterface(val interface{}, stream *Stream) {
|
||||
WriteToStream(val, stream, encoder)
|
||||
func (codec *uint8Codec) EncodeInterface(val interface{}, stream *Stream) {
|
||||
WriteToStream(val, stream, codec)
|
||||
}
|
||||
|
||||
func (codec *uint8Codec) IsEmpty(ptr unsafe.Pointer) bool {
|
||||
@ -183,16 +200,18 @@ func (codec *uint8Codec) IsEmpty(ptr unsafe.Pointer) bool {
|
||||
type uint16Codec struct {
|
||||
}
|
||||
|
||||
func (decoder *uint16Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
*((*uint16)(ptr)) = iter.ReadUint16()
|
||||
func (codec *uint16Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
if !iter.ReadNil() {
|
||||
*((*uint16)(ptr)) = iter.ReadUint16()
|
||||
}
|
||||
}
|
||||
|
||||
func (codec *uint16Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||
stream.WriteUint16(*((*uint16)(ptr)))
|
||||
}
|
||||
|
||||
func (encoder *uint16Codec) EncodeInterface(val interface{}, stream *Stream) {
|
||||
WriteToStream(val, stream, encoder)
|
||||
func (codec *uint16Codec) EncodeInterface(val interface{}, stream *Stream) {
|
||||
WriteToStream(val, stream, codec)
|
||||
}
|
||||
|
||||
func (codec *uint16Codec) IsEmpty(ptr unsafe.Pointer) bool {
|
||||
@ -203,15 +222,17 @@ type uint32Codec struct {
|
||||
}
|
||||
|
||||
func (codec *uint32Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
*((*uint32)(ptr)) = iter.ReadUint32()
|
||||
if !iter.ReadNil() {
|
||||
*((*uint32)(ptr)) = iter.ReadUint32()
|
||||
}
|
||||
}
|
||||
|
||||
func (codec *uint32Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||
stream.WriteUint32(*((*uint32)(ptr)))
|
||||
}
|
||||
|
||||
func (encoder *uint32Codec) EncodeInterface(val interface{}, stream *Stream) {
|
||||
WriteToStream(val, stream, encoder)
|
||||
func (codec *uint32Codec) EncodeInterface(val interface{}, stream *Stream) {
|
||||
WriteToStream(val, stream, codec)
|
||||
}
|
||||
|
||||
func (codec *uint32Codec) IsEmpty(ptr unsafe.Pointer) bool {
|
||||
@ -222,15 +243,17 @@ type uint64Codec struct {
|
||||
}
|
||||
|
||||
func (codec *uint64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
*((*uint64)(ptr)) = iter.ReadUint64()
|
||||
if !iter.ReadNil() {
|
||||
*((*uint64)(ptr)) = iter.ReadUint64()
|
||||
}
|
||||
}
|
||||
|
||||
func (codec *uint64Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||
stream.WriteUint64(*((*uint64)(ptr)))
|
||||
}
|
||||
|
||||
func (encoder *uint64Codec) EncodeInterface(val interface{}, stream *Stream) {
|
||||
WriteToStream(val, stream, encoder)
|
||||
func (codec *uint64Codec) EncodeInterface(val interface{}, stream *Stream) {
|
||||
WriteToStream(val, stream, codec)
|
||||
}
|
||||
|
||||
func (codec *uint64Codec) IsEmpty(ptr unsafe.Pointer) bool {
|
||||
@ -241,15 +264,17 @@ type float32Codec struct {
|
||||
}
|
||||
|
||||
func (codec *float32Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
*((*float32)(ptr)) = iter.ReadFloat32()
|
||||
if !iter.ReadNil() {
|
||||
*((*float32)(ptr)) = iter.ReadFloat32()
|
||||
}
|
||||
}
|
||||
|
||||
func (codec *float32Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||
stream.WriteFloat32(*((*float32)(ptr)))
|
||||
}
|
||||
|
||||
func (encoder *float32Codec) EncodeInterface(val interface{}, stream *Stream) {
|
||||
WriteToStream(val, stream, encoder)
|
||||
func (codec *float32Codec) EncodeInterface(val interface{}, stream *Stream) {
|
||||
WriteToStream(val, stream, codec)
|
||||
}
|
||||
|
||||
func (codec *float32Codec) IsEmpty(ptr unsafe.Pointer) bool {
|
||||
@ -260,15 +285,17 @@ type float64Codec struct {
|
||||
}
|
||||
|
||||
func (codec *float64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
*((*float64)(ptr)) = iter.ReadFloat64()
|
||||
if !iter.ReadNil() {
|
||||
*((*float64)(ptr)) = iter.ReadFloat64()
|
||||
}
|
||||
}
|
||||
|
||||
func (codec *float64Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||
stream.WriteFloat64(*((*float64)(ptr)))
|
||||
}
|
||||
|
||||
func (encoder *float64Codec) EncodeInterface(val interface{}, stream *Stream) {
|
||||
WriteToStream(val, stream, encoder)
|
||||
func (codec *float64Codec) EncodeInterface(val interface{}, stream *Stream) {
|
||||
WriteToStream(val, stream, codec)
|
||||
}
|
||||
|
||||
func (codec *float64Codec) IsEmpty(ptr unsafe.Pointer) bool {
|
||||
@ -279,15 +306,17 @@ type boolCodec struct {
|
||||
}
|
||||
|
||||
func (codec *boolCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
*((*bool)(ptr)) = iter.ReadBool()
|
||||
if !iter.ReadNil() {
|
||||
*((*bool)(ptr)) = iter.ReadBool()
|
||||
}
|
||||
}
|
||||
|
||||
func (codec *boolCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||
stream.WriteBool(*((*bool)(ptr)))
|
||||
}
|
||||
|
||||
func (encoder *boolCodec) EncodeInterface(val interface{}, stream *Stream) {
|
||||
WriteToStream(val, stream, encoder)
|
||||
func (codec *boolCodec) EncodeInterface(val interface{}, stream *Stream) {
|
||||
WriteToStream(val, stream, codec)
|
||||
}
|
||||
|
||||
func (codec *boolCodec) IsEmpty(ptr unsafe.Pointer) bool {
|
||||
@ -298,25 +327,66 @@ type emptyInterfaceCodec struct {
|
||||
}
|
||||
|
||||
func (codec *emptyInterfaceCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
*((*interface{})(ptr)) = iter.Read()
|
||||
existing := *((*interface{})(ptr))
|
||||
|
||||
// Checking for both typed and untyped nil pointers.
|
||||
if existing != nil &&
|
||||
reflect.TypeOf(existing).Kind() == reflect.Ptr &&
|
||||
!reflect.ValueOf(existing).IsNil() {
|
||||
|
||||
var ptrToExisting interface{}
|
||||
for {
|
||||
elem := reflect.ValueOf(existing).Elem()
|
||||
if elem.Kind() != reflect.Ptr || elem.IsNil() {
|
||||
break
|
||||
}
|
||||
ptrToExisting = existing
|
||||
existing = elem.Interface()
|
||||
}
|
||||
|
||||
if iter.ReadNil() {
|
||||
if ptrToExisting != nil {
|
||||
nilPtr := reflect.Zero(reflect.TypeOf(ptrToExisting).Elem())
|
||||
reflect.ValueOf(ptrToExisting).Elem().Set(nilPtr)
|
||||
} else {
|
||||
*((*interface{})(ptr)) = nil
|
||||
}
|
||||
} else {
|
||||
iter.ReadVal(existing)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if iter.ReadNil() {
|
||||
*((*interface{})(ptr)) = nil
|
||||
} else {
|
||||
*((*interface{})(ptr)) = iter.Read()
|
||||
}
|
||||
}
|
||||
|
||||
func (codec *emptyInterfaceCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||
stream.WriteVal(*((*interface{})(ptr)))
|
||||
}
|
||||
|
||||
func (encoder *emptyInterfaceCodec) EncodeInterface(val interface{}, stream *Stream) {
|
||||
func (codec *emptyInterfaceCodec) EncodeInterface(val interface{}, stream *Stream) {
|
||||
stream.WriteVal(val)
|
||||
}
|
||||
|
||||
func (codec *emptyInterfaceCodec) IsEmpty(ptr unsafe.Pointer) bool {
|
||||
return ptr == nil
|
||||
emptyInterface := (*emptyInterface)(ptr)
|
||||
return emptyInterface.typ == nil
|
||||
}
|
||||
|
||||
type nonEmptyInterfaceCodec struct {
|
||||
}
|
||||
|
||||
func (codec *nonEmptyInterfaceCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
if iter.WhatIsNext() == NilValue {
|
||||
iter.skipFourBytes('n', 'u', 'l', 'l')
|
||||
*((*interface{})(ptr)) = nil
|
||||
return
|
||||
}
|
||||
nonEmptyInterface := (*nonEmptyInterface)(ptr)
|
||||
if nonEmptyInterface.itab == nil {
|
||||
iter.ReportError("read non-empty interface", "do not know which concrete type to decode to")
|
||||
@ -327,19 +397,24 @@ func (codec *nonEmptyInterfaceCodec) Decode(ptr unsafe.Pointer, iter *Iterator)
|
||||
e.typ = nonEmptyInterface.itab.typ
|
||||
e.word = nonEmptyInterface.word
|
||||
iter.ReadVal(&i)
|
||||
if e.word == nil {
|
||||
nonEmptyInterface.itab = nil
|
||||
}
|
||||
nonEmptyInterface.word = e.word
|
||||
}
|
||||
|
||||
func (codec *nonEmptyInterfaceCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||
nonEmptyInterface := (*nonEmptyInterface)(ptr)
|
||||
var i interface{}
|
||||
e := (*emptyInterface)(unsafe.Pointer(&i))
|
||||
e.typ = nonEmptyInterface.itab.typ
|
||||
e.word = nonEmptyInterface.word
|
||||
if nonEmptyInterface.itab != nil {
|
||||
e := (*emptyInterface)(unsafe.Pointer(&i))
|
||||
e.typ = nonEmptyInterface.itab.typ
|
||||
e.word = nonEmptyInterface.word
|
||||
}
|
||||
stream.WriteVal(i)
|
||||
}
|
||||
|
||||
func (encoder *nonEmptyInterfaceCodec) EncodeInterface(val interface{}, stream *Stream) {
|
||||
func (codec *nonEmptyInterfaceCodec) EncodeInterface(val interface{}, stream *Stream) {
|
||||
stream.WriteVal(val)
|
||||
}
|
||||
|
||||
@ -359,11 +434,11 @@ func (codec *anyCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||
(*((*Any)(ptr))).WriteTo(stream)
|
||||
}
|
||||
|
||||
func (encoder *anyCodec) EncodeInterface(val interface{}, stream *Stream) {
|
||||
func (codec *anyCodec) EncodeInterface(val interface{}, stream *Stream) {
|
||||
(val.(Any)).WriteTo(stream)
|
||||
}
|
||||
|
||||
func (encoder *anyCodec) IsEmpty(ptr unsafe.Pointer) bool {
|
||||
func (codec *anyCodec) IsEmpty(ptr unsafe.Pointer) bool {
|
||||
return (*((*Any)(ptr))).Size() == 0
|
||||
}
|
||||
|
||||
@ -371,21 +446,76 @@ type jsonNumberCodec struct {
|
||||
}
|
||||
|
||||
func (codec *jsonNumberCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
*((*json.Number)(ptr)) = json.Number([]byte(iter.readNumberAsString()))
|
||||
switch iter.WhatIsNext() {
|
||||
case StringValue:
|
||||
*((*json.Number)(ptr)) = json.Number(iter.ReadString())
|
||||
case NilValue:
|
||||
iter.skipFourBytes('n', 'u', 'l', 'l')
|
||||
*((*json.Number)(ptr)) = ""
|
||||
default:
|
||||
*((*json.Number)(ptr)) = json.Number([]byte(iter.readNumberAsString()))
|
||||
}
|
||||
}
|
||||
|
||||
func (codec *jsonNumberCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||
stream.WriteRaw(string(*((*json.Number)(ptr))))
|
||||
number := *((*json.Number)(ptr))
|
||||
if len(number) == 0 {
|
||||
stream.WriteRaw("0")
|
||||
} else {
|
||||
stream.WriteRaw(string(number))
|
||||
}
|
||||
}
|
||||
|
||||
func (encoder *jsonNumberCodec) EncodeInterface(val interface{}, stream *Stream) {
|
||||
stream.WriteRaw(string(val.(json.Number)))
|
||||
func (codec *jsonNumberCodec) EncodeInterface(val interface{}, stream *Stream) {
|
||||
number := val.(json.Number)
|
||||
if len(number) == 0 {
|
||||
stream.WriteRaw("0")
|
||||
} else {
|
||||
stream.WriteRaw(string(number))
|
||||
}
|
||||
}
|
||||
|
||||
func (encoder *jsonNumberCodec) IsEmpty(ptr unsafe.Pointer) bool {
|
||||
func (codec *jsonNumberCodec) IsEmpty(ptr unsafe.Pointer) bool {
|
||||
return len(*((*json.Number)(ptr))) == 0
|
||||
}
|
||||
|
||||
type jsoniterNumberCodec struct {
|
||||
}
|
||||
|
||||
func (codec *jsoniterNumberCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
switch iter.WhatIsNext() {
|
||||
case StringValue:
|
||||
*((*Number)(ptr)) = Number(iter.ReadString())
|
||||
case NilValue:
|
||||
iter.skipFourBytes('n', 'u', 'l', 'l')
|
||||
*((*Number)(ptr)) = ""
|
||||
default:
|
||||
*((*Number)(ptr)) = Number([]byte(iter.readNumberAsString()))
|
||||
}
|
||||
}
|
||||
|
||||
func (codec *jsoniterNumberCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||
number := *((*Number)(ptr))
|
||||
if len(number) == 0 {
|
||||
stream.WriteRaw("0")
|
||||
} else {
|
||||
stream.WriteRaw(string(number))
|
||||
}
|
||||
}
|
||||
|
||||
func (codec *jsoniterNumberCodec) EncodeInterface(val interface{}, stream *Stream) {
|
||||
number := val.(Number)
|
||||
if len(number) == 0 {
|
||||
stream.WriteRaw("0")
|
||||
} else {
|
||||
stream.WriteRaw(string(number))
|
||||
}
|
||||
}
|
||||
|
||||
func (codec *jsoniterNumberCodec) IsEmpty(ptr unsafe.Pointer) bool {
|
||||
return len(*((*Number)(ptr))) == 0
|
||||
}
|
||||
|
||||
type jsonRawMessageCodec struct {
|
||||
}
|
||||
|
||||
@ -397,11 +527,11 @@ func (codec *jsonRawMessageCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||
stream.WriteRaw(string(*((*json.RawMessage)(ptr))))
|
||||
}
|
||||
|
||||
func (encoder *jsonRawMessageCodec) EncodeInterface(val interface{}, stream *Stream) {
|
||||
func (codec *jsonRawMessageCodec) EncodeInterface(val interface{}, stream *Stream) {
|
||||
stream.WriteRaw(string(val.(json.RawMessage)))
|
||||
}
|
||||
|
||||
func (encoder *jsonRawMessageCodec) IsEmpty(ptr unsafe.Pointer) bool {
|
||||
func (codec *jsonRawMessageCodec) IsEmpty(ptr unsafe.Pointer) bool {
|
||||
return len(*((*json.RawMessage)(ptr))) == 0
|
||||
}
|
||||
|
||||
@ -416,16 +546,16 @@ func (codec *jsoniterRawMessageCodec) Encode(ptr unsafe.Pointer, stream *Stream)
|
||||
stream.WriteRaw(string(*((*RawMessage)(ptr))))
|
||||
}
|
||||
|
||||
func (encoder *jsoniterRawMessageCodec) EncodeInterface(val interface{}, stream *Stream) {
|
||||
func (codec *jsoniterRawMessageCodec) EncodeInterface(val interface{}, stream *Stream) {
|
||||
stream.WriteRaw(string(val.(RawMessage)))
|
||||
}
|
||||
|
||||
func (encoder *jsoniterRawMessageCodec) IsEmpty(ptr unsafe.Pointer) bool {
|
||||
func (codec *jsoniterRawMessageCodec) IsEmpty(ptr unsafe.Pointer) bool {
|
||||
return len(*((*RawMessage)(ptr))) == 0
|
||||
}
|
||||
|
||||
type base64Codec struct {
|
||||
actualType reflect.Type
|
||||
sliceDecoder ValDecoder
|
||||
}
|
||||
|
||||
func (codec *base64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
@ -436,21 +566,28 @@ func (codec *base64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
ptrSlice.Data = nil
|
||||
return
|
||||
}
|
||||
encoding := base64.StdEncoding
|
||||
src := iter.SkipAndReturnBytes()
|
||||
src = src[1 : len(src)-1]
|
||||
decodedLen := encoding.DecodedLen(len(src))
|
||||
dst := make([]byte, decodedLen)
|
||||
len, err := encoding.Decode(dst, src)
|
||||
if err != nil {
|
||||
iter.ReportError("decode base64", err.Error())
|
||||
} else {
|
||||
dst = dst[:len]
|
||||
dstSlice := (*sliceHeader)(unsafe.Pointer(&dst))
|
||||
ptrSlice := (*sliceHeader)(ptr)
|
||||
ptrSlice.Data = dstSlice.Data
|
||||
ptrSlice.Cap = dstSlice.Cap
|
||||
ptrSlice.Len = dstSlice.Len
|
||||
switch iter.WhatIsNext() {
|
||||
case StringValue:
|
||||
encoding := base64.StdEncoding
|
||||
src := iter.SkipAndReturnBytes()
|
||||
src = src[1 : len(src)-1]
|
||||
decodedLen := encoding.DecodedLen(len(src))
|
||||
dst := make([]byte, decodedLen)
|
||||
len, err := encoding.Decode(dst, src)
|
||||
if err != nil {
|
||||
iter.ReportError("decode base64", err.Error())
|
||||
} else {
|
||||
dst = dst[:len]
|
||||
dstSlice := (*sliceHeader)(unsafe.Pointer(&dst))
|
||||
ptrSlice := (*sliceHeader)(ptr)
|
||||
ptrSlice.Data = dstSlice.Data
|
||||
ptrSlice.Cap = dstSlice.Cap
|
||||
ptrSlice.Len = dstSlice.Len
|
||||
}
|
||||
case ArrayValue:
|
||||
codec.sliceDecoder.Decode(ptr, iter)
|
||||
default:
|
||||
iter.ReportError("base64Codec", "invalid input")
|
||||
}
|
||||
}
|
||||
|
||||
@ -462,14 +599,14 @@ func (codec *base64Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||
}
|
||||
encoding := base64.StdEncoding
|
||||
stream.writeByte('"')
|
||||
toGrow := encoding.EncodedLen(len(src))
|
||||
stream.ensure(toGrow)
|
||||
encoding.Encode(stream.buf[stream.n:], src)
|
||||
stream.n += toGrow
|
||||
size := encoding.EncodedLen(len(src))
|
||||
buf := make([]byte, size)
|
||||
encoding.Encode(buf, src)
|
||||
stream.buf = append(stream.buf, buf...)
|
||||
stream.writeByte('"')
|
||||
}
|
||||
|
||||
func (encoder *base64Codec) EncodeInterface(val interface{}, stream *Stream) {
|
||||
func (codec *base64Codec) EncodeInterface(val interface{}, stream *Stream) {
|
||||
ptr := extractInterface(val).word
|
||||
src := *((*[]byte)(ptr))
|
||||
if len(src) == 0 {
|
||||
@ -478,14 +615,14 @@ func (encoder *base64Codec) EncodeInterface(val interface{}, stream *Stream) {
|
||||
}
|
||||
encoding := base64.StdEncoding
|
||||
stream.writeByte('"')
|
||||
toGrow := encoding.EncodedLen(len(src))
|
||||
stream.ensure(toGrow)
|
||||
encoding.Encode(stream.buf[stream.n:], src)
|
||||
stream.n += toGrow
|
||||
size := encoding.EncodedLen(len(src))
|
||||
buf := make([]byte, size)
|
||||
encoding.Encode(buf, src)
|
||||
stream.buf = append(stream.buf, buf...)
|
||||
stream.writeByte('"')
|
||||
}
|
||||
|
||||
func (encoder *base64Codec) IsEmpty(ptr unsafe.Pointer) bool {
|
||||
func (codec *base64Codec) IsEmpty(ptr unsafe.Pointer) bool {
|
||||
return len(*((*[]byte)(ptr))) == 0
|
||||
}
|
||||
|
||||
@ -496,7 +633,7 @@ type stringModeNumberDecoder struct {
|
||||
func (decoder *stringModeNumberDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
c := iter.nextToken()
|
||||
if c != '"' {
|
||||
iter.ReportError("stringModeNumberDecoder", `expect "`)
|
||||
iter.ReportError("stringModeNumberDecoder", `expect ", but found `+string([]byte{c}))
|
||||
return
|
||||
}
|
||||
decoder.elemDecoder.Decode(ptr, iter)
|
||||
@ -505,7 +642,7 @@ func (decoder *stringModeNumberDecoder) Decode(ptr unsafe.Pointer, iter *Iterato
|
||||
}
|
||||
c = iter.readByte()
|
||||
if c != '"' {
|
||||
iter.ReportError("stringModeNumberDecoder", `expect "`)
|
||||
iter.ReportError("stringModeNumberDecoder", `expect ", but found `+string([]byte{c}))
|
||||
return
|
||||
}
|
||||
}
|
||||
@ -570,7 +707,12 @@ func (encoder *marshalerEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||
templateInterface := encoder.templateInterface
|
||||
templateInterface.word = ptr
|
||||
realInterface := (*interface{})(unsafe.Pointer(&templateInterface))
|
||||
marshaler := (*realInterface).(json.Marshaler)
|
||||
marshaler, ok := (*realInterface).(json.Marshaler)
|
||||
if !ok {
|
||||
stream.WriteVal(nil)
|
||||
return
|
||||
}
|
||||
|
||||
bytes, err := marshaler.MarshalJSON()
|
||||
if err != nil {
|
||||
stream.Error = err
|
||||
|
File diff suppressed because it is too large
Load Diff
124
feature_reflect_optional.go
Normal file
124
feature_reflect_optional.go
Normal file
@ -0,0 +1,124 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
func decoderOfOptional(cfg *frozenConfig, prefix string, typ reflect.Type) ValDecoder {
|
||||
elemType := typ.Elem()
|
||||
decoder := decoderOfType(cfg, prefix, elemType)
|
||||
return &OptionalDecoder{elemType, decoder}
|
||||
}
|
||||
|
||||
func encoderOfOptional(cfg *frozenConfig, prefix string, typ reflect.Type) ValEncoder {
|
||||
elemType := typ.Elem()
|
||||
elemEncoder := encoderOfType(cfg, prefix, elemType)
|
||||
encoder := &OptionalEncoder{elemEncoder}
|
||||
if elemType.Kind() == reflect.Map {
|
||||
encoder = &OptionalEncoder{encoder}
|
||||
}
|
||||
return encoder
|
||||
}
|
||||
|
||||
type OptionalDecoder struct {
|
||||
ValueType reflect.Type
|
||||
ValueDecoder ValDecoder
|
||||
}
|
||||
|
||||
func (decoder *OptionalDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
if iter.ReadNil() {
|
||||
*((*unsafe.Pointer)(ptr)) = nil
|
||||
} else {
|
||||
if *((*unsafe.Pointer)(ptr)) == nil {
|
||||
//pointer to null, we have to allocate memory to hold the value
|
||||
value := reflect.New(decoder.ValueType)
|
||||
newPtr := extractInterface(value.Interface()).word
|
||||
decoder.ValueDecoder.Decode(newPtr, iter)
|
||||
*((*uintptr)(ptr)) = uintptr(newPtr)
|
||||
} else {
|
||||
//reuse existing instance
|
||||
decoder.ValueDecoder.Decode(*((*unsafe.Pointer)(ptr)), iter)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type dereferenceDecoder struct {
|
||||
// only to deference a pointer
|
||||
valueType reflect.Type
|
||||
valueDecoder ValDecoder
|
||||
}
|
||||
|
||||
func (decoder *dereferenceDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
if *((*unsafe.Pointer)(ptr)) == nil {
|
||||
//pointer to null, we have to allocate memory to hold the value
|
||||
value := reflect.New(decoder.valueType)
|
||||
newPtr := extractInterface(value.Interface()).word
|
||||
decoder.valueDecoder.Decode(newPtr, iter)
|
||||
*((*uintptr)(ptr)) = uintptr(newPtr)
|
||||
} else {
|
||||
//reuse existing instance
|
||||
decoder.valueDecoder.Decode(*((*unsafe.Pointer)(ptr)), iter)
|
||||
}
|
||||
}
|
||||
|
||||
type OptionalEncoder struct {
|
||||
ValueEncoder ValEncoder
|
||||
}
|
||||
|
||||
func (encoder *OptionalEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||
if *((*unsafe.Pointer)(ptr)) == nil {
|
||||
stream.WriteNil()
|
||||
} else {
|
||||
encoder.ValueEncoder.Encode(*((*unsafe.Pointer)(ptr)), stream)
|
||||
}
|
||||
}
|
||||
|
||||
func (encoder *OptionalEncoder) EncodeInterface(val interface{}, stream *Stream) {
|
||||
WriteToStream(val, stream, encoder)
|
||||
}
|
||||
|
||||
func (encoder *OptionalEncoder) IsEmpty(ptr unsafe.Pointer) bool {
|
||||
return *((*unsafe.Pointer)(ptr)) == nil
|
||||
}
|
||||
|
||||
type dereferenceEncoder struct {
|
||||
ValueEncoder ValEncoder
|
||||
}
|
||||
|
||||
func (encoder *dereferenceEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||
if *((*unsafe.Pointer)(ptr)) == nil {
|
||||
stream.WriteNil()
|
||||
} else {
|
||||
encoder.ValueEncoder.Encode(*((*unsafe.Pointer)(ptr)), stream)
|
||||
}
|
||||
}
|
||||
|
||||
func (encoder *dereferenceEncoder) EncodeInterface(val interface{}, stream *Stream) {
|
||||
WriteToStream(val, stream, encoder)
|
||||
}
|
||||
|
||||
func (encoder *dereferenceEncoder) IsEmpty(ptr unsafe.Pointer) bool {
|
||||
return encoder.ValueEncoder.IsEmpty(*((*unsafe.Pointer)(ptr)))
|
||||
}
|
||||
|
||||
type optionalMapEncoder struct {
|
||||
valueEncoder ValEncoder
|
||||
}
|
||||
|
||||
func (encoder *optionalMapEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||
if *((*unsafe.Pointer)(ptr)) == nil {
|
||||
stream.WriteNil()
|
||||
} else {
|
||||
encoder.valueEncoder.Encode(*((*unsafe.Pointer)(ptr)), stream)
|
||||
}
|
||||
}
|
||||
|
||||
func (encoder *optionalMapEncoder) EncodeInterface(val interface{}, stream *Stream) {
|
||||
WriteToStream(val, stream, encoder)
|
||||
}
|
||||
|
||||
func (encoder *optionalMapEncoder) IsEmpty(ptr unsafe.Pointer) bool {
|
||||
p := *((*unsafe.Pointer)(ptr))
|
||||
return p == nil || encoder.valueEncoder.IsEmpty(p)
|
||||
}
|
@ -7,23 +7,17 @@ import (
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
func decoderOfSlice(cfg *frozenConfig, typ reflect.Type) (ValDecoder, error) {
|
||||
decoder, err := decoderOfType(cfg, typ.Elem())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &sliceDecoder{typ, typ.Elem(), decoder}, nil
|
||||
func decoderOfSlice(cfg *frozenConfig, prefix string, typ reflect.Type) ValDecoder {
|
||||
decoder := decoderOfType(cfg, prefix+"[slice]->", typ.Elem())
|
||||
return &sliceDecoder{typ, typ.Elem(), decoder}
|
||||
}
|
||||
|
||||
func encoderOfSlice(cfg *frozenConfig, typ reflect.Type) (ValEncoder, error) {
|
||||
encoder, err := encoderOfType(cfg, typ.Elem())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
func encoderOfSlice(cfg *frozenConfig, prefix string, typ reflect.Type) ValEncoder {
|
||||
encoder := encoderOfType(cfg, prefix+"[slice]->", typ.Elem())
|
||||
if typ.Elem().Kind() == reflect.Map {
|
||||
encoder = &optionalEncoder{encoder}
|
||||
encoder = &OptionalEncoder{encoder}
|
||||
}
|
||||
return &sliceEncoder{typ, typ.Elem(), encoder}, nil
|
||||
return &sliceEncoder{typ, typ.Elem(), encoder}
|
||||
}
|
||||
|
||||
type sliceEncoder struct {
|
||||
@ -43,11 +37,11 @@ func (encoder *sliceEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||
return
|
||||
}
|
||||
stream.WriteArrayStart()
|
||||
elemPtr := uintptr(slice.Data)
|
||||
elemPtr := unsafe.Pointer(slice.Data)
|
||||
encoder.elemEncoder.Encode(unsafe.Pointer(elemPtr), stream)
|
||||
for i := 1; i < slice.Len; i++ {
|
||||
stream.WriteMore()
|
||||
elemPtr += encoder.elemType.Size()
|
||||
elemPtr = unsafe.Pointer(uintptr(elemPtr) + encoder.elemType.Size())
|
||||
encoder.elemEncoder.Encode(unsafe.Pointer(elemPtr), stream)
|
||||
}
|
||||
stream.WriteArrayEnd()
|
||||
@ -94,35 +88,14 @@ func (decoder *sliceDecoder) doDecode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
return
|
||||
}
|
||||
reuseSlice(slice, decoder.sliceType, 4)
|
||||
if !iter.ReadArray() {
|
||||
return
|
||||
}
|
||||
slice.Len = 0
|
||||
offset := uintptr(0)
|
||||
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)
|
||||
if !iter.ReadArray() {
|
||||
slice.Len = 2
|
||||
return
|
||||
}
|
||||
offset += decoder.elemType.Size()
|
||||
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)
|
||||
slice.Len = 4
|
||||
for iter.ReadArray() {
|
||||
iter.ReadArrayCB(func(iter *Iterator) bool {
|
||||
growOne(slice, decoder.sliceType, decoder.elemType)
|
||||
offset += decoder.elemType.Size()
|
||||
decoder.elemDecoder.Decode(unsafe.Pointer(uintptr(slice.Data)+offset), iter)
|
||||
}
|
||||
offset += decoder.elemType.Size()
|
||||
return true
|
||||
})
|
||||
}
|
||||
|
||||
// grow grows the slice s so that it can hold extra more values, allocating
|
||||
@ -145,24 +118,26 @@ func growOne(slice *sliceHeader, sliceType reflect.Type, elementType reflect.Typ
|
||||
}
|
||||
}
|
||||
}
|
||||
dst := unsafe.Pointer(reflect.MakeSlice(sliceType, newLen, newCap).Pointer())
|
||||
newVal := reflect.MakeSlice(sliceType, newLen, newCap).Interface()
|
||||
newValPtr := extractInterface(newVal).word
|
||||
dst := (*sliceHeader)(newValPtr).Data
|
||||
// copy old array into new array
|
||||
originalBytesCount := uintptr(slice.Len) * elementType.Size()
|
||||
srcPtr := (*[1 << 30]byte)(slice.Data)
|
||||
dstPtr := (*[1 << 30]byte)(dst)
|
||||
for i := uintptr(0); i < originalBytesCount; i++ {
|
||||
dstPtr[i] = srcPtr[i]
|
||||
}
|
||||
originalBytesCount := slice.Len * int(elementType.Size())
|
||||
srcSliceHeader := (unsafe.Pointer)(&sliceHeader{slice.Data, originalBytesCount, originalBytesCount})
|
||||
dstSliceHeader := (unsafe.Pointer)(&sliceHeader{dst, originalBytesCount, originalBytesCount})
|
||||
copy(*(*[]byte)(dstSliceHeader), *(*[]byte)(srcSliceHeader))
|
||||
slice.Data = dst
|
||||
slice.Len = newLen
|
||||
slice.Cap = newCap
|
||||
slice.Data = dst
|
||||
}
|
||||
|
||||
func reuseSlice(slice *sliceHeader, sliceType reflect.Type, expectedCap int) {
|
||||
if expectedCap <= slice.Cap {
|
||||
return
|
||||
}
|
||||
dst := unsafe.Pointer(reflect.MakeSlice(sliceType, 0, expectedCap).Pointer())
|
||||
slice.Cap = expectedCap
|
||||
newVal := reflect.MakeSlice(sliceType, 0, expectedCap).Interface()
|
||||
newValPtr := extractInterface(newVal).word
|
||||
dst := (*sliceHeader)(newValPtr).Data
|
||||
slice.Data = dst
|
||||
slice.Cap = expectedCap
|
||||
}
|
||||
|
974
feature_reflect_struct_decoder.go
Normal file
974
feature_reflect_struct_decoder.go
Normal file
@ -0,0 +1,974 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"reflect"
|
||||
"strings"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
func createStructDecoder(cfg *frozenConfig, typ reflect.Type, fields map[string]*structFieldDecoder) ValDecoder {
|
||||
if cfg.disallowUnknownFields {
|
||||
return &generalStructDecoder{typ: typ, fields: fields, disallowUnknownFields: true}
|
||||
}
|
||||
knownHash := map[int64]struct{}{
|
||||
0: {},
|
||||
}
|
||||
switch len(fields) {
|
||||
case 0:
|
||||
return &skipObjectDecoder{typ}
|
||||
case 1:
|
||||
for fieldName, fieldDecoder := range fields {
|
||||
fieldHash := calcHash(fieldName)
|
||||
_, known := knownHash[fieldHash]
|
||||
if known {
|
||||
return &generalStructDecoder{typ, fields, false}
|
||||
}
|
||||
knownHash[fieldHash] = struct{}{}
|
||||
return &oneFieldStructDecoder{typ, fieldHash, fieldDecoder}
|
||||
}
|
||||
case 2:
|
||||
var fieldHash1 int64
|
||||
var fieldHash2 int64
|
||||
var fieldDecoder1 *structFieldDecoder
|
||||
var fieldDecoder2 *structFieldDecoder
|
||||
for fieldName, fieldDecoder := range fields {
|
||||
fieldHash := calcHash(fieldName)
|
||||
_, known := knownHash[fieldHash]
|
||||
if known {
|
||||
return &generalStructDecoder{typ, fields, false}
|
||||
}
|
||||
knownHash[fieldHash] = struct{}{}
|
||||
if fieldHash1 == 0 {
|
||||
fieldHash1 = fieldHash
|
||||
fieldDecoder1 = fieldDecoder
|
||||
} else {
|
||||
fieldHash2 = fieldHash
|
||||
fieldDecoder2 = fieldDecoder
|
||||
}
|
||||
}
|
||||
return &twoFieldsStructDecoder{typ, fieldHash1, fieldDecoder1, fieldHash2, fieldDecoder2}
|
||||
case 3:
|
||||
var fieldName1 int64
|
||||
var fieldName2 int64
|
||||
var fieldName3 int64
|
||||
var fieldDecoder1 *structFieldDecoder
|
||||
var fieldDecoder2 *structFieldDecoder
|
||||
var fieldDecoder3 *structFieldDecoder
|
||||
for fieldName, fieldDecoder := range fields {
|
||||
fieldHash := calcHash(fieldName)
|
||||
_, known := knownHash[fieldHash]
|
||||
if known {
|
||||
return &generalStructDecoder{typ, fields, false}
|
||||
}
|
||||
knownHash[fieldHash] = struct{}{}
|
||||
if fieldName1 == 0 {
|
||||
fieldName1 = fieldHash
|
||||
fieldDecoder1 = fieldDecoder
|
||||
} else if fieldName2 == 0 {
|
||||
fieldName2 = fieldHash
|
||||
fieldDecoder2 = fieldDecoder
|
||||
} else {
|
||||
fieldName3 = fieldHash
|
||||
fieldDecoder3 = fieldDecoder
|
||||
}
|
||||
}
|
||||
return &threeFieldsStructDecoder{typ,
|
||||
fieldName1, fieldDecoder1,
|
||||
fieldName2, fieldDecoder2,
|
||||
fieldName3, fieldDecoder3}
|
||||
case 4:
|
||||
var fieldName1 int64
|
||||
var fieldName2 int64
|
||||
var fieldName3 int64
|
||||
var fieldName4 int64
|
||||
var fieldDecoder1 *structFieldDecoder
|
||||
var fieldDecoder2 *structFieldDecoder
|
||||
var fieldDecoder3 *structFieldDecoder
|
||||
var fieldDecoder4 *structFieldDecoder
|
||||
for fieldName, fieldDecoder := range fields {
|
||||
fieldHash := calcHash(fieldName)
|
||||
_, known := knownHash[fieldHash]
|
||||
if known {
|
||||
return &generalStructDecoder{typ, fields, false}
|
||||
}
|
||||
knownHash[fieldHash] = struct{}{}
|
||||
if fieldName1 == 0 {
|
||||
fieldName1 = fieldHash
|
||||
fieldDecoder1 = fieldDecoder
|
||||
} else if fieldName2 == 0 {
|
||||
fieldName2 = fieldHash
|
||||
fieldDecoder2 = fieldDecoder
|
||||
} else if fieldName3 == 0 {
|
||||
fieldName3 = fieldHash
|
||||
fieldDecoder3 = fieldDecoder
|
||||
} else {
|
||||
fieldName4 = fieldHash
|
||||
fieldDecoder4 = fieldDecoder
|
||||
}
|
||||
}
|
||||
return &fourFieldsStructDecoder{typ,
|
||||
fieldName1, fieldDecoder1,
|
||||
fieldName2, fieldDecoder2,
|
||||
fieldName3, fieldDecoder3,
|
||||
fieldName4, fieldDecoder4}
|
||||
case 5:
|
||||
var fieldName1 int64
|
||||
var fieldName2 int64
|
||||
var fieldName3 int64
|
||||
var fieldName4 int64
|
||||
var fieldName5 int64
|
||||
var fieldDecoder1 *structFieldDecoder
|
||||
var fieldDecoder2 *structFieldDecoder
|
||||
var fieldDecoder3 *structFieldDecoder
|
||||
var fieldDecoder4 *structFieldDecoder
|
||||
var fieldDecoder5 *structFieldDecoder
|
||||
for fieldName, fieldDecoder := range fields {
|
||||
fieldHash := calcHash(fieldName)
|
||||
_, known := knownHash[fieldHash]
|
||||
if known {
|
||||
return &generalStructDecoder{typ, fields, false}
|
||||
}
|
||||
knownHash[fieldHash] = struct{}{}
|
||||
if fieldName1 == 0 {
|
||||
fieldName1 = fieldHash
|
||||
fieldDecoder1 = fieldDecoder
|
||||
} else if fieldName2 == 0 {
|
||||
fieldName2 = fieldHash
|
||||
fieldDecoder2 = fieldDecoder
|
||||
} else if fieldName3 == 0 {
|
||||
fieldName3 = fieldHash
|
||||
fieldDecoder3 = fieldDecoder
|
||||
} else if fieldName4 == 0 {
|
||||
fieldName4 = fieldHash
|
||||
fieldDecoder4 = fieldDecoder
|
||||
} else {
|
||||
fieldName5 = fieldHash
|
||||
fieldDecoder5 = fieldDecoder
|
||||
}
|
||||
}
|
||||
return &fiveFieldsStructDecoder{typ,
|
||||
fieldName1, fieldDecoder1,
|
||||
fieldName2, fieldDecoder2,
|
||||
fieldName3, fieldDecoder3,
|
||||
fieldName4, fieldDecoder4,
|
||||
fieldName5, fieldDecoder5}
|
||||
case 6:
|
||||
var fieldName1 int64
|
||||
var fieldName2 int64
|
||||
var fieldName3 int64
|
||||
var fieldName4 int64
|
||||
var fieldName5 int64
|
||||
var fieldName6 int64
|
||||
var fieldDecoder1 *structFieldDecoder
|
||||
var fieldDecoder2 *structFieldDecoder
|
||||
var fieldDecoder3 *structFieldDecoder
|
||||
var fieldDecoder4 *structFieldDecoder
|
||||
var fieldDecoder5 *structFieldDecoder
|
||||
var fieldDecoder6 *structFieldDecoder
|
||||
for fieldName, fieldDecoder := range fields {
|
||||
fieldHash := calcHash(fieldName)
|
||||
_, known := knownHash[fieldHash]
|
||||
if known {
|
||||
return &generalStructDecoder{typ, fields, false}
|
||||
}
|
||||
knownHash[fieldHash] = struct{}{}
|
||||
if fieldName1 == 0 {
|
||||
fieldName1 = fieldHash
|
||||
fieldDecoder1 = fieldDecoder
|
||||
} else if fieldName2 == 0 {
|
||||
fieldName2 = fieldHash
|
||||
fieldDecoder2 = fieldDecoder
|
||||
} else if fieldName3 == 0 {
|
||||
fieldName3 = fieldHash
|
||||
fieldDecoder3 = fieldDecoder
|
||||
} else if fieldName4 == 0 {
|
||||
fieldName4 = fieldHash
|
||||
fieldDecoder4 = fieldDecoder
|
||||
} else if fieldName5 == 0 {
|
||||
fieldName5 = fieldHash
|
||||
fieldDecoder5 = fieldDecoder
|
||||
} else {
|
||||
fieldName6 = fieldHash
|
||||
fieldDecoder6 = fieldDecoder
|
||||
}
|
||||
}
|
||||
return &sixFieldsStructDecoder{typ,
|
||||
fieldName1, fieldDecoder1,
|
||||
fieldName2, fieldDecoder2,
|
||||
fieldName3, fieldDecoder3,
|
||||
fieldName4, fieldDecoder4,
|
||||
fieldName5, fieldDecoder5,
|
||||
fieldName6, fieldDecoder6}
|
||||
case 7:
|
||||
var fieldName1 int64
|
||||
var fieldName2 int64
|
||||
var fieldName3 int64
|
||||
var fieldName4 int64
|
||||
var fieldName5 int64
|
||||
var fieldName6 int64
|
||||
var fieldName7 int64
|
||||
var fieldDecoder1 *structFieldDecoder
|
||||
var fieldDecoder2 *structFieldDecoder
|
||||
var fieldDecoder3 *structFieldDecoder
|
||||
var fieldDecoder4 *structFieldDecoder
|
||||
var fieldDecoder5 *structFieldDecoder
|
||||
var fieldDecoder6 *structFieldDecoder
|
||||
var fieldDecoder7 *structFieldDecoder
|
||||
for fieldName, fieldDecoder := range fields {
|
||||
fieldHash := calcHash(fieldName)
|
||||
_, known := knownHash[fieldHash]
|
||||
if known {
|
||||
return &generalStructDecoder{typ, fields, false}
|
||||
}
|
||||
knownHash[fieldHash] = struct{}{}
|
||||
if fieldName1 == 0 {
|
||||
fieldName1 = fieldHash
|
||||
fieldDecoder1 = fieldDecoder
|
||||
} else if fieldName2 == 0 {
|
||||
fieldName2 = fieldHash
|
||||
fieldDecoder2 = fieldDecoder
|
||||
} else if fieldName3 == 0 {
|
||||
fieldName3 = fieldHash
|
||||
fieldDecoder3 = fieldDecoder
|
||||
} else if fieldName4 == 0 {
|
||||
fieldName4 = fieldHash
|
||||
fieldDecoder4 = fieldDecoder
|
||||
} else if fieldName5 == 0 {
|
||||
fieldName5 = fieldHash
|
||||
fieldDecoder5 = fieldDecoder
|
||||
} else if fieldName6 == 0 {
|
||||
fieldName6 = fieldHash
|
||||
fieldDecoder6 = fieldDecoder
|
||||
} else {
|
||||
fieldName7 = fieldHash
|
||||
fieldDecoder7 = fieldDecoder
|
||||
}
|
||||
}
|
||||
return &sevenFieldsStructDecoder{typ,
|
||||
fieldName1, fieldDecoder1,
|
||||
fieldName2, fieldDecoder2,
|
||||
fieldName3, fieldDecoder3,
|
||||
fieldName4, fieldDecoder4,
|
||||
fieldName5, fieldDecoder5,
|
||||
fieldName6, fieldDecoder6,
|
||||
fieldName7, fieldDecoder7}
|
||||
case 8:
|
||||
var fieldName1 int64
|
||||
var fieldName2 int64
|
||||
var fieldName3 int64
|
||||
var fieldName4 int64
|
||||
var fieldName5 int64
|
||||
var fieldName6 int64
|
||||
var fieldName7 int64
|
||||
var fieldName8 int64
|
||||
var fieldDecoder1 *structFieldDecoder
|
||||
var fieldDecoder2 *structFieldDecoder
|
||||
var fieldDecoder3 *structFieldDecoder
|
||||
var fieldDecoder4 *structFieldDecoder
|
||||
var fieldDecoder5 *structFieldDecoder
|
||||
var fieldDecoder6 *structFieldDecoder
|
||||
var fieldDecoder7 *structFieldDecoder
|
||||
var fieldDecoder8 *structFieldDecoder
|
||||
for fieldName, fieldDecoder := range fields {
|
||||
fieldHash := calcHash(fieldName)
|
||||
_, known := knownHash[fieldHash]
|
||||
if known {
|
||||
return &generalStructDecoder{typ, fields, false}
|
||||
}
|
||||
knownHash[fieldHash] = struct{}{}
|
||||
if fieldName1 == 0 {
|
||||
fieldName1 = fieldHash
|
||||
fieldDecoder1 = fieldDecoder
|
||||
} else if fieldName2 == 0 {
|
||||
fieldName2 = fieldHash
|
||||
fieldDecoder2 = fieldDecoder
|
||||
} else if fieldName3 == 0 {
|
||||
fieldName3 = fieldHash
|
||||
fieldDecoder3 = fieldDecoder
|
||||
} else if fieldName4 == 0 {
|
||||
fieldName4 = fieldHash
|
||||
fieldDecoder4 = fieldDecoder
|
||||
} else if fieldName5 == 0 {
|
||||
fieldName5 = fieldHash
|
||||
fieldDecoder5 = fieldDecoder
|
||||
} else if fieldName6 == 0 {
|
||||
fieldName6 = fieldHash
|
||||
fieldDecoder6 = fieldDecoder
|
||||
} else if fieldName7 == 0 {
|
||||
fieldName7 = fieldHash
|
||||
fieldDecoder7 = fieldDecoder
|
||||
} else {
|
||||
fieldName8 = fieldHash
|
||||
fieldDecoder8 = fieldDecoder
|
||||
}
|
||||
}
|
||||
return &eightFieldsStructDecoder{typ,
|
||||
fieldName1, fieldDecoder1,
|
||||
fieldName2, fieldDecoder2,
|
||||
fieldName3, fieldDecoder3,
|
||||
fieldName4, fieldDecoder4,
|
||||
fieldName5, fieldDecoder5,
|
||||
fieldName6, fieldDecoder6,
|
||||
fieldName7, fieldDecoder7,
|
||||
fieldName8, fieldDecoder8}
|
||||
case 9:
|
||||
var fieldName1 int64
|
||||
var fieldName2 int64
|
||||
var fieldName3 int64
|
||||
var fieldName4 int64
|
||||
var fieldName5 int64
|
||||
var fieldName6 int64
|
||||
var fieldName7 int64
|
||||
var fieldName8 int64
|
||||
var fieldName9 int64
|
||||
var fieldDecoder1 *structFieldDecoder
|
||||
var fieldDecoder2 *structFieldDecoder
|
||||
var fieldDecoder3 *structFieldDecoder
|
||||
var fieldDecoder4 *structFieldDecoder
|
||||
var fieldDecoder5 *structFieldDecoder
|
||||
var fieldDecoder6 *structFieldDecoder
|
||||
var fieldDecoder7 *structFieldDecoder
|
||||
var fieldDecoder8 *structFieldDecoder
|
||||
var fieldDecoder9 *structFieldDecoder
|
||||
for fieldName, fieldDecoder := range fields {
|
||||
fieldHash := calcHash(fieldName)
|
||||
_, known := knownHash[fieldHash]
|
||||
if known {
|
||||
return &generalStructDecoder{typ, fields, false}
|
||||
}
|
||||
knownHash[fieldHash] = struct{}{}
|
||||
if fieldName1 == 0 {
|
||||
fieldName1 = fieldHash
|
||||
fieldDecoder1 = fieldDecoder
|
||||
} else if fieldName2 == 0 {
|
||||
fieldName2 = fieldHash
|
||||
fieldDecoder2 = fieldDecoder
|
||||
} else if fieldName3 == 0 {
|
||||
fieldName3 = fieldHash
|
||||
fieldDecoder3 = fieldDecoder
|
||||
} else if fieldName4 == 0 {
|
||||
fieldName4 = fieldHash
|
||||
fieldDecoder4 = fieldDecoder
|
||||
} else if fieldName5 == 0 {
|
||||
fieldName5 = fieldHash
|
||||
fieldDecoder5 = fieldDecoder
|
||||
} else if fieldName6 == 0 {
|
||||
fieldName6 = fieldHash
|
||||
fieldDecoder6 = fieldDecoder
|
||||
} else if fieldName7 == 0 {
|
||||
fieldName7 = fieldHash
|
||||
fieldDecoder7 = fieldDecoder
|
||||
} else if fieldName8 == 0 {
|
||||
fieldName8 = fieldHash
|
||||
fieldDecoder8 = fieldDecoder
|
||||
} else {
|
||||
fieldName9 = fieldHash
|
||||
fieldDecoder9 = fieldDecoder
|
||||
}
|
||||
}
|
||||
return &nineFieldsStructDecoder{typ,
|
||||
fieldName1, fieldDecoder1,
|
||||
fieldName2, fieldDecoder2,
|
||||
fieldName3, fieldDecoder3,
|
||||
fieldName4, fieldDecoder4,
|
||||
fieldName5, fieldDecoder5,
|
||||
fieldName6, fieldDecoder6,
|
||||
fieldName7, fieldDecoder7,
|
||||
fieldName8, fieldDecoder8,
|
||||
fieldName9, fieldDecoder9}
|
||||
case 10:
|
||||
var fieldName1 int64
|
||||
var fieldName2 int64
|
||||
var fieldName3 int64
|
||||
var fieldName4 int64
|
||||
var fieldName5 int64
|
||||
var fieldName6 int64
|
||||
var fieldName7 int64
|
||||
var fieldName8 int64
|
||||
var fieldName9 int64
|
||||
var fieldName10 int64
|
||||
var fieldDecoder1 *structFieldDecoder
|
||||
var fieldDecoder2 *structFieldDecoder
|
||||
var fieldDecoder3 *structFieldDecoder
|
||||
var fieldDecoder4 *structFieldDecoder
|
||||
var fieldDecoder5 *structFieldDecoder
|
||||
var fieldDecoder6 *structFieldDecoder
|
||||
var fieldDecoder7 *structFieldDecoder
|
||||
var fieldDecoder8 *structFieldDecoder
|
||||
var fieldDecoder9 *structFieldDecoder
|
||||
var fieldDecoder10 *structFieldDecoder
|
||||
for fieldName, fieldDecoder := range fields {
|
||||
fieldHash := calcHash(fieldName)
|
||||
_, known := knownHash[fieldHash]
|
||||
if known {
|
||||
return &generalStructDecoder{typ, fields, false}
|
||||
}
|
||||
knownHash[fieldHash] = struct{}{}
|
||||
if fieldName1 == 0 {
|
||||
fieldName1 = fieldHash
|
||||
fieldDecoder1 = fieldDecoder
|
||||
} else if fieldName2 == 0 {
|
||||
fieldName2 = fieldHash
|
||||
fieldDecoder2 = fieldDecoder
|
||||
} else if fieldName3 == 0 {
|
||||
fieldName3 = fieldHash
|
||||
fieldDecoder3 = fieldDecoder
|
||||
} else if fieldName4 == 0 {
|
||||
fieldName4 = fieldHash
|
||||
fieldDecoder4 = fieldDecoder
|
||||
} else if fieldName5 == 0 {
|
||||
fieldName5 = fieldHash
|
||||
fieldDecoder5 = fieldDecoder
|
||||
} else if fieldName6 == 0 {
|
||||
fieldName6 = fieldHash
|
||||
fieldDecoder6 = fieldDecoder
|
||||
} else if fieldName7 == 0 {
|
||||
fieldName7 = fieldHash
|
||||
fieldDecoder7 = fieldDecoder
|
||||
} else if fieldName8 == 0 {
|
||||
fieldName8 = fieldHash
|
||||
fieldDecoder8 = fieldDecoder
|
||||
} else if fieldName9 == 0 {
|
||||
fieldName9 = fieldHash
|
||||
fieldDecoder9 = fieldDecoder
|
||||
} else {
|
||||
fieldName10 = fieldHash
|
||||
fieldDecoder10 = fieldDecoder
|
||||
}
|
||||
}
|
||||
return &tenFieldsStructDecoder{typ,
|
||||
fieldName1, fieldDecoder1,
|
||||
fieldName2, fieldDecoder2,
|
||||
fieldName3, fieldDecoder3,
|
||||
fieldName4, fieldDecoder4,
|
||||
fieldName5, fieldDecoder5,
|
||||
fieldName6, fieldDecoder6,
|
||||
fieldName7, fieldDecoder7,
|
||||
fieldName8, fieldDecoder8,
|
||||
fieldName9, fieldDecoder9,
|
||||
fieldName10, fieldDecoder10}
|
||||
}
|
||||
return &generalStructDecoder{typ, fields, false}
|
||||
}
|
||||
|
||||
type generalStructDecoder struct {
|
||||
typ reflect.Type
|
||||
fields map[string]*structFieldDecoder
|
||||
disallowUnknownFields bool
|
||||
}
|
||||
|
||||
func (decoder *generalStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
if !iter.readObjectStart() {
|
||||
return
|
||||
}
|
||||
decoder.decodeOneField(ptr, iter)
|
||||
for iter.nextToken() == ',' {
|
||||
decoder.decodeOneField(ptr, iter)
|
||||
}
|
||||
if iter.Error != nil && iter.Error != io.EOF {
|
||||
iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func (decoder *generalStructDecoder) decodeOneField(ptr unsafe.Pointer, iter *Iterator) {
|
||||
var field string
|
||||
var fieldDecoder *structFieldDecoder
|
||||
if iter.cfg.objectFieldMustBeSimpleString {
|
||||
fieldBytes := iter.ReadStringAsSlice()
|
||||
field = *(*string)(unsafe.Pointer(&fieldBytes))
|
||||
fieldDecoder = decoder.fields[field]
|
||||
if fieldDecoder == nil {
|
||||
fieldDecoder = decoder.fields[strings.ToLower(field)]
|
||||
}
|
||||
} else {
|
||||
field = iter.ReadString()
|
||||
fieldDecoder = decoder.fields[field]
|
||||
if fieldDecoder == nil {
|
||||
fieldDecoder = decoder.fields[strings.ToLower(field)]
|
||||
}
|
||||
}
|
||||
if fieldDecoder == nil {
|
||||
msg := "found unknown field: " + field
|
||||
if decoder.disallowUnknownFields {
|
||||
iter.ReportError("ReadObject", msg)
|
||||
}
|
||||
c := iter.nextToken()
|
||||
if c != ':' {
|
||||
iter.ReportError("ReadObject", "expect : after object field, but found "+string([]byte{c}))
|
||||
}
|
||||
iter.Skip()
|
||||
return
|
||||
}
|
||||
c := iter.nextToken()
|
||||
if c != ':' {
|
||||
iter.ReportError("ReadObject", "expect : after object field, but found "+string([]byte{c}))
|
||||
}
|
||||
fieldDecoder.Decode(ptr, iter)
|
||||
}
|
||||
|
||||
type skipObjectDecoder struct {
|
||||
typ reflect.Type
|
||||
}
|
||||
|
||||
func (decoder *skipObjectDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
valueType := iter.WhatIsNext()
|
||||
if valueType != ObjectValue && valueType != NilValue {
|
||||
iter.ReportError("skipObjectDecoder", "expect object or null")
|
||||
return
|
||||
}
|
||||
iter.Skip()
|
||||
}
|
||||
|
||||
type oneFieldStructDecoder struct {
|
||||
typ reflect.Type
|
||||
fieldHash int64
|
||||
fieldDecoder *structFieldDecoder
|
||||
}
|
||||
|
||||
func (decoder *oneFieldStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
if !iter.readObjectStart() {
|
||||
return
|
||||
}
|
||||
for {
|
||||
if iter.readFieldHash() == decoder.fieldHash {
|
||||
decoder.fieldDecoder.Decode(ptr, iter)
|
||||
} else {
|
||||
iter.Skip()
|
||||
}
|
||||
if iter.isObjectEnd() {
|
||||
break
|
||||
}
|
||||
}
|
||||
if iter.Error != nil && iter.Error != io.EOF {
|
||||
iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
|
||||
}
|
||||
}
|
||||
|
||||
type twoFieldsStructDecoder struct {
|
||||
typ reflect.Type
|
||||
fieldHash1 int64
|
||||
fieldDecoder1 *structFieldDecoder
|
||||
fieldHash2 int64
|
||||
fieldDecoder2 *structFieldDecoder
|
||||
}
|
||||
|
||||
func (decoder *twoFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
if !iter.readObjectStart() {
|
||||
return
|
||||
}
|
||||
for {
|
||||
switch iter.readFieldHash() {
|
||||
case decoder.fieldHash1:
|
||||
decoder.fieldDecoder1.Decode(ptr, iter)
|
||||
case decoder.fieldHash2:
|
||||
decoder.fieldDecoder2.Decode(ptr, iter)
|
||||
default:
|
||||
iter.Skip()
|
||||
}
|
||||
if iter.isObjectEnd() {
|
||||
break
|
||||
}
|
||||
}
|
||||
if iter.Error != nil && iter.Error != io.EOF {
|
||||
iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
|
||||
}
|
||||
}
|
||||
|
||||
type threeFieldsStructDecoder struct {
|
||||
typ reflect.Type
|
||||
fieldHash1 int64
|
||||
fieldDecoder1 *structFieldDecoder
|
||||
fieldHash2 int64
|
||||
fieldDecoder2 *structFieldDecoder
|
||||
fieldHash3 int64
|
||||
fieldDecoder3 *structFieldDecoder
|
||||
}
|
||||
|
||||
func (decoder *threeFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
if !iter.readObjectStart() {
|
||||
return
|
||||
}
|
||||
for {
|
||||
switch iter.readFieldHash() {
|
||||
case decoder.fieldHash1:
|
||||
decoder.fieldDecoder1.Decode(ptr, iter)
|
||||
case decoder.fieldHash2:
|
||||
decoder.fieldDecoder2.Decode(ptr, iter)
|
||||
case decoder.fieldHash3:
|
||||
decoder.fieldDecoder3.Decode(ptr, iter)
|
||||
default:
|
||||
iter.Skip()
|
||||
}
|
||||
if iter.isObjectEnd() {
|
||||
break
|
||||
}
|
||||
}
|
||||
if iter.Error != nil && iter.Error != io.EOF {
|
||||
iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
|
||||
}
|
||||
}
|
||||
|
||||
type fourFieldsStructDecoder struct {
|
||||
typ reflect.Type
|
||||
fieldHash1 int64
|
||||
fieldDecoder1 *structFieldDecoder
|
||||
fieldHash2 int64
|
||||
fieldDecoder2 *structFieldDecoder
|
||||
fieldHash3 int64
|
||||
fieldDecoder3 *structFieldDecoder
|
||||
fieldHash4 int64
|
||||
fieldDecoder4 *structFieldDecoder
|
||||
}
|
||||
|
||||
func (decoder *fourFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
if !iter.readObjectStart() {
|
||||
return
|
||||
}
|
||||
for {
|
||||
switch iter.readFieldHash() {
|
||||
case decoder.fieldHash1:
|
||||
decoder.fieldDecoder1.Decode(ptr, iter)
|
||||
case decoder.fieldHash2:
|
||||
decoder.fieldDecoder2.Decode(ptr, iter)
|
||||
case decoder.fieldHash3:
|
||||
decoder.fieldDecoder3.Decode(ptr, iter)
|
||||
case decoder.fieldHash4:
|
||||
decoder.fieldDecoder4.Decode(ptr, iter)
|
||||
default:
|
||||
iter.Skip()
|
||||
}
|
||||
if iter.isObjectEnd() {
|
||||
break
|
||||
}
|
||||
}
|
||||
if iter.Error != nil && iter.Error != io.EOF {
|
||||
iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
|
||||
}
|
||||
}
|
||||
|
||||
type fiveFieldsStructDecoder struct {
|
||||
typ reflect.Type
|
||||
fieldHash1 int64
|
||||
fieldDecoder1 *structFieldDecoder
|
||||
fieldHash2 int64
|
||||
fieldDecoder2 *structFieldDecoder
|
||||
fieldHash3 int64
|
||||
fieldDecoder3 *structFieldDecoder
|
||||
fieldHash4 int64
|
||||
fieldDecoder4 *structFieldDecoder
|
||||
fieldHash5 int64
|
||||
fieldDecoder5 *structFieldDecoder
|
||||
}
|
||||
|
||||
func (decoder *fiveFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
if !iter.readObjectStart() {
|
||||
return
|
||||
}
|
||||
for {
|
||||
switch iter.readFieldHash() {
|
||||
case decoder.fieldHash1:
|
||||
decoder.fieldDecoder1.Decode(ptr, iter)
|
||||
case decoder.fieldHash2:
|
||||
decoder.fieldDecoder2.Decode(ptr, iter)
|
||||
case decoder.fieldHash3:
|
||||
decoder.fieldDecoder3.Decode(ptr, iter)
|
||||
case decoder.fieldHash4:
|
||||
decoder.fieldDecoder4.Decode(ptr, iter)
|
||||
case decoder.fieldHash5:
|
||||
decoder.fieldDecoder5.Decode(ptr, iter)
|
||||
default:
|
||||
iter.Skip()
|
||||
}
|
||||
if iter.isObjectEnd() {
|
||||
break
|
||||
}
|
||||
}
|
||||
if iter.Error != nil && iter.Error != io.EOF {
|
||||
iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
|
||||
}
|
||||
}
|
||||
|
||||
type sixFieldsStructDecoder struct {
|
||||
typ reflect.Type
|
||||
fieldHash1 int64
|
||||
fieldDecoder1 *structFieldDecoder
|
||||
fieldHash2 int64
|
||||
fieldDecoder2 *structFieldDecoder
|
||||
fieldHash3 int64
|
||||
fieldDecoder3 *structFieldDecoder
|
||||
fieldHash4 int64
|
||||
fieldDecoder4 *structFieldDecoder
|
||||
fieldHash5 int64
|
||||
fieldDecoder5 *structFieldDecoder
|
||||
fieldHash6 int64
|
||||
fieldDecoder6 *structFieldDecoder
|
||||
}
|
||||
|
||||
func (decoder *sixFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
if !iter.readObjectStart() {
|
||||
return
|
||||
}
|
||||
for {
|
||||
switch iter.readFieldHash() {
|
||||
case decoder.fieldHash1:
|
||||
decoder.fieldDecoder1.Decode(ptr, iter)
|
||||
case decoder.fieldHash2:
|
||||
decoder.fieldDecoder2.Decode(ptr, iter)
|
||||
case decoder.fieldHash3:
|
||||
decoder.fieldDecoder3.Decode(ptr, iter)
|
||||
case decoder.fieldHash4:
|
||||
decoder.fieldDecoder4.Decode(ptr, iter)
|
||||
case decoder.fieldHash5:
|
||||
decoder.fieldDecoder5.Decode(ptr, iter)
|
||||
case decoder.fieldHash6:
|
||||
decoder.fieldDecoder6.Decode(ptr, iter)
|
||||
default:
|
||||
iter.Skip()
|
||||
}
|
||||
if iter.isObjectEnd() {
|
||||
break
|
||||
}
|
||||
}
|
||||
if iter.Error != nil && iter.Error != io.EOF {
|
||||
iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
|
||||
}
|
||||
}
|
||||
|
||||
type sevenFieldsStructDecoder struct {
|
||||
typ reflect.Type
|
||||
fieldHash1 int64
|
||||
fieldDecoder1 *structFieldDecoder
|
||||
fieldHash2 int64
|
||||
fieldDecoder2 *structFieldDecoder
|
||||
fieldHash3 int64
|
||||
fieldDecoder3 *structFieldDecoder
|
||||
fieldHash4 int64
|
||||
fieldDecoder4 *structFieldDecoder
|
||||
fieldHash5 int64
|
||||
fieldDecoder5 *structFieldDecoder
|
||||
fieldHash6 int64
|
||||
fieldDecoder6 *structFieldDecoder
|
||||
fieldHash7 int64
|
||||
fieldDecoder7 *structFieldDecoder
|
||||
}
|
||||
|
||||
func (decoder *sevenFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
if !iter.readObjectStart() {
|
||||
return
|
||||
}
|
||||
for {
|
||||
switch iter.readFieldHash() {
|
||||
case decoder.fieldHash1:
|
||||
decoder.fieldDecoder1.Decode(ptr, iter)
|
||||
case decoder.fieldHash2:
|
||||
decoder.fieldDecoder2.Decode(ptr, iter)
|
||||
case decoder.fieldHash3:
|
||||
decoder.fieldDecoder3.Decode(ptr, iter)
|
||||
case decoder.fieldHash4:
|
||||
decoder.fieldDecoder4.Decode(ptr, iter)
|
||||
case decoder.fieldHash5:
|
||||
decoder.fieldDecoder5.Decode(ptr, iter)
|
||||
case decoder.fieldHash6:
|
||||
decoder.fieldDecoder6.Decode(ptr, iter)
|
||||
case decoder.fieldHash7:
|
||||
decoder.fieldDecoder7.Decode(ptr, iter)
|
||||
default:
|
||||
iter.Skip()
|
||||
}
|
||||
if iter.isObjectEnd() {
|
||||
break
|
||||
}
|
||||
}
|
||||
if iter.Error != nil && iter.Error != io.EOF {
|
||||
iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
|
||||
}
|
||||
}
|
||||
|
||||
type eightFieldsStructDecoder struct {
|
||||
typ reflect.Type
|
||||
fieldHash1 int64
|
||||
fieldDecoder1 *structFieldDecoder
|
||||
fieldHash2 int64
|
||||
fieldDecoder2 *structFieldDecoder
|
||||
fieldHash3 int64
|
||||
fieldDecoder3 *structFieldDecoder
|
||||
fieldHash4 int64
|
||||
fieldDecoder4 *structFieldDecoder
|
||||
fieldHash5 int64
|
||||
fieldDecoder5 *structFieldDecoder
|
||||
fieldHash6 int64
|
||||
fieldDecoder6 *structFieldDecoder
|
||||
fieldHash7 int64
|
||||
fieldDecoder7 *structFieldDecoder
|
||||
fieldHash8 int64
|
||||
fieldDecoder8 *structFieldDecoder
|
||||
}
|
||||
|
||||
func (decoder *eightFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
if !iter.readObjectStart() {
|
||||
return
|
||||
}
|
||||
for {
|
||||
switch iter.readFieldHash() {
|
||||
case decoder.fieldHash1:
|
||||
decoder.fieldDecoder1.Decode(ptr, iter)
|
||||
case decoder.fieldHash2:
|
||||
decoder.fieldDecoder2.Decode(ptr, iter)
|
||||
case decoder.fieldHash3:
|
||||
decoder.fieldDecoder3.Decode(ptr, iter)
|
||||
case decoder.fieldHash4:
|
||||
decoder.fieldDecoder4.Decode(ptr, iter)
|
||||
case decoder.fieldHash5:
|
||||
decoder.fieldDecoder5.Decode(ptr, iter)
|
||||
case decoder.fieldHash6:
|
||||
decoder.fieldDecoder6.Decode(ptr, iter)
|
||||
case decoder.fieldHash7:
|
||||
decoder.fieldDecoder7.Decode(ptr, iter)
|
||||
case decoder.fieldHash8:
|
||||
decoder.fieldDecoder8.Decode(ptr, iter)
|
||||
default:
|
||||
iter.Skip()
|
||||
}
|
||||
if iter.isObjectEnd() {
|
||||
break
|
||||
}
|
||||
}
|
||||
if iter.Error != nil && iter.Error != io.EOF {
|
||||
iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
|
||||
}
|
||||
}
|
||||
|
||||
type nineFieldsStructDecoder struct {
|
||||
typ reflect.Type
|
||||
fieldHash1 int64
|
||||
fieldDecoder1 *structFieldDecoder
|
||||
fieldHash2 int64
|
||||
fieldDecoder2 *structFieldDecoder
|
||||
fieldHash3 int64
|
||||
fieldDecoder3 *structFieldDecoder
|
||||
fieldHash4 int64
|
||||
fieldDecoder4 *structFieldDecoder
|
||||
fieldHash5 int64
|
||||
fieldDecoder5 *structFieldDecoder
|
||||
fieldHash6 int64
|
||||
fieldDecoder6 *structFieldDecoder
|
||||
fieldHash7 int64
|
||||
fieldDecoder7 *structFieldDecoder
|
||||
fieldHash8 int64
|
||||
fieldDecoder8 *structFieldDecoder
|
||||
fieldHash9 int64
|
||||
fieldDecoder9 *structFieldDecoder
|
||||
}
|
||||
|
||||
func (decoder *nineFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
if !iter.readObjectStart() {
|
||||
return
|
||||
}
|
||||
for {
|
||||
switch iter.readFieldHash() {
|
||||
case decoder.fieldHash1:
|
||||
decoder.fieldDecoder1.Decode(ptr, iter)
|
||||
case decoder.fieldHash2:
|
||||
decoder.fieldDecoder2.Decode(ptr, iter)
|
||||
case decoder.fieldHash3:
|
||||
decoder.fieldDecoder3.Decode(ptr, iter)
|
||||
case decoder.fieldHash4:
|
||||
decoder.fieldDecoder4.Decode(ptr, iter)
|
||||
case decoder.fieldHash5:
|
||||
decoder.fieldDecoder5.Decode(ptr, iter)
|
||||
case decoder.fieldHash6:
|
||||
decoder.fieldDecoder6.Decode(ptr, iter)
|
||||
case decoder.fieldHash7:
|
||||
decoder.fieldDecoder7.Decode(ptr, iter)
|
||||
case decoder.fieldHash8:
|
||||
decoder.fieldDecoder8.Decode(ptr, iter)
|
||||
case decoder.fieldHash9:
|
||||
decoder.fieldDecoder9.Decode(ptr, iter)
|
||||
default:
|
||||
iter.Skip()
|
||||
}
|
||||
if iter.isObjectEnd() {
|
||||
break
|
||||
}
|
||||
}
|
||||
if iter.Error != nil && iter.Error != io.EOF {
|
||||
iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
|
||||
}
|
||||
}
|
||||
|
||||
type tenFieldsStructDecoder struct {
|
||||
typ reflect.Type
|
||||
fieldHash1 int64
|
||||
fieldDecoder1 *structFieldDecoder
|
||||
fieldHash2 int64
|
||||
fieldDecoder2 *structFieldDecoder
|
||||
fieldHash3 int64
|
||||
fieldDecoder3 *structFieldDecoder
|
||||
fieldHash4 int64
|
||||
fieldDecoder4 *structFieldDecoder
|
||||
fieldHash5 int64
|
||||
fieldDecoder5 *structFieldDecoder
|
||||
fieldHash6 int64
|
||||
fieldDecoder6 *structFieldDecoder
|
||||
fieldHash7 int64
|
||||
fieldDecoder7 *structFieldDecoder
|
||||
fieldHash8 int64
|
||||
fieldDecoder8 *structFieldDecoder
|
||||
fieldHash9 int64
|
||||
fieldDecoder9 *structFieldDecoder
|
||||
fieldHash10 int64
|
||||
fieldDecoder10 *structFieldDecoder
|
||||
}
|
||||
|
||||
func (decoder *tenFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
if !iter.readObjectStart() {
|
||||
return
|
||||
}
|
||||
for {
|
||||
switch iter.readFieldHash() {
|
||||
case decoder.fieldHash1:
|
||||
decoder.fieldDecoder1.Decode(ptr, iter)
|
||||
case decoder.fieldHash2:
|
||||
decoder.fieldDecoder2.Decode(ptr, iter)
|
||||
case decoder.fieldHash3:
|
||||
decoder.fieldDecoder3.Decode(ptr, iter)
|
||||
case decoder.fieldHash4:
|
||||
decoder.fieldDecoder4.Decode(ptr, iter)
|
||||
case decoder.fieldHash5:
|
||||
decoder.fieldDecoder5.Decode(ptr, iter)
|
||||
case decoder.fieldHash6:
|
||||
decoder.fieldDecoder6.Decode(ptr, iter)
|
||||
case decoder.fieldHash7:
|
||||
decoder.fieldDecoder7.Decode(ptr, iter)
|
||||
case decoder.fieldHash8:
|
||||
decoder.fieldDecoder8.Decode(ptr, iter)
|
||||
case decoder.fieldHash9:
|
||||
decoder.fieldDecoder9.Decode(ptr, iter)
|
||||
case decoder.fieldHash10:
|
||||
decoder.fieldDecoder10.Decode(ptr, iter)
|
||||
default:
|
||||
iter.Skip()
|
||||
}
|
||||
if iter.isObjectEnd() {
|
||||
break
|
||||
}
|
||||
}
|
||||
if iter.Error != nil && iter.Error != io.EOF {
|
||||
iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
|
||||
}
|
||||
}
|
||||
|
||||
type structFieldDecoder struct {
|
||||
field *reflect.StructField
|
||||
fieldDecoder ValDecoder
|
||||
}
|
||||
|
||||
func (decoder *structFieldDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
fieldPtr := unsafe.Pointer(uintptr(ptr) + decoder.field.Offset)
|
||||
decoder.fieldDecoder.Decode(fieldPtr, iter)
|
||||
if iter.Error != nil && iter.Error != io.EOF {
|
||||
iter.Error = fmt.Errorf("%s: %s", decoder.field.Name, iter.Error.Error())
|
||||
}
|
||||
}
|
@ -4,211 +4,132 @@ import (
|
||||
"io"
|
||||
)
|
||||
|
||||
// stream is a io.Writer like object, with JSON specific write functions.
|
||||
// Error is not returned as return value, but stored as Error member on this stream instance.
|
||||
type Stream struct {
|
||||
cfg *frozenConfig
|
||||
out io.Writer
|
||||
buf []byte
|
||||
n int
|
||||
Error error
|
||||
indention int
|
||||
cfg *frozenConfig
|
||||
out io.Writer
|
||||
buf []byte
|
||||
Error error
|
||||
indention int
|
||||
Attachment interface{} // open for customized encoder
|
||||
}
|
||||
|
||||
func NewStream(cfg *frozenConfig, out io.Writer, bufSize int) *Stream {
|
||||
// NewStream create new stream instance.
|
||||
// cfg can be jsoniter.ConfigDefault.
|
||||
// out can be nil if write to internal buffer.
|
||||
// bufSize is the initial size for the internal buffer in bytes.
|
||||
func NewStream(cfg API, out io.Writer, bufSize int) *Stream {
|
||||
return &Stream{
|
||||
cfg: cfg,
|
||||
cfg: cfg.(*frozenConfig),
|
||||
out: out,
|
||||
buf: make([]byte, bufSize),
|
||||
n: 0,
|
||||
buf: make([]byte, 0, bufSize),
|
||||
Error: nil,
|
||||
indention: 0,
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Stream) Reset(out io.Writer) {
|
||||
b.out = out
|
||||
b.n = 0
|
||||
// Pool returns a pool can provide more stream with same configuration
|
||||
func (stream *Stream) Pool() StreamPool {
|
||||
return stream.cfg
|
||||
}
|
||||
|
||||
// Reset reuse this stream instance by assign a new writer
|
||||
func (stream *Stream) Reset(out io.Writer) {
|
||||
stream.out = out
|
||||
stream.buf = stream.buf[:0]
|
||||
}
|
||||
|
||||
// Available returns how many bytes are unused in the buffer.
|
||||
func (b *Stream) Available() int {
|
||||
return len(b.buf) - b.n
|
||||
func (stream *Stream) Available() int {
|
||||
return cap(stream.buf) - len(stream.buf)
|
||||
}
|
||||
|
||||
// Buffered returns the number of bytes that have been written into the current buffer.
|
||||
func (b *Stream) Buffered() int {
|
||||
return b.n
|
||||
func (stream *Stream) Buffered() int {
|
||||
return len(stream.buf)
|
||||
}
|
||||
|
||||
func (b *Stream) Buffer() []byte {
|
||||
return b.buf[:b.n]
|
||||
// Buffer if writer is nil, use this method to take the result
|
||||
func (stream *Stream) Buffer() []byte {
|
||||
return stream.buf
|
||||
}
|
||||
|
||||
// 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 {
|
||||
if b.out == nil {
|
||||
b.growAtLeast(len(p))
|
||||
} else {
|
||||
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:]
|
||||
}
|
||||
func (stream *Stream) Write(p []byte) (nn int, err error) {
|
||||
stream.buf = append(stream.buf, p...)
|
||||
if stream.out != nil {
|
||||
nn, err = stream.out.Write(stream.buf)
|
||||
stream.buf = stream.buf[nn:]
|
||||
return
|
||||
}
|
||||
if b.Error != nil {
|
||||
return nn, b.Error
|
||||
}
|
||||
n := copy(b.buf[b.n:], p)
|
||||
b.n += n
|
||||
nn += n
|
||||
return nn, nil
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
// WriteByte writes a single byte.
|
||||
func (b *Stream) writeByte(c byte) {
|
||||
if b.Error != nil {
|
||||
return
|
||||
}
|
||||
if b.Available() < 1 {
|
||||
b.growAtLeast(1)
|
||||
}
|
||||
b.buf[b.n] = c
|
||||
b.n++
|
||||
func (stream *Stream) writeByte(c byte) {
|
||||
stream.buf = append(stream.buf, c)
|
||||
}
|
||||
|
||||
func (b *Stream) writeTwoBytes(c1 byte, c2 byte) {
|
||||
if b.Error != nil {
|
||||
return
|
||||
}
|
||||
if b.Available() < 2 {
|
||||
b.growAtLeast(2)
|
||||
}
|
||||
b.buf[b.n] = c1
|
||||
b.buf[b.n+1] = c2
|
||||
b.n += 2
|
||||
func (stream *Stream) writeTwoBytes(c1 byte, c2 byte) {
|
||||
stream.buf = append(stream.buf, c1, c2)
|
||||
}
|
||||
|
||||
func (b *Stream) writeThreeBytes(c1 byte, c2 byte, c3 byte) {
|
||||
if b.Error != 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.n += 3
|
||||
func (stream *Stream) writeThreeBytes(c1 byte, c2 byte, c3 byte) {
|
||||
stream.buf = append(stream.buf, c1, c2, c3)
|
||||
}
|
||||
|
||||
func (b *Stream) writeFourBytes(c1 byte, c2 byte, c3 byte, c4 byte) {
|
||||
if b.Error != 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.n += 4
|
||||
func (stream *Stream) writeFourBytes(c1 byte, c2 byte, c3 byte, c4 byte) {
|
||||
stream.buf = append(stream.buf, c1, c2, c3, c4)
|
||||
}
|
||||
|
||||
func (b *Stream) writeFiveBytes(c1 byte, c2 byte, c3 byte, c4 byte, c5 byte) {
|
||||
if b.Error != 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.n += 5
|
||||
func (stream *Stream) writeFiveBytes(c1 byte, c2 byte, c3 byte, c4 byte, c5 byte) {
|
||||
stream.buf = append(stream.buf, c1, c2, c3, c4, c5)
|
||||
}
|
||||
|
||||
// Flush writes any buffered data to the underlying io.Writer.
|
||||
func (b *Stream) Flush() error {
|
||||
if b.out == nil {
|
||||
func (stream *Stream) Flush() error {
|
||||
if stream.out == nil {
|
||||
return nil
|
||||
}
|
||||
if b.Error != nil {
|
||||
return b.Error
|
||||
}
|
||||
if b.n == 0 {
|
||||
return nil
|
||||
}
|
||||
n, err := b.out.Write(b.buf[0:b.n])
|
||||
if n < b.n && err == nil {
|
||||
err = io.ErrShortWrite
|
||||
if stream.Error != nil {
|
||||
return stream.Error
|
||||
}
|
||||
n, err := stream.out.Write(stream.buf)
|
||||
if err != nil {
|
||||
if n > 0 && n < b.n {
|
||||
copy(b.buf[0:b.n-n], b.buf[n:b.n])
|
||||
if stream.Error == nil {
|
||||
stream.Error = err
|
||||
}
|
||||
b.n -= n
|
||||
b.Error = err
|
||||
return err
|
||||
}
|
||||
b.n = 0
|
||||
stream.buf = stream.buf[n:]
|
||||
return nil
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
n := copy(b.buf[b.n:], s)
|
||||
b.n += n
|
||||
// WriteRaw write string out without quotes, just like []byte
|
||||
func (stream *Stream) WriteRaw(s string) {
|
||||
stream.buf = append(stream.buf, s...)
|
||||
}
|
||||
|
||||
// WriteNil write null to stream
|
||||
func (stream *Stream) WriteNil() {
|
||||
stream.writeFourBytes('n', 'u', 'l', 'l')
|
||||
}
|
||||
|
||||
// WriteTrue write true to stream
|
||||
func (stream *Stream) WriteTrue() {
|
||||
stream.writeFourBytes('t', 'r', 'u', 'e')
|
||||
}
|
||||
|
||||
// WriteFalse write false to stream
|
||||
func (stream *Stream) WriteFalse() {
|
||||
stream.writeFiveBytes('f', 'a', 'l', 's', 'e')
|
||||
}
|
||||
|
||||
// WriteBool write true or false into stream
|
||||
func (stream *Stream) WriteBool(val bool) {
|
||||
if val {
|
||||
stream.WriteTrue()
|
||||
@ -217,12 +138,14 @@ func (stream *Stream) WriteBool(val bool) {
|
||||
}
|
||||
}
|
||||
|
||||
// WriteObjectStart write { with possible indention
|
||||
func (stream *Stream) WriteObjectStart() {
|
||||
stream.indention += stream.cfg.indentionStep
|
||||
stream.writeByte('{')
|
||||
stream.writeIndention(0)
|
||||
}
|
||||
|
||||
// WriteObjectField write "field": with possible indention
|
||||
func (stream *Stream) WriteObjectField(field string) {
|
||||
stream.WriteString(field)
|
||||
if stream.indention > 0 {
|
||||
@ -232,33 +155,39 @@ func (stream *Stream) WriteObjectField(field string) {
|
||||
}
|
||||
}
|
||||
|
||||
// WriteObjectEnd write } with possible indention
|
||||
func (stream *Stream) WriteObjectEnd() {
|
||||
stream.writeIndention(stream.cfg.indentionStep)
|
||||
stream.indention -= stream.cfg.indentionStep
|
||||
stream.writeByte('}')
|
||||
}
|
||||
|
||||
// WriteEmptyObject write {}
|
||||
func (stream *Stream) WriteEmptyObject() {
|
||||
stream.writeByte('{')
|
||||
stream.writeByte('}')
|
||||
}
|
||||
|
||||
// WriteMore write , with possible indention
|
||||
func (stream *Stream) WriteMore() {
|
||||
stream.writeByte(',')
|
||||
stream.writeIndention(0)
|
||||
stream.Flush()
|
||||
}
|
||||
|
||||
// WriteArrayStart write [ with possible indention
|
||||
func (stream *Stream) WriteArrayStart() {
|
||||
stream.indention += stream.cfg.indentionStep
|
||||
stream.writeByte('[')
|
||||
stream.writeIndention(0)
|
||||
}
|
||||
|
||||
// WriteEmptyArray write []
|
||||
func (stream *Stream) WriteEmptyArray() {
|
||||
stream.writeByte('[')
|
||||
stream.writeByte(']')
|
||||
stream.writeTwoBytes('[', ']')
|
||||
}
|
||||
|
||||
// WriteArrayEnd write ] with possible indention
|
||||
func (stream *Stream) WriteArrayEnd() {
|
||||
stream.writeIndention(stream.cfg.indentionStep)
|
||||
stream.indention -= stream.cfg.indentionStep
|
||||
@ -271,9 +200,7 @@ func (stream *Stream) writeIndention(delta int) {
|
||||
}
|
||||
stream.writeByte('\n')
|
||||
toWrite := stream.indention - delta
|
||||
stream.ensure(toWrite)
|
||||
for i := 0; i < toWrite && stream.n < len(stream.buf); i++ {
|
||||
stream.buf[stream.n] = ' '
|
||||
stream.n++
|
||||
for i := 0; i < toWrite; i++ {
|
||||
stream.buf = append(stream.buf, ' ')
|
||||
}
|
||||
}
|
||||
|
@ -1,16 +1,17 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"math"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
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}
|
||||
}
|
||||
|
||||
// WriteFloat32 write float32 to stream
|
||||
func (stream *Stream) WriteFloat32(val float32) {
|
||||
abs := math.Abs(float64(val))
|
||||
fmt := byte('f')
|
||||
@ -20,9 +21,10 @@ func (stream *Stream) WriteFloat32(val float32) {
|
||||
fmt = 'e'
|
||||
}
|
||||
}
|
||||
stream.WriteRaw(strconv.FormatFloat(float64(val), fmt, -1, 32))
|
||||
stream.buf = strconv.AppendFloat(stream.buf, float64(val), fmt, -1, 32)
|
||||
}
|
||||
|
||||
// WriteFloat32Lossy write float32 to stream with ONLY 6 digits precision although much much faster
|
||||
func (stream *Stream) WriteFloat32Lossy(val float32) {
|
||||
if val < 0 {
|
||||
stream.writeByte('-')
|
||||
@ -41,16 +43,16 @@ func (stream *Stream) WriteFloat32Lossy(val float32) {
|
||||
return
|
||||
}
|
||||
stream.writeByte('.')
|
||||
stream.ensure(10)
|
||||
for p := precision - 1; p > 0 && fval < _POW10[p]; p-- {
|
||||
for p := precision - 1; p > 0 && fval < pow10[p]; p-- {
|
||||
stream.writeByte('0')
|
||||
}
|
||||
stream.WriteUint64(fval)
|
||||
for stream.buf[stream.n-1] == '0' {
|
||||
stream.n--
|
||||
for stream.buf[len(stream.buf)-1] == '0' {
|
||||
stream.buf = stream.buf[:len(stream.buf)-1]
|
||||
}
|
||||
}
|
||||
|
||||
// WriteFloat64 write float64 to stream
|
||||
func (stream *Stream) WriteFloat64(val float64) {
|
||||
abs := math.Abs(val)
|
||||
fmt := byte('f')
|
||||
@ -60,9 +62,10 @@ func (stream *Stream) WriteFloat64(val float64) {
|
||||
fmt = 'e'
|
||||
}
|
||||
}
|
||||
stream.WriteRaw(strconv.FormatFloat(float64(val), fmt, -1, 64))
|
||||
stream.buf = strconv.AppendFloat(stream.buf, float64(val), fmt, -1, 64)
|
||||
}
|
||||
|
||||
// WriteFloat64Lossy write float64 to stream with ONLY 6 digits precision although much much faster
|
||||
func (stream *Stream) WriteFloat64Lossy(val float64) {
|
||||
if val < 0 {
|
||||
stream.writeByte('-')
|
||||
@ -81,12 +84,11 @@ func (stream *Stream) WriteFloat64Lossy(val float64) {
|
||||
return
|
||||
}
|
||||
stream.writeByte('.')
|
||||
stream.ensure(10)
|
||||
for p := precision - 1; p > 0 && fval < _POW10[p]; p-- {
|
||||
for p := precision - 1; p > 0 && fval < pow10[p]; p-- {
|
||||
stream.writeByte('0')
|
||||
}
|
||||
stream.WriteUint64(fval)
|
||||
for stream.buf[stream.n-1] == '0' {
|
||||
stream.n--
|
||||
for stream.buf[len(stream.buf)-1] == '0' {
|
||||
stream.buf = stream.buf[:len(stream.buf)-1]
|
||||
}
|
||||
}
|
||||
|
@ -1,310 +1,190 @@
|
||||
package jsoniter
|
||||
|
||||
var _DIGITS []uint32
|
||||
var digits []uint32
|
||||
|
||||
func init() {
|
||||
_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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func writeFirstBuf(buf []byte, v uint32, n int) int {
|
||||
func writeFirstBuf(space []byte, v uint32) []byte {
|
||||
start := v >> 24
|
||||
if start == 0 {
|
||||
buf[n] = byte(v >> 16)
|
||||
n++
|
||||
buf[n] = byte(v >> 8)
|
||||
n++
|
||||
space = append(space, byte(v >> 16), byte(v >> 8))
|
||||
} else if start == 1 {
|
||||
buf[n] = byte(v >> 8)
|
||||
n++
|
||||
space = append(space, byte(v >> 8))
|
||||
}
|
||||
buf[n] = byte(v)
|
||||
n++
|
||||
return n
|
||||
space = append(space, byte(v))
|
||||
return space
|
||||
}
|
||||
|
||||
func writeBuf(buf []byte, v uint32, n int) {
|
||||
buf[n] = byte(v >> 16)
|
||||
buf[n+1] = byte(v >> 8)
|
||||
buf[n+2] = byte(v)
|
||||
func writeBuf(buf []byte, v uint32) []byte {
|
||||
return append(buf, byte(v >> 16), byte(v >> 8), byte(v))
|
||||
}
|
||||
|
||||
// WriteUint8 write uint8 to stream
|
||||
func (stream *Stream) WriteUint8(val uint8) {
|
||||
stream.ensure(3)
|
||||
stream.n = writeFirstBuf(stream.buf, _DIGITS[val], stream.n)
|
||||
stream.buf = writeFirstBuf(stream.buf, digits[val])
|
||||
}
|
||||
|
||||
// WriteInt8 write int8 to stream
|
||||
func (stream *Stream) WriteInt8(nval int8) {
|
||||
stream.ensure(4)
|
||||
n := stream.n
|
||||
var val uint8
|
||||
if nval < 0 {
|
||||
val = uint8(-nval)
|
||||
stream.buf[n] = '-'
|
||||
n++
|
||||
stream.buf = append(stream.buf, '-')
|
||||
} else {
|
||||
val = uint8(nval)
|
||||
}
|
||||
stream.n = writeFirstBuf(stream.buf, _DIGITS[val], n)
|
||||
stream.buf = writeFirstBuf(stream.buf, digits[val])
|
||||
}
|
||||
|
||||
// WriteUint16 write uint16 to stream
|
||||
func (stream *Stream) WriteUint16(val uint16) {
|
||||
stream.ensure(5)
|
||||
q1 := val / 1000
|
||||
if q1 == 0 {
|
||||
stream.n = writeFirstBuf(stream.buf, _DIGITS[val], stream.n)
|
||||
stream.buf = writeFirstBuf(stream.buf, digits[val])
|
||||
return
|
||||
}
|
||||
r1 := val - q1*1000
|
||||
n := writeFirstBuf(stream.buf, _DIGITS[q1], stream.n)
|
||||
writeBuf(stream.buf, _DIGITS[r1], n)
|
||||
stream.n = n + 3
|
||||
stream.buf = writeFirstBuf(stream.buf, digits[q1])
|
||||
stream.buf = writeBuf(stream.buf, digits[r1])
|
||||
return
|
||||
}
|
||||
|
||||
// WriteInt16 write int16 to stream
|
||||
func (stream *Stream) WriteInt16(nval int16) {
|
||||
stream.ensure(6)
|
||||
n := stream.n
|
||||
var val uint16
|
||||
if nval < 0 {
|
||||
val = uint16(-nval)
|
||||
stream.buf[n] = '-'
|
||||
n++
|
||||
stream.buf = append(stream.buf, '-')
|
||||
} else {
|
||||
val = uint16(nval)
|
||||
}
|
||||
q1 := val / 1000
|
||||
if q1 == 0 {
|
||||
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)
|
||||
stream.n = n + 3
|
||||
return
|
||||
stream.WriteUint16(val)
|
||||
}
|
||||
|
||||
// WriteUint32 write uint32 to stream
|
||||
func (stream *Stream) WriteUint32(val uint32) {
|
||||
stream.ensure(10)
|
||||
n := stream.n
|
||||
q1 := val / 1000
|
||||
if q1 == 0 {
|
||||
stream.n = writeFirstBuf(stream.buf, _DIGITS[val], n)
|
||||
stream.buf = writeFirstBuf(stream.buf, digits[val])
|
||||
return
|
||||
}
|
||||
r1 := val - q1*1000
|
||||
q2 := q1 / 1000
|
||||
if q2 == 0 {
|
||||
n := writeFirstBuf(stream.buf, _DIGITS[q1], n)
|
||||
writeBuf(stream.buf, _DIGITS[r1], n)
|
||||
stream.n = n + 3
|
||||
stream.buf = writeFirstBuf(stream.buf, digits[q1])
|
||||
stream.buf = writeBuf(stream.buf, digits[r1])
|
||||
return
|
||||
}
|
||||
r2 := q1 - q2*1000
|
||||
q3 := q2 / 1000
|
||||
if q3 == 0 {
|
||||
n = writeFirstBuf(stream.buf, _DIGITS[q2], n)
|
||||
stream.buf = writeFirstBuf(stream.buf, digits[q2])
|
||||
} else {
|
||||
r3 := q2 - q3*1000
|
||||
stream.buf[n] = byte(q3 + '0')
|
||||
n++
|
||||
writeBuf(stream.buf, _DIGITS[r3], n)
|
||||
n += 3
|
||||
stream.buf = append(stream.buf, byte(q3 + '0'))
|
||||
stream.buf = writeBuf(stream.buf, digits[r3])
|
||||
}
|
||||
writeBuf(stream.buf, _DIGITS[r2], n)
|
||||
writeBuf(stream.buf, _DIGITS[r1], n+3)
|
||||
stream.n = n + 6
|
||||
stream.buf = writeBuf(stream.buf, digits[r2])
|
||||
stream.buf = writeBuf(stream.buf, digits[r1])
|
||||
}
|
||||
|
||||
// WriteInt32 write int32 to stream
|
||||
func (stream *Stream) WriteInt32(nval int32) {
|
||||
stream.ensure(11)
|
||||
n := stream.n
|
||||
var val uint32
|
||||
if nval < 0 {
|
||||
val = uint32(-nval)
|
||||
stream.buf[n] = '-'
|
||||
n++
|
||||
stream.buf = append(stream.buf, '-')
|
||||
} else {
|
||||
val = uint32(nval)
|
||||
}
|
||||
q1 := val / 1000
|
||||
if q1 == 0 {
|
||||
stream.n = writeFirstBuf(stream.buf, _DIGITS[val], n)
|
||||
return
|
||||
}
|
||||
r1 := val - q1*1000
|
||||
q2 := q1 / 1000
|
||||
if q2 == 0 {
|
||||
n := writeFirstBuf(stream.buf, _DIGITS[q1], n)
|
||||
writeBuf(stream.buf, _DIGITS[r1], n)
|
||||
stream.n = n + 3
|
||||
return
|
||||
}
|
||||
r2 := q1 - q2*1000
|
||||
q3 := q2 / 1000
|
||||
if q3 == 0 {
|
||||
n = writeFirstBuf(stream.buf, _DIGITS[q2], n)
|
||||
} else {
|
||||
r3 := q2 - q3*1000
|
||||
stream.buf[n] = byte(q3 + '0')
|
||||
n++
|
||||
writeBuf(stream.buf, _DIGITS[r3], n)
|
||||
n += 3
|
||||
}
|
||||
writeBuf(stream.buf, _DIGITS[r2], n)
|
||||
writeBuf(stream.buf, _DIGITS[r1], n+3)
|
||||
stream.n = n + 6
|
||||
stream.WriteUint32(val)
|
||||
}
|
||||
|
||||
// WriteUint64 write uint64 to stream
|
||||
func (stream *Stream) WriteUint64(val uint64) {
|
||||
stream.ensure(20)
|
||||
n := stream.n
|
||||
q1 := val / 1000
|
||||
if q1 == 0 {
|
||||
stream.n = writeFirstBuf(stream.buf, _DIGITS[val], n)
|
||||
stream.buf = writeFirstBuf(stream.buf, digits[val])
|
||||
return
|
||||
}
|
||||
r1 := val - q1*1000
|
||||
q2 := q1 / 1000
|
||||
if q2 == 0 {
|
||||
n := writeFirstBuf(stream.buf, _DIGITS[q1], n)
|
||||
writeBuf(stream.buf, _DIGITS[r1], n)
|
||||
stream.n = n + 3
|
||||
stream.buf = writeFirstBuf(stream.buf, digits[q1])
|
||||
stream.buf = writeBuf(stream.buf, digits[r1])
|
||||
return
|
||||
}
|
||||
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)
|
||||
stream.n = n + 6
|
||||
stream.buf = writeFirstBuf(stream.buf, digits[q2])
|
||||
stream.buf = writeBuf(stream.buf, digits[r2])
|
||||
stream.buf = writeBuf(stream.buf, digits[r1])
|
||||
return
|
||||
}
|
||||
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)
|
||||
stream.n = n + 9
|
||||
stream.buf = writeFirstBuf(stream.buf, digits[q3])
|
||||
stream.buf = writeBuf(stream.buf, digits[r3])
|
||||
stream.buf = writeBuf(stream.buf, digits[r2])
|
||||
stream.buf = writeBuf(stream.buf, digits[r1])
|
||||
return
|
||||
}
|
||||
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)
|
||||
stream.n = n + 12
|
||||
stream.buf = writeFirstBuf(stream.buf, digits[q4])
|
||||
stream.buf = writeBuf(stream.buf, digits[r4])
|
||||
stream.buf = writeBuf(stream.buf, digits[r3])
|
||||
stream.buf = writeBuf(stream.buf, digits[r2])
|
||||
stream.buf = writeBuf(stream.buf, digits[r1])
|
||||
return
|
||||
}
|
||||
r5 := q4 - q5*1000
|
||||
q6 := q5 / 1000
|
||||
if q6 == 0 {
|
||||
n = writeFirstBuf(stream.buf, _DIGITS[q5], n)
|
||||
stream.buf = writeFirstBuf(stream.buf, digits[q5])
|
||||
} else {
|
||||
n = writeFirstBuf(stream.buf, _DIGITS[q6], n)
|
||||
stream.buf = writeFirstBuf(stream.buf, digits[q6])
|
||||
r6 := q5 - q6*1000
|
||||
writeBuf(stream.buf, _DIGITS[r6], n)
|
||||
n += 3
|
||||
stream.buf = writeBuf(stream.buf, digits[r6])
|
||||
}
|
||||
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
|
||||
stream.buf = writeBuf(stream.buf, digits[r5])
|
||||
stream.buf = writeBuf(stream.buf, digits[r4])
|
||||
stream.buf = writeBuf(stream.buf, digits[r3])
|
||||
stream.buf = writeBuf(stream.buf, digits[r2])
|
||||
stream.buf = writeBuf(stream.buf, digits[r1])
|
||||
}
|
||||
|
||||
// WriteInt64 write int64 to stream
|
||||
func (stream *Stream) WriteInt64(nval int64) {
|
||||
stream.ensure(20)
|
||||
n := stream.n
|
||||
var val uint64
|
||||
if nval < 0 {
|
||||
val = uint64(-nval)
|
||||
stream.buf[n] = '-'
|
||||
n++
|
||||
stream.buf = append(stream.buf, '-')
|
||||
} else {
|
||||
val = uint64(nval)
|
||||
}
|
||||
q1 := val / 1000
|
||||
if q1 == 0 {
|
||||
stream.n = writeFirstBuf(stream.buf, _DIGITS[val], n)
|
||||
return
|
||||
}
|
||||
r1 := val - q1*1000
|
||||
q2 := q1 / 1000
|
||||
if q2 == 0 {
|
||||
n := writeFirstBuf(stream.buf, _DIGITS[q1], n)
|
||||
writeBuf(stream.buf, _DIGITS[r1], n)
|
||||
stream.n = n + 3
|
||||
return
|
||||
}
|
||||
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)
|
||||
stream.n = n + 6
|
||||
return
|
||||
}
|
||||
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)
|
||||
stream.n = n + 9
|
||||
return
|
||||
}
|
||||
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)
|
||||
stream.n = n + 12
|
||||
return
|
||||
}
|
||||
r5 := q4 - q5*1000
|
||||
q6 := q5 / 1000
|
||||
if q6 == 0 {
|
||||
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)
|
||||
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)
|
||||
stream.n = n + 15
|
||||
stream.WriteUint64(val)
|
||||
}
|
||||
|
||||
// WriteInt write int to stream
|
||||
func (stream *Stream) WriteInt(val int) {
|
||||
stream.WriteInt64(int64(val))
|
||||
}
|
||||
|
||||
// WriteUint write uint to stream
|
||||
func (stream *Stream) WriteUint(val uint) {
|
||||
stream.WriteUint64(uint64(val))
|
||||
}
|
||||
|
@ -217,39 +217,28 @@ var safeSet = [utf8.RuneSelf]bool{
|
||||
|
||||
var hex = "0123456789abcdef"
|
||||
|
||||
func (stream *Stream) WriteStringWithHtmlEscaped(s string) {
|
||||
stream.ensure(32)
|
||||
// WriteStringWithHTMLEscaped write string to stream with html special characters escaped
|
||||
func (stream *Stream) WriteStringWithHTMLEscaped(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
|
||||
}
|
||||
n := stream.n
|
||||
stream.buf[n] = '"'
|
||||
n++
|
||||
stream.buf = append(stream.buf, '"')
|
||||
// write string, the fast path, without utf8 and escape support
|
||||
i := 0
|
||||
for ; i < toWriteLen; i++ {
|
||||
for ; i < valLen; i++ {
|
||||
c := s[i]
|
||||
if c < utf8.RuneSelf && htmlSafeSet[c] {
|
||||
stream.buf[n] = c
|
||||
n++
|
||||
stream.buf = append(stream.buf, c)
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
if i == valLen {
|
||||
stream.buf[n] = '"'
|
||||
n++
|
||||
stream.n = n
|
||||
stream.buf = append(stream.buf, '"')
|
||||
return
|
||||
}
|
||||
stream.n = n
|
||||
writeStringSlowPathWithHtmlEscaped(stream, i, s, valLen)
|
||||
writeStringSlowPathWithHTMLEscaped(stream, i, s, valLen)
|
||||
}
|
||||
|
||||
func writeStringSlowPathWithHtmlEscaped(stream *Stream, i int, s string, valLen int) {
|
||||
func writeStringSlowPathWithHTMLEscaped(stream *Stream, i int, s string, valLen int) {
|
||||
start := i
|
||||
// for the remaining parts, we process them char by char
|
||||
for i < valLen {
|
||||
@ -288,6 +277,8 @@ func writeStringSlowPathWithHtmlEscaped(stream *Stream, i int, s string, valLen
|
||||
if start < i {
|
||||
stream.WriteRaw(s[start:i])
|
||||
}
|
||||
stream.WriteRaw(`\ufffd`)
|
||||
i++
|
||||
start = i
|
||||
continue
|
||||
}
|
||||
@ -316,35 +307,24 @@ func writeStringSlowPathWithHtmlEscaped(stream *Stream, i int, s string, valLen
|
||||
stream.writeByte('"')
|
||||
}
|
||||
|
||||
// WriteString write string to stream without html escape
|
||||
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++
|
||||
stream.buf = append(stream.buf, '"')
|
||||
// write string, the fast path, without utf8 and escape support
|
||||
i := 0
|
||||
for ; i < toWriteLen; i++ {
|
||||
for ; i < valLen; i++ {
|
||||
c := s[i]
|
||||
if c > 31 && c != '"' && c != '\\' {
|
||||
stream.buf[n] = c
|
||||
n++
|
||||
stream.buf = append(stream.buf, c)
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
if i == valLen {
|
||||
stream.buf[n] = '"'
|
||||
n++
|
||||
stream.n = n
|
||||
stream.buf = append(stream.buf, '"')
|
||||
return
|
||||
}
|
||||
stream.n = n
|
||||
writeStringSlowPath(stream, i, s, valLen)
|
||||
}
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"github.com/json-iterator/go/require"
|
||||
"github.com/stretchr/testify/require"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@ -23,10 +23,10 @@ func Test_writeBytes_should_grow_buffer(t *testing.T) {
|
||||
stream := NewStream(ConfigDefault, nil, 1)
|
||||
stream.Write([]byte{'1', '2'})
|
||||
should.Equal("12", string(stream.Buffer()))
|
||||
should.Equal(3, len(stream.buf))
|
||||
should.Equal(2, len(stream.buf))
|
||||
stream.Write([]byte{'3', '4', '5', '6', '7'})
|
||||
should.Equal("1234567", string(stream.Buffer()))
|
||||
should.Equal(8, len(stream.buf))
|
||||
should.Equal(7, len(stream.buf))
|
||||
}
|
||||
|
||||
func Test_writeIndention_should_grow_buffer(t *testing.T) {
|
||||
@ -51,3 +51,19 @@ func Test_writeString_should_grow_buffer(t *testing.T) {
|
||||
should.Nil(stream.Error)
|
||||
should.Equal(`"123"`, string(stream.Buffer()))
|
||||
}
|
||||
|
||||
type NopWriter struct {
|
||||
bufferSize int
|
||||
}
|
||||
|
||||
func (w *NopWriter) Write(p []byte) (n int, err error) {
|
||||
w.bufferSize = cap(p)
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
func Test_flush_buffer_should_stop_grow_buffer(t *testing.T) {
|
||||
writer := new(NopWriter)
|
||||
NewEncoder(writer).Encode(make([]int, 10000000))
|
||||
should := require.New(t)
|
||||
should.Equal(8, writer.bufferSize)
|
||||
}
|
7
fuzzy_mode_convert_table.md
Normal file
7
fuzzy_mode_convert_table.md
Normal file
@ -0,0 +1,7 @@
|
||||
| json type \ dest type | bool | int | uint | float |string|
|
||||
| --- | --- | --- | --- |--|--|
|
||||
| number | positive => true <br/> negative => true <br/> zero => false| 23.2 => 23 <br/> -32.1 => -32| 12.1 => 12 <br/> -12.1 => 0|as normal|same as origin|
|
||||
| string | empty string => false <br/> string "0" => false <br/> other strings => true | "123.32" => 123 <br/> "-123.4" => -123 <br/> "123.23xxxw" => 123 <br/> "abcde12" => 0 <br/> "-32.1" => -32| 13.2 => 13 <br/> -1.1 => 0 |12.1 => 12.1 <br/> -12.3 => -12.3<br/> 12.4xxa => 12.4 <br/> +1.1e2 =>110 |same as origin|
|
||||
| bool | true => true <br/> false => false| true => 1 <br/> false => 0 | true => 1 <br/> false => 0 |true => 1 <br/>false => 0|true => "true" <br/> false => "false"|
|
||||
| object | true | 0 | 0 |0|originnal json|
|
||||
| array | empty array => false <br/> nonempty array => true| [] => 0 <br/> [1,2] => 1 | [] => 0 <br/> [1,2] => 1 |[] => 0<br/>[1,2] => 1|original json|
|
18
jsoniter.go
Normal file
18
jsoniter.go
Normal file
@ -0,0 +1,18 @@
|
||||
// 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.
|
||||
//
|
||||
// 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
|
@ -1,96 +0,0 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"github.com/json-iterator/go/require"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_read_empty_array_as_any(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := Get([]byte("[]"))
|
||||
should.Equal(Array, any.Get().ValueType())
|
||||
should.Equal(Invalid, any.Get(0.3).ValueType())
|
||||
should.Equal(0, any.Size())
|
||||
should.Equal(Array, any.ValueType())
|
||||
should.Nil(any.LastError())
|
||||
should.Equal(0, any.ToInt())
|
||||
should.Equal(int32(0), any.ToInt32())
|
||||
should.Equal(int64(0), any.ToInt64())
|
||||
should.Equal(uint(0), any.ToUint())
|
||||
should.Equal(uint32(0), any.ToUint32())
|
||||
should.Equal(uint64(0), any.ToUint64())
|
||||
should.Equal(float32(0), any.ToFloat32())
|
||||
should.Equal(float64(0), any.ToFloat64())
|
||||
}
|
||||
|
||||
func Test_read_one_element_array_as_any(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := Get([]byte("[1]"))
|
||||
should.Equal(1, any.Size())
|
||||
}
|
||||
|
||||
func Test_read_two_element_array_as_any(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := Get([]byte("[1,2]"))
|
||||
should.Equal(1, any.Get(0).ToInt())
|
||||
should.Equal(2, any.Size())
|
||||
should.True(any.ToBool())
|
||||
should.Equal(1, any.ToInt())
|
||||
should.Equal(1, any.GetArray()[0].ToInt())
|
||||
should.Equal([]interface{}{float64(1), float64(2)}, any.GetInterface())
|
||||
stream := NewStream(ConfigDefault, nil, 32)
|
||||
any.WriteTo(stream)
|
||||
should.Equal("[1,2]", string(stream.Buffer()))
|
||||
arr := []int{}
|
||||
any.ToVal(&arr)
|
||||
should.Equal([]int{1, 2}, arr)
|
||||
}
|
||||
|
||||
func Test_wrap_array(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := Wrap([]int{1, 2, 3})
|
||||
should.Equal("[1,2,3]", any.ToString())
|
||||
}
|
||||
|
||||
func Test_array_lazy_any_get(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := Get([]byte("[1,[2,3],4]"))
|
||||
should.Equal(3, any.Get(1, 1).ToInt())
|
||||
should.Equal("[1,[2,3],4]", any.ToString())
|
||||
}
|
||||
|
||||
func Test_array_lazy_any_get_all(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := Get([]byte("[[1],[2],[3,4]]"))
|
||||
should.Equal("[1,2,3]", any.Get('*', 0).ToString())
|
||||
any = Get([]byte("[[[1],[2],[3,4]]]"), 0, '*', 0)
|
||||
should.Equal("[1,2,3]", any.ToString())
|
||||
}
|
||||
|
||||
func Test_array_wrapper_any_get_all(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := wrapArray([][]int{
|
||||
{1, 2},
|
||||
{3, 4},
|
||||
{5, 6},
|
||||
})
|
||||
should.Equal("[1,3,5]", any.Get('*', 0).ToString())
|
||||
should.Equal(Array, any.ValueType())
|
||||
should.True(any.ToBool())
|
||||
should.Equal(1, any.Get(0, 0).ToInt())
|
||||
}
|
||||
|
||||
func Test_array_lazy_any_get_invalid(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := Get([]byte("[]"))
|
||||
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())
|
||||
}
|
||||
|
||||
func Test_invalid_array(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := Get([]byte("["), 0)
|
||||
should.Equal(Invalid, any.ValueType())
|
||||
}
|
@ -1,12 +0,0 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"github.com/json-iterator/go/require"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_read_bool_as_any(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := Get([]byte("true"))
|
||||
should.True(any.ToBool())
|
||||
}
|
@ -1,14 +0,0 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"github.com/json-iterator/go/require"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_read_float_as_any(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := Get([]byte("12.3"))
|
||||
should.Equal(float64(12.3), any.ToFloat64())
|
||||
should.Equal("12.3", any.ToString())
|
||||
should.True(any.ToBool())
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"github.com/json-iterator/go/require"
|
||||
"io"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_read_int64_as_any(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := Get([]byte("1234"))
|
||||
should.Equal(1234, any.ToInt())
|
||||
should.Equal(io.EOF, any.LastError())
|
||||
should.Equal("1234", any.ToString())
|
||||
should.True(any.ToBool())
|
||||
}
|
||||
|
||||
func Test_int_lazy_any_get(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := Get([]byte("1234"))
|
||||
should.Equal(Invalid, any.Get(1, "2").ValueType())
|
||||
}
|
@ -1,14 +0,0 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"github.com/json-iterator/go/require"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_wrap_map(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := Wrap(map[string]string{"Field1": "hello"})
|
||||
should.Equal("hello", any.Get("Field1").ToString())
|
||||
any = Wrap(map[string]string{"Field1": "hello"})
|
||||
should.Equal(1, any.Size())
|
||||
}
|
@ -1,75 +0,0 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"github.com/json-iterator/go/require"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_read_object_as_any(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := Get([]byte(`{"a":"b","c":"d"}`))
|
||||
should.Equal(`{"a":"b","c":"d"}`, any.ToString())
|
||||
// partial parse
|
||||
should.Equal("b", any.Get("a").ToString())
|
||||
should.Equal("d", any.Get("c").ToString())
|
||||
should.Equal(2, len(any.Keys()))
|
||||
any = Get([]byte(`{"a":"b","c":"d"}`))
|
||||
// full parse
|
||||
should.Equal(2, len(any.Keys()))
|
||||
should.Equal(2, any.Size())
|
||||
should.True(any.ToBool())
|
||||
should.Equal(1, any.ToInt())
|
||||
should.Equal(Object, any.ValueType())
|
||||
should.Nil(any.LastError())
|
||||
should.Equal("b", any.GetObject()["a"].ToString())
|
||||
obj := struct {
|
||||
A string
|
||||
}{}
|
||||
any.ToVal(&obj)
|
||||
should.Equal("b", obj.A)
|
||||
}
|
||||
|
||||
func Test_object_lazy_any_get(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := Get([]byte(`{"a":{"b":{"c":"d"}}}`))
|
||||
should.Equal("d", any.Get("a", "b", "c").ToString())
|
||||
}
|
||||
|
||||
func Test_object_lazy_any_get_all(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := Get([]byte(`{"a":[0],"b":[1]}`))
|
||||
should.Contains(any.Get('*', 0).ToString(), `"a":0`)
|
||||
}
|
||||
|
||||
func Test_object_lazy_any_get_invalid(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := Get([]byte(`{}`))
|
||||
should.Equal(Invalid, any.Get("a", "b", "c").ValueType())
|
||||
should.Equal(Invalid, any.Get(1).ValueType())
|
||||
}
|
||||
|
||||
func Test_wrap_object(t *testing.T) {
|
||||
should := require.New(t)
|
||||
type TestObject struct {
|
||||
Field1 string
|
||||
field2 string
|
||||
}
|
||||
any := Wrap(TestObject{"hello", "world"})
|
||||
should.Equal("hello", any.Get("Field1").ToString())
|
||||
any = Wrap(TestObject{"hello", "world"})
|
||||
should.Equal(2, any.Size())
|
||||
should.Equal(`{"Field1":"hello"}`, any.Get('*').ToString())
|
||||
}
|
||||
|
||||
func Test_any_within_struct(t *testing.T) {
|
||||
should := require.New(t)
|
||||
type TestObject struct {
|
||||
Field1 Any
|
||||
Field2 Any
|
||||
}
|
||||
obj := TestObject{}
|
||||
err := UnmarshalFromString(`{"Field1": "hello", "Field2": [1,2,3]}`, &obj)
|
||||
should.Nil(err)
|
||||
should.Equal("hello", obj.Field1.ToString())
|
||||
should.Equal("[1,2,3]", obj.Field2.ToString())
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"github.com/json-iterator/go/require"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_read_string_as_any(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := Get([]byte(`"hello"`))
|
||||
should.Equal("hello", any.ToString())
|
||||
should.True(any.ToBool())
|
||||
any = Get([]byte(`" "`))
|
||||
should.False(any.ToBool())
|
||||
any = Get([]byte(`"false"`))
|
||||
should.False(any.ToBool())
|
||||
any = Get([]byte(`"123"`))
|
||||
should.Equal(123, any.ToInt())
|
||||
}
|
||||
|
||||
func Test_wrap_string(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := WrapString("123")
|
||||
should.Equal(123, any.ToInt())
|
||||
}
|
@ -1,80 +0,0 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"github.com/json-iterator/go/require"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_true(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := ParseString(ConfigDefault, `true`)
|
||||
should.True(iter.ReadBool())
|
||||
iter = ParseString(ConfigDefault, `true`)
|
||||
should.Equal(true, iter.Read())
|
||||
}
|
||||
|
||||
func Test_false(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := ParseString(ConfigDefault, `false`)
|
||||
should.False(iter.ReadBool())
|
||||
}
|
||||
|
||||
func Test_write_true_false(t *testing.T) {
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(ConfigDefault, buf, 4096)
|
||||
stream.WriteTrue()
|
||||
stream.WriteFalse()
|
||||
stream.Flush()
|
||||
should.Nil(stream.Error)
|
||||
should.Equal("truefalse", buf.String())
|
||||
}
|
||||
|
||||
func Test_write_val_bool(t *testing.T) {
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(ConfigDefault, buf, 4096)
|
||||
stream.WriteVal(true)
|
||||
stream.Flush()
|
||||
should.Nil(stream.Error)
|
||||
should.Equal("true", buf.String())
|
||||
}
|
||||
|
||||
func Test_encode_string_bool(t *testing.T) {
|
||||
type TestObject struct {
|
||||
Field bool `json:",omitempty,string"`
|
||||
}
|
||||
should := require.New(t)
|
||||
output, err := json.Marshal(TestObject{true})
|
||||
should.Nil(err)
|
||||
should.Equal(`{"Field":"true"}`, string(output))
|
||||
output, err = Marshal(TestObject{true})
|
||||
should.Nil(err)
|
||||
should.Equal(`{"Field":"true"}`, string(output))
|
||||
}
|
||||
|
||||
func Test_decode_string_bool(t *testing.T) {
|
||||
type TestObject struct {
|
||||
Field bool `json:",omitempty,string"`
|
||||
}
|
||||
should := require.New(t)
|
||||
obj := TestObject{}
|
||||
err := json.Unmarshal([]byte(`{"Field":"true"}`), &obj)
|
||||
should.Nil(err)
|
||||
should.True(obj.Field)
|
||||
|
||||
obj = TestObject{}
|
||||
err = json.Unmarshal([]byte(`{"Field":true}`), &obj)
|
||||
should.NotNil(err)
|
||||
|
||||
obj = TestObject{}
|
||||
err = Unmarshal([]byte(`{"Field":"true"}`), &obj)
|
||||
should.Nil(err)
|
||||
should.True(obj.Field)
|
||||
|
||||
obj = TestObject{}
|
||||
err = Unmarshal([]byte(`{"Field":true}`), &obj)
|
||||
should.NotNil(err)
|
||||
}
|
@ -1,287 +0,0 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/json-iterator/go/require"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
func Test_customize_type_decoder(t *testing.T) {
|
||||
RegisterTypeDecoderFunc("time.Time", func(ptr unsafe.Pointer, iter *Iterator) {
|
||||
t, err := time.ParseInLocation("2006-01-02 15:04:05", iter.ReadString(), time.UTC)
|
||||
if err != nil {
|
||||
iter.Error = err
|
||||
return
|
||||
}
|
||||
*((*time.Time)(ptr)) = t
|
||||
})
|
||||
defer ConfigDefault.cleanDecoders()
|
||||
val := time.Time{}
|
||||
err := Unmarshal([]byte(`"2016-12-05 08:43:28"`), &val)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
year, month, day := val.Date()
|
||||
if year != 2016 || month != 12 || day != 5 {
|
||||
t.Fatal(val)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_customize_type_encoder(t *testing.T) {
|
||||
should := require.New(t)
|
||||
RegisterTypeEncoderFunc("time.Time", func(ptr unsafe.Pointer, stream *Stream) {
|
||||
t := *((*time.Time)(ptr))
|
||||
stream.WriteString(t.UTC().Format("2006-01-02 15:04:05"))
|
||||
}, nil)
|
||||
defer ConfigDefault.cleanEncoders()
|
||||
val := time.Unix(0, 0)
|
||||
str, err := MarshalToString(val)
|
||||
should.Nil(err)
|
||||
should.Equal(`"1970-01-01 00:00:00"`, str)
|
||||
}
|
||||
|
||||
func Test_customize_byte_array_encoder(t *testing.T) {
|
||||
ConfigDefault.cleanEncoders()
|
||||
should := require.New(t)
|
||||
RegisterTypeEncoderFunc("[]uint8", func(ptr unsafe.Pointer, stream *Stream) {
|
||||
t := *((*[]byte)(ptr))
|
||||
stream.WriteString(string(t))
|
||||
}, nil)
|
||||
defer ConfigDefault.cleanEncoders()
|
||||
val := []byte("abc")
|
||||
str, err := MarshalToString(val)
|
||||
should.Nil(err)
|
||||
should.Equal(`"abc"`, str)
|
||||
}
|
||||
|
||||
func Test_customize_float_marshal(t *testing.T) {
|
||||
should := require.New(t)
|
||||
json := Config{MarshalFloatWith6Digits: true}.Froze()
|
||||
str, err := json.MarshalToString(float32(1.23456789))
|
||||
should.Nil(err)
|
||||
should.Equal("1.234568", str)
|
||||
}
|
||||
|
||||
type Tom struct {
|
||||
field1 string
|
||||
}
|
||||
|
||||
func Test_customize_field_decoder(t *testing.T) {
|
||||
RegisterFieldDecoderFunc("jsoniter.Tom", "field1", func(ptr unsafe.Pointer, iter *Iterator) {
|
||||
*((*string)(ptr)) = strconv.Itoa(iter.ReadInt())
|
||||
})
|
||||
defer ConfigDefault.cleanDecoders()
|
||||
tom := Tom{}
|
||||
err := Unmarshal([]byte(`{"field1": 100}`), &tom)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
type TestObject1 struct {
|
||||
field1 string
|
||||
}
|
||||
|
||||
type testExtension struct {
|
||||
DummyExtension
|
||||
}
|
||||
|
||||
func (extension *testExtension) UpdateStructDescriptor(structDescriptor *StructDescriptor) {
|
||||
if structDescriptor.Type.String() != "jsoniter.TestObject1" {
|
||||
return
|
||||
}
|
||||
binding := structDescriptor.GetField("field1")
|
||||
binding.Encoder = &funcEncoder{fun: func(ptr unsafe.Pointer, stream *Stream) {
|
||||
str := *((*string)(ptr))
|
||||
val, _ := strconv.Atoi(str)
|
||||
stream.WriteInt(val)
|
||||
}}
|
||||
binding.Decoder = &funcDecoder{func(ptr unsafe.Pointer, iter *Iterator) {
|
||||
*((*string)(ptr)) = strconv.Itoa(iter.ReadInt())
|
||||
}}
|
||||
binding.ToNames = []string{"field-1"}
|
||||
binding.FromNames = []string{"field-1"}
|
||||
}
|
||||
|
||||
func Test_customize_field_by_extension(t *testing.T) {
|
||||
should := require.New(t)
|
||||
RegisterExtension(&testExtension{})
|
||||
obj := TestObject1{}
|
||||
err := UnmarshalFromString(`{"field-1": 100}`, &obj)
|
||||
should.Nil(err)
|
||||
should.Equal("100", obj.field1)
|
||||
str, err := MarshalToString(obj)
|
||||
should.Nil(err)
|
||||
should.Equal(`{"field-1":100}`, str)
|
||||
}
|
||||
|
||||
type timeImplementedMarshaler time.Time
|
||||
|
||||
func (obj timeImplementedMarshaler) MarshalJSON() ([]byte, error) {
|
||||
seconds := time.Time(obj).Unix()
|
||||
return []byte(strconv.FormatInt(seconds, 10)), nil
|
||||
}
|
||||
|
||||
func Test_marshaler(t *testing.T) {
|
||||
type TestObject struct {
|
||||
Field timeImplementedMarshaler
|
||||
}
|
||||
should := require.New(t)
|
||||
val := timeImplementedMarshaler(time.Unix(123, 0))
|
||||
obj := TestObject{val}
|
||||
bytes, err := json.Marshal(obj)
|
||||
should.Nil(err)
|
||||
should.Equal(`{"Field":123}`, string(bytes))
|
||||
str, err := MarshalToString(obj)
|
||||
should.Nil(err)
|
||||
should.Equal(`{"Field":123}`, str)
|
||||
}
|
||||
|
||||
func Test_marshaler_and_encoder(t *testing.T) {
|
||||
type TestObject struct {
|
||||
Field *timeImplementedMarshaler
|
||||
}
|
||||
ConfigDefault.cleanEncoders()
|
||||
should := require.New(t)
|
||||
RegisterTypeEncoderFunc("jsoniter.timeImplementedMarshaler", func(ptr unsafe.Pointer, stream *Stream) {
|
||||
stream.WriteString("hello from encoder")
|
||||
}, nil)
|
||||
val := timeImplementedMarshaler(time.Unix(123, 0))
|
||||
obj := TestObject{&val}
|
||||
bytes, err := json.Marshal(obj)
|
||||
should.Nil(err)
|
||||
should.Equal(`{"Field":123}`, string(bytes))
|
||||
str, err := MarshalToString(obj)
|
||||
should.Nil(err)
|
||||
should.Equal(`{"Field":"hello from encoder"}`, str)
|
||||
}
|
||||
|
||||
type ObjectImplementedUnmarshaler int
|
||||
|
||||
func (obj *ObjectImplementedUnmarshaler) UnmarshalJSON(s []byte) error {
|
||||
val, _ := strconv.ParseInt(string(s[1:len(s)-1]), 10, 64)
|
||||
*obj = ObjectImplementedUnmarshaler(val)
|
||||
return nil
|
||||
}
|
||||
|
||||
func Test_unmarshaler(t *testing.T) {
|
||||
should := require.New(t)
|
||||
var obj ObjectImplementedUnmarshaler
|
||||
err := json.Unmarshal([]byte(` "100" `), &obj)
|
||||
should.Nil(err)
|
||||
should.Equal(100, int(obj))
|
||||
iter := ParseString(ConfigDefault, ` "100" `)
|
||||
iter.ReadVal(&obj)
|
||||
should.Nil(err)
|
||||
should.Equal(100, int(obj))
|
||||
}
|
||||
|
||||
func Test_unmarshaler_and_decoder(t *testing.T) {
|
||||
type TestObject struct {
|
||||
Field *ObjectImplementedUnmarshaler
|
||||
Field2 string
|
||||
}
|
||||
ConfigDefault.cleanDecoders()
|
||||
should := require.New(t)
|
||||
RegisterTypeDecoderFunc("jsoniter.ObjectImplementedUnmarshaler", func(ptr unsafe.Pointer, iter *Iterator) {
|
||||
*(*ObjectImplementedUnmarshaler)(ptr) = 10
|
||||
iter.Skip()
|
||||
})
|
||||
obj := TestObject{}
|
||||
val := ObjectImplementedUnmarshaler(0)
|
||||
obj.Field = &val
|
||||
err := json.Unmarshal([]byte(`{"Field":"100"}`), &obj)
|
||||
should.Nil(err)
|
||||
should.Equal(100, int(*obj.Field))
|
||||
err = Unmarshal([]byte(`{"Field":"100"}`), &obj)
|
||||
should.Nil(err)
|
||||
should.Equal(10, int(*obj.Field))
|
||||
}
|
||||
|
||||
type tmString string
|
||||
type tmStruct struct {
|
||||
String tmString
|
||||
}
|
||||
|
||||
func (s tmStruct) MarshalJSON() ([]byte, error) {
|
||||
var b []byte
|
||||
b = append(b, '"')
|
||||
b = append(b, s.String...)
|
||||
b = append(b, '"')
|
||||
return b, nil
|
||||
}
|
||||
|
||||
func Test_marshaler_on_struct(t *testing.T) {
|
||||
fixed := tmStruct{"hello"}
|
||||
//json.Marshal(fixed)
|
||||
Marshal(fixed)
|
||||
}
|
||||
|
||||
type withChan struct {
|
||||
F2 chan []byte
|
||||
}
|
||||
|
||||
func (q withChan) MarshalJSON() ([]byte, error) {
|
||||
return []byte(`""`), nil
|
||||
}
|
||||
|
||||
func (q *withChan) UnmarshalJSON(value []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func Test_marshal_json_with_chan(t *testing.T) {
|
||||
type TestObject struct {
|
||||
F1 withChan
|
||||
}
|
||||
should := require.New(t)
|
||||
output, err := MarshalToString(TestObject{})
|
||||
should.Nil(err)
|
||||
should.Equal(`{"F1":""}`, output)
|
||||
}
|
||||
|
||||
type withTime struct {
|
||||
time.Time
|
||||
}
|
||||
|
||||
func (t *withTime) UnmarshalJSON(b []byte) error {
|
||||
return nil
|
||||
}
|
||||
func (t withTime) MarshalJSON() ([]byte, error) {
|
||||
return []byte(`"fake"`), nil
|
||||
}
|
||||
|
||||
func Test_marshal_json_with_time(t *testing.T) {
|
||||
type S1 struct {
|
||||
F1 withTime
|
||||
F2 *withTime
|
||||
}
|
||||
type TestObject struct {
|
||||
TF1 S1
|
||||
}
|
||||
should := require.New(t)
|
||||
obj := TestObject{
|
||||
S1{
|
||||
F1: withTime{
|
||||
time.Unix(0, 0),
|
||||
},
|
||||
F2: &withTime{
|
||||
time.Unix(0, 0),
|
||||
},
|
||||
},
|
||||
}
|
||||
output, err := json.Marshal(obj)
|
||||
should.Nil(err)
|
||||
should.Equal(`{"TF1":{"F1":"fake","F2":"fake"}}`, string(output))
|
||||
output, err = Marshal(obj)
|
||||
should.Nil(err)
|
||||
should.Equal(`{"TF1":{"F1":"fake","F2":"fake"}}`, string(output))
|
||||
obj = TestObject{}
|
||||
should.Nil(json.Unmarshal([]byte(`{"TF1":{"F1":"fake","F2":"fake"}}`), &obj))
|
||||
should.NotNil(obj.TF1.F2)
|
||||
obj = TestObject{}
|
||||
should.Nil(Unmarshal([]byte(`{"TF1":{"F1":"fake","F2":"fake"}}`), &obj))
|
||||
should.NotNil(obj.TF1.F2)
|
||||
}
|
@ -1,86 +0,0 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/json-iterator/go/require"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_bind_api_demo(t *testing.T) {
|
||||
should := require.New(t)
|
||||
val := []int{}
|
||||
err := UnmarshalFromString(`[0,1,2,3] `, &val)
|
||||
should.Nil(err)
|
||||
should.Equal([]int{0, 1, 2, 3}, val)
|
||||
}
|
||||
|
||||
func Test_iterator_api_demo(t *testing.T) {
|
||||
iter := ParseString(ConfigDefault, `[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)
|
||||
}
|
||||
}
|
||||
}
|
@ -1,37 +0,0 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/json-iterator/go/require"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_encode_fixed_array(t *testing.T) {
|
||||
should := require.New(t)
|
||||
type FixedArray [2]float64
|
||||
fixed := FixedArray{0.1, 1.0}
|
||||
output, err := MarshalToString(fixed)
|
||||
should.Nil(err)
|
||||
should.Equal("[0.1,1]", output)
|
||||
}
|
||||
|
||||
func Test_encode_fixed_array_of_map(t *testing.T) {
|
||||
should := require.New(t)
|
||||
type FixedArray [2]map[string]string
|
||||
fixed := FixedArray{map[string]string{"1": "2"}, map[string]string{"3": "4"}}
|
||||
output, err := MarshalToString(fixed)
|
||||
should.Nil(err)
|
||||
should.Equal(`[{"1":"2"},{"3":"4"}]`, output)
|
||||
}
|
||||
|
||||
func Test_decode_fixed_array(t *testing.T) {
|
||||
should := require.New(t)
|
||||
type FixedArray [2]float64
|
||||
var fixed FixedArray
|
||||
should.Nil(json.Unmarshal([]byte("[1,2,3]"), &fixed))
|
||||
should.Equal(float64(1), fixed[0])
|
||||
should.Equal(float64(2), fixed[1])
|
||||
should.Nil(Unmarshal([]byte("[1,2,3]"), &fixed))
|
||||
should.Equal(float64(1), fixed[0])
|
||||
should.Equal(float64(2), fixed[1])
|
||||
}
|
@ -1,197 +0,0 @@
|
||||
// +build go1.8
|
||||
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/json-iterator/go/require"
|
||||
"strconv"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_read_big_float(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := ParseString(ConfigDefault, `12.3`)
|
||||
val := iter.ReadBigFloat()
|
||||
val64, _ := val.Float64()
|
||||
should.Equal(12.3, val64)
|
||||
}
|
||||
|
||||
func Test_read_big_int(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := ParseString(ConfigDefault, `92233720368547758079223372036854775807`)
|
||||
val := iter.ReadBigInt()
|
||||
should.NotNil(val)
|
||||
should.Equal(`92233720368547758079223372036854775807`, val.String())
|
||||
}
|
||||
|
||||
func Test_read_float(t *testing.T) {
|
||||
inputs := []string{`1.1`, `1000`, `9223372036854775807`, `12.3`, `-12.3`, `720368.54775807`, `720368.547758075`}
|
||||
for _, input := range inputs {
|
||||
// non-streaming
|
||||
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := ParseString(ConfigDefault, 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(ConfigDefault, input+",")
|
||||
expected, err := strconv.ParseFloat(input, 64)
|
||||
should.Nil(err)
|
||||
should.Equal(expected, iter.ReadFloat64())
|
||||
})
|
||||
// streaming
|
||||
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := Parse(ConfigDefault, 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(ConfigDefault, bytes.NewBufferString(input+","), 2)
|
||||
expected, err := strconv.ParseFloat(input, 64)
|
||||
should.Nil(err)
|
||||
should.Equal(expected, iter.ReadFloat64())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_read_float_as_interface(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := ParseString(ConfigDefault, `12.3`)
|
||||
should.Equal(float64(12.3), iter.Read())
|
||||
}
|
||||
|
||||
func Test_wrap_float(t *testing.T) {
|
||||
should := require.New(t)
|
||||
str, err := MarshalToString(WrapFloat64(12.3))
|
||||
should.Nil(err)
|
||||
should.Equal("12.3", str)
|
||||
}
|
||||
|
||||
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}
|
||||
for _, val := range vals {
|
||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(ConfigDefault, buf, 4096)
|
||||
stream.WriteFloat32Lossy(val)
|
||||
stream.Flush()
|
||||
should.Nil(stream.Error)
|
||||
output, err := json.Marshal(val)
|
||||
should.Nil(err)
|
||||
should.Equal(string(output), buf.String())
|
||||
})
|
||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(ConfigDefault, buf, 4096)
|
||||
stream.WriteVal(val)
|
||||
stream.Flush()
|
||||
should.Nil(stream.Error)
|
||||
output, err := json.Marshal(val)
|
||||
should.Nil(err)
|
||||
should.Equal(string(output), buf.String())
|
||||
})
|
||||
}
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(ConfigDefault, buf, 10)
|
||||
stream.WriteRaw("abcdefg")
|
||||
stream.WriteFloat32Lossy(1.123456)
|
||||
stream.Flush()
|
||||
should.Nil(stream.Error)
|
||||
should.Equal("abcdefg1.123456", buf.String())
|
||||
}
|
||||
|
||||
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}
|
||||
for _, val := range vals {
|
||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(ConfigDefault, buf, 4096)
|
||||
stream.WriteFloat64Lossy(val)
|
||||
stream.Flush()
|
||||
should.Nil(stream.Error)
|
||||
should.Equal(strconv.FormatFloat(val, 'f', -1, 64), buf.String())
|
||||
})
|
||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(ConfigDefault, buf, 4096)
|
||||
stream.WriteVal(val)
|
||||
stream.Flush()
|
||||
should.Nil(stream.Error)
|
||||
should.Equal(strconv.FormatFloat(val, 'f', -1, 64), buf.String())
|
||||
})
|
||||
}
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(ConfigDefault, buf, 10)
|
||||
stream.WriteRaw("abcdefg")
|
||||
stream.WriteFloat64Lossy(1.123456)
|
||||
stream.Flush()
|
||||
should.Nil(stream.Error)
|
||||
should.Equal("abcdefg1.123456", buf.String())
|
||||
}
|
||||
|
||||
func Test_read_float64_cursor(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := ParseString(ConfigDefault, "[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 Test_lossy_float_marshal(t *testing.T) {
|
||||
should := require.New(t)
|
||||
api := Config{MarshalFloatWith6Digits: true}.Froze()
|
||||
output, err := api.MarshalToString(float64(0.1234567))
|
||||
should.Nil(err)
|
||||
should.Equal("0.123457", output)
|
||||
output, err = api.MarshalToString(float32(0.1234567))
|
||||
should.Nil(err)
|
||||
should.Equal("0.123457", output)
|
||||
}
|
||||
|
||||
func Benchmark_jsoniter_float(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
input := []byte(`1.1123,`)
|
||||
iter := NewIterator(ConfigDefault)
|
||||
for n := 0; n < b.N; n++ {
|
||||
iter.ResetBytes(input)
|
||||
iter.ReadFloat64()
|
||||
}
|
||||
}
|
||||
|
||||
func Benchmark_json_float(b *testing.B) {
|
||||
for n := 0; n < b.N; n++ {
|
||||
result := float64(0)
|
||||
json.Unmarshal([]byte(`1.1`), &result)
|
||||
}
|
||||
}
|
@ -1,290 +0,0 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/json-iterator/go/require"
|
||||
"testing"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
func Test_write_array_of_interface(t *testing.T) {
|
||||
should := require.New(t)
|
||||
array := []interface{}{"hello"}
|
||||
str, err := MarshalToString(array)
|
||||
should.Nil(err)
|
||||
should.Equal(`["hello"]`, str)
|
||||
}
|
||||
|
||||
func Test_write_map_of_interface(t *testing.T) {
|
||||
should := require.New(t)
|
||||
val := map[string]interface{}{"hello": "world"}
|
||||
str, err := MarshalToString(val)
|
||||
should.Nil(err)
|
||||
should.Equal(`{"hello":"world"}`, str)
|
||||
}
|
||||
|
||||
func Test_write_map_of_interface_in_struct(t *testing.T) {
|
||||
type TestObject struct {
|
||||
Field map[string]interface{}
|
||||
}
|
||||
should := require.New(t)
|
||||
val := TestObject{map[string]interface{}{"hello": "world"}}
|
||||
str, err := MarshalToString(val)
|
||||
should.Nil(err)
|
||||
should.Equal(`{"Field":{"hello":"world"}}`, str)
|
||||
}
|
||||
|
||||
func Test_write_map_of_interface_in_struct_with_two_fields(t *testing.T) {
|
||||
type TestObject struct {
|
||||
Field map[string]interface{}
|
||||
Field2 string
|
||||
}
|
||||
should := require.New(t)
|
||||
val := TestObject{map[string]interface{}{"hello": "world"}, ""}
|
||||
str, err := MarshalToString(val)
|
||||
should.Nil(err)
|
||||
should.Contains(str, `"Field":{"hello":"world"}`)
|
||||
}
|
||||
|
||||
type MyInterface interface {
|
||||
Hello() string
|
||||
}
|
||||
|
||||
type MyString string
|
||||
|
||||
func (ms MyString) Hello() string {
|
||||
return string(ms)
|
||||
}
|
||||
|
||||
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}
|
||||
str, err := MarshalToString(val)
|
||||
should.Nil(err)
|
||||
should.Equal(`{"hello":"world"}`, str)
|
||||
}
|
||||
|
||||
func Test_write_interface(t *testing.T) {
|
||||
should := require.New(t)
|
||||
var val interface{}
|
||||
val = "hello"
|
||||
str, err := MarshalToString(val)
|
||||
should.Nil(err)
|
||||
should.Equal(`"hello"`, str)
|
||||
}
|
||||
|
||||
func Test_read_interface(t *testing.T) {
|
||||
should := require.New(t)
|
||||
var val interface{}
|
||||
err := UnmarshalFromString(`"hello"`, &val)
|
||||
should.Nil(err)
|
||||
should.Equal("hello", val)
|
||||
}
|
||||
|
||||
func Test_read_custom_interface(t *testing.T) {
|
||||
should := require.New(t)
|
||||
var val MyInterface
|
||||
RegisterTypeDecoderFunc("jsoniter.MyInterface", func(ptr unsafe.Pointer, iter *Iterator) {
|
||||
*((*MyInterface)(ptr)) = MyString(iter.ReadString())
|
||||
})
|
||||
err := UnmarshalFromString(`"hello"`, &val)
|
||||
should.Nil(err)
|
||||
should.Equal("hello", val.Hello())
|
||||
}
|
||||
|
||||
func Test_decode_object_contain_empty_interface(t *testing.T) {
|
||||
type TestObject struct {
|
||||
Field interface{}
|
||||
}
|
||||
should := require.New(t)
|
||||
obj := TestObject{}
|
||||
obj.Field = 1024
|
||||
should.Nil(UnmarshalFromString(`{"Field": "hello"}`, &obj))
|
||||
should.Equal("hello", obj.Field)
|
||||
}
|
||||
|
||||
func Test_decode_object_contain_non_empty_interface(t *testing.T) {
|
||||
type TestObject struct {
|
||||
Field MyInterface
|
||||
}
|
||||
should := require.New(t)
|
||||
obj := TestObject{}
|
||||
obj.Field = MyString("abc")
|
||||
should.Nil(UnmarshalFromString(`{"Field": "hello"}`, &obj))
|
||||
should.Equal(MyString("hello"), obj.Field)
|
||||
}
|
||||
|
||||
func Test_encode_object_contain_empty_interface(t *testing.T) {
|
||||
type TestObject struct {
|
||||
Field interface{}
|
||||
}
|
||||
should := require.New(t)
|
||||
obj := TestObject{}
|
||||
obj.Field = 1024
|
||||
str, err := MarshalToString(obj)
|
||||
should.Nil(err)
|
||||
should.Equal(`{"Field":1024}`, str)
|
||||
}
|
||||
|
||||
func Test_encode_object_contain_non_empty_interface(t *testing.T) {
|
||||
type TestObject struct {
|
||||
Field MyInterface
|
||||
}
|
||||
should := require.New(t)
|
||||
obj := TestObject{}
|
||||
obj.Field = MyString("hello")
|
||||
str, err := MarshalToString(obj)
|
||||
should.Nil(err)
|
||||
should.Equal(`{"Field":"hello"}`, str)
|
||||
}
|
||||
|
||||
func Test_nil_non_empty_interface(t *testing.T) {
|
||||
ConfigDefault.cleanEncoders()
|
||||
ConfigDefault.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))
|
||||
}
|
||||
|
||||
func Test_read_large_number_as_interface(t *testing.T) {
|
||||
should := require.New(t)
|
||||
var val interface{}
|
||||
err := Config{UseNumber: true}.Froze().UnmarshalFromString(`123456789123456789123456789`, &val)
|
||||
should.Nil(err)
|
||||
output, err := MarshalToString(val)
|
||||
should.Nil(err)
|
||||
should.Equal(`123456789123456789123456789`, output)
|
||||
}
|
||||
|
||||
func Test_nested_one_field_struct(t *testing.T) {
|
||||
should := require.New(t)
|
||||
type YetYetAnotherObject struct {
|
||||
Field string
|
||||
}
|
||||
type YetAnotherObject struct {
|
||||
Field *YetYetAnotherObject
|
||||
}
|
||||
type AnotherObject struct {
|
||||
Field *YetAnotherObject
|
||||
}
|
||||
type TestObject struct {
|
||||
Me *AnotherObject
|
||||
}
|
||||
obj := TestObject{&AnotherObject{&YetAnotherObject{&YetYetAnotherObject{"abc"}}}}
|
||||
str, err := MarshalToString(obj)
|
||||
should.Nil(err)
|
||||
should.Equal(`{"Me":{"Field":{"Field":{"Field":"abc"}}}}`, str)
|
||||
str, err = MarshalToString(&obj)
|
||||
should.Nil(err)
|
||||
should.Equal(`{"Me":{"Field":{"Field":{"Field":"abc"}}}}`, str)
|
||||
}
|
||||
|
||||
func Test_struct_with_embedded_ptr_with_tag(t *testing.T) {
|
||||
type O1 struct {
|
||||
O1F string
|
||||
}
|
||||
|
||||
type Option struct {
|
||||
O1 *O1
|
||||
}
|
||||
|
||||
type T struct {
|
||||
Option `json:","`
|
||||
}
|
||||
var obj T
|
||||
should := require.New(t)
|
||||
output, err := MarshalToString(obj)
|
||||
should.Nil(err)
|
||||
should.Equal(`{"O1":null}`, output)
|
||||
}
|
||||
|
||||
func Test_struct_with_one_nil(t *testing.T) {
|
||||
type TestObject struct {
|
||||
F *float64
|
||||
}
|
||||
var obj TestObject
|
||||
should := require.New(t)
|
||||
output, err := MarshalToString(obj)
|
||||
should.Nil(err)
|
||||
should.Equal(`{"F":null}`, output)
|
||||
}
|
||||
|
||||
func Test_struct_with_one_nil_embedded(t *testing.T) {
|
||||
type Parent struct {
|
||||
Field1 string
|
||||
Field2 string
|
||||
}
|
||||
type TestObject struct {
|
||||
*Parent
|
||||
}
|
||||
obj := TestObject{}
|
||||
should := require.New(t)
|
||||
bytes, err := json.Marshal(obj)
|
||||
should.Nil(err)
|
||||
should.Equal("{}", string(bytes))
|
||||
output, err := MarshalToString(obj)
|
||||
should.Nil(err)
|
||||
should.Equal(`{}`, output)
|
||||
}
|
||||
|
||||
func Test_struct_with_not_nil_embedded(t *testing.T) {
|
||||
type Parent struct {
|
||||
Field0 string
|
||||
Field1 []string
|
||||
Field2 map[string]interface{}
|
||||
}
|
||||
type TestObject struct {
|
||||
*Parent
|
||||
}
|
||||
should := require.New(t)
|
||||
var obj TestObject
|
||||
err := UnmarshalFromString(`{"Field0":"1","Field1":null,"Field2":{"K":"V"}}`, &obj)
|
||||
should.Nil(err)
|
||||
should.Nil(obj.Field1)
|
||||
should.Equal(map[string]interface{}{"K": "V"}, obj.Field2)
|
||||
should.Equal("1", obj.Field0)
|
||||
}
|
||||
|
||||
func Test_array_with_one_nil_ptr(t *testing.T) {
|
||||
obj := [1]*float64{nil}
|
||||
should := require.New(t)
|
||||
output, err := MarshalToString(obj)
|
||||
should.Nil(err)
|
||||
should.Equal(`[null]`, output)
|
||||
}
|
||||
|
||||
func Test_array_with_one_not_nil_ptr(t *testing.T) {
|
||||
two := float64(2)
|
||||
obj := [1]*float64{&two}
|
||||
should := require.New(t)
|
||||
output, err := MarshalToString(obj)
|
||||
should.Nil(err)
|
||||
should.Equal(`[2]`, output)
|
||||
}
|
||||
|
||||
func Test_embedded_array_with_one_nil(t *testing.T) {
|
||||
type TestObject struct {
|
||||
Field1 int
|
||||
Field2 [1]*float64
|
||||
}
|
||||
var obj TestObject
|
||||
should := require.New(t)
|
||||
output, err := MarshalToString(obj)
|
||||
should.Nil(err)
|
||||
should.Contains(output, `"Field2":[null]`)
|
||||
}
|
||||
|
||||
func Test_array_with_nothing(t *testing.T) {
|
||||
var obj [2]*float64
|
||||
should := require.New(t)
|
||||
output, err := MarshalToString(obj)
|
||||
should.Nil(err)
|
||||
should.Equal(`[null,null]`, output)
|
||||
}
|
@ -1,84 +0,0 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_read_by_one(t *testing.T) {
|
||||
iter := Parse(ConfigDefault, bytes.NewBufferString("abc"), 1)
|
||||
b := iter.readByte()
|
||||
if iter.Error != nil {
|
||||
t.Fatal(iter.Error)
|
||||
}
|
||||
if b != 'a' {
|
||||
t.Fatal(b)
|
||||
}
|
||||
iter.unreadByte()
|
||||
if iter.Error != nil {
|
||||
t.Fatal(iter.Error)
|
||||
}
|
||||
iter.unreadByte()
|
||||
if iter.Error == nil {
|
||||
t.FailNow()
|
||||
}
|
||||
iter.Error = nil
|
||||
b = iter.readByte()
|
||||
if iter.Error != nil {
|
||||
t.Fatal(iter.Error)
|
||||
}
|
||||
if b != 'a' {
|
||||
t.Fatal(b)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_read_by_two(t *testing.T) {
|
||||
iter := Parse(ConfigDefault, bytes.NewBufferString("abc"), 2)
|
||||
b := iter.readByte()
|
||||
if iter.Error != nil {
|
||||
t.Fatal(iter.Error)
|
||||
}
|
||||
if b != 'a' {
|
||||
t.Fatal(b)
|
||||
}
|
||||
b = iter.readByte()
|
||||
if iter.Error != nil {
|
||||
t.Fatal(iter.Error)
|
||||
}
|
||||
if b != 'b' {
|
||||
t.Fatal(b)
|
||||
}
|
||||
iter.unreadByte()
|
||||
if iter.Error != nil {
|
||||
t.Fatal(iter.Error)
|
||||
}
|
||||
iter.unreadByte()
|
||||
if iter.Error != nil {
|
||||
t.Fatal(iter.Error)
|
||||
}
|
||||
b = iter.readByte()
|
||||
if iter.Error != nil {
|
||||
t.Fatal(iter.Error)
|
||||
}
|
||||
if b != 'a' {
|
||||
t.Fatal(b)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_read_until_eof(t *testing.T) {
|
||||
iter := Parse(ConfigDefault, bytes.NewBufferString("abc"), 2)
|
||||
iter.readByte()
|
||||
iter.readByte()
|
||||
b := iter.readByte()
|
||||
if iter.Error != nil {
|
||||
t.Fatal(iter.Error)
|
||||
}
|
||||
if b != 'c' {
|
||||
t.Fatal(b)
|
||||
}
|
||||
iter.readByte()
|
||||
if iter.Error != io.EOF {
|
||||
t.Fatal(iter.Error)
|
||||
}
|
||||
}
|
@ -1,49 +0,0 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
//func Test_large_file(t *testing.T) {
|
||||
// file, err := os.Open("/tmp/large-file.json")
|
||||
// if err != nil {
|
||||
// t.Fatal(err)
|
||||
// }
|
||||
// iter := Parse(file, 4096)
|
||||
// count := 0
|
||||
// for iter.ReadArray() {
|
||||
// iter.Skip()
|
||||
// count++
|
||||
// }
|
||||
// if count != 11351 {
|
||||
// t.Fatal(count)
|
||||
// }
|
||||
//}
|
||||
|
||||
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(ConfigDefault, file, 4096)
|
||||
count := 0
|
||||
for iter.ReadArray() {
|
||||
iter.Skip()
|
||||
count++
|
||||
}
|
||||
file.Close()
|
||||
}
|
||||
}
|
||||
|
||||
func Benchmark_json_large_file(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
for n := 0; n < b.N; n++ {
|
||||
file, _ := os.Open("/tmp/large-file.json")
|
||||
bytes, _ := ioutil.ReadAll(file)
|
||||
file.Close()
|
||||
result := []struct{}{}
|
||||
json.Unmarshal(bytes, &result)
|
||||
}
|
||||
}
|
@ -1,122 +0,0 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/json-iterator/go/require"
|
||||
"math/big"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_read_map(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := ParseString(ConfigDefault, `{"hello": "world"}`)
|
||||
m := map[string]string{"1": "2"}
|
||||
iter.ReadVal(&m)
|
||||
copy(iter.buf, []byte{0, 0, 0, 0, 0, 0})
|
||||
should.Equal(map[string]string{"1": "2", "hello": "world"}, m)
|
||||
}
|
||||
|
||||
func Test_read_map_of_interface(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := ParseString(ConfigDefault, `{"hello": "world"}`)
|
||||
m := map[string]interface{}{"1": "2"}
|
||||
iter.ReadVal(&m)
|
||||
should.Equal(map[string]interface{}{"1": "2", "hello": "world"}, m)
|
||||
iter = ParseString(ConfigDefault, `{"hello": "world"}`)
|
||||
should.Equal(map[string]interface{}{"hello": "world"}, iter.Read())
|
||||
}
|
||||
|
||||
func Test_map_wrapper_any_get_all(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := Wrap(map[string][]int{"Field1": {1, 2}})
|
||||
should.Equal(`{"Field1":1}`, any.Get('*', 0).ToString())
|
||||
}
|
||||
|
||||
func Test_write_val_map(t *testing.T) {
|
||||
should := require.New(t)
|
||||
val := map[string]string{"1": "2"}
|
||||
str, err := MarshalToString(val)
|
||||
should.Nil(err)
|
||||
should.Equal(`{"1":"2"}`, str)
|
||||
}
|
||||
|
||||
func Test_slice_of_map(t *testing.T) {
|
||||
should := require.New(t)
|
||||
val := []map[string]string{{"1": "2"}}
|
||||
str, err := MarshalToString(val)
|
||||
should.Nil(err)
|
||||
should.Equal(`[{"1":"2"}]`, str)
|
||||
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)
|
||||
}
|
@ -1,130 +0,0 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"github.com/json-iterator/go/require"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_read_null(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := ParseString(ConfigDefault, `null`)
|
||||
should.True(iter.ReadNil())
|
||||
iter = ParseString(ConfigDefault, `null`)
|
||||
should.Nil(iter.Read())
|
||||
}
|
||||
|
||||
func Test_write_null(t *testing.T) {
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(ConfigDefault, buf, 4096)
|
||||
stream.WriteNil()
|
||||
stream.Flush()
|
||||
should.Nil(stream.Error)
|
||||
should.Equal("null", buf.String())
|
||||
}
|
||||
|
||||
func Test_encode_null(t *testing.T) {
|
||||
should := require.New(t)
|
||||
str, err := MarshalToString(nil)
|
||||
should.Nil(err)
|
||||
should.Equal("null", str)
|
||||
}
|
||||
|
||||
func Test_decode_null_object_field(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := ParseString(ConfigDefault, `[null,"a"]`)
|
||||
iter.ReadArray()
|
||||
if iter.ReadObject() != "" {
|
||||
t.FailNow()
|
||||
}
|
||||
iter.ReadArray()
|
||||
if iter.ReadString() != "a" {
|
||||
t.FailNow()
|
||||
}
|
||||
type TestObject struct {
|
||||
Field string
|
||||
}
|
||||
objs := []TestObject{}
|
||||
should.Nil(UnmarshalFromString("[null]", &objs))
|
||||
should.Len(objs, 1)
|
||||
}
|
||||
|
||||
func Test_decode_null_array_element(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := ParseString(ConfigDefault, `[null,"a"]`)
|
||||
should.True(iter.ReadArray())
|
||||
should.True(iter.ReadNil())
|
||||
should.True(iter.ReadArray())
|
||||
should.Equal("a", iter.ReadString())
|
||||
}
|
||||
|
||||
func Test_decode_null_array(t *testing.T) {
|
||||
should := require.New(t)
|
||||
arr := []string{}
|
||||
should.Nil(UnmarshalFromString("null", &arr))
|
||||
should.Nil(arr)
|
||||
}
|
||||
|
||||
func Test_decode_null_map(t *testing.T) {
|
||||
should := require.New(t)
|
||||
arr := map[string]string{}
|
||||
should.Nil(UnmarshalFromString("null", &arr))
|
||||
should.Nil(arr)
|
||||
}
|
||||
|
||||
func Test_decode_null_string(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := ParseString(ConfigDefault, `[null,"a"]`)
|
||||
should.True(iter.ReadArray())
|
||||
should.Equal("", iter.ReadString())
|
||||
should.True(iter.ReadArray())
|
||||
should.Equal("a", iter.ReadString())
|
||||
}
|
||||
|
||||
func Test_decode_null_skip(t *testing.T) {
|
||||
iter := ParseString(ConfigDefault, `[null,"a"]`)
|
||||
iter.ReadArray()
|
||||
iter.Skip()
|
||||
iter.ReadArray()
|
||||
if iter.ReadString() != "a" {
|
||||
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))
|
||||
}
|
@ -1,437 +0,0 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"github.com/json-iterator/go/require"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_empty_object(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := ParseString(ConfigDefault, `{}`)
|
||||
field := iter.ReadObject()
|
||||
should.Equal("", field)
|
||||
iter = ParseString(ConfigDefault, `{}`)
|
||||
iter.ReadObjectCB(func(iter *Iterator, field string) bool {
|
||||
should.FailNow("should not call")
|
||||
return true
|
||||
})
|
||||
}
|
||||
|
||||
func Test_one_field(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := ParseString(ConfigDefault, `{"a": "b"}`)
|
||||
field := iter.ReadObject()
|
||||
should.Equal("a", field)
|
||||
value := iter.ReadString()
|
||||
should.Equal("b", value)
|
||||
field = iter.ReadObject()
|
||||
should.Equal("", field)
|
||||
iter = ParseString(ConfigDefault, `{"a": "b"}`)
|
||||
should.True(iter.ReadObjectCB(func(iter *Iterator, field string) bool {
|
||||
should.Equal("a", field)
|
||||
return true
|
||||
}))
|
||||
}
|
||||
|
||||
func Test_two_field(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := ParseString(ConfigDefault, `{ "a": "b" , "c": "d" }`)
|
||||
field := iter.ReadObject()
|
||||
should.Equal("a", field)
|
||||
value := iter.ReadString()
|
||||
should.Equal("b", value)
|
||||
field = iter.ReadObject()
|
||||
should.Equal("c", field)
|
||||
value = iter.ReadString()
|
||||
should.Equal("d", value)
|
||||
field = iter.ReadObject()
|
||||
should.Equal("", field)
|
||||
iter = ParseString(ConfigDefault, `{"field1": "1", "field2": 2}`)
|
||||
for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
|
||||
switch field {
|
||||
case "field1":
|
||||
iter.ReadString()
|
||||
case "field2":
|
||||
iter.ReadInt64()
|
||||
default:
|
||||
iter.ReportError("bind object", "unexpected field")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func Test_object_wrapper_any_get_all(t *testing.T) {
|
||||
should := require.New(t)
|
||||
type TestObject struct {
|
||||
Field1 []int
|
||||
Field2 []int
|
||||
}
|
||||
any := Wrap(TestObject{[]int{1, 2}, []int{3, 4}})
|
||||
should.Contains(any.Get('*', 0).ToString(), `"Field2":3`)
|
||||
}
|
||||
|
||||
func Test_write_object(t *testing.T) {
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(Config{IndentionStep: 2}.Froze(), buf, 4096)
|
||||
stream.WriteObjectStart()
|
||||
stream.WriteObjectField("hello")
|
||||
stream.WriteInt(1)
|
||||
stream.WriteMore()
|
||||
stream.WriteObjectField("world")
|
||||
stream.WriteInt(2)
|
||||
stream.WriteObjectEnd()
|
||||
stream.Flush()
|
||||
should.Nil(stream.Error)
|
||||
should.Equal("{\n \"hello\": 1,\n \"world\": 2\n}", buf.String())
|
||||
}
|
||||
|
||||
func Test_decode_one_field_struct(t *testing.T) {
|
||||
should := require.New(t)
|
||||
type TestObject struct {
|
||||
Field1 string
|
||||
}
|
||||
obj := TestObject{}
|
||||
should.Nil(UnmarshalFromString(`{}`, &obj))
|
||||
should.Equal("", obj.Field1)
|
||||
should.Nil(UnmarshalFromString(`{"field1": "hello"}`, &obj))
|
||||
should.Equal("hello", obj.Field1)
|
||||
}
|
||||
|
||||
func Test_decode_two_fields_struct(t *testing.T) {
|
||||
should := require.New(t)
|
||||
type TestObject struct {
|
||||
Field1 string
|
||||
Field2 string
|
||||
}
|
||||
obj := TestObject{}
|
||||
should.Nil(UnmarshalFromString(`{}`, &obj))
|
||||
should.Equal("", obj.Field1)
|
||||
should.Nil(UnmarshalFromString(`{"Field1": "a", "Field2": "b"}`, &obj))
|
||||
should.Equal("a", obj.Field1)
|
||||
should.Equal("b", obj.Field2)
|
||||
}
|
||||
|
||||
func Test_decode_three_fields_struct(t *testing.T) {
|
||||
should := require.New(t)
|
||||
type TestObject struct {
|
||||
Field1 string
|
||||
Field2 string
|
||||
Field3 string
|
||||
}
|
||||
obj := TestObject{}
|
||||
should.Nil(UnmarshalFromString(`{}`, &obj))
|
||||
should.Equal("", obj.Field1)
|
||||
should.Nil(UnmarshalFromString(`{"Field1": "a", "Field2": "b", "Field3": "c"}`, &obj))
|
||||
should.Equal("a", obj.Field1)
|
||||
should.Equal("b", obj.Field2)
|
||||
should.Equal("c", obj.Field3)
|
||||
}
|
||||
|
||||
func Test_decode_four_fields_struct(t *testing.T) {
|
||||
should := require.New(t)
|
||||
type TestObject struct {
|
||||
Field1 string
|
||||
Field2 string
|
||||
Field3 string
|
||||
Field4 string
|
||||
}
|
||||
obj := TestObject{}
|
||||
should.Nil(UnmarshalFromString(`{}`, &obj))
|
||||
should.Equal("", obj.Field1)
|
||||
should.Nil(UnmarshalFromString(`{"Field1": "a", "Field2": "b", "Field3": "c", "Field4": "d"}`, &obj))
|
||||
should.Equal("a", obj.Field1)
|
||||
should.Equal("b", obj.Field2)
|
||||
should.Equal("c", obj.Field3)
|
||||
should.Equal("d", obj.Field4)
|
||||
}
|
||||
|
||||
func Test_decode_five_fields_struct(t *testing.T) {
|
||||
should := require.New(t)
|
||||
type TestObject struct {
|
||||
Field1 string
|
||||
Field2 string
|
||||
Field3 string
|
||||
Field4 string
|
||||
Field5 string
|
||||
}
|
||||
obj := TestObject{}
|
||||
should.Nil(UnmarshalFromString(`{}`, &obj))
|
||||
should.Equal("", obj.Field1)
|
||||
should.Nil(UnmarshalFromString(`{"Field1": "a", "Field2": "b", "Field3": "c", "Field4": "d", "Field5": "e"}`, &obj))
|
||||
should.Equal("a", obj.Field1)
|
||||
should.Equal("b", obj.Field2)
|
||||
should.Equal("c", obj.Field3)
|
||||
should.Equal("d", obj.Field4)
|
||||
should.Equal("e", obj.Field5)
|
||||
}
|
||||
|
||||
func Test_decode_ten_fields_struct(t *testing.T) {
|
||||
should := require.New(t)
|
||||
type TestObject struct {
|
||||
Field1 string
|
||||
Field2 string
|
||||
Field3 string
|
||||
Field4 string
|
||||
Field5 string
|
||||
Field6 string
|
||||
Field7 string
|
||||
Field8 string
|
||||
Field9 string
|
||||
Field10 string
|
||||
}
|
||||
obj := TestObject{}
|
||||
should.Nil(UnmarshalFromString(`{}`, &obj))
|
||||
should.Equal("", obj.Field1)
|
||||
should.Nil(UnmarshalFromString(`{"Field1": "a", "Field2": "b", "Field3": "c", "Field4": "d", "Field5": "e"}`, &obj))
|
||||
should.Equal("a", obj.Field1)
|
||||
should.Equal("b", obj.Field2)
|
||||
should.Equal("c", obj.Field3)
|
||||
should.Equal("d", obj.Field4)
|
||||
should.Equal("e", obj.Field5)
|
||||
}
|
||||
|
||||
func Test_decode_struct_field_with_tag(t *testing.T) {
|
||||
should := require.New(t)
|
||||
type TestObject struct {
|
||||
Field1 string `json:"field-1"`
|
||||
Field2 string `json:"-"`
|
||||
Field3 int `json:",string"`
|
||||
}
|
||||
obj := TestObject{Field2: "world"}
|
||||
UnmarshalFromString(`{"field-1": "hello", "field2": "", "Field3": "100"}`, &obj)
|
||||
should.Equal("hello", obj.Field1)
|
||||
should.Equal("world", obj.Field2)
|
||||
should.Equal(100, obj.Field3)
|
||||
}
|
||||
|
||||
func Test_decode_struct_field_with_tag_string(t *testing.T) {
|
||||
should := require.New(t)
|
||||
type TestObject struct {
|
||||
Field1 int `json:",string"`
|
||||
}
|
||||
obj := TestObject{Field1: 100}
|
||||
should.Nil(UnmarshalFromString(`{"Field1": "100"}`, &obj))
|
||||
should.Equal(100, obj.Field1)
|
||||
}
|
||||
|
||||
func Test_write_val_zero_field_struct(t *testing.T) {
|
||||
should := require.New(t)
|
||||
type TestObject struct {
|
||||
}
|
||||
obj := TestObject{}
|
||||
str, err := MarshalToString(obj)
|
||||
should.Nil(err)
|
||||
should.Equal(`{}`, str)
|
||||
}
|
||||
|
||||
func Test_write_val_one_field_struct(t *testing.T) {
|
||||
should := require.New(t)
|
||||
type TestObject struct {
|
||||
Field1 string `json:"field-1"`
|
||||
}
|
||||
obj := TestObject{"hello"}
|
||||
str, err := MarshalToString(obj)
|
||||
should.Nil(err)
|
||||
should.Equal(`{"field-1":"hello"}`, str)
|
||||
}
|
||||
|
||||
func Test_mixed(t *testing.T) {
|
||||
should := require.New(t)
|
||||
type AA struct {
|
||||
ID int `json:"id"`
|
||||
Payload map[string]interface{} `json:"payload"`
|
||||
buf *bytes.Buffer `json:"-"`
|
||||
}
|
||||
aa := AA{}
|
||||
err := UnmarshalFromString(` {"id":1, "payload":{"account":"123","password":"456"}}`, &aa)
|
||||
should.Nil(err)
|
||||
should.Equal(1, aa.ID)
|
||||
should.Equal("123", aa.Payload["account"])
|
||||
}
|
||||
|
||||
func Test_omit_empty(t *testing.T) {
|
||||
should := require.New(t)
|
||||
type TestObject struct {
|
||||
Field1 string `json:"field-1,omitempty"`
|
||||
Field2 string `json:"field-2,omitempty"`
|
||||
Field3 string `json:"field-3,omitempty"`
|
||||
}
|
||||
obj := TestObject{}
|
||||
obj.Field2 = "hello"
|
||||
str, err := MarshalToString(&obj)
|
||||
should.Nil(err)
|
||||
should.Equal(`{"field-2":"hello"}`, str)
|
||||
}
|
||||
|
||||
func Test_recursive_struct(t *testing.T) {
|
||||
should := require.New(t)
|
||||
type TestObject struct {
|
||||
Field1 string
|
||||
Me *TestObject
|
||||
}
|
||||
obj := TestObject{}
|
||||
str, err := MarshalToString(obj)
|
||||
should.Nil(err)
|
||||
should.Contains(str, `"Field1":""`)
|
||||
should.Contains(str, `"Me":null`)
|
||||
err = UnmarshalFromString(str, &obj)
|
||||
should.Nil(err)
|
||||
}
|
||||
|
||||
func Test_encode_anonymous_struct(t *testing.T) {
|
||||
should := require.New(t)
|
||||
type TestObject struct {
|
||||
Field string
|
||||
}
|
||||
str, err := MarshalToString(struct {
|
||||
TestObject
|
||||
Field int
|
||||
}{
|
||||
Field: 100,
|
||||
})
|
||||
should.Nil(err)
|
||||
should.Equal(`{"Field":100}`, str)
|
||||
}
|
||||
|
||||
func Test_decode_anonymous_struct(t *testing.T) {
|
||||
should := require.New(t)
|
||||
type Inner struct {
|
||||
Key string `json:"key"`
|
||||
}
|
||||
|
||||
type Outer struct {
|
||||
Inner
|
||||
}
|
||||
var outer Outer
|
||||
j := []byte("{\"key\":\"value\"}")
|
||||
should.Nil(Unmarshal(j, &outer))
|
||||
should.Equal("value", outer.Key)
|
||||
}
|
||||
|
||||
func Test_multiple_level_anonymous_struct(t *testing.T) {
|
||||
type Level1 struct {
|
||||
Field1 string
|
||||
}
|
||||
type Level2 struct {
|
||||
Level1
|
||||
Field2 string
|
||||
}
|
||||
type Level3 struct {
|
||||
Level2
|
||||
Field3 string
|
||||
}
|
||||
should := require.New(t)
|
||||
obj := Level3{Level2{Level1{"1"}, "2"}, "3"}
|
||||
output, err := MarshalToString(obj)
|
||||
should.Nil(err)
|
||||
should.Equal(`{"Field1":"1","Field2":"2","Field3":"3"}`, output)
|
||||
}
|
||||
|
||||
func Test_multiple_level_anonymous_struct_with_ptr(t *testing.T) {
|
||||
type Level1 struct {
|
||||
Field1 string
|
||||
Field2 string
|
||||
Field4 string
|
||||
}
|
||||
type Level2 struct {
|
||||
*Level1
|
||||
Field2 string
|
||||
Field3 string
|
||||
}
|
||||
type Level3 struct {
|
||||
*Level2
|
||||
Field3 string
|
||||
}
|
||||
should := require.New(t)
|
||||
obj := Level3{&Level2{&Level1{"1", "", "4"}, "2", ""}, "3"}
|
||||
output, err := MarshalToString(obj)
|
||||
should.Nil(err)
|
||||
should.Contains(output, `"Field1":"1"`)
|
||||
should.Contains(output, `"Field2":"2"`)
|
||||
should.Contains(output, `"Field3":"3"`)
|
||||
should.Contains(output, `"Field4":"4"`)
|
||||
}
|
||||
|
||||
func Test_shadow_struct_field(t *testing.T) {
|
||||
should := require.New(t)
|
||||
type omit *struct{}
|
||||
type CacheItem struct {
|
||||
Key string `json:"key"`
|
||||
MaxAge int `json:"cacheAge"`
|
||||
}
|
||||
output, err := MarshalToString(struct {
|
||||
*CacheItem
|
||||
|
||||
// Omit bad keys
|
||||
OmitMaxAge omit `json:"cacheAge,omitempty"`
|
||||
|
||||
// Add nice keys
|
||||
MaxAge int `json:"max_age"`
|
||||
}{
|
||||
CacheItem: &CacheItem{
|
||||
Key: "value",
|
||||
MaxAge: 100,
|
||||
},
|
||||
MaxAge: 20,
|
||||
})
|
||||
should.Nil(err)
|
||||
should.Contains(output, `"key":"value"`)
|
||||
should.Contains(output, `"max_age":20`)
|
||||
}
|
||||
|
||||
func Test_embeded_order(t *testing.T) {
|
||||
type A struct {
|
||||
Field2 string
|
||||
}
|
||||
|
||||
type C struct {
|
||||
Field5 string
|
||||
}
|
||||
|
||||
type B struct {
|
||||
Field4 string
|
||||
C
|
||||
Field6 string
|
||||
}
|
||||
|
||||
type TestObject struct {
|
||||
Field1 string
|
||||
A
|
||||
Field3 string
|
||||
B
|
||||
Field7 string
|
||||
}
|
||||
should := require.New(t)
|
||||
s := TestObject{}
|
||||
output, err := MarshalToString(s)
|
||||
should.Nil(err)
|
||||
should.Equal(`{"Field1":"","Field2":"","Field3":"","Field4":"","Field5":"","Field6":"","Field7":""}`, output)
|
||||
}
|
||||
|
||||
func Test_decode_nested(t *testing.T) {
|
||||
type StructOfString struct {
|
||||
Field1 string
|
||||
Field2 string
|
||||
}
|
||||
iter := ParseString(ConfigDefault, `[{"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" {
|
||||
fmt.Println(iter.Error)
|
||||
t.Fatal(slice[0])
|
||||
}
|
||||
if slice[1] != nil {
|
||||
fmt.Println(iter.Error)
|
||||
t.Fatal(slice[1])
|
||||
}
|
||||
if slice[2].Field2 != "world" {
|
||||
fmt.Println(iter.Error)
|
||||
t.Fatal(slice[2])
|
||||
}
|
||||
}
|
@ -1,46 +0,0 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"github.com/json-iterator/go/require"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_encode_optional_int_pointer(t *testing.T) {
|
||||
should := require.New(t)
|
||||
var ptr *int
|
||||
str, err := MarshalToString(ptr)
|
||||
should.Nil(err)
|
||||
should.Equal("null", str)
|
||||
val := 100
|
||||
ptr = &val
|
||||
str, err = MarshalToString(ptr)
|
||||
should.Nil(err)
|
||||
should.Equal("100", str)
|
||||
}
|
||||
|
||||
func Test_decode_struct_with_optional_field(t *testing.T) {
|
||||
should := require.New(t)
|
||||
type TestObject struct {
|
||||
Field1 *string
|
||||
Field2 *string
|
||||
}
|
||||
obj := TestObject{}
|
||||
UnmarshalFromString(`{"field1": null, "field2": "world"}`, &obj)
|
||||
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
|
||||
}
|
||||
obj := TestObject{}
|
||||
world := "world"
|
||||
obj.Field2 = &world
|
||||
str, err := MarshalToString(obj)
|
||||
should.Nil(err)
|
||||
should.Contains(str, `"Field1":null`)
|
||||
should.Contains(str, `"Field2":"world"`)
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user