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
261 Commits
jsoniter-g
...
jsoniter-g
Author | SHA1 | Date | |
---|---|---|---|
8700644196 | |||
6a7bf91c45 | |||
e417330822 | |||
af876518e1 | |||
21b9254da4 | |||
c009421781 | |||
c4f54740f7 | |||
368bd0c1d8 | |||
11975d2a26 | |||
e7404f3065 | |||
f60a6a17c2 | |||
2dfdcdd9db | |||
496a8ea225 | |||
ed6c434851 | |||
bf002a02be | |||
815aa331a8 | |||
54ab168362 | |||
87d1ea0c60 | |||
6268a1cbc8 | |||
d56566286c | |||
f2c50ef73b | |||
d0c5988985 | |||
17eed15fb5 | |||
e260979001 | |||
3cf9b7c253 | |||
800df52ccd | |||
e3ba0e7b7e | |||
e366c72b81 | |||
69551ef38f | |||
abcf9a8d76 | |||
fa0965a968 | |||
04eae11ba5 | |||
ea8fa7cc63 | |||
6540266aaf | |||
ddfbb0c62e | |||
a3a2d1cd25 | |||
401a56bc20 | |||
cfaa11f837 | |||
c1411e0ad5 | |||
9ec64591b6 | |||
b07d1abc4f | |||
79c4040505 | |||
7fa780bd5d | |||
7244d730b9 | |||
12be6e0d43 | |||
3cfe590a13 | |||
678c297af3 | |||
1253b8edd3 | |||
fb382c0ec1 | |||
09cb1d9236 | |||
e6c24947ee | |||
a6673c983a | |||
ec7b40d104 | |||
84fa033353 | |||
4e608af2c7 | |||
3458ccdb20 | |||
45ccfb031f | |||
545a32f2a1 | |||
08dbc98040 | |||
82dabdcdbf | |||
76e62088df | |||
faaa59222a | |||
91ef89a6a2 | |||
3e3caf9184 | |||
03a2daaeee | |||
4652ac6cc2 | |||
8a9f2b9179 | |||
0db2d74de8 | |||
d6ef711c18 | |||
628fedf63c | |||
5bb7a1f7af | |||
c2c9981062 | |||
e40d614037 | |||
1589ab2fd7 | |||
97ee4ad4a2 | |||
f09f778ca9 | |||
9fc858b117 | |||
f93d25f8b1 | |||
7cd7a6cc7c | |||
7d6c9374e8 | |||
e16ee7f8ac | |||
cf6367546b | |||
dc44e85a86 | |||
85f7a1b0b3 | |||
507a446eda | |||
76eefc25ba | |||
21a16bd252 | |||
0c0c9f119f | |||
cf77980493 | |||
7d681fe2c2 | |||
0c07128d3c | |||
f771d32291 | |||
d100b0d41f | |||
81e64121ba | |||
dcc91365ee | |||
8f3de9c412 | |||
5d3508979f | |||
8f8e16b4c2 | |||
d7ea1acd3f | |||
ebed7df895 | |||
caaa04195e | |||
ff3c624fa9 | |||
3333ec11a0 | |||
29a928e1d2 | |||
83fa27ff9a | |||
cefb2972fd | |||
486534c67c | |||
ed79b1726e | |||
85be06b145 | |||
086001225d | |||
2ea4d48e1f | |||
417011b497 | |||
ae6ce2fc3f | |||
8ef0c22f25 | |||
a5ae3a2649 | |||
306b2896cf | |||
818ae1331a | |||
8f6a840c63 | |||
be221df432 | |||
499412ec4c | |||
c36a7ed7cd | |||
14588726a1 | |||
aa01f57b7f | |||
b3170a8cef | |||
43a832beee | |||
39c9bb226a | |||
945fe53724 | |||
8367a97ad8 | |||
365d399192 | |||
839247df05 | |||
f5edf564c8 | |||
c3f5a2c536 | |||
c6a598e292 | |||
eecb062c32 | |||
50583f6bae | |||
3b883aeffc | |||
baca358b53 | |||
514db10f97 | |||
31afe6450e | |||
7e9017caa2 | |||
b6dfbbd6bc | |||
3ffa5af7ec | |||
02cf6a73cc | |||
15c92d48df | |||
a84cdaa694 | |||
9f9ca4c9fc | |||
985e263300 | |||
1ec246d16b | |||
54dbcda64d | |||
7a049ec79c | |||
77dcffe77d | |||
8ab46965bd | |||
2503ef17eb | |||
0195110b5b | |||
55fc498d27 | |||
50e4910c63 | |||
f29fe7407e | |||
3c8bd9ef54 | |||
952a42af6c | |||
17bd91fd71 | |||
3d5f6d3a4a | |||
b31b1301e2 | |||
69bc64b6d8 | |||
e0e2423e9a | |||
a6ea770365 | |||
5f22e50c89 | |||
d867c8ba5c | |||
d0418857ce | |||
48e9f6ec84 | |||
acddcf5bbf | |||
788918b85d | |||
6e5817b773 | |||
7480e41836 | |||
9215b3c508 | |||
64e500f3c8 | |||
3307ce3ba2 | |||
6f50f15678 | |||
cee09816e3 | |||
cdbad22d22 | |||
b0c9f047e2 | |||
6bd13c2948 | |||
84ad508437 | |||
4f909776cf | |||
962c470806 | |||
46d443fbad | |||
2608d40f2a | |||
3cf822853f | |||
26708bccc9 | |||
d75b539bad | |||
cfffa29c8a | |||
925df245d3 | |||
962a8cd303 | |||
6509ba05df | |||
579dbf3c1d | |||
aa5181db67 | |||
67be6df2b1 | |||
0f5379494a | |||
d09e2419ba | |||
e1a71f6ba1 | |||
dcb78991c4 | |||
9e8238cdc6 | |||
a4e5abf492 | |||
3979955e69 | |||
5fd09f0e02 | |||
af4982b22c | |||
29dc1d407d | |||
5b27aaa62c | |||
106636a191 | |||
f50c4cfbbe | |||
87149ae489 | |||
c0a4ad72e1 | |||
404c0ee44b | |||
10c1506f87 | |||
9a43fe6468 | |||
95e03f2937 | |||
4406ed9e62 | |||
ff027701f5 | |||
c69b61f879 | |||
d97f5db769 | |||
45bbb40a9f | |||
e36f926072 | |||
59e71bacc8 | |||
5cb0d35610 | |||
69b742e73a | |||
a7f992f0e1 | |||
4cc44e7380 | |||
5310d4aa9a | |||
2051e3b8ae | |||
fe9fa8900e | |||
ad3a7fde32 | |||
377b892102 | |||
707ed3b091 | |||
a7a7c7879a | |||
f20f74519d | |||
7d2ae80c37 | |||
f6f159e108 | |||
e5a1e704ad | |||
7d5f90261e | |||
6126a6d3ca | |||
5fbe4e387d | |||
fc44cb2d91 | |||
7e046e6aa7 | |||
5488fde97f | |||
53f8d370b5 | |||
3f1fcaff87 | |||
1df353727b | |||
b893a0359d | |||
a92111261c | |||
91b9e828b7 | |||
6bd835aeb1 | |||
90888390bc | |||
ccb972f58c | |||
8711c74c85 | |||
abcf2759ed | |||
e5476f70e7 | |||
b986d86f26 | |||
9a138f8b6a | |||
d1aa59e34e | |||
ceb8c8a733 | |||
62028f1ede | |||
696f962eda |
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
.idea
|
||||||
|
/coverage.txt
|
||||||
|
/profile.out
|
13
.travis.yml
Normal file
13
.travis.yml
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
language: go
|
||||||
|
|
||||||
|
go:
|
||||||
|
- 1.8.x
|
||||||
|
|
||||||
|
before_install:
|
||||||
|
- go get -t -v ./...
|
||||||
|
|
||||||
|
script:
|
||||||
|
- ./test.sh
|
||||||
|
|
||||||
|
after_success:
|
||||||
|
- bash <(curl -s https://codecov.io/bash)
|
79
README.md
79
README.md
@ -1,61 +1,63 @@
|
|||||||
|
[](https://sourcegraph.com/github.com/json-iterator/go?badge)
|
||||||
|
[](http://godoc.org/github.com/json-iterator/go)
|
||||||
|
[](https://travis-ci.org/json-iterator/go)
|
||||||
|
[](https://codecov.io/gh/json-iterator/go)
|
||||||
[](https://goreportcard.com/report/github.com/json-iterator/go)
|
[](https://goreportcard.com/report/github.com/json-iterator/go)
|
||||||
|
[](https://raw.githubusercontent.com/json-iterator/go/master/LICENSE)
|
||||||
|
[](https://gitter.im/json-iterator/Lobby)
|
||||||
|
|
||||||
jsoniter (json-iterator) is fast and flexible JSON parser available in [Java](https://github.com/json-iterator/java) and [Go](https://github.com/json-iterator/go)
|
A high-performance 100% compatible drop-in replacement of "encoding/json"
|
||||||
|
|
||||||
# Why jsoniter?
|
# Benchmark
|
||||||
|
|
||||||
* Jsoniter is the fastest JSON parser. It could be up to 10x faster than normal parser, data binding included. Shameless self [benchmark](http://jsoniter.com/benchmark.html)
|

|
||||||
* Extremely flexible api. You can mix and match three different styles: bind-api, any-api or iterator-api. Checkout your [api choices](http://jsoniter.com/api.html)
|
|
||||||
* Unique iterator api can iterate through JSON directly, zero memory allocation! See how [iterator](http://jsoniter.com/api.html#iterator-api) works
|
|
||||||
|
|
||||||
# Show off
|
Source code: https://github.com/json-iterator/go-benchmark/blob/master/src/github.com/json-iterator/go-benchmark/benchmark_medium_payload_test.go
|
||||||
|
|
||||||
Here is a quick show off, for more complete report you can checkout the full [benchmark](http://jsoniter.com/benchmark.html) with [in-depth optimization](http://jsoniter.com/benchmark.html#optimization-used) to back the numbers up
|
Raw Result (easyjson requires static code generation)
|
||||||
|
|
||||||

|
| | ns/op | allocation bytes | allocation times |
|
||||||
|
| --- | --- | --- | --- |
|
||||||
|
| std decode | 35510 ns/op | 1960 B/op | 99 allocs/op |
|
||||||
|
| easyjson decode | 8499 ns/op | 160 B/op | 4 allocs/op |
|
||||||
|
| jsoniter decode | 5623 ns/op | 160 B/op | 3 allocs/op |
|
||||||
|
| std encode | 2213 ns/op | 712 B/op | 5 allocs/op |
|
||||||
|
| easyjson encode | 883 ns/op | 576 B/op | 3 allocs/op |
|
||||||
|
| jsoniter encode | 837 ns/op | 384 B/op | 4 allocs/op |
|
||||||
|
|
||||||
# Bind-API is the best
|
# Usage
|
||||||
|
|
||||||
Bind-api should always be the first choice. Given this JSON document `[0,1,2,3]`
|
100% compatibility with standard lib
|
||||||
|
|
||||||
Parse with Go bind-api
|
Replace
|
||||||
|
|
||||||
|
```go
|
||||||
|
import "encoding/json"
|
||||||
|
json.Marshal(&data)
|
||||||
|
```
|
||||||
|
|
||||||
|
with
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/json-iterator/go"
|
import "github.com/json-iterator/go"
|
||||||
iter := jsoniter.ParseString(`[0,1,2,3]`)
|
jsoniter.Marshal(&data)
|
||||||
var := iter.Read()
|
|
||||||
fmt.Println(val)
|
|
||||||
```
|
```
|
||||||
|
|
||||||
# Iterator-API for quick extraction
|
Replace
|
||||||
|
|
||||||
When you do not need to get all the data back, just extract some.
|
```go
|
||||||
|
import "encoding/json"
|
||||||
|
json.Unmarshal(input, &data)
|
||||||
|
```
|
||||||
|
|
||||||
Parse with Go iterator-api
|
with
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/json-iterator/go"
|
import "github.com/json-iterator/go"
|
||||||
iter := ParseString(`[0, [1, 2], [3, 4], 5]`)
|
jsoniter.Unmarshal(input, &data)
|
||||||
count := 0
|
|
||||||
for iter.ReadArray() {
|
|
||||||
iter.Skip()
|
|
||||||
count++
|
|
||||||
}
|
|
||||||
fmt.Println(count) // 4
|
|
||||||
```
|
```
|
||||||
|
|
||||||
# Any-API for maximum flexibility
|
[More documentation](http://jsoniter.com/migrate-from-go-std.html)
|
||||||
|
|
||||||
Parse with Go any-api
|
|
||||||
|
|
||||||
```go
|
|
||||||
import "github.com/json-iterator/go"
|
|
||||||
iter := jsoniter.ParseString(`[{"field1":"11","field2":"12"},{"field1":"21","field2":"22"}]`)
|
|
||||||
val := iter.ReadAny()
|
|
||||||
fmt.Println(val.ToInt(1, "field2")) // 22
|
|
||||||
```
|
|
||||||
|
|
||||||
Notice you can extract from nested data structure, and convert any type to the type to you want.
|
|
||||||
|
|
||||||
# How to get
|
# How to get
|
||||||
|
|
||||||
@ -65,4 +67,9 @@ go get github.com/json-iterator/go
|
|||||||
|
|
||||||
# Contribution Welcomed !
|
# Contribution Welcomed !
|
||||||
|
|
||||||
|
Contributors
|
||||||
|
|
||||||
|
* [thockin](https://github.com/thockin)
|
||||||
|
* [cch123](https://github.com/cch123)
|
||||||
|
|
||||||
Report issue or pull request, or email taowen@gmail.com, or [](https://gitter.im/json-iterator/Lobby)
|
Report issue or pull request, or email taowen@gmail.com, or [](https://gitter.im/json-iterator/Lobby)
|
||||||
|
97
example_test.go
Normal file
97
example_test.go
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
package jsoniter_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/json-iterator/go"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ExampleMarshal() {
|
||||||
|
type ColorGroup struct {
|
||||||
|
ID int
|
||||||
|
Name string
|
||||||
|
Colors []string
|
||||||
|
}
|
||||||
|
group := ColorGroup{
|
||||||
|
ID: 1,
|
||||||
|
Name: "Reds",
|
||||||
|
Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
|
||||||
|
}
|
||||||
|
b, err := jsoniter.Marshal(group)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("error:", err)
|
||||||
|
}
|
||||||
|
os.Stdout.Write(b)
|
||||||
|
// Output:
|
||||||
|
// {"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleUnmarshal() {
|
||||||
|
var jsonBlob = []byte(`[
|
||||||
|
{"Name": "Platypus", "Order": "Monotremata"},
|
||||||
|
{"Name": "Quoll", "Order": "Dasyuromorphia"}
|
||||||
|
]`)
|
||||||
|
type Animal struct {
|
||||||
|
Name string
|
||||||
|
Order string
|
||||||
|
}
|
||||||
|
var animals []Animal
|
||||||
|
err := jsoniter.Unmarshal(jsonBlob, &animals)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("error:", err)
|
||||||
|
}
|
||||||
|
fmt.Printf("%+v", animals)
|
||||||
|
// Output:
|
||||||
|
// [{Name:Platypus Order:Monotremata} {Name:Quoll Order:Dasyuromorphia}]
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleMarshalWithBestPerformance() {
|
||||||
|
type ColorGroup struct {
|
||||||
|
ID int
|
||||||
|
Name string
|
||||||
|
Colors []string
|
||||||
|
}
|
||||||
|
group := ColorGroup{
|
||||||
|
ID: 1,
|
||||||
|
Name: "Reds",
|
||||||
|
Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
|
||||||
|
}
|
||||||
|
stream := jsoniter.ConfigFastest.BorrowStream(nil)
|
||||||
|
defer jsoniter.ConfigFastest.ReturnStream(stream)
|
||||||
|
stream.WriteVal(group)
|
||||||
|
if stream.Error != nil {
|
||||||
|
fmt.Println("error:", stream.Error)
|
||||||
|
}
|
||||||
|
os.Stdout.Write(stream.Buffer())
|
||||||
|
// Output:
|
||||||
|
// {"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleUnmarshalWithBestPerformance() {
|
||||||
|
var jsonBlob = []byte(`[
|
||||||
|
{"Name": "Platypus", "Order": "Monotremata"},
|
||||||
|
{"Name": "Quoll", "Order": "Dasyuromorphia"}
|
||||||
|
]`)
|
||||||
|
type Animal struct {
|
||||||
|
Name string
|
||||||
|
Order string
|
||||||
|
}
|
||||||
|
var animals []Animal
|
||||||
|
iter := jsoniter.ConfigFastest.BorrowIterator(jsonBlob)
|
||||||
|
defer jsoniter.ConfigFastest.ReturnIterator(iter)
|
||||||
|
iter.ReadVal(&animals)
|
||||||
|
if iter.Error != nil {
|
||||||
|
fmt.Println("error:", iter.Error)
|
||||||
|
}
|
||||||
|
fmt.Printf("%+v", animals)
|
||||||
|
// Output:
|
||||||
|
// [{Name:Platypus Order:Monotremata} {Name:Quoll Order:Dasyuromorphia}]
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleOneLine() {
|
||||||
|
val := []byte(`{"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]}`)
|
||||||
|
fmt.Printf(jsoniter.Get(val, "Colors", 0).ToString())
|
||||||
|
// Output:
|
||||||
|
// Crimson
|
||||||
|
}
|
254
extra/fuzzy_decoder.go
Normal file
254
extra/fuzzy_decoder.go
Normal file
@ -0,0 +1,254 @@
|
|||||||
|
package extra
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"github.com/json-iterator/go"
|
||||||
|
"math"
|
||||||
|
"reflect"
|
||||||
|
"strings"
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
const MaxUint = ^uint(0)
|
||||||
|
const MaxInt = int(MaxUint >> 1)
|
||||||
|
const MinInt = -MaxInt - 1
|
||||||
|
|
||||||
|
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) {
|
||||||
|
if isFloat {
|
||||||
|
val := iter.ReadFloat64()
|
||||||
|
if val > float64(MaxInt) || val < float64(MinInt) {
|
||||||
|
iter.ReportError("fuzzy decode int", "exceed range")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
*((*int)(ptr)) = int(val)
|
||||||
|
} else {
|
||||||
|
*((*int)(ptr)) = iter.ReadInt()
|
||||||
|
}
|
||||||
|
}})
|
||||||
|
jsoniter.RegisterTypeDecoder("uint", &FuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||||
|
if isFloat {
|
||||||
|
val := iter.ReadFloat64()
|
||||||
|
if val > float64(MaxUint) || val < 0 {
|
||||||
|
iter.ReportError("fuzzy decode uint", "exceed range")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
*((*uint)(ptr)) = uint(val)
|
||||||
|
} else {
|
||||||
|
*((*uint)(ptr)) = iter.ReadUint()
|
||||||
|
}
|
||||||
|
}})
|
||||||
|
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) {
|
||||||
|
iter.ReportError("fuzzy decode int8", "exceed range")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
*((*int8)(ptr)) = int8(val)
|
||||||
|
} else {
|
||||||
|
*((*int8)(ptr)) = iter.ReadInt8()
|
||||||
|
}
|
||||||
|
}})
|
||||||
|
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 {
|
||||||
|
iter.ReportError("fuzzy decode uint8", "exceed range")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
*((*uint8)(ptr)) = uint8(val)
|
||||||
|
} else {
|
||||||
|
*((*uint8)(ptr)) = iter.ReadUint8()
|
||||||
|
}
|
||||||
|
}})
|
||||||
|
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) {
|
||||||
|
iter.ReportError("fuzzy decode int16", "exceed range")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
*((*int16)(ptr)) = int16(val)
|
||||||
|
} else {
|
||||||
|
*((*int16)(ptr)) = iter.ReadInt16()
|
||||||
|
}
|
||||||
|
}})
|
||||||
|
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 {
|
||||||
|
iter.ReportError("fuzzy decode uint16", "exceed range")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
*((*uint16)(ptr)) = uint16(val)
|
||||||
|
} else {
|
||||||
|
*((*uint16)(ptr)) = iter.ReadUint16()
|
||||||
|
}
|
||||||
|
}})
|
||||||
|
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) {
|
||||||
|
iter.ReportError("fuzzy decode int32", "exceed range")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
*((*int32)(ptr)) = int32(val)
|
||||||
|
} else {
|
||||||
|
*((*int32)(ptr)) = iter.ReadInt32()
|
||||||
|
}
|
||||||
|
}})
|
||||||
|
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 {
|
||||||
|
iter.ReportError("fuzzy decode uint32", "exceed range")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
*((*uint32)(ptr)) = uint32(val)
|
||||||
|
} else {
|
||||||
|
*((*uint32)(ptr)) = iter.ReadUint32()
|
||||||
|
}
|
||||||
|
}})
|
||||||
|
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) {
|
||||||
|
iter.ReportError("fuzzy decode int64", "exceed range")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
*((*int64)(ptr)) = int64(val)
|
||||||
|
} else {
|
||||||
|
*((*int64)(ptr)) = iter.ReadInt64()
|
||||||
|
}
|
||||||
|
}})
|
||||||
|
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 {
|
||||||
|
iter.ReportError("fuzzy decode uint64", "exceed range")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
*((*uint64)(ptr)) = uint64(val)
|
||||||
|
} else {
|
||||||
|
*((*uint64)(ptr)) = iter.ReadUint64()
|
||||||
|
}
|
||||||
|
}})
|
||||||
|
}
|
||||||
|
|
||||||
|
type tolerateEmptyArrayExtension struct {
|
||||||
|
jsoniter.DummyExtension
|
||||||
|
}
|
||||||
|
|
||||||
|
func (extension *tolerateEmptyArrayExtension) DecorateDecoder(typ reflect.Type, decoder jsoniter.ValDecoder) jsoniter.ValDecoder {
|
||||||
|
if typ.Kind() == reflect.Struct || typ.Kind() == reflect.Map {
|
||||||
|
return &tolerateEmptyArrayDecoder{decoder}
|
||||||
|
}
|
||||||
|
return decoder
|
||||||
|
}
|
||||||
|
|
||||||
|
type tolerateEmptyArrayDecoder struct {
|
||||||
|
valDecoder jsoniter.ValDecoder
|
||||||
|
}
|
||||||
|
|
||||||
|
func (decoder *tolerateEmptyArrayDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||||
|
if iter.WhatIsNext() == jsoniter.Array {
|
||||||
|
iter.Skip()
|
||||||
|
newIter := iter.Config().BorrowIterator([]byte("{}"))
|
||||||
|
defer iter.Config().ReturnIterator(newIter)
|
||||||
|
decoder.valDecoder.Decode(ptr, newIter)
|
||||||
|
} else {
|
||||||
|
decoder.valDecoder.Decode(ptr, iter)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type FuzzyStringDecoder struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (decoder *FuzzyStringDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||||
|
valueType := iter.WhatIsNext()
|
||||||
|
switch valueType {
|
||||||
|
case jsoniter.Number:
|
||||||
|
var number json.Number
|
||||||
|
iter.ReadVal(&number)
|
||||||
|
*((*string)(ptr)) = string(number)
|
||||||
|
case jsoniter.String:
|
||||||
|
*((*string)(ptr)) = iter.ReadString()
|
||||||
|
default:
|
||||||
|
iter.ReportError("FuzzyStringDecoder", "not number or string")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type FuzzyIntegerDecoder struct {
|
||||||
|
fun func(isFloat bool, 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:
|
||||||
|
var number json.Number
|
||||||
|
iter.ReadVal(&number)
|
||||||
|
str = string(number)
|
||||||
|
case jsoniter.String:
|
||||||
|
str = iter.ReadString()
|
||||||
|
default:
|
||||||
|
iter.ReportError("FuzzyIntegerDecoder", "not number or string")
|
||||||
|
}
|
||||||
|
newIter := iter.Config().BorrowIterator([]byte(str))
|
||||||
|
defer iter.Config().ReturnIterator(newIter)
|
||||||
|
isFloat := strings.IndexByte(str, '.') != -1
|
||||||
|
decoder.fun(isFloat, ptr, newIter)
|
||||||
|
if newIter.Error != nil {
|
||||||
|
iter.Error = newIter.Error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type FuzzyFloat32Decoder struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (decoder *FuzzyFloat32Decoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||||
|
valueType := iter.WhatIsNext()
|
||||||
|
var str string
|
||||||
|
switch valueType {
|
||||||
|
case jsoniter.Number:
|
||||||
|
*((*float32)(ptr)) = iter.ReadFloat32()
|
||||||
|
case jsoniter.String:
|
||||||
|
str = iter.ReadString()
|
||||||
|
newIter := iter.Config().BorrowIterator([]byte(str))
|
||||||
|
defer iter.Config().ReturnIterator(newIter)
|
||||||
|
*((*float32)(ptr)) = newIter.ReadFloat32()
|
||||||
|
if newIter.Error != nil {
|
||||||
|
iter.Error = newIter.Error
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
iter.ReportError("FuzzyFloat32Decoder", "not number or string")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type FuzzyFloat64Decoder struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (decoder *FuzzyFloat64Decoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||||
|
valueType := iter.WhatIsNext()
|
||||||
|
var str string
|
||||||
|
switch valueType {
|
||||||
|
case jsoniter.Number:
|
||||||
|
*((*float64)(ptr)) = iter.ReadFloat64()
|
||||||
|
case jsoniter.String:
|
||||||
|
str = iter.ReadString()
|
||||||
|
newIter := iter.Config().BorrowIterator([]byte(str))
|
||||||
|
defer iter.Config().ReturnIterator(newIter)
|
||||||
|
*((*float64)(ptr)) = newIter.ReadFloat64()
|
||||||
|
if newIter.Error != nil {
|
||||||
|
iter.Error = newIter.Error
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
iter.ReportError("FuzzyFloat32Decoder", "not number or string")
|
||||||
|
}
|
||||||
|
}
|
101
extra/fuzzy_decoder_test.go
Normal file
101
extra/fuzzy_decoder_test.go
Normal file
@ -0,0 +1,101 @@
|
|||||||
|
package extra
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/json-iterator/go"
|
||||||
|
"github.com/json-iterator/go/require"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
RegisterFuzzyDecoders()
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_string_to_string(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
var val string
|
||||||
|
should.Nil(jsoniter.UnmarshalFromString(`"100"`, &val))
|
||||||
|
should.Equal("100", val)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_int_to_string(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
var val string
|
||||||
|
should.Nil(jsoniter.UnmarshalFromString(`100`, &val))
|
||||||
|
should.Equal("100", val)
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
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.NotNil(jsoniter.UnmarshalFromString(`1234512345123451234512345.0`, &val))
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_string_to_float32(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
var val float32
|
||||||
|
should.Nil(jsoniter.UnmarshalFromString(`"100"`, &val))
|
||||||
|
should.Equal(float32(100), 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) {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_empty_array_as_map(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
var val map[string]interface{}
|
||||||
|
should.Nil(jsoniter.UnmarshalFromString(`[]`, &val))
|
||||||
|
should.Equal(map[string]interface{}{}, val)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_empty_array_as_object(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
var val struct{}
|
||||||
|
should.Nil(jsoniter.UnmarshalFromString(`[]`, &val))
|
||||||
|
should.Equal(struct{}{}, val)
|
||||||
|
}
|
39
extra/naming_strategy.go
Normal file
39
extra/naming_strategy.go
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
package extra
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/json-iterator/go"
|
||||||
|
"unicode"
|
||||||
|
)
|
||||||
|
|
||||||
|
func SetNamingStrategy(translate func(string) string) {
|
||||||
|
jsoniter.RegisterExtension(&namingStrategyExtension{jsoniter.DummyExtension{}, translate})
|
||||||
|
}
|
||||||
|
|
||||||
|
type namingStrategyExtension struct {
|
||||||
|
jsoniter.DummyExtension
|
||||||
|
translate func(string) string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (extension *namingStrategyExtension) UpdateStructDescriptor(structDescriptor *jsoniter.StructDescriptor) {
|
||||||
|
for _, binding := range structDescriptor.Fields {
|
||||||
|
binding.ToNames = []string{extension.translate(binding.Field.Name)}
|
||||||
|
binding.FromNames = []string{extension.translate(binding.Field.Name)}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func LowerCaseWithUnderscores(name string) string {
|
||||||
|
newName := []rune{}
|
||||||
|
for i, c := range name {
|
||||||
|
if i == 0 {
|
||||||
|
newName = append(newName, unicode.ToLower(c))
|
||||||
|
} else {
|
||||||
|
if unicode.IsUpper(c) {
|
||||||
|
newName = append(newName, '_')
|
||||||
|
newName = append(newName, unicode.ToLower(c))
|
||||||
|
} else {
|
||||||
|
newName = append(newName, c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return string(newName)
|
||||||
|
}
|
23
extra/naming_strategy_test.go
Normal file
23
extra/naming_strategy_test.go
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
package extra
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/json-iterator/go"
|
||||||
|
"github.com/json-iterator/go/require"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_lower_case_with_underscores(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
should.Equal("hello_world", LowerCaseWithUnderscores("helloWorld"))
|
||||||
|
should.Equal("hello_world", LowerCaseWithUnderscores("HelloWorld"))
|
||||||
|
SetNamingStrategy(LowerCaseWithUnderscores)
|
||||||
|
output, err := jsoniter.Marshal(struct {
|
||||||
|
UserName string
|
||||||
|
FirstLanguage string
|
||||||
|
}{
|
||||||
|
UserName: "taowen",
|
||||||
|
FirstLanguage: "Chinese",
|
||||||
|
})
|
||||||
|
should.Nil(err)
|
||||||
|
should.Equal(`{"user_name":"taowen","first_language":"Chinese"}`, string(output))
|
||||||
|
}
|
24
extra/privat_fields.go
Normal file
24
extra/privat_fields.go
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
package extra
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/json-iterator/go"
|
||||||
|
"unicode"
|
||||||
|
)
|
||||||
|
|
||||||
|
func SupportPrivateFields() {
|
||||||
|
jsoniter.RegisterExtension(&privateFieldsExtension{})
|
||||||
|
}
|
||||||
|
|
||||||
|
type privateFieldsExtension struct {
|
||||||
|
jsoniter.DummyExtension
|
||||||
|
}
|
||||||
|
|
||||||
|
func (extension *privateFieldsExtension) UpdateStructDescriptor(structDescriptor *jsoniter.StructDescriptor) {
|
||||||
|
for _, binding := range structDescriptor.Fields {
|
||||||
|
isPrivate := unicode.IsLower(rune(binding.Field.Name[0]))
|
||||||
|
if isPrivate {
|
||||||
|
binding.FromNames = []string{binding.Field.Name}
|
||||||
|
binding.ToNames = []string{binding.Field.Name}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
18
extra/private_fields_test.go
Normal file
18
extra/private_fields_test.go
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
package extra
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/json-iterator/go"
|
||||||
|
"github.com/json-iterator/go/require"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_private_fields(t *testing.T) {
|
||||||
|
type TestObject struct {
|
||||||
|
field1 string
|
||||||
|
}
|
||||||
|
SupportPrivateFields()
|
||||||
|
should := require.New(t)
|
||||||
|
obj := TestObject{}
|
||||||
|
should.Nil(jsoniter.UnmarshalFromString(`{"field1":"Hello"}`, &obj))
|
||||||
|
should.Equal("Hello", obj.field1)
|
||||||
|
}
|
34
extra/time_as_int64_codec.go
Normal file
34
extra/time_as_int64_codec.go
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
package extra
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/json-iterator/go"
|
||||||
|
"time"
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
// keep epoch milliseconds
|
||||||
|
func RegisterTimeAsInt64Codec(precision time.Duration) {
|
||||||
|
jsoniter.RegisterTypeEncoder("time.Time", &timeAsInt64Codec{precision})
|
||||||
|
jsoniter.RegisterTypeDecoder("time.Time", &timeAsInt64Codec{precision})
|
||||||
|
}
|
||||||
|
|
||||||
|
type timeAsInt64Codec struct {
|
||||||
|
precision time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
func (codec *timeAsInt64Codec) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||||
|
nanoseconds := iter.ReadInt64() * codec.precision.Nanoseconds()
|
||||||
|
*((*time.Time)(ptr)) = time.Unix(0, nanoseconds)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (codec *timeAsInt64Codec) IsEmpty(ptr unsafe.Pointer) bool {
|
||||||
|
ts := *((*time.Time)(ptr))
|
||||||
|
return ts.UnixNano() == 0
|
||||||
|
}
|
||||||
|
func (codec *timeAsInt64Codec) Encode(ptr unsafe.Pointer, stream *jsoniter.Stream) {
|
||||||
|
ts := *((*time.Time)(ptr))
|
||||||
|
stream.WriteInt64(ts.UnixNano() / codec.precision.Nanoseconds())
|
||||||
|
}
|
||||||
|
func (codec *timeAsInt64Codec) EncodeInterface(val interface{}, stream *jsoniter.Stream) {
|
||||||
|
jsoniter.WriteToStream(val, stream, codec)
|
||||||
|
}
|
31
extra/time_as_int64_codec_test.go
Normal file
31
extra/time_as_int64_codec_test.go
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
package extra
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/json-iterator/go"
|
||||||
|
"github.com/json-iterator/go/require"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_time_as_int64(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
RegisterTimeAsInt64Codec(time.Nanosecond)
|
||||||
|
output, err := jsoniter.Marshal(time.Unix(1497952257, 1002))
|
||||||
|
should.Nil(err)
|
||||||
|
should.Equal("1497952257000001002", string(output))
|
||||||
|
var val time.Time
|
||||||
|
should.Nil(jsoniter.Unmarshal(output, &val))
|
||||||
|
should.Equal(int64(1497952257000001002), val.UnixNano())
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_time_as_int64_keep_microsecond(t *testing.T) {
|
||||||
|
t.Skip("conflict")
|
||||||
|
should := require.New(t)
|
||||||
|
RegisterTimeAsInt64Codec(time.Microsecond)
|
||||||
|
output, err := jsoniter.Marshal(time.Unix(1, 1002))
|
||||||
|
should.Nil(err)
|
||||||
|
should.Equal("1000001", string(output))
|
||||||
|
var val time.Time
|
||||||
|
should.Nil(jsoniter.Unmarshal(output, &val))
|
||||||
|
should.Equal(int64(1000001000), val.UnixNano())
|
||||||
|
}
|
@ -1,88 +1,124 @@
|
|||||||
|
// Package jsoniter implements encoding and decoding of JSON as defined in
|
||||||
|
// RFC 4627 and provides interfaces with identical syntax of standard lib encoding/json.
|
||||||
|
// Converting from encoding/json to jsoniter is no more than replacing the package with jsoniter
|
||||||
|
// and variable type declarations (if any).
|
||||||
|
// jsoniter interfaces gives 100% compatibility with code using standard lib.
|
||||||
|
//
|
||||||
|
// "JSON and Go"
|
||||||
|
// (https://golang.org/doc/articles/json_and_go.html)
|
||||||
|
// gives a description of how Marshal/Unmarshal operate
|
||||||
|
// between arbitrary or predefined json objects and bytes,
|
||||||
|
// and it applies to jsoniter.Marshal/Unmarshal as well.
|
||||||
package jsoniter
|
package jsoniter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Unmarshal adapts to json/encoding APIs
|
type RawMessage []byte
|
||||||
|
|
||||||
|
// Unmarshal adapts to json/encoding Unmarshal API
|
||||||
|
//
|
||||||
|
// Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v.
|
||||||
|
// Refer to https://godoc.org/encoding/json#Unmarshal for more information
|
||||||
func Unmarshal(data []byte, v interface{}) error {
|
func Unmarshal(data []byte, v interface{}) error {
|
||||||
iter := ParseBytes(data)
|
return ConfigDefault.Unmarshal(data, v)
|
||||||
iter.ReadVal(v)
|
|
||||||
if iter.head == iter.tail {
|
|
||||||
iter.loadMore()
|
|
||||||
}
|
|
||||||
if iter.Error == io.EOF {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if iter.Error == nil {
|
|
||||||
iter.reportError("Unmarshal", "there are bytes left after unmarshal")
|
|
||||||
}
|
|
||||||
return iter.Error
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func UnmarshalAny(data []byte) (Any, error) {
|
func lastNotSpacePos(data []byte) int {
|
||||||
iter := ParseBytes(data)
|
for i := len(data) - 1; i >= 0; i-- {
|
||||||
any := iter.ReadAny()
|
if data[i] != ' ' && data[i] != '\t' && data[i] != '\r' && data[i] != '\n' {
|
||||||
if iter.head == iter.tail {
|
return i + 1
|
||||||
iter.loadMore()
|
}
|
||||||
}
|
}
|
||||||
if iter.Error == io.EOF {
|
return 0
|
||||||
return any, nil
|
|
||||||
}
|
|
||||||
if iter.Error == nil {
|
|
||||||
iter.reportError("UnmarshalAny", "there are bytes left after unmarshal")
|
|
||||||
}
|
|
||||||
return any, iter.Error
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func UnmarshalFromString(str string, v interface{}) error {
|
func UnmarshalFromString(str string, v interface{}) error {
|
||||||
data := []byte(str)
|
return ConfigDefault.UnmarshalFromString(str, v)
|
||||||
iter := ParseBytes(data)
|
|
||||||
iter.ReadVal(v)
|
|
||||||
if iter.head == iter.tail {
|
|
||||||
iter.loadMore()
|
|
||||||
}
|
|
||||||
if iter.Error == io.EOF {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if iter.Error == nil {
|
|
||||||
iter.reportError("UnmarshalFromString", "there are bytes left after unmarshal")
|
|
||||||
}
|
|
||||||
return iter.Error
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func UnmarshalAnyFromString(str string) (Any, error) {
|
func Get(data []byte, path ...interface{}) Any {
|
||||||
data := []byte(str)
|
return ConfigDefault.Get(data, path...)
|
||||||
iter := ParseBytes(data)
|
|
||||||
any := iter.ReadAny()
|
|
||||||
if iter.head == iter.tail {
|
|
||||||
iter.loadMore()
|
|
||||||
}
|
|
||||||
if iter.Error == io.EOF {
|
|
||||||
return any, nil
|
|
||||||
}
|
|
||||||
if iter.Error == nil {
|
|
||||||
iter.reportError("UnmarshalAnyFromString", "there are bytes left after unmarshal")
|
|
||||||
}
|
|
||||||
return nil, iter.Error
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Marshal adapts to json/encoding Marshal API
|
||||||
|
//
|
||||||
|
// Marshal returns the JSON encoding of v, adapts to json/encoding Marshal API
|
||||||
|
// Refer to https://godoc.org/encoding/json#Marshal for more information
|
||||||
func Marshal(v interface{}) ([]byte, error) {
|
func Marshal(v interface{}) ([]byte, error) {
|
||||||
buf := &bytes.Buffer{}
|
return ConfigDefault.Marshal(v)
|
||||||
stream := NewStream(buf, 4096)
|
}
|
||||||
stream.WriteVal(v)
|
|
||||||
stream.Flush()
|
func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) {
|
||||||
if stream.Error != nil {
|
return ConfigDefault.MarshalIndent(v, prefix, indent)
|
||||||
return nil, stream.Error
|
|
||||||
}
|
|
||||||
return buf.Bytes(), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func MarshalToString(v interface{}) (string, error) {
|
func MarshalToString(v interface{}) (string, error) {
|
||||||
buf, err := Marshal(v)
|
return ConfigDefault.MarshalToString(v)
|
||||||
if err != nil {
|
}
|
||||||
return "", err
|
|
||||||
}
|
// NewDecoder adapts to json/stream NewDecoder API.
|
||||||
return string(buf), nil
|
//
|
||||||
|
// NewDecoder returns a new decoder that reads from r.
|
||||||
|
//
|
||||||
|
// Instead of a json/encoding Decoder, an Decoder is returned
|
||||||
|
// Refer to https://godoc.org/encoding/json#NewDecoder for more information
|
||||||
|
func NewDecoder(reader io.Reader) *Decoder {
|
||||||
|
return ConfigDefault.NewDecoder(reader)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decoder reads and decodes JSON values from an input stream.
|
||||||
|
// Decoder provides identical APIs with json/stream Decoder (Token() and UseNumber() are in progress)
|
||||||
|
type Decoder struct {
|
||||||
|
iter *Iterator
|
||||||
|
}
|
||||||
|
|
||||||
|
func (adapter *Decoder) Decode(obj interface{}) error {
|
||||||
|
adapter.iter.ReadVal(obj)
|
||||||
|
err := adapter.iter.Error
|
||||||
|
if err == io.EOF {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return adapter.iter.Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (adapter *Decoder) More() bool {
|
||||||
|
return adapter.iter.head != adapter.iter.tail
|
||||||
|
}
|
||||||
|
|
||||||
|
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()
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewEncoder(writer io.Writer) *Encoder {
|
||||||
|
return ConfigDefault.NewEncoder(writer)
|
||||||
|
}
|
||||||
|
|
||||||
|
type Encoder struct {
|
||||||
|
stream *Stream
|
||||||
|
}
|
||||||
|
|
||||||
|
func (adapter *Encoder) Encode(val interface{}) error {
|
||||||
|
adapter.stream.WriteVal(val)
|
||||||
|
adapter.stream.Flush()
|
||||||
|
return adapter.stream.Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (adapter *Encoder) SetIndent(prefix, indent string) {
|
||||||
|
adapter.stream.cfg.indentionStep = len(indent)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (adapter *Encoder) SetEscapeHTML(escapeHtml bool) {
|
||||||
|
config := adapter.stream.cfg.configBeforeFrozen
|
||||||
|
config.EscapeHtml = escapeHtml
|
||||||
|
adapter.stream.cfg = config.Froze()
|
||||||
}
|
}
|
242
feature_any.go
242
feature_any.go
@ -2,12 +2,14 @@ package jsoniter
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"reflect"
|
"reflect"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Any interface {
|
type Any interface {
|
||||||
LastError() error
|
LastError() error
|
||||||
ValueType() ValueType
|
ValueType() ValueType
|
||||||
|
MustBeValid() Any
|
||||||
ToBool() bool
|
ToBool() bool
|
||||||
ToInt() int
|
ToInt() int
|
||||||
ToInt32() int32
|
ToInt32() int32
|
||||||
@ -18,18 +20,17 @@ type Any interface {
|
|||||||
ToFloat32() float32
|
ToFloat32() float32
|
||||||
ToFloat64() float64
|
ToFloat64() float64
|
||||||
ToString() string
|
ToString() string
|
||||||
|
ToVal(val interface{})
|
||||||
Get(path ...interface{}) Any
|
Get(path ...interface{}) Any
|
||||||
|
// TODO: add Set
|
||||||
Size() int
|
Size() int
|
||||||
Keys() []string
|
Keys() []string
|
||||||
IterateObject() (func() (string, Any, bool), bool)
|
// TODO: remove me
|
||||||
IterateArray() (func() (Any, bool), bool)
|
|
||||||
GetArray() []Any
|
GetArray() []Any
|
||||||
SetArray(newList []Any) bool
|
// TODO: remove me
|
||||||
GetObject() map[string]Any
|
GetObject() map[string]Any
|
||||||
SetObject(map[string]Any) bool
|
|
||||||
GetInterface() interface{}
|
GetInterface() interface{}
|
||||||
WriteTo(stream *Stream)
|
WriteTo(stream *Stream)
|
||||||
Parse() *Iterator
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type baseAny struct{}
|
type baseAny struct{}
|
||||||
@ -46,28 +47,16 @@ func (any *baseAny) Keys() []string {
|
|||||||
return []string{}
|
return []string{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *baseAny) IterateObject() (func() (string, Any, bool), bool) {
|
|
||||||
return nil, false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *baseAny) IterateArray() (func() (Any, bool), bool) {
|
|
||||||
return nil, false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *baseAny) GetArray() []Any {
|
func (any *baseAny) GetArray() []Any {
|
||||||
return []Any{}
|
return []Any{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *baseAny) SetArray(newList []Any) bool {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *baseAny) GetObject() map[string]Any {
|
func (any *baseAny) GetObject() map[string]Any {
|
||||||
return map[string]Any{}
|
return map[string]Any{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *baseAny) SetObject(map[string]Any) bool {
|
func (any *baseAny) ToVal(obj interface{}) {
|
||||||
return false
|
panic("not implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
func WrapInt32(val int32) Any {
|
func WrapInt32(val int32) Any {
|
||||||
@ -91,13 +80,17 @@ func WrapFloat64(val float64) Any {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func WrapString(val string) Any {
|
func WrapString(val string) Any {
|
||||||
return &stringAny{baseAny{}, nil, val}
|
return &stringAny{baseAny{}, val}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Wrap(val interface{}) Any {
|
func Wrap(val interface{}) Any {
|
||||||
if val == nil {
|
if val == nil {
|
||||||
return &nilAny{}
|
return &nilAny{}
|
||||||
}
|
}
|
||||||
|
asAny, isAny := val.(Any)
|
||||||
|
if isAny {
|
||||||
|
return asAny
|
||||||
|
}
|
||||||
type_ := reflect.TypeOf(val)
|
type_ := reflect.TypeOf(val)
|
||||||
switch type_.Kind() {
|
switch type_.Kind() {
|
||||||
case reflect.Slice:
|
case reflect.Slice:
|
||||||
@ -143,14 +136,15 @@ func Wrap(val interface{}) Any {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (iter *Iterator) ReadAny() Any {
|
func (iter *Iterator) ReadAny() Any {
|
||||||
return iter.readAny(nil)
|
return iter.readAny()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (iter *Iterator) readAny(reusableIter *Iterator) Any {
|
func (iter *Iterator) readAny() Any {
|
||||||
c := iter.nextToken()
|
c := iter.nextToken()
|
||||||
switch c {
|
switch c {
|
||||||
case '"':
|
case '"':
|
||||||
return iter.readStringAny(reusableIter)
|
iter.unreadByte()
|
||||||
|
return &stringAny{baseAny{}, iter.ReadString()}
|
||||||
case 'n':
|
case 'n':
|
||||||
iter.skipFixedBytes(3) // null
|
iter.skipFixedBytes(3) // null
|
||||||
return &nilAny{}
|
return &nilAny{}
|
||||||
@ -161,140 +155,92 @@ func (iter *Iterator) readAny(reusableIter *Iterator) Any {
|
|||||||
iter.skipFixedBytes(4) // false
|
iter.skipFixedBytes(4) // false
|
||||||
return &falseAny{}
|
return &falseAny{}
|
||||||
case '{':
|
case '{':
|
||||||
return iter.readObjectAny(reusableIter)
|
return iter.readObjectAny()
|
||||||
case '[':
|
case '[':
|
||||||
return iter.readArrayAny(reusableIter)
|
return iter.readArrayAny()
|
||||||
|
case '-':
|
||||||
|
return iter.readNumberAny(false)
|
||||||
default:
|
default:
|
||||||
return iter.readNumberAny(reusableIter, c)
|
return iter.readNumberAny(true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (iter *Iterator) readNumberAny(reusableIter *Iterator, firstByte byte) Any {
|
func (iter *Iterator) readNumberAny(positive bool) Any {
|
||||||
dotFound := false
|
iter.startCapture(iter.head - 1)
|
||||||
lazyBuf := make([]byte, 1, 8)
|
iter.skipNumber()
|
||||||
lazyBuf[0] = firstByte
|
lazyBuf := iter.stopCapture()
|
||||||
for {
|
return &numberLazyAny{baseAny{}, iter.cfg, lazyBuf, nil}
|
||||||
for i := iter.head; i < iter.tail; i++ {
|
}
|
||||||
c := iter.buf[i]
|
|
||||||
if c == '.' {
|
func (iter *Iterator) readObjectAny() Any {
|
||||||
dotFound = true
|
iter.startCapture(iter.head - 1)
|
||||||
continue
|
iter.skipObject()
|
||||||
}
|
lazyBuf := iter.stopCapture()
|
||||||
switch c {
|
return &objectLazyAny{baseAny{}, iter.cfg, lazyBuf, nil}
|
||||||
case ' ', '\n', '\r', '\t', ',', '}', ']':
|
}
|
||||||
lazyBuf = append(lazyBuf, iter.buf[iter.head:i]...)
|
|
||||||
iter.head = i
|
func (iter *Iterator) readArrayAny() Any {
|
||||||
if dotFound {
|
iter.startCapture(iter.head - 1)
|
||||||
return &float64LazyAny{baseAny{}, lazyBuf, reusableIter, nil, 0}
|
iter.skipArray()
|
||||||
} else {
|
lazyBuf := iter.stopCapture()
|
||||||
if firstByte == '-' {
|
return &arrayLazyAny{baseAny{}, iter.cfg, lazyBuf, nil}
|
||||||
return &int64LazyAny{baseAny{}, lazyBuf, reusableIter, nil, 0}
|
}
|
||||||
} else {
|
|
||||||
return &uint64LazyAny{baseAny{}, lazyBuf, reusableIter, nil, 0}
|
func locateObjectField(iter *Iterator, target string) []byte {
|
||||||
}
|
var found []byte
|
||||||
}
|
iter.ReadObjectCB(func(iter *Iterator, field string) bool {
|
||||||
}
|
if field == target {
|
||||||
|
found = iter.SkipAndReturnBytes()
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
lazyBuf = append(lazyBuf, iter.buf[iter.head:iter.tail]...)
|
iter.Skip()
|
||||||
if !iter.loadMore() {
|
return true
|
||||||
iter.head = iter.tail
|
})
|
||||||
if dotFound {
|
return found
|
||||||
return &float64LazyAny{baseAny{}, lazyBuf, reusableIter, nil, 0}
|
}
|
||||||
|
|
||||||
|
func locateArrayElement(iter *Iterator, target int) []byte {
|
||||||
|
var found []byte
|
||||||
|
n := 0
|
||||||
|
iter.ReadArrayCB(func(iter *Iterator) bool {
|
||||||
|
if n == target {
|
||||||
|
found = iter.SkipAndReturnBytes()
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
iter.Skip()
|
||||||
|
n++
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
return found
|
||||||
|
}
|
||||||
|
|
||||||
|
func locatePath(iter *Iterator, path []interface{}) Any {
|
||||||
|
for i, pathKeyObj := range path {
|
||||||
|
switch pathKey := pathKeyObj.(type) {
|
||||||
|
case string:
|
||||||
|
valueBytes := locateObjectField(iter, pathKey)
|
||||||
|
if valueBytes == nil {
|
||||||
|
return newInvalidAny(path[i:])
|
||||||
|
}
|
||||||
|
iter.ResetBytes(valueBytes)
|
||||||
|
case int:
|
||||||
|
valueBytes := locateArrayElement(iter, pathKey)
|
||||||
|
if valueBytes == nil {
|
||||||
|
return newInvalidAny(path[i:])
|
||||||
|
}
|
||||||
|
iter.ResetBytes(valueBytes)
|
||||||
|
case int32:
|
||||||
|
if '*' == pathKey {
|
||||||
|
return iter.readAny().Get(path[i:]...)
|
||||||
} else {
|
} else {
|
||||||
if firstByte == '-' {
|
return newInvalidAny(path[i:])
|
||||||
return &int64LazyAny{baseAny{}, lazyBuf, reusableIter, nil, 0}
|
|
||||||
} else {
|
|
||||||
return &uint64LazyAny{baseAny{}, lazyBuf, reusableIter, nil, 0}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
default:
|
||||||
|
return newInvalidAny(path[i:])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
if iter.Error != nil && iter.Error != io.EOF {
|
||||||
|
return &invalidAny{baseAny{}, iter.Error}
|
||||||
func (iter *Iterator) readStringAny(reusableIter *Iterator) Any {
|
|
||||||
lazyBuf := make([]byte, 1, 8)
|
|
||||||
lazyBuf[0] = '"'
|
|
||||||
for {
|
|
||||||
end, escaped := iter.findStringEnd()
|
|
||||||
if end == -1 {
|
|
||||||
lazyBuf = append(lazyBuf, iter.buf[iter.head:iter.tail]...)
|
|
||||||
if !iter.loadMore() {
|
|
||||||
iter.reportError("readStringAny", "incomplete string")
|
|
||||||
return &invalidAny{}
|
|
||||||
}
|
|
||||||
if escaped {
|
|
||||||
iter.head = 1 // skip the first char as last char read is \
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
lazyBuf = append(lazyBuf, iter.buf[iter.head:end]...)
|
|
||||||
iter.head = end
|
|
||||||
return &stringLazyAny{baseAny{}, lazyBuf, reusableIter, nil, ""}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (iter *Iterator) readObjectAny(reusableIter *Iterator) Any {
|
|
||||||
level := 1
|
|
||||||
lazyBuf := make([]byte, 1, 32)
|
|
||||||
lazyBuf[0] = '{'
|
|
||||||
for {
|
|
||||||
start := iter.head
|
|
||||||
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
|
|
||||||
lazyBuf = append(lazyBuf, iter.buf[start:iter.head]...)
|
|
||||||
return &objectLazyAny{baseAny{}, lazyBuf, reusableIter, nil, nil, lazyBuf}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
lazyBuf = append(lazyBuf, iter.buf[iter.head:iter.tail]...)
|
|
||||||
if !iter.loadMore() {
|
|
||||||
iter.reportError("skipObject", "incomplete object")
|
|
||||||
return &invalidAny{}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (iter *Iterator) readArrayAny(reusableIter *Iterator) Any {
|
|
||||||
level := 1
|
|
||||||
lazyBuf := make([]byte, 1, 32)
|
|
||||||
lazyBuf[0] = '['
|
|
||||||
for {
|
|
||||||
start := iter.head
|
|
||||||
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
|
|
||||||
lazyBuf = append(lazyBuf, iter.buf[start:iter.head]...)
|
|
||||||
return &arrayLazyAny{baseAny{}, lazyBuf, reusableIter, nil, nil, lazyBuf}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
lazyBuf = append(lazyBuf, iter.buf[iter.head:iter.tail]...)
|
|
||||||
if !iter.loadMore() {
|
|
||||||
iter.reportError("skipArray", "incomplete array")
|
|
||||||
return &invalidAny{}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
return iter.readAny()
|
||||||
}
|
}
|
||||||
|
@ -1,108 +1,23 @@
|
|||||||
package jsoniter
|
package jsoniter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"unsafe"
|
|
||||||
"fmt"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
type arrayLazyAny struct {
|
type arrayLazyAny struct {
|
||||||
baseAny
|
baseAny
|
||||||
buf []byte
|
cfg *frozenConfig
|
||||||
iter *Iterator
|
buf []byte
|
||||||
err error
|
err error
|
||||||
cache []Any
|
|
||||||
remaining []byte
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *arrayLazyAny) ValueType() ValueType {
|
func (any *arrayLazyAny) ValueType() ValueType {
|
||||||
return Array
|
return Array
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *arrayLazyAny) Parse() *Iterator {
|
func (any *arrayLazyAny) MustBeValid() Any {
|
||||||
iter := any.iter
|
return any
|
||||||
if iter == nil {
|
|
||||||
iter = NewIterator()
|
|
||||||
any.iter = iter
|
|
||||||
}
|
|
||||||
iter.ResetBytes(any.remaining)
|
|
||||||
return iter
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *arrayLazyAny) fillCacheUntil(target int) Any {
|
|
||||||
if any.remaining == nil {
|
|
||||||
if target >= len(any.cache) {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return any.cache[target]
|
|
||||||
}
|
|
||||||
if any.cache == nil {
|
|
||||||
any.cache = make([]Any, 0, 8)
|
|
||||||
}
|
|
||||||
i := len(any.cache)
|
|
||||||
if target < i {
|
|
||||||
return any.cache[target]
|
|
||||||
}
|
|
||||||
iter := any.Parse()
|
|
||||||
if (len(any.remaining) == len(any.buf)) {
|
|
||||||
iter.head++
|
|
||||||
c := iter.nextToken()
|
|
||||||
if c != ']' {
|
|
||||||
iter.unreadByte()
|
|
||||||
element := iter.readAny(iter)
|
|
||||||
any.cache = append(any.cache, element)
|
|
||||||
if target == 0 {
|
|
||||||
any.remaining = iter.buf[iter.head:]
|
|
||||||
any.err = iter.Error
|
|
||||||
return element
|
|
||||||
}
|
|
||||||
i = 1
|
|
||||||
} else {
|
|
||||||
any.remaining = nil
|
|
||||||
any.err = iter.Error
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for iter.nextToken() == ',' {
|
|
||||||
element := iter.readAny(iter)
|
|
||||||
any.cache = append(any.cache, element)
|
|
||||||
if i == target {
|
|
||||||
any.remaining = iter.buf[iter.head:]
|
|
||||||
any.err = iter.Error
|
|
||||||
return element
|
|
||||||
}
|
|
||||||
i++
|
|
||||||
}
|
|
||||||
any.remaining = nil
|
|
||||||
any.err = iter.Error
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *arrayLazyAny) fillCache() {
|
|
||||||
if any.remaining == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if any.cache == nil {
|
|
||||||
any.cache = make([]Any, 0, 8)
|
|
||||||
}
|
|
||||||
iter := any.Parse()
|
|
||||||
if len(any.remaining) == len(any.buf) {
|
|
||||||
iter.head++
|
|
||||||
c := iter.nextToken()
|
|
||||||
if c != ']' {
|
|
||||||
iter.unreadByte()
|
|
||||||
any.cache = append(any.cache, iter.readAny(iter))
|
|
||||||
} else {
|
|
||||||
any.remaining = nil
|
|
||||||
any.err = iter.Error
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for iter.nextToken() == ',' {
|
|
||||||
any.cache = append(any.cache, iter.readAny(iter))
|
|
||||||
}
|
|
||||||
any.remaining = nil
|
|
||||||
any.err = iter.Error
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *arrayLazyAny) LastError() error {
|
func (any *arrayLazyAny) LastError() error {
|
||||||
@ -110,252 +25,173 @@ func (any *arrayLazyAny) LastError() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (any *arrayLazyAny) ToBool() bool {
|
func (any *arrayLazyAny) ToBool() bool {
|
||||||
if any.cache == nil {
|
iter := any.cfg.BorrowIterator(any.buf)
|
||||||
any.IterateArray() // trigger first element read
|
defer any.cfg.ReturnIterator(iter)
|
||||||
}
|
return iter.ReadArray()
|
||||||
return len(any.cache) != 0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *arrayLazyAny) ToInt() int {
|
func (any *arrayLazyAny) ToInt() int {
|
||||||
if any.cache == nil {
|
if any.ToBool() {
|
||||||
any.IterateArray() // trigger first element read
|
return 1
|
||||||
}
|
} else {
|
||||||
if len(any.cache) == 0 {
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
return 1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *arrayLazyAny) ToInt32() int32 {
|
func (any *arrayLazyAny) ToInt32() int32 {
|
||||||
if any.cache == nil {
|
if any.ToBool() {
|
||||||
any.IterateArray() // trigger first element read
|
return 1
|
||||||
}
|
} else {
|
||||||
if len(any.cache) == 0 {
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
return 1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *arrayLazyAny) ToInt64() int64 {
|
func (any *arrayLazyAny) ToInt64() int64 {
|
||||||
if any.cache == nil {
|
if any.ToBool() {
|
||||||
any.IterateArray() // trigger first element read
|
return 1
|
||||||
}
|
} else {
|
||||||
if len(any.cache) == 0 {
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
return 1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *arrayLazyAny) ToUint() uint {
|
func (any *arrayLazyAny) ToUint() uint {
|
||||||
if any.cache == nil {
|
if any.ToBool() {
|
||||||
any.IterateArray() // trigger first element read
|
return 1
|
||||||
}
|
} else {
|
||||||
if len(any.cache) == 0 {
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
return 1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *arrayLazyAny) ToUint32() uint32 {
|
func (any *arrayLazyAny) ToUint32() uint32 {
|
||||||
if any.cache == nil {
|
if any.ToBool() {
|
||||||
any.IterateArray() // trigger first element read
|
return 1
|
||||||
}
|
} else {
|
||||||
if len(any.cache) == 0 {
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
return 1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *arrayLazyAny) ToUint64() uint64 {
|
func (any *arrayLazyAny) ToUint64() uint64 {
|
||||||
if any.cache == nil {
|
if any.ToBool() {
|
||||||
any.IterateArray() // trigger first element read
|
return 1
|
||||||
}
|
} else {
|
||||||
if len(any.cache) == 0 {
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
return 1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *arrayLazyAny) ToFloat32() float32 {
|
func (any *arrayLazyAny) ToFloat32() float32 {
|
||||||
if any.cache == nil {
|
if any.ToBool() {
|
||||||
any.IterateArray() // trigger first element read
|
return 1
|
||||||
}
|
} else {
|
||||||
if len(any.cache) == 0 {
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
return 1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *arrayLazyAny) ToFloat64() float64 {
|
func (any *arrayLazyAny) ToFloat64() float64 {
|
||||||
if any.cache == nil {
|
if any.ToBool() {
|
||||||
any.IterateArray() // trigger first element read
|
return 1
|
||||||
}
|
} else {
|
||||||
if len(any.cache) == 0 {
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
return 1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *arrayLazyAny) ToString() string {
|
func (any *arrayLazyAny) ToString() string {
|
||||||
if len(any.remaining) == len(any.buf) {
|
return *(*string)(unsafe.Pointer(&any.buf))
|
||||||
// nothing has been parsed yet
|
}
|
||||||
return *(*string)(unsafe.Pointer(&any.buf))
|
|
||||||
} else {
|
func (any *arrayLazyAny) ToVal(val interface{}) {
|
||||||
any.fillCache()
|
iter := any.cfg.BorrowIterator(any.buf)
|
||||||
str, err := MarshalToString(any.cache)
|
defer any.cfg.ReturnIterator(iter)
|
||||||
any.err = err
|
iter.ReadVal(val)
|
||||||
return str
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *arrayLazyAny) Get(path ...interface{}) Any {
|
func (any *arrayLazyAny) Get(path ...interface{}) Any {
|
||||||
if len(path) == 0 {
|
if len(path) == 0 {
|
||||||
return any
|
return any
|
||||||
}
|
}
|
||||||
var element Any
|
|
||||||
switch firstPath := path[0].(type) {
|
switch firstPath := path[0].(type) {
|
||||||
case int:
|
case int:
|
||||||
element = any.fillCacheUntil(firstPath)
|
iter := any.cfg.BorrowIterator(any.buf)
|
||||||
if element == nil {
|
defer any.cfg.ReturnIterator(iter)
|
||||||
element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", firstPath, any.cache)}
|
valueBytes := locateArrayElement(iter, firstPath)
|
||||||
|
if valueBytes == nil {
|
||||||
|
return newInvalidAny(path)
|
||||||
|
} else {
|
||||||
|
iter.ResetBytes(valueBytes)
|
||||||
|
return locatePath(iter, path[1:])
|
||||||
}
|
}
|
||||||
case int32:
|
case int32:
|
||||||
if '*' == firstPath {
|
if '*' == firstPath {
|
||||||
any.fillCache()
|
iter := any.cfg.BorrowIterator(any.buf)
|
||||||
arr := make([]Any, 0, len(any.cache))
|
defer any.cfg.ReturnIterator(iter)
|
||||||
for _, element := range any.cache {
|
arr := make([]Any, 0)
|
||||||
found := element.Get(path[1:]...)
|
iter.ReadArrayCB(func(iter *Iterator) bool {
|
||||||
|
found := iter.readAny().Get(path[1:]...)
|
||||||
if found.ValueType() != Invalid {
|
if found.ValueType() != Invalid {
|
||||||
arr = append(arr, found)
|
arr = append(arr, found)
|
||||||
}
|
}
|
||||||
}
|
return true
|
||||||
|
})
|
||||||
return wrapArray(arr)
|
return wrapArray(arr)
|
||||||
} else {
|
} else {
|
||||||
element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", path[0], any.cache)}
|
return newInvalidAny(path)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", path[0], any.cache)}
|
return newInvalidAny(path)
|
||||||
}
|
|
||||||
if len(path) == 1 {
|
|
||||||
return element
|
|
||||||
} else {
|
|
||||||
return element.Get(path[1:]...)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *arrayLazyAny) Size() int {
|
func (any *arrayLazyAny) Size() int {
|
||||||
any.fillCache()
|
size := 0
|
||||||
return len(any.cache)
|
iter := any.cfg.BorrowIterator(any.buf)
|
||||||
}
|
defer any.cfg.ReturnIterator(iter)
|
||||||
|
iter.ReadArrayCB(func(iter *Iterator) bool {
|
||||||
func (any *arrayLazyAny) IterateArray() (func() (Any, bool), bool) {
|
size++
|
||||||
if any.cache == nil {
|
iter.Skip()
|
||||||
any.cache = make([]Any, 0, 8)
|
return true
|
||||||
}
|
})
|
||||||
remaining := any.remaining
|
return size
|
||||||
if len(remaining) == len(any.buf) {
|
|
||||||
iter := any.Parse()
|
|
||||||
iter.head++
|
|
||||||
c := iter.nextToken()
|
|
||||||
if c != ']' {
|
|
||||||
iter.unreadByte()
|
|
||||||
v := iter.readAny(iter)
|
|
||||||
any.cache = append(any.cache, v)
|
|
||||||
remaining = iter.buf[iter.head:]
|
|
||||||
any.remaining = remaining
|
|
||||||
} else {
|
|
||||||
remaining = nil
|
|
||||||
any.remaining = nil
|
|
||||||
any.err = iter.Error
|
|
||||||
return nil, false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(any.cache) == 0 {
|
|
||||||
return nil, false
|
|
||||||
}
|
|
||||||
arr := any.cache
|
|
||||||
nextValue := arr[0]
|
|
||||||
i := 1
|
|
||||||
return func() (Any, bool) {
|
|
||||||
value := nextValue
|
|
||||||
if i < len(arr) {
|
|
||||||
// read from cache
|
|
||||||
nextValue = arr[i]
|
|
||||||
i++
|
|
||||||
return value, true
|
|
||||||
} else {
|
|
||||||
// read from buffer
|
|
||||||
iter := any.iter
|
|
||||||
if iter == nil {
|
|
||||||
iter = NewIterator()
|
|
||||||
any.iter = iter
|
|
||||||
}
|
|
||||||
iter.ResetBytes(remaining)
|
|
||||||
c := iter.nextToken()
|
|
||||||
if c == ',' {
|
|
||||||
nextValue = iter.readAny(iter)
|
|
||||||
any.cache = append(any.cache, nextValue)
|
|
||||||
remaining = iter.buf[iter.head:]
|
|
||||||
any.remaining = remaining
|
|
||||||
any.err = iter.Error
|
|
||||||
return value, true
|
|
||||||
} else {
|
|
||||||
remaining = nil
|
|
||||||
any.remaining = nil
|
|
||||||
any.err = iter.Error
|
|
||||||
return value, false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}, true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *arrayLazyAny) GetArray() []Any {
|
func (any *arrayLazyAny) GetArray() []Any {
|
||||||
any.fillCache()
|
elements := make([]Any, 0)
|
||||||
return any.cache
|
iter := any.cfg.BorrowIterator(any.buf)
|
||||||
}
|
defer any.cfg.ReturnIterator(iter)
|
||||||
|
iter.ReadArrayCB(func(iter *Iterator) bool {
|
||||||
func (any *arrayLazyAny) SetArray(newList []Any) bool {
|
elements = append(elements, iter.ReadAny())
|
||||||
any.fillCache()
|
return true
|
||||||
any.cache = newList
|
})
|
||||||
return true
|
return elements
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *arrayLazyAny) WriteTo(stream *Stream) {
|
func (any *arrayLazyAny) WriteTo(stream *Stream) {
|
||||||
if len(any.remaining) == len(any.buf) {
|
stream.Write(any.buf)
|
||||||
// nothing has been parsed yet
|
|
||||||
stream.Write(any.buf)
|
|
||||||
} else {
|
|
||||||
any.fillCache()
|
|
||||||
stream.WriteVal(any.cache)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *arrayLazyAny) GetInterface() interface{} {
|
func (any *arrayLazyAny) GetInterface() interface{} {
|
||||||
any.fillCache()
|
iter := any.cfg.BorrowIterator(any.buf)
|
||||||
return any.cache
|
defer any.cfg.ReturnIterator(iter)
|
||||||
|
return iter.Read()
|
||||||
}
|
}
|
||||||
|
|
||||||
type arrayAny struct {
|
type arrayAny struct {
|
||||||
baseAny
|
baseAny
|
||||||
err error
|
val reflect.Value
|
||||||
cache []Any
|
|
||||||
val reflect.Value
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func wrapArray(val interface{}) *arrayAny {
|
func wrapArray(val interface{}) *arrayAny {
|
||||||
return &arrayAny{baseAny{}, nil, nil, reflect.ValueOf(val)}
|
return &arrayAny{baseAny{}, reflect.ValueOf(val)}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *arrayAny) ValueType() ValueType {
|
func (any *arrayAny) ValueType() ValueType {
|
||||||
return Array
|
return Array
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *arrayAny) Parse() *Iterator {
|
func (any *arrayAny) MustBeValid() Any {
|
||||||
return nil
|
return any
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *arrayAny) LastError() error {
|
func (any *arrayAny) LastError() error {
|
||||||
return any.err
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *arrayAny) ToBool() bool {
|
func (any *arrayAny) ToBool() bool {
|
||||||
@ -419,121 +255,54 @@ func (any *arrayAny) ToFloat64() float64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (any *arrayAny) ToString() string {
|
func (any *arrayAny) ToString() string {
|
||||||
if len(any.cache) == 0 {
|
str, _ := MarshalToString(any.val.Interface())
|
||||||
// nothing has been parsed yet
|
return str
|
||||||
str, err := MarshalToString(any.val.Interface())
|
|
||||||
any.err = err
|
|
||||||
return str
|
|
||||||
} else {
|
|
||||||
any.fillCache()
|
|
||||||
str, err := MarshalToString(any.cache)
|
|
||||||
any.err = err
|
|
||||||
return str
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *arrayAny) fillCacheUntil(idx int) Any {
|
|
||||||
if idx < len(any.cache) {
|
|
||||||
return any.cache[idx]
|
|
||||||
} else {
|
|
||||||
for i := len(any.cache); i < any.val.Len(); i++ {
|
|
||||||
element := Wrap(any.val.Index(i).Interface())
|
|
||||||
any.cache = append(any.cache, element)
|
|
||||||
if idx == i {
|
|
||||||
return element
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *arrayAny) fillCache() {
|
|
||||||
any.cache = make([]Any, any.val.Len())
|
|
||||||
for i := 0; i < any.val.Len(); i++ {
|
|
||||||
any.cache[i] = Wrap(any.val.Index(i).Interface())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *arrayAny) Get(path ...interface{}) Any {
|
func (any *arrayAny) Get(path ...interface{}) Any {
|
||||||
if len(path) == 0 {
|
if len(path) == 0 {
|
||||||
return any
|
return any
|
||||||
}
|
}
|
||||||
var element Any
|
|
||||||
switch firstPath := path[0].(type) {
|
switch firstPath := path[0].(type) {
|
||||||
case int:
|
case int:
|
||||||
element = any.fillCacheUntil(firstPath)
|
if firstPath < 0 || firstPath >= any.val.Len() {
|
||||||
if element == nil {
|
return newInvalidAny(path)
|
||||||
element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", firstPath, any.cache)}
|
|
||||||
}
|
}
|
||||||
|
return Wrap(any.val.Index(firstPath).Interface())
|
||||||
case int32:
|
case int32:
|
||||||
if '*' == firstPath {
|
if '*' == firstPath {
|
||||||
any.fillCache()
|
mappedAll := make([]Any, 0)
|
||||||
mappedAll := make([]Any, 0, len(any.cache))
|
for i := 0; i < any.val.Len(); i++ {
|
||||||
for _, element := range any.cache {
|
mapped := Wrap(any.val.Index(i).Interface()).Get(path[1:]...)
|
||||||
mapped := element.Get(path[1:]...)
|
|
||||||
if mapped.ValueType() != Invalid {
|
if mapped.ValueType() != Invalid {
|
||||||
mappedAll = append(mappedAll, mapped)
|
mappedAll = append(mappedAll, mapped)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return wrapArray(mappedAll)
|
return wrapArray(mappedAll)
|
||||||
} else {
|
} else {
|
||||||
element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", path[0], any.cache)}
|
return newInvalidAny(path)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", path[0], any.cache)}
|
return newInvalidAny(path)
|
||||||
}
|
|
||||||
if len(path) == 1 {
|
|
||||||
return element
|
|
||||||
} else {
|
|
||||||
return element.Get(path[1:]...)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *arrayAny) Size() int {
|
func (any *arrayAny) Size() int {
|
||||||
any.fillCache()
|
return any.val.Len()
|
||||||
return len(any.cache)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *arrayAny) IterateArray() (func() (Any, bool), bool) {
|
|
||||||
if any.val.Len() == 0 {
|
|
||||||
return nil, false
|
|
||||||
}
|
|
||||||
i := 0
|
|
||||||
return func() (Any, bool) {
|
|
||||||
if i == any.val.Len() {
|
|
||||||
return nil, false
|
|
||||||
}
|
|
||||||
if i == len(any.cache) {
|
|
||||||
any.cache = append(any.cache, Wrap(any.val.Index(i).Interface()))
|
|
||||||
}
|
|
||||||
val := any.cache[i]
|
|
||||||
i++
|
|
||||||
return val, i != any.val.Len()
|
|
||||||
}, true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *arrayAny) GetArray() []Any {
|
func (any *arrayAny) GetArray() []Any {
|
||||||
any.fillCache()
|
elements := make([]Any, any.val.Len())
|
||||||
return any.cache
|
for i := 0; i < any.val.Len(); i++ {
|
||||||
}
|
elements[i] = Wrap(any.val.Index(i).Interface())
|
||||||
|
}
|
||||||
func (any *arrayAny) SetArray(newList []Any) bool {
|
return elements
|
||||||
any.fillCache()
|
|
||||||
any.cache = newList
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *arrayAny) WriteTo(stream *Stream) {
|
func (any *arrayAny) WriteTo(stream *Stream) {
|
||||||
if len(any.cache) == 0 {
|
stream.WriteVal(any.val)
|
||||||
// nothing has been parsed yet
|
|
||||||
stream.WriteVal(any.val)
|
|
||||||
} else {
|
|
||||||
any.fillCache()
|
|
||||||
stream.WriteVal(any.cache)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *arrayAny) GetInterface() interface{} {
|
func (any *arrayAny) GetInterface() interface{} {
|
||||||
any.fillCache()
|
return any.val.Interface()
|
||||||
return any.cache
|
|
||||||
}
|
}
|
@ -64,6 +64,10 @@ func (any *trueAny) ValueType() ValueType {
|
|||||||
return Bool
|
return Bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (any *trueAny) MustBeValid() Any {
|
||||||
|
return any
|
||||||
|
}
|
||||||
|
|
||||||
type falseAny struct {
|
type falseAny struct {
|
||||||
baseAny
|
baseAny
|
||||||
}
|
}
|
||||||
@ -127,3 +131,7 @@ func (any *falseAny) GetInterface() interface{} {
|
|||||||
func (any *falseAny) ValueType() ValueType {
|
func (any *falseAny) ValueType() ValueType {
|
||||||
return Bool
|
return Bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (any *falseAny) MustBeValid() Any {
|
||||||
|
return any
|
||||||
|
}
|
||||||
|
@ -1,105 +1,9 @@
|
|||||||
package jsoniter
|
package jsoniter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
|
||||||
"unsafe"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
type float64LazyAny struct {
|
|
||||||
baseAny
|
|
||||||
buf []byte
|
|
||||||
iter *Iterator
|
|
||||||
err error
|
|
||||||
cache float64
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *float64LazyAny) Parse() *Iterator {
|
|
||||||
iter := any.iter
|
|
||||||
if iter == nil {
|
|
||||||
iter = NewIterator()
|
|
||||||
}
|
|
||||||
iter.ResetBytes(any.buf)
|
|
||||||
return iter
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *float64LazyAny) ValueType() ValueType {
|
|
||||||
return Number
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *float64LazyAny) fillCache() {
|
|
||||||
if any.err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
iter := any.Parse()
|
|
||||||
any.cache = iter.ReadFloat64()
|
|
||||||
if iter.Error != io.EOF {
|
|
||||||
iter.reportError("floatLazyAny", "there are bytes left")
|
|
||||||
}
|
|
||||||
any.err = iter.Error
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *float64LazyAny) LastError() error {
|
|
||||||
return any.err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *float64LazyAny) ToBool() bool {
|
|
||||||
return any.ToFloat64() != 0
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *float64LazyAny) ToInt() int {
|
|
||||||
any.fillCache()
|
|
||||||
return int(any.cache)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *float64LazyAny) ToInt32() int32 {
|
|
||||||
any.fillCache()
|
|
||||||
return int32(any.cache)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *float64LazyAny) ToInt64() int64 {
|
|
||||||
any.fillCache()
|
|
||||||
return int64(any.cache)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *float64LazyAny) ToUint() uint {
|
|
||||||
any.fillCache()
|
|
||||||
return uint(any.cache)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *float64LazyAny) ToUint32() uint32 {
|
|
||||||
any.fillCache()
|
|
||||||
return uint32(any.cache)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *float64LazyAny) ToUint64() uint64 {
|
|
||||||
any.fillCache()
|
|
||||||
return uint64(any.cache)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *float64LazyAny) ToFloat32() float32 {
|
|
||||||
any.fillCache()
|
|
||||||
return float32(any.cache)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *float64LazyAny) ToFloat64() float64 {
|
|
||||||
any.fillCache()
|
|
||||||
return any.cache
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *float64LazyAny) ToString() string {
|
|
||||||
return *(*string)(unsafe.Pointer(&any.buf))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *float64LazyAny) WriteTo(stream *Stream) {
|
|
||||||
stream.Write(any.buf)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *float64LazyAny) GetInterface() interface{} {
|
|
||||||
any.fillCache()
|
|
||||||
return any.cache
|
|
||||||
}
|
|
||||||
|
|
||||||
type floatAny struct {
|
type floatAny struct {
|
||||||
baseAny
|
baseAny
|
||||||
val float64
|
val float64
|
||||||
@ -113,6 +17,10 @@ func (any *floatAny) ValueType() ValueType {
|
|||||||
return Number
|
return Number
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (any *floatAny) MustBeValid() Any {
|
||||||
|
return any
|
||||||
|
}
|
||||||
|
|
||||||
func (any *floatAny) LastError() error {
|
func (any *floatAny) LastError() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,10 @@ func (any *int32Any) ValueType() ValueType {
|
|||||||
return Number
|
return Number
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (any *int32Any) MustBeValid() Any {
|
||||||
|
return any
|
||||||
|
}
|
||||||
|
|
||||||
func (any *int32Any) ToBool() bool {
|
func (any *int32Any) ToBool() bool {
|
||||||
return any.val != 0
|
return any.val != 0
|
||||||
}
|
}
|
||||||
|
@ -1,105 +1,9 @@
|
|||||||
package jsoniter
|
package jsoniter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
|
||||||
"unsafe"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
type int64LazyAny struct {
|
|
||||||
baseAny
|
|
||||||
buf []byte
|
|
||||||
iter *Iterator
|
|
||||||
err error
|
|
||||||
cache int64
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *int64LazyAny) ValueType() ValueType {
|
|
||||||
return Number
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *int64LazyAny) Parse() *Iterator {
|
|
||||||
iter := any.iter
|
|
||||||
if iter == nil {
|
|
||||||
iter = NewIterator()
|
|
||||||
}
|
|
||||||
iter.ResetBytes(any.buf)
|
|
||||||
return iter
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *int64LazyAny) fillCache() {
|
|
||||||
if any.err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
iter := any.Parse()
|
|
||||||
any.cache = iter.ReadInt64()
|
|
||||||
if iter.Error != io.EOF {
|
|
||||||
iter.reportError("intLazyAny", "there are bytes left")
|
|
||||||
}
|
|
||||||
any.err = iter.Error
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *int64LazyAny) LastError() error {
|
|
||||||
return any.err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *int64LazyAny) ToBool() bool {
|
|
||||||
return any.ToInt64() != 0
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *int64LazyAny) ToInt() int {
|
|
||||||
any.fillCache()
|
|
||||||
return int(any.cache)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *int64LazyAny) ToInt32() int32 {
|
|
||||||
any.fillCache()
|
|
||||||
return int32(any.cache)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *int64LazyAny) ToInt64() int64 {
|
|
||||||
any.fillCache()
|
|
||||||
return any.cache
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *int64LazyAny) ToUint() uint {
|
|
||||||
any.fillCache()
|
|
||||||
return uint(any.cache)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *int64LazyAny) ToUint32() uint32 {
|
|
||||||
any.fillCache()
|
|
||||||
return uint32(any.cache)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *int64LazyAny) ToUint64() uint64 {
|
|
||||||
any.fillCache()
|
|
||||||
return uint64(any.cache)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *int64LazyAny) ToFloat32() float32 {
|
|
||||||
any.fillCache()
|
|
||||||
return float32(any.cache)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *int64LazyAny) ToFloat64() float64 {
|
|
||||||
any.fillCache()
|
|
||||||
return float64(any.cache)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *int64LazyAny) ToString() string {
|
|
||||||
return *(*string)(unsafe.Pointer(&any.buf))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *int64LazyAny) WriteTo(stream *Stream) {
|
|
||||||
stream.Write(any.buf)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *int64LazyAny) GetInterface() interface{} {
|
|
||||||
any.fillCache()
|
|
||||||
return any.cache
|
|
||||||
}
|
|
||||||
|
|
||||||
type int64Any struct {
|
type int64Any struct {
|
||||||
baseAny
|
baseAny
|
||||||
val int64
|
val int64
|
||||||
@ -113,6 +17,10 @@ func (any *int64Any) ValueType() ValueType {
|
|||||||
return Number
|
return Number
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (any *int64Any) MustBeValid() Any {
|
||||||
|
return any
|
||||||
|
}
|
||||||
|
|
||||||
func (any *int64Any) ToBool() bool {
|
func (any *int64Any) ToBool() bool {
|
||||||
return any.val != 0
|
return any.val != 0
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,10 @@ type invalidAny struct {
|
|||||||
err error
|
err error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func newInvalidAny(path []interface{}) *invalidAny {
|
||||||
|
return &invalidAny{baseAny{}, fmt.Errorf("%v not found", path)}
|
||||||
|
}
|
||||||
|
|
||||||
func (any *invalidAny) LastError() error {
|
func (any *invalidAny) LastError() error {
|
||||||
return any.err
|
return any.err
|
||||||
}
|
}
|
||||||
@ -15,6 +19,11 @@ func (any *invalidAny) ValueType() ValueType {
|
|||||||
return Invalid
|
return Invalid
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (any *invalidAny) MustBeValid() Any {
|
||||||
|
panic(any.err)
|
||||||
|
return any
|
||||||
|
}
|
||||||
|
|
||||||
func (any *invalidAny) ToBool() bool {
|
func (any *invalidAny) ToBool() bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,10 @@ func (any *nilAny) ValueType() ValueType {
|
|||||||
return Nil
|
return Nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (any *nilAny) MustBeValid() Any {
|
||||||
|
return any
|
||||||
|
}
|
||||||
|
|
||||||
func (any *nilAny) ToBool() bool {
|
func (any *nilAny) ToBool() bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
106
feature_any_number.go
Normal file
106
feature_any_number.go
Normal file
@ -0,0 +1,106 @@
|
|||||||
|
package jsoniter
|
||||||
|
|
||||||
|
import (
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
type numberLazyAny struct {
|
||||||
|
baseAny
|
||||||
|
cfg *frozenConfig
|
||||||
|
buf []byte
|
||||||
|
err error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (any *numberLazyAny) ValueType() ValueType {
|
||||||
|
return Number
|
||||||
|
}
|
||||||
|
|
||||||
|
func (any *numberLazyAny) MustBeValid() Any {
|
||||||
|
return any
|
||||||
|
}
|
||||||
|
|
||||||
|
func (any *numberLazyAny) LastError() error {
|
||||||
|
return any.err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (any *numberLazyAny) ToBool() bool {
|
||||||
|
return any.ToFloat64() != 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (any *numberLazyAny) ToInt() int {
|
||||||
|
iter := any.cfg.BorrowIterator(any.buf)
|
||||||
|
defer any.cfg.ReturnIterator(iter)
|
||||||
|
val := iter.ReadInt()
|
||||||
|
any.err = iter.Error
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
|
||||||
|
func (any *numberLazyAny) ToInt32() int32 {
|
||||||
|
iter := any.cfg.BorrowIterator(any.buf)
|
||||||
|
defer any.cfg.ReturnIterator(iter)
|
||||||
|
val := iter.ReadInt32()
|
||||||
|
any.err = iter.Error
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
|
||||||
|
func (any *numberLazyAny) ToInt64() int64 {
|
||||||
|
iter := any.cfg.BorrowIterator(any.buf)
|
||||||
|
defer any.cfg.ReturnIterator(iter)
|
||||||
|
val := iter.ReadInt64()
|
||||||
|
any.err = iter.Error
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
|
||||||
|
func (any *numberLazyAny) ToUint() uint {
|
||||||
|
iter := any.cfg.BorrowIterator(any.buf)
|
||||||
|
defer any.cfg.ReturnIterator(iter)
|
||||||
|
val := iter.ReadUint()
|
||||||
|
any.err = iter.Error
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
|
||||||
|
func (any *numberLazyAny) ToUint32() uint32 {
|
||||||
|
iter := any.cfg.BorrowIterator(any.buf)
|
||||||
|
defer any.cfg.ReturnIterator(iter)
|
||||||
|
val := iter.ReadUint32()
|
||||||
|
any.err = iter.Error
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
|
||||||
|
func (any *numberLazyAny) ToUint64() uint64 {
|
||||||
|
iter := any.cfg.BorrowIterator(any.buf)
|
||||||
|
defer any.cfg.ReturnIterator(iter)
|
||||||
|
val := iter.ReadUint64()
|
||||||
|
any.err = iter.Error
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
|
||||||
|
func (any *numberLazyAny) ToFloat32() float32 {
|
||||||
|
iter := any.cfg.BorrowIterator(any.buf)
|
||||||
|
defer any.cfg.ReturnIterator(iter)
|
||||||
|
val := iter.ReadFloat32()
|
||||||
|
any.err = iter.Error
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
|
||||||
|
func (any *numberLazyAny) ToFloat64() float64 {
|
||||||
|
iter := any.cfg.BorrowIterator(any.buf)
|
||||||
|
defer any.cfg.ReturnIterator(iter)
|
||||||
|
val := iter.ReadFloat64()
|
||||||
|
any.err = iter.Error
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
|
||||||
|
func (any *numberLazyAny) ToString() string {
|
||||||
|
return *(*string)(unsafe.Pointer(&any.buf))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (any *numberLazyAny) WriteTo(stream *Stream) {
|
||||||
|
stream.Write(any.buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (any *numberLazyAny) GetInterface() interface{} {
|
||||||
|
iter := any.cfg.BorrowIterator(any.buf)
|
||||||
|
defer any.cfg.ReturnIterator(iter)
|
||||||
|
return iter.Read()
|
||||||
|
}
|
@ -1,110 +1,23 @@
|
|||||||
package jsoniter
|
package jsoniter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"unsafe"
|
|
||||||
"fmt"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
type objectLazyAny struct {
|
type objectLazyAny struct {
|
||||||
baseAny
|
baseAny
|
||||||
buf []byte
|
cfg *frozenConfig
|
||||||
iter *Iterator
|
buf []byte
|
||||||
err error
|
err error
|
||||||
cache map[string]Any
|
|
||||||
remaining []byte
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *objectLazyAny) ValueType() ValueType {
|
func (any *objectLazyAny) ValueType() ValueType {
|
||||||
return Object
|
return Object
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *objectLazyAny) Parse() *Iterator {
|
func (any *objectLazyAny) MustBeValid() Any {
|
||||||
iter := any.iter
|
return any
|
||||||
if iter == nil {
|
|
||||||
iter = NewIterator()
|
|
||||||
any.iter = iter
|
|
||||||
}
|
|
||||||
iter.ResetBytes(any.remaining)
|
|
||||||
return iter
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *objectLazyAny) fillCacheUntil(target string) Any {
|
|
||||||
if any.remaining == nil {
|
|
||||||
return any.cache[target]
|
|
||||||
}
|
|
||||||
if any.cache == nil {
|
|
||||||
any.cache = map[string]Any{}
|
|
||||||
}
|
|
||||||
val := any.cache[target]
|
|
||||||
if val != nil {
|
|
||||||
return val
|
|
||||||
}
|
|
||||||
iter := any.Parse()
|
|
||||||
if len(any.remaining) == len(any.buf) {
|
|
||||||
iter.head++
|
|
||||||
c := iter.nextToken()
|
|
||||||
if c != '}' {
|
|
||||||
iter.unreadByte()
|
|
||||||
k := string(iter.readObjectFieldAsBytes())
|
|
||||||
v := iter.readAny(iter)
|
|
||||||
any.cache[k] = v
|
|
||||||
if target == k {
|
|
||||||
any.remaining = iter.buf[iter.head:]
|
|
||||||
any.err = iter.Error
|
|
||||||
return v
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
any.remaining = nil
|
|
||||||
any.err = iter.Error
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for iter.nextToken() == ',' {
|
|
||||||
k := string(iter.readObjectFieldAsBytes())
|
|
||||||
v := iter.readAny(iter)
|
|
||||||
any.cache[k] = v
|
|
||||||
if target == k {
|
|
||||||
any.remaining = iter.buf[iter.head:]
|
|
||||||
any.err = iter.Error
|
|
||||||
return v
|
|
||||||
}
|
|
||||||
}
|
|
||||||
any.remaining = nil
|
|
||||||
any.err = iter.Error
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *objectLazyAny) fillCache() {
|
|
||||||
if any.remaining == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if any.cache == nil {
|
|
||||||
any.cache = map[string]Any{}
|
|
||||||
}
|
|
||||||
iter := any.Parse()
|
|
||||||
if len(any.remaining) == len(any.buf) {
|
|
||||||
iter.head++
|
|
||||||
c := iter.nextToken()
|
|
||||||
if c != '}' {
|
|
||||||
iter.unreadByte()
|
|
||||||
k := string(iter.readObjectFieldAsBytes())
|
|
||||||
v := iter.readAny(iter)
|
|
||||||
any.cache[k] = v
|
|
||||||
} else {
|
|
||||||
any.remaining = nil
|
|
||||||
any.err = iter.Error
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for iter.nextToken() == ',' {
|
|
||||||
k := string(iter.readObjectFieldAsBytes())
|
|
||||||
v := iter.readAny(iter)
|
|
||||||
any.cache[k] = v
|
|
||||||
}
|
|
||||||
any.remaining = nil
|
|
||||||
any.err = iter.Error
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *objectLazyAny) LastError() error {
|
func (any *objectLazyAny) LastError() error {
|
||||||
@ -112,315 +25,188 @@ func (any *objectLazyAny) LastError() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (any *objectLazyAny) ToBool() bool {
|
func (any *objectLazyAny) ToBool() bool {
|
||||||
if any.cache == nil {
|
iter := any.cfg.BorrowIterator(any.buf)
|
||||||
any.IterateObject() // trigger first value read
|
defer any.cfg.ReturnIterator(iter)
|
||||||
}
|
return iter.ReadObject() != ""
|
||||||
return len(any.cache) != 0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *objectLazyAny) ToInt() int {
|
func (any *objectLazyAny) ToInt() int {
|
||||||
if any.cache == nil {
|
if any.ToBool() {
|
||||||
any.IterateObject() // trigger first value read
|
return 1
|
||||||
}
|
} else {
|
||||||
if len(any.cache) == 0 {
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
return 1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *objectLazyAny) ToInt32() int32 {
|
func (any *objectLazyAny) ToInt32() int32 {
|
||||||
if any.cache == nil {
|
if any.ToBool() {
|
||||||
any.IterateObject() // trigger first value read
|
return 1
|
||||||
}
|
} else {
|
||||||
if len(any.cache) == 0 {
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
return 1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *objectLazyAny) ToInt64() int64 {
|
func (any *objectLazyAny) ToInt64() int64 {
|
||||||
if any.cache == nil {
|
if any.ToBool() {
|
||||||
any.IterateObject() // trigger first value read
|
return 1
|
||||||
}
|
} else {
|
||||||
if len(any.cache) == 0 {
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
return 1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *objectLazyAny) ToUint() uint {
|
func (any *objectLazyAny) ToUint() uint {
|
||||||
if any.cache == nil {
|
if any.ToBool() {
|
||||||
any.IterateObject() // trigger first value read
|
return 1
|
||||||
}
|
} else {
|
||||||
if len(any.cache) == 0 {
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
return 1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *objectLazyAny) ToUint32() uint32 {
|
func (any *objectLazyAny) ToUint32() uint32 {
|
||||||
if any.cache == nil {
|
if any.ToBool() {
|
||||||
any.IterateObject() // trigger first value read
|
return 1
|
||||||
}
|
} else {
|
||||||
if len(any.cache) == 0 {
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
return 1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *objectLazyAny) ToUint64() uint64 {
|
func (any *objectLazyAny) ToUint64() uint64 {
|
||||||
if any.cache == nil {
|
if any.ToBool() {
|
||||||
any.IterateObject() // trigger first value read
|
return 1
|
||||||
}
|
} else {
|
||||||
if len(any.cache) == 0 {
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
return 1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *objectLazyAny) ToFloat32() float32 {
|
func (any *objectLazyAny) ToFloat32() float32 {
|
||||||
if any.cache == nil {
|
if any.ToBool() {
|
||||||
any.IterateObject() // trigger first value read
|
return 1
|
||||||
}
|
} else {
|
||||||
if len(any.cache) == 0 {
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
return 1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *objectLazyAny) ToFloat64() float64 {
|
func (any *objectLazyAny) ToFloat64() float64 {
|
||||||
if any.cache == nil {
|
if any.ToBool() {
|
||||||
any.IterateObject() // trigger first value read
|
return 1
|
||||||
}
|
} else {
|
||||||
if len(any.cache) == 0 {
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
return 1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *objectLazyAny) ToString() string {
|
func (any *objectLazyAny) ToString() string {
|
||||||
if len(any.remaining) == len(any.buf) {
|
return *(*string)(unsafe.Pointer(&any.buf))
|
||||||
// nothing has been parsed yet
|
}
|
||||||
return *(*string)(unsafe.Pointer(&any.buf))
|
|
||||||
} else {
|
func (any *objectLazyAny) ToVal(obj interface{}) {
|
||||||
any.fillCache()
|
iter := any.cfg.BorrowIterator(any.buf)
|
||||||
str, err := MarshalToString(any.cache)
|
defer any.cfg.ReturnIterator(iter)
|
||||||
any.err = err
|
iter.ReadVal(obj)
|
||||||
return str
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *objectLazyAny) Get(path ...interface{}) Any {
|
func (any *objectLazyAny) Get(path ...interface{}) Any {
|
||||||
if len(path) == 0 {
|
if len(path) == 0 {
|
||||||
return any
|
return any
|
||||||
}
|
}
|
||||||
var element Any
|
|
||||||
|
|
||||||
switch firstPath := path[0].(type) {
|
switch firstPath := path[0].(type) {
|
||||||
case string:
|
case string:
|
||||||
element = any.fillCacheUntil(firstPath)
|
iter := any.cfg.BorrowIterator(any.buf)
|
||||||
if element == nil {
|
defer any.cfg.ReturnIterator(iter)
|
||||||
element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", firstPath, any.cache)}
|
valueBytes := locateObjectField(iter, firstPath)
|
||||||
|
if valueBytes == nil {
|
||||||
|
return newInvalidAny(path)
|
||||||
|
} else {
|
||||||
|
iter.ResetBytes(valueBytes)
|
||||||
|
return locatePath(iter, path[1:])
|
||||||
}
|
}
|
||||||
case int32:
|
case int32:
|
||||||
if '*' == firstPath {
|
if '*' == firstPath {
|
||||||
any.fillCache()
|
|
||||||
mappedAll := map[string]Any{}
|
mappedAll := map[string]Any{}
|
||||||
for key, value := range any.cache {
|
iter := any.cfg.BorrowIterator(any.buf)
|
||||||
mapped := value.Get(path[1:]...)
|
defer any.cfg.ReturnIterator(iter)
|
||||||
|
iter.ReadObjectCB(func(iter *Iterator, field string) bool {
|
||||||
|
mapped := locatePath(iter, path[1:])
|
||||||
if mapped.ValueType() != Invalid {
|
if mapped.ValueType() != Invalid {
|
||||||
mappedAll[key] = mapped
|
mappedAll[field] = mapped
|
||||||
}
|
}
|
||||||
}
|
return true
|
||||||
|
})
|
||||||
return wrapMap(mappedAll)
|
return wrapMap(mappedAll)
|
||||||
} else {
|
} else {
|
||||||
element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", firstPath, any.cache)}
|
return newInvalidAny(path)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", firstPath, any.cache)}
|
return newInvalidAny(path)
|
||||||
}
|
|
||||||
if len(path) == 1 {
|
|
||||||
return element
|
|
||||||
} else {
|
|
||||||
return element.Get(path[1:]...)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *objectLazyAny) Keys() []string {
|
func (any *objectLazyAny) Keys() []string {
|
||||||
any.fillCache()
|
keys := []string{}
|
||||||
keys := make([]string, 0, len(any.cache))
|
iter := any.cfg.BorrowIterator(any.buf)
|
||||||
for key := range any.cache {
|
defer any.cfg.ReturnIterator(iter)
|
||||||
keys = append(keys, key)
|
iter.ReadObjectCB(func(iter *Iterator, field string) bool {
|
||||||
}
|
iter.Skip()
|
||||||
|
keys = append(keys, field)
|
||||||
|
return true
|
||||||
|
})
|
||||||
return keys
|
return keys
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *objectLazyAny) Size() int {
|
func (any *objectLazyAny) Size() int {
|
||||||
any.fillCache()
|
size := 0
|
||||||
return len(any.cache)
|
iter := any.cfg.BorrowIterator(any.buf)
|
||||||
}
|
defer any.cfg.ReturnIterator(iter)
|
||||||
|
iter.ReadObjectCB(func(iter *Iterator, field string) bool {
|
||||||
func (any *objectLazyAny) IterateObject() (func() (string, Any, bool), bool) {
|
iter.Skip()
|
||||||
if any.cache == nil {
|
size++
|
||||||
any.cache = map[string]Any{}
|
return true
|
||||||
}
|
})
|
||||||
remaining := any.remaining
|
return size
|
||||||
if len(remaining) == len(any.buf) {
|
|
||||||
iter := any.Parse()
|
|
||||||
iter.head++
|
|
||||||
c := iter.nextToken()
|
|
||||||
if c != '}' {
|
|
||||||
iter.unreadByte()
|
|
||||||
k := string(iter.readObjectFieldAsBytes())
|
|
||||||
v := iter.readAny(iter)
|
|
||||||
any.cache[k] = v
|
|
||||||
remaining = iter.buf[iter.head:]
|
|
||||||
any.remaining = remaining
|
|
||||||
} else {
|
|
||||||
remaining = nil
|
|
||||||
any.remaining = nil
|
|
||||||
any.err = iter.Error
|
|
||||||
return nil, false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(any.cache) == 0 {
|
|
||||||
return nil, false
|
|
||||||
}
|
|
||||||
keys := make([]string, 0, len(any.cache))
|
|
||||||
values := make([]Any, 0, len(any.cache))
|
|
||||||
for key, value := range any.cache {
|
|
||||||
keys = append(keys, key)
|
|
||||||
values = append(values, value)
|
|
||||||
}
|
|
||||||
nextKey := keys[0]
|
|
||||||
nextValue := values[0]
|
|
||||||
i := 1
|
|
||||||
return func() (string, Any, bool) {
|
|
||||||
key := nextKey
|
|
||||||
value := nextValue
|
|
||||||
if i < len(keys) {
|
|
||||||
// read from cache
|
|
||||||
nextKey = keys[i]
|
|
||||||
nextValue = values[i]
|
|
||||||
i++
|
|
||||||
return key, value, true
|
|
||||||
} else {
|
|
||||||
// read from buffer
|
|
||||||
iter := any.iter
|
|
||||||
if iter == nil {
|
|
||||||
iter = NewIterator()
|
|
||||||
any.iter = iter
|
|
||||||
}
|
|
||||||
iter.ResetBytes(remaining)
|
|
||||||
c := iter.nextToken()
|
|
||||||
if c == ',' {
|
|
||||||
nextKey = string(iter.readObjectFieldAsBytes())
|
|
||||||
nextValue = iter.readAny(iter)
|
|
||||||
any.cache[nextKey] = nextValue
|
|
||||||
remaining = iter.buf[iter.head:]
|
|
||||||
any.remaining = remaining
|
|
||||||
any.err = iter.Error
|
|
||||||
return key, value, true
|
|
||||||
} else {
|
|
||||||
remaining = nil
|
|
||||||
any.remaining = nil
|
|
||||||
any.err = iter.Error
|
|
||||||
return key, value, false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}, true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *objectLazyAny) GetObject() map[string]Any {
|
func (any *objectLazyAny) GetObject() map[string]Any {
|
||||||
any.fillCache()
|
asMap := map[string]Any{}
|
||||||
return any.cache
|
iter := any.cfg.BorrowIterator(any.buf)
|
||||||
}
|
defer any.cfg.ReturnIterator(iter)
|
||||||
|
iter.ReadObjectCB(func(iter *Iterator, field string) bool {
|
||||||
func (any *objectLazyAny) SetObject(val map[string]Any) bool {
|
asMap[field] = iter.ReadAny()
|
||||||
any.fillCache()
|
return true
|
||||||
any.cache = val
|
})
|
||||||
return true
|
return asMap
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *objectLazyAny) WriteTo(stream *Stream) {
|
func (any *objectLazyAny) WriteTo(stream *Stream) {
|
||||||
if len(any.remaining) == len(any.buf) {
|
stream.Write(any.buf)
|
||||||
// nothing has been parsed yet
|
|
||||||
stream.Write(any.buf)
|
|
||||||
} else {
|
|
||||||
any.fillCache()
|
|
||||||
stream.WriteVal(any.cache)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *objectLazyAny) GetInterface() interface{} {
|
func (any *objectLazyAny) GetInterface() interface{} {
|
||||||
any.fillCache()
|
iter := any.cfg.BorrowIterator(any.buf)
|
||||||
return any.cache
|
defer any.cfg.ReturnIterator(iter)
|
||||||
|
return iter.Read()
|
||||||
}
|
}
|
||||||
|
|
||||||
type objectAny struct {
|
type objectAny struct {
|
||||||
baseAny
|
baseAny
|
||||||
err error
|
err error
|
||||||
cache map[string]Any
|
val reflect.Value
|
||||||
val reflect.Value
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func wrapStruct(val interface{}) *objectAny {
|
func wrapStruct(val interface{}) *objectAny {
|
||||||
return &objectAny{baseAny{}, nil, nil, reflect.ValueOf(val)}
|
return &objectAny{baseAny{}, nil, reflect.ValueOf(val)}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *objectAny) ValueType() ValueType {
|
func (any *objectAny) ValueType() ValueType {
|
||||||
return Object
|
return Object
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (any *objectAny) MustBeValid() Any {
|
||||||
|
return any
|
||||||
|
}
|
||||||
|
|
||||||
func (any *objectAny) Parse() *Iterator {
|
func (any *objectAny) Parse() *Iterator {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *objectAny) fillCacheUntil(target string) Any {
|
|
||||||
if any.cache == nil {
|
|
||||||
any.cache = map[string]Any{}
|
|
||||||
}
|
|
||||||
element, found := any.cache[target]
|
|
||||||
if found {
|
|
||||||
return element
|
|
||||||
}
|
|
||||||
for i := len(any.cache); i < any.val.NumField(); i++ {
|
|
||||||
field := any.val.Field(i)
|
|
||||||
fieldName := any.val.Type().Field(i).Name
|
|
||||||
var element Any
|
|
||||||
if field.CanInterface() {
|
|
||||||
element = Wrap(field.Interface())
|
|
||||||
} else {
|
|
||||||
element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", fieldName, any.cache)}
|
|
||||||
}
|
|
||||||
any.cache[fieldName] = element
|
|
||||||
if fieldName == target {
|
|
||||||
return element
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *objectAny) fillCache() {
|
|
||||||
if any.cache == nil {
|
|
||||||
any.cache = map[string]Any{}
|
|
||||||
}
|
|
||||||
if len(any.cache) == any.val.NumField() {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
for i := 0; i < any.val.NumField(); i++ {
|
|
||||||
field := any.val.Field(i)
|
|
||||||
fieldName := any.val.Type().Field(i).Name
|
|
||||||
var element Any
|
|
||||||
if field.CanInterface() {
|
|
||||||
element = Wrap(field.Interface())
|
|
||||||
} else {
|
|
||||||
element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", fieldName, any.cache)}
|
|
||||||
}
|
|
||||||
any.cache[fieldName] = element
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *objectAny) LastError() error {
|
func (any *objectAny) LastError() error {
|
||||||
return any.err
|
return any.err
|
||||||
}
|
}
|
||||||
@ -486,189 +272,96 @@ func (any *objectAny) ToFloat64() float64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (any *objectAny) ToString() string {
|
func (any *objectAny) ToString() string {
|
||||||
if len(any.cache) == 0 {
|
str, err := MarshalToString(any.val.Interface())
|
||||||
str, err := MarshalToString(any.val.Interface())
|
any.err = err
|
||||||
any.err = err
|
return str
|
||||||
return str
|
|
||||||
} else {
|
|
||||||
any.fillCache()
|
|
||||||
str, err := MarshalToString(any.cache)
|
|
||||||
any.err = err
|
|
||||||
return str
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *objectAny) Get(path ...interface{}) Any {
|
func (any *objectAny) Get(path ...interface{}) Any {
|
||||||
if len(path) == 0 {
|
if len(path) == 0 {
|
||||||
return any
|
return any
|
||||||
}
|
}
|
||||||
var element Any
|
|
||||||
switch firstPath := path[0].(type) {
|
switch firstPath := path[0].(type) {
|
||||||
case string:
|
case string:
|
||||||
element = any.fillCacheUntil(firstPath)
|
field := any.val.FieldByName(firstPath)
|
||||||
if element == nil {
|
if !field.IsValid() {
|
||||||
element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", firstPath, any.cache)}
|
return newInvalidAny(path)
|
||||||
}
|
}
|
||||||
|
return Wrap(field.Interface())
|
||||||
case int32:
|
case int32:
|
||||||
if '*' == firstPath {
|
if '*' == firstPath {
|
||||||
any.fillCache()
|
|
||||||
mappedAll := map[string]Any{}
|
mappedAll := map[string]Any{}
|
||||||
for key, value := range any.cache {
|
for i := 0; i < any.val.NumField(); i++ {
|
||||||
mapped := value.Get(path[1:]...)
|
field := any.val.Field(i)
|
||||||
if mapped.ValueType() != Invalid {
|
if field.CanInterface() {
|
||||||
mappedAll[key] = mapped
|
mapped := Wrap(field.Interface()).Get(path[1:]...)
|
||||||
|
if mapped.ValueType() != Invalid {
|
||||||
|
mappedAll[any.val.Type().Field(i).Name] = mapped
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return wrapMap(mappedAll)
|
return wrapMap(mappedAll)
|
||||||
} else {
|
} else {
|
||||||
element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", firstPath, any.cache)}
|
return newInvalidAny(path)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", firstPath, any.cache)}
|
return newInvalidAny(path)
|
||||||
}
|
|
||||||
if len(path) == 1 {
|
|
||||||
return element
|
|
||||||
} else {
|
|
||||||
return element.Get(path[1:]...)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *objectAny) Keys() []string {
|
func (any *objectAny) Keys() []string {
|
||||||
any.fillCache()
|
keys := make([]string, 0, any.val.NumField())
|
||||||
keys := make([]string, 0, len(any.cache))
|
for i := 0; i < any.val.NumField(); i++ {
|
||||||
for key := range any.cache {
|
keys = append(keys, any.val.Type().Field(i).Name)
|
||||||
keys = append(keys, key)
|
|
||||||
}
|
}
|
||||||
return keys
|
return keys
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *objectAny) Size() int {
|
func (any *objectAny) Size() int {
|
||||||
any.fillCache()
|
return any.val.NumField()
|
||||||
return len(any.cache)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *objectAny) IterateObject() (func() (string, Any, bool), bool) {
|
|
||||||
if any.cache == nil {
|
|
||||||
any.cache = map[string]Any{}
|
|
||||||
}
|
|
||||||
if any.val.NumField() == 0 {
|
|
||||||
return nil, false
|
|
||||||
}
|
|
||||||
cacheKeys := make([]string, len(any.cache))
|
|
||||||
i := 0
|
|
||||||
for key := range any.cache {
|
|
||||||
cacheKeys[i] = key
|
|
||||||
i++
|
|
||||||
}
|
|
||||||
i = 0
|
|
||||||
return func() (string, Any, bool) {
|
|
||||||
if i == any.val.NumField() {
|
|
||||||
return "", nil, false
|
|
||||||
}
|
|
||||||
var fieldName string
|
|
||||||
var fieldValueAsAny Any
|
|
||||||
if i == len(cacheKeys) {
|
|
||||||
fieldName = any.val.Type().Field(i).Name
|
|
||||||
cacheKeys = append(cacheKeys, fieldName)
|
|
||||||
fieldValue := any.val.Field(i)
|
|
||||||
if fieldValue.CanInterface() {
|
|
||||||
fieldValueAsAny = Wrap(fieldValue.Interface())
|
|
||||||
any.cache[fieldName] = fieldValueAsAny
|
|
||||||
} else {
|
|
||||||
fieldValueAsAny = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", fieldName, any.cache)}
|
|
||||||
any.cache[fieldName] = fieldValueAsAny
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
fieldName = cacheKeys[i]
|
|
||||||
fieldValueAsAny = any.cache[fieldName]
|
|
||||||
}
|
|
||||||
i++
|
|
||||||
return fieldName, fieldValueAsAny, i != any.val.NumField()
|
|
||||||
}, true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *objectAny) GetObject() map[string]Any {
|
func (any *objectAny) GetObject() map[string]Any {
|
||||||
any.fillCache()
|
object := map[string]Any{}
|
||||||
return any.cache
|
for i := 0; i < any.val.NumField(); i++ {
|
||||||
}
|
field := any.val.Field(i)
|
||||||
|
if field.CanInterface() {
|
||||||
func (any *objectAny) SetObject(val map[string]Any) bool {
|
object[any.val.Type().Field(i).Name] = Wrap(field.Interface())
|
||||||
any.fillCache()
|
}
|
||||||
any.cache = val
|
}
|
||||||
return true
|
return object
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *objectAny) WriteTo(stream *Stream) {
|
func (any *objectAny) WriteTo(stream *Stream) {
|
||||||
if len(any.cache) == 0 {
|
stream.WriteVal(any.val)
|
||||||
// nothing has been parsed yet
|
|
||||||
stream.WriteVal(any.val)
|
|
||||||
} else {
|
|
||||||
any.fillCache()
|
|
||||||
stream.WriteVal(any.cache)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *objectAny) GetInterface() interface{} {
|
func (any *objectAny) GetInterface() interface{} {
|
||||||
any.fillCache()
|
return any.val.Interface()
|
||||||
return any.cache
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type mapAny struct {
|
type mapAny struct {
|
||||||
baseAny
|
baseAny
|
||||||
err error
|
err error
|
||||||
cache map[string]Any
|
val reflect.Value
|
||||||
val reflect.Value
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func wrapMap(val interface{}) *mapAny {
|
func wrapMap(val interface{}) *mapAny {
|
||||||
return &mapAny{baseAny{}, nil, nil, reflect.ValueOf(val)}
|
return &mapAny{baseAny{}, nil, reflect.ValueOf(val)}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *mapAny) ValueType() ValueType {
|
func (any *mapAny) ValueType() ValueType {
|
||||||
return Object
|
return Object
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (any *mapAny) MustBeValid() Any {
|
||||||
|
return any
|
||||||
|
}
|
||||||
|
|
||||||
func (any *mapAny) Parse() *Iterator {
|
func (any *mapAny) Parse() *Iterator {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *mapAny) fillCacheUntil(target string) Any {
|
|
||||||
if any.cache == nil {
|
|
||||||
any.cache = map[string]Any{}
|
|
||||||
}
|
|
||||||
element, found := any.cache[target]
|
|
||||||
if found {
|
|
||||||
return element
|
|
||||||
}
|
|
||||||
for _, key := range any.val.MapKeys() {
|
|
||||||
keyAsStr := key.String()
|
|
||||||
_, found := any.cache[keyAsStr]
|
|
||||||
if found {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
element := Wrap(any.val.MapIndex(key).Interface())
|
|
||||||
any.cache[keyAsStr] = element
|
|
||||||
if keyAsStr == target {
|
|
||||||
return element
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *mapAny) fillCache() {
|
|
||||||
if any.cache == nil {
|
|
||||||
any.cache = map[string]Any{}
|
|
||||||
}
|
|
||||||
if len(any.cache) == any.val.Len() {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
for _, key := range any.val.MapKeys() {
|
|
||||||
keyAsStr := key.String()
|
|
||||||
element := Wrap(any.val.MapIndex(key).Interface())
|
|
||||||
any.cache[keyAsStr] = element
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *mapAny) LastError() error {
|
func (any *mapAny) LastError() error {
|
||||||
return any.err
|
return any.err
|
||||||
}
|
}
|
||||||
@ -734,114 +427,66 @@ func (any *mapAny) ToFloat64() float64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (any *mapAny) ToString() string {
|
func (any *mapAny) ToString() string {
|
||||||
if len(any.cache) == 0 {
|
str, err := MarshalToString(any.val.Interface())
|
||||||
str, err := MarshalToString(any.val.Interface())
|
any.err = err
|
||||||
any.err = err
|
return str
|
||||||
return str
|
|
||||||
} else {
|
|
||||||
any.fillCache()
|
|
||||||
str, err := MarshalToString(any.cache)
|
|
||||||
any.err = err
|
|
||||||
return str
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *mapAny) Get(path ...interface{}) Any {
|
func (any *mapAny) Get(path ...interface{}) Any {
|
||||||
if len(path) == 0 {
|
if len(path) == 0 {
|
||||||
return any
|
return any
|
||||||
}
|
}
|
||||||
var element Any
|
|
||||||
switch firstPath := path[0].(type) {
|
switch firstPath := path[0].(type) {
|
||||||
case string:
|
|
||||||
element = any.fillCacheUntil(firstPath)
|
|
||||||
if element == nil {
|
|
||||||
element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", firstPath, any.cache)}
|
|
||||||
}
|
|
||||||
case int32:
|
case int32:
|
||||||
if '*' == firstPath {
|
if '*' == firstPath {
|
||||||
any.fillCache()
|
|
||||||
mappedAll := map[string]Any{}
|
mappedAll := map[string]Any{}
|
||||||
for key, value := range any.cache {
|
for _, key := range any.val.MapKeys() {
|
||||||
mapped := value.Get(path[1:]...)
|
keyAsStr := key.String()
|
||||||
|
element := Wrap(any.val.MapIndex(key).Interface())
|
||||||
|
mapped := element.Get(path[1:]...)
|
||||||
if mapped.ValueType() != Invalid {
|
if mapped.ValueType() != Invalid {
|
||||||
mappedAll[key] = mapped
|
mappedAll[keyAsStr] = mapped
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return wrapMap(mappedAll)
|
return wrapMap(mappedAll)
|
||||||
} else {
|
} else {
|
||||||
element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", firstPath, any.cache)}
|
return newInvalidAny(path)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", firstPath, any.cache)}
|
value := any.val.MapIndex(reflect.ValueOf(firstPath))
|
||||||
}
|
if !value.IsValid() {
|
||||||
if len(path) == 1 {
|
return newInvalidAny(path)
|
||||||
return element
|
}
|
||||||
} else {
|
return Wrap(value.Interface())
|
||||||
return element.Get(path[1:]...)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *mapAny) Keys() []string {
|
func (any *mapAny) Keys() []string {
|
||||||
any.fillCache()
|
keys := make([]string, 0, any.val.Len())
|
||||||
keys := make([]string, 0, len(any.cache))
|
for _, key := range any.val.MapKeys() {
|
||||||
for key := range any.cache {
|
keys = append(keys, key.String())
|
||||||
keys = append(keys, key)
|
|
||||||
}
|
}
|
||||||
return keys
|
return keys
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *mapAny) Size() int {
|
func (any *mapAny) Size() int {
|
||||||
any.fillCache()
|
return any.val.Len()
|
||||||
return len(any.cache)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *mapAny) IterateObject() (func() (string, Any, bool), bool) {
|
|
||||||
any.fillCache()
|
|
||||||
if len(any.cache) == 0 {
|
|
||||||
return nil, false
|
|
||||||
}
|
|
||||||
keys := make([]string, len(any.cache))
|
|
||||||
values := make([]Any, len(any.cache))
|
|
||||||
i := 0
|
|
||||||
for k, v := range any.cache {
|
|
||||||
keys[i] = k
|
|
||||||
values[i] = v
|
|
||||||
i++
|
|
||||||
}
|
|
||||||
i = 0
|
|
||||||
return func() (string, Any, bool) {
|
|
||||||
if i == len(keys) {
|
|
||||||
return "", nil, false
|
|
||||||
}
|
|
||||||
k := keys[i]
|
|
||||||
v := values[i]
|
|
||||||
i++
|
|
||||||
return k, v, i != len(keys)
|
|
||||||
}, true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *mapAny) GetObject() map[string]Any {
|
func (any *mapAny) GetObject() map[string]Any {
|
||||||
any.fillCache()
|
object := map[string]Any{}
|
||||||
return any.cache
|
for _, key := range any.val.MapKeys() {
|
||||||
}
|
keyAsStr := key.String()
|
||||||
|
element := Wrap(any.val.MapIndex(key).Interface())
|
||||||
func (any *mapAny) SetObject(val map[string]Any) bool {
|
object[keyAsStr] = element
|
||||||
any.fillCache()
|
}
|
||||||
any.cache = val
|
return object
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *mapAny) WriteTo(stream *Stream) {
|
func (any *mapAny) WriteTo(stream *Stream) {
|
||||||
if len(any.cache) == 0 {
|
stream.WriteVal(any.val)
|
||||||
// nothing has been parsed yet
|
|
||||||
stream.WriteVal(any.val)
|
|
||||||
} else {
|
|
||||||
any.fillCache()
|
|
||||||
stream.WriteVal(any.cache)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *mapAny) GetInterface() interface{} {
|
func (any *mapAny) GetInterface() interface{} {
|
||||||
any.fillCache()
|
return any.val.Interface()
|
||||||
return any.cache
|
|
||||||
}
|
}
|
||||||
|
@ -1,158 +1,36 @@
|
|||||||
package jsoniter
|
package jsoniter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
type stringLazyAny struct{
|
type stringAny struct {
|
||||||
baseAny
|
baseAny
|
||||||
buf []byte
|
|
||||||
iter *Iterator
|
|
||||||
err error
|
|
||||||
cache string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *stringLazyAny) ValueType() ValueType {
|
|
||||||
return String
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *stringLazyAny) Parse() *Iterator {
|
|
||||||
iter := any.iter
|
|
||||||
if iter == nil {
|
|
||||||
iter = NewIterator()
|
|
||||||
any.iter = iter
|
|
||||||
}
|
|
||||||
iter.ResetBytes(any.buf)
|
|
||||||
return iter
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *stringLazyAny) fillCache() {
|
|
||||||
if any.err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
iter := any.Parse()
|
|
||||||
any.cache = iter.ReadString()
|
|
||||||
if iter.Error != io.EOF {
|
|
||||||
iter.reportError("stringLazyAny", "there are bytes left")
|
|
||||||
}
|
|
||||||
any.err = iter.Error
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *stringLazyAny) LastError() error {
|
|
||||||
return any.err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *stringLazyAny) ToBool() bool {
|
|
||||||
str := any.ToString()
|
|
||||||
if str == "false" {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
for _, c := range str {
|
|
||||||
switch c {
|
|
||||||
case ' ', '\n', '\r', '\t':
|
|
||||||
default:
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *stringLazyAny) ToInt() int {
|
|
||||||
iter := any.Parse()
|
|
||||||
iter.head++
|
|
||||||
val := iter.ReadInt()
|
|
||||||
any.err = iter.Error
|
|
||||||
return val
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *stringLazyAny) ToInt32() int32 {
|
|
||||||
iter := any.Parse()
|
|
||||||
iter.head++
|
|
||||||
val := iter.ReadInt32()
|
|
||||||
any.err = iter.Error
|
|
||||||
return val
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *stringLazyAny) ToInt64() int64 {
|
|
||||||
iter := any.Parse()
|
|
||||||
iter.head++
|
|
||||||
val := iter.ReadInt64()
|
|
||||||
any.err = iter.Error
|
|
||||||
return val
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *stringLazyAny) ToUint() uint {
|
|
||||||
iter := any.Parse()
|
|
||||||
iter.head++
|
|
||||||
val := iter.ReadUint()
|
|
||||||
any.err = iter.Error
|
|
||||||
return val
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *stringLazyAny) ToUint32() uint32 {
|
|
||||||
iter := any.Parse()
|
|
||||||
iter.head++
|
|
||||||
val := iter.ReadUint32()
|
|
||||||
any.err = iter.Error
|
|
||||||
return val
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *stringLazyAny) ToUint64() uint64 {
|
|
||||||
iter := any.Parse()
|
|
||||||
iter.head++
|
|
||||||
val := iter.ReadUint64()
|
|
||||||
any.err = iter.Error
|
|
||||||
return val
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *stringLazyAny) ToFloat32() float32 {
|
|
||||||
iter := any.Parse()
|
|
||||||
iter.head++
|
|
||||||
val := iter.ReadFloat32()
|
|
||||||
any.err = iter.Error
|
|
||||||
return val
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *stringLazyAny) ToFloat64() float64 {
|
|
||||||
iter := any.Parse()
|
|
||||||
iter.head++
|
|
||||||
val := iter.ReadFloat64()
|
|
||||||
any.err = iter.Error
|
|
||||||
return val
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *stringLazyAny) ToString() string {
|
|
||||||
any.fillCache()
|
|
||||||
return any.cache
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *stringLazyAny) WriteTo(stream *Stream) {
|
|
||||||
stream.Write(any.buf)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *stringLazyAny) GetInterface() interface{} {
|
|
||||||
any.fillCache()
|
|
||||||
return any.cache
|
|
||||||
}
|
|
||||||
|
|
||||||
type stringAny struct{
|
|
||||||
baseAny
|
|
||||||
err error
|
|
||||||
val string
|
val string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (any *stringAny) Get(path ...interface{}) Any {
|
||||||
|
if len(path) == 0 {
|
||||||
|
return any
|
||||||
|
}
|
||||||
|
return &invalidAny{baseAny{}, fmt.Errorf("Get %v from simple value", path)}
|
||||||
|
}
|
||||||
|
|
||||||
func (any *stringAny) Parse() *Iterator {
|
func (any *stringAny) Parse() *Iterator {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (any *stringAny) ValueType() ValueType {
|
func (any *stringAny) ValueType() ValueType {
|
||||||
return String
|
return String
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (any *stringAny) MustBeValid() Any {
|
||||||
|
return any
|
||||||
|
}
|
||||||
|
|
||||||
func (any *stringAny) LastError() error {
|
func (any *stringAny) LastError() error {
|
||||||
return any.err
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *stringAny) ToBool() bool {
|
func (any *stringAny) ToBool() bool {
|
||||||
@ -171,50 +49,42 @@ func (any *stringAny) ToBool() bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (any *stringAny) ToInt() int {
|
func (any *stringAny) ToInt() int {
|
||||||
parsed, err := strconv.ParseInt(any.val, 10, 64)
|
parsed, _ := strconv.ParseInt(any.val, 10, 64)
|
||||||
any.err = err
|
|
||||||
return int(parsed)
|
return int(parsed)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *stringAny) ToInt32() int32 {
|
func (any *stringAny) ToInt32() int32 {
|
||||||
parsed, err := strconv.ParseInt(any.val, 10, 32)
|
parsed, _ := strconv.ParseInt(any.val, 10, 32)
|
||||||
any.err = err
|
|
||||||
return int32(parsed)
|
return int32(parsed)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *stringAny) ToInt64() int64 {
|
func (any *stringAny) ToInt64() int64 {
|
||||||
parsed, err := strconv.ParseInt(any.val, 10, 64)
|
parsed, _ := strconv.ParseInt(any.val, 10, 64)
|
||||||
any.err = err
|
|
||||||
return parsed
|
return parsed
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *stringAny) ToUint() uint {
|
func (any *stringAny) ToUint() uint {
|
||||||
parsed, err := strconv.ParseUint(any.val, 10, 64)
|
parsed, _ := strconv.ParseUint(any.val, 10, 64)
|
||||||
any.err = err
|
|
||||||
return uint(parsed)
|
return uint(parsed)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *stringAny) ToUint32() uint32 {
|
func (any *stringAny) ToUint32() uint32 {
|
||||||
parsed, err := strconv.ParseUint(any.val, 10, 32)
|
parsed, _ := strconv.ParseUint(any.val, 10, 32)
|
||||||
any.err = err
|
|
||||||
return uint32(parsed)
|
return uint32(parsed)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *stringAny) ToUint64() uint64 {
|
func (any *stringAny) ToUint64() uint64 {
|
||||||
parsed, err := strconv.ParseUint(any.val, 10, 64)
|
parsed, _ := strconv.ParseUint(any.val, 10, 64)
|
||||||
any.err = err
|
|
||||||
return parsed
|
return parsed
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *stringAny) ToFloat32() float32 {
|
func (any *stringAny) ToFloat32() float32 {
|
||||||
parsed, err := strconv.ParseFloat(any.val, 32)
|
parsed, _ := strconv.ParseFloat(any.val, 32)
|
||||||
any.err = err
|
|
||||||
return float32(parsed)
|
return float32(parsed)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *stringAny) ToFloat64() float64 {
|
func (any *stringAny) ToFloat64() float64 {
|
||||||
parsed, err := strconv.ParseFloat(any.val, 64)
|
parsed, _ := strconv.ParseFloat(any.val, 64)
|
||||||
any.err = err
|
|
||||||
return parsed
|
return parsed
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,6 +17,10 @@ func (any *uint32Any) ValueType() ValueType {
|
|||||||
return Number
|
return Number
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (any *uint32Any) MustBeValid() Any {
|
||||||
|
return any
|
||||||
|
}
|
||||||
|
|
||||||
func (any *uint32Any) ToBool() bool {
|
func (any *uint32Any) ToBool() bool {
|
||||||
return any.val != 0
|
return any.val != 0
|
||||||
}
|
}
|
||||||
|
@ -2,105 +2,8 @@ package jsoniter
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"unsafe"
|
|
||||||
"io"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
type uint64LazyAny struct {
|
|
||||||
baseAny
|
|
||||||
buf []byte
|
|
||||||
iter *Iterator
|
|
||||||
err error
|
|
||||||
cache uint64
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *uint64LazyAny) ValueType() ValueType {
|
|
||||||
return Number
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *uint64LazyAny) Parse() *Iterator {
|
|
||||||
iter := any.iter
|
|
||||||
if iter == nil {
|
|
||||||
iter = NewIterator()
|
|
||||||
}
|
|
||||||
iter.ResetBytes(any.buf)
|
|
||||||
return iter
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *uint64LazyAny) fillCache() {
|
|
||||||
if any.err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
iter := any.Parse()
|
|
||||||
any.cache = iter.ReadUint64()
|
|
||||||
if iter.Error != io.EOF {
|
|
||||||
iter.reportError("intLazyAny", "there are bytes left")
|
|
||||||
}
|
|
||||||
any.err = iter.Error
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *uint64LazyAny) LastError() error {
|
|
||||||
return any.err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *uint64LazyAny) ToBool() bool {
|
|
||||||
return any.ToInt64() != 0
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *uint64LazyAny) ToInt() int {
|
|
||||||
any.fillCache()
|
|
||||||
return int(any.cache)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *uint64LazyAny) ToInt32() int32 {
|
|
||||||
any.fillCache()
|
|
||||||
return int32(any.cache)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *uint64LazyAny) ToInt64() int64 {
|
|
||||||
any.fillCache()
|
|
||||||
return int64(any.cache)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *uint64LazyAny) ToUint() uint {
|
|
||||||
any.fillCache()
|
|
||||||
return uint(any.cache)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *uint64LazyAny) ToUint32() uint32 {
|
|
||||||
any.fillCache()
|
|
||||||
return uint32(any.cache)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *uint64LazyAny) ToUint64() uint64 {
|
|
||||||
any.fillCache()
|
|
||||||
return any.cache
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *uint64LazyAny) ToFloat32() float32 {
|
|
||||||
any.fillCache()
|
|
||||||
return float32(any.cache)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *uint64LazyAny) ToFloat64() float64 {
|
|
||||||
any.fillCache()
|
|
||||||
return float64(any.cache)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *uint64LazyAny) ToString() string {
|
|
||||||
return *(*string)(unsafe.Pointer(&any.buf))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *uint64LazyAny) WriteTo(stream *Stream) {
|
|
||||||
stream.Write(any.buf)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (any *uint64LazyAny) GetInterface() interface{} {
|
|
||||||
any.fillCache()
|
|
||||||
return any.cache
|
|
||||||
}
|
|
||||||
|
|
||||||
type uint64Any struct {
|
type uint64Any struct {
|
||||||
baseAny
|
baseAny
|
||||||
val uint64
|
val uint64
|
||||||
@ -114,6 +17,10 @@ func (any *uint64Any) ValueType() ValueType {
|
|||||||
return Number
|
return Number
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (any *uint64Any) MustBeValid() Any {
|
||||||
|
return any
|
||||||
|
}
|
||||||
|
|
||||||
func (any *uint64Any) ToBool() bool {
|
func (any *uint64Any) ToBool() bool {
|
||||||
return any.val != 0
|
return any.val != 0
|
||||||
}
|
}
|
||||||
|
297
feature_config.go
Normal file
297
feature_config.go
Normal file
@ -0,0 +1,297 @@
|
|||||||
|
package jsoniter
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
|
"reflect"
|
||||||
|
"sync/atomic"
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
IndentionStep int
|
||||||
|
MarshalFloatWith6Digits bool
|
||||||
|
EscapeHtml bool
|
||||||
|
SortMapKeys bool
|
||||||
|
UseNumber 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 {
|
||||||
|
MarshalToString(v interface{}) (string, error)
|
||||||
|
Marshal(v interface{}) ([]byte, error)
|
||||||
|
MarshalIndent(v interface{}, prefix, indent string) ([]byte, error)
|
||||||
|
UnmarshalFromString(str string, v interface{}) error
|
||||||
|
Unmarshal(data []byte, v interface{}) error
|
||||||
|
Get(data []byte, path ...interface{}) Any
|
||||||
|
NewEncoder(writer io.Writer) *Encoder
|
||||||
|
NewDecoder(reader io.Reader) *Decoder
|
||||||
|
}
|
||||||
|
|
||||||
|
var ConfigDefault = Config{
|
||||||
|
EscapeHtml: true,
|
||||||
|
}.Froze()
|
||||||
|
|
||||||
|
// Trying to be 100% compatible with standard library behavior
|
||||||
|
var ConfigCompatibleWithStandardLibrary = Config{
|
||||||
|
EscapeHtml: true,
|
||||||
|
SortMapKeys: true,
|
||||||
|
}.Froze()
|
||||||
|
|
||||||
|
var ConfigFastest = Config{
|
||||||
|
EscapeHtml: false,
|
||||||
|
MarshalFloatWith6Digits: true,
|
||||||
|
}.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),
|
||||||
|
}
|
||||||
|
atomic.StorePointer(&frozenConfig.decoderCache, unsafe.Pointer(&map[string]ValDecoder{}))
|
||||||
|
atomic.StorePointer(&frozenConfig.encoderCache, unsafe.Pointer(&map[string]ValEncoder{}))
|
||||||
|
if cfg.MarshalFloatWith6Digits {
|
||||||
|
frozenConfig.marshalFloatWith6Digits()
|
||||||
|
}
|
||||||
|
if cfg.EscapeHtml {
|
||||||
|
frozenConfig.escapeHtml()
|
||||||
|
}
|
||||||
|
if cfg.UseNumber {
|
||||||
|
frozenConfig.useNumber()
|
||||||
|
}
|
||||||
|
frozenConfig.configBeforeFrozen = cfg
|
||||||
|
return frozenConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cfg *frozenConfig) useNumber() {
|
||||||
|
cfg.addDecoderToCache(reflect.TypeOf((*interface{})(nil)).Elem(), &funcDecoder{func(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
|
if iter.WhatIsNext() == Number {
|
||||||
|
*((*interface{})(ptr)) = json.Number(iter.readNumberAsString())
|
||||||
|
} else {
|
||||||
|
*((*interface{})(ptr)) = iter.Read()
|
||||||
|
}
|
||||||
|
}})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cfg *frozenConfig) registerExtension(extension Extension) {
|
||||||
|
cfg.extensions = append(cfg.extensions, extension)
|
||||||
|
}
|
||||||
|
|
||||||
|
type lossyFloat32Encoder struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *lossyFloat32Encoder) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||||
|
stream.WriteFloat32Lossy(*((*float32)(ptr)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *lossyFloat32Encoder) EncodeInterface(val interface{}, stream *Stream) {
|
||||||
|
WriteToStream(val, stream, encoder)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *lossyFloat32Encoder) IsEmpty(ptr unsafe.Pointer) bool {
|
||||||
|
return *((*float32)(ptr)) == 0
|
||||||
|
}
|
||||||
|
|
||||||
|
type lossyFloat64Encoder struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *lossyFloat64Encoder) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||||
|
stream.WriteFloat64Lossy(*((*float64)(ptr)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *lossyFloat64Encoder) EncodeInterface(val interface{}, stream *Stream) {
|
||||||
|
WriteToStream(val, stream, encoder)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *lossyFloat64Encoder) IsEmpty(ptr unsafe.Pointer) bool {
|
||||||
|
return *((*float64)(ptr)) == 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// EnableLossyFloatMarshalling keeps 10**(-6) precision
|
||||||
|
// for float variables for better performance.
|
||||||
|
func (cfg *frozenConfig) marshalFloatWith6Digits() {
|
||||||
|
// for better performance
|
||||||
|
cfg.addEncoderToCache(reflect.TypeOf((*float32)(nil)).Elem(), &lossyFloat32Encoder{})
|
||||||
|
cfg.addEncoderToCache(reflect.TypeOf((*float64)(nil)).Elem(), &lossyFloat64Encoder{})
|
||||||
|
}
|
||||||
|
|
||||||
|
type htmlEscapedStringEncoder struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *htmlEscapedStringEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||||
|
str := *((*string)(ptr))
|
||||||
|
stream.WriteStringWithHtmlEscaped(str)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *htmlEscapedStringEncoder) EncodeInterface(val interface{}, stream *Stream) {
|
||||||
|
WriteToStream(val, stream, encoder)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *htmlEscapedStringEncoder) IsEmpty(ptr unsafe.Pointer) bool {
|
||||||
|
return *((*string)(ptr)) == ""
|
||||||
|
}
|
||||||
|
|
||||||
|
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()
|
||||||
|
}
|
||||||
|
|
||||||
|
// cleanEncoders cleans encoders registered or cached
|
||||||
|
func (cfg *frozenConfig) cleanEncoders() {
|
||||||
|
typeEncoders = map[string]ValEncoder{}
|
||||||
|
fieldEncoders = map[string]ValEncoder{}
|
||||||
|
*cfg = *cfg.configBeforeFrozen.Froze()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cfg *frozenConfig) MarshalToString(v interface{}) (string, error) {
|
||||||
|
stream := cfg.BorrowStream(nil)
|
||||||
|
defer cfg.ReturnStream(stream)
|
||||||
|
stream.WriteVal(v)
|
||||||
|
if stream.Error != nil {
|
||||||
|
return "", stream.Error
|
||||||
|
}
|
||||||
|
return string(stream.Buffer()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cfg *frozenConfig) Marshal(v interface{}) ([]byte, error) {
|
||||||
|
stream := cfg.BorrowStream(nil)
|
||||||
|
defer cfg.ReturnStream(stream)
|
||||||
|
stream.WriteVal(v)
|
||||||
|
if stream.Error != nil {
|
||||||
|
return nil, stream.Error
|
||||||
|
}
|
||||||
|
result := stream.Buffer()
|
||||||
|
copied := make([]byte, len(result))
|
||||||
|
copy(copied, result)
|
||||||
|
return copied, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cfg *frozenConfig) MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) {
|
||||||
|
if prefix != "" {
|
||||||
|
panic("prefix is not supported")
|
||||||
|
}
|
||||||
|
for _, r := range indent {
|
||||||
|
if r != ' ' {
|
||||||
|
panic("indent can only be space")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
newCfg := cfg.configBeforeFrozen
|
||||||
|
newCfg.IndentionStep = len(indent)
|
||||||
|
return newCfg.Froze().Marshal(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cfg *frozenConfig) UnmarshalFromString(str string, v interface{}) error {
|
||||||
|
data := []byte(str)
|
||||||
|
data = data[:lastNotSpacePos(data)]
|
||||||
|
iter := cfg.BorrowIterator(data)
|
||||||
|
defer cfg.ReturnIterator(iter)
|
||||||
|
iter.ReadVal(v)
|
||||||
|
if iter.head == iter.tail {
|
||||||
|
iter.loadMore()
|
||||||
|
}
|
||||||
|
if iter.Error == io.EOF {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if iter.Error == nil {
|
||||||
|
iter.ReportError("UnmarshalFromString", "there are bytes left after unmarshal")
|
||||||
|
}
|
||||||
|
return iter.Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cfg *frozenConfig) Get(data []byte, path ...interface{}) Any {
|
||||||
|
iter := cfg.BorrowIterator(data)
|
||||||
|
defer cfg.ReturnIterator(iter)
|
||||||
|
return locatePath(iter, path)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cfg *frozenConfig) Unmarshal(data []byte, v interface{}) error {
|
||||||
|
data = data[:lastNotSpacePos(data)]
|
||||||
|
iter := cfg.BorrowIterator(data)
|
||||||
|
defer cfg.ReturnIterator(iter)
|
||||||
|
typ := reflect.TypeOf(v)
|
||||||
|
if typ.Kind() != reflect.Ptr {
|
||||||
|
// return non-pointer error
|
||||||
|
return errors.New("the second param must be ptr type")
|
||||||
|
}
|
||||||
|
iter.ReadVal(v)
|
||||||
|
if iter.head == iter.tail {
|
||||||
|
iter.loadMore()
|
||||||
|
}
|
||||||
|
if iter.Error == io.EOF {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if iter.Error == nil {
|
||||||
|
iter.ReportError("Unmarshal", "there are bytes left after unmarshal")
|
||||||
|
}
|
||||||
|
return iter.Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cfg *frozenConfig) NewEncoder(writer io.Writer) *Encoder {
|
||||||
|
stream := NewStream(cfg, writer, 512)
|
||||||
|
return &Encoder{stream}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cfg *frozenConfig) NewDecoder(reader io.Reader) *Decoder {
|
||||||
|
iter := Parse(cfg, reader, 512)
|
||||||
|
return &Decoder{iter}
|
||||||
|
}
|
@ -1,7 +1,12 @@
|
|||||||
|
//
|
||||||
|
// Besides, jsoniter.Iterator provides a different set of interfaces
|
||||||
|
// iterating given bytes/string/reader
|
||||||
|
// and yielding parsed elements one by one.
|
||||||
|
// This set of interfaces reads input as required and gives
|
||||||
|
// better performance.
|
||||||
package jsoniter
|
package jsoniter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/base64"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
@ -60,16 +65,20 @@ func init() {
|
|||||||
|
|
||||||
// Iterator is a fast and flexible JSON parser
|
// Iterator is a fast and flexible JSON parser
|
||||||
type Iterator struct {
|
type Iterator struct {
|
||||||
reader io.Reader
|
cfg *frozenConfig
|
||||||
buf []byte
|
reader io.Reader
|
||||||
head int
|
buf []byte
|
||||||
tail int
|
head int
|
||||||
Error error
|
tail int
|
||||||
|
captureStartedAt int
|
||||||
|
captured []byte
|
||||||
|
Error error
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create creates an empty Iterator instance
|
// Create creates an empty Iterator instance
|
||||||
func NewIterator() *Iterator {
|
func NewIterator(cfg *frozenConfig) *Iterator {
|
||||||
return &Iterator{
|
return &Iterator{
|
||||||
|
cfg: cfg,
|
||||||
reader: nil,
|
reader: nil,
|
||||||
buf: nil,
|
buf: nil,
|
||||||
head: 0,
|
head: 0,
|
||||||
@ -78,8 +87,9 @@ func NewIterator() *Iterator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Parse parses a json buffer in io.Reader into an Iterator instance
|
// Parse parses a json buffer in io.Reader into an Iterator instance
|
||||||
func Parse(reader io.Reader, bufSize int) *Iterator {
|
func Parse(cfg *frozenConfig, reader io.Reader, bufSize int) *Iterator {
|
||||||
return &Iterator{
|
return &Iterator{
|
||||||
|
cfg: cfg,
|
||||||
reader: reader,
|
reader: reader,
|
||||||
buf: make([]byte, bufSize),
|
buf: make([]byte, bufSize),
|
||||||
head: 0,
|
head: 0,
|
||||||
@ -88,8 +98,9 @@ func Parse(reader io.Reader, bufSize int) *Iterator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ParseBytes parses a json byte slice into an Iterator instance
|
// ParseBytes parses a json byte slice into an Iterator instance
|
||||||
func ParseBytes(input []byte) *Iterator {
|
func ParseBytes(cfg *frozenConfig, input []byte) *Iterator {
|
||||||
return &Iterator{
|
return &Iterator{
|
||||||
|
cfg: cfg,
|
||||||
reader: nil,
|
reader: nil,
|
||||||
buf: input,
|
buf: input,
|
||||||
head: 0,
|
head: 0,
|
||||||
@ -98,8 +109,12 @@ func ParseBytes(input []byte) *Iterator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ParseString parses a json string into an Iterator instance
|
// ParseString parses a json string into an Iterator instance
|
||||||
func ParseString(input string) *Iterator {
|
func ParseString(cfg *frozenConfig, input string) *Iterator {
|
||||||
return ParseBytes([]byte(input))
|
return ParseBytes(cfg, []byte(input))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (iter *Iterator) Config() *frozenConfig {
|
||||||
|
return iter.cfg
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset can reset an Iterator instance for another json buffer in io.Reader
|
// Reset can reset an Iterator instance for another json buffer in io.Reader
|
||||||
@ -113,7 +128,6 @@ func (iter *Iterator) Reset(reader io.Reader) *Iterator {
|
|||||||
// ResetBytes can reset an Iterator instance for another json byte slice
|
// ResetBytes can reset an Iterator instance for another json byte slice
|
||||||
func (iter *Iterator) ResetBytes(input []byte) *Iterator {
|
func (iter *Iterator) ResetBytes(input []byte) *Iterator {
|
||||||
iter.reader = nil
|
iter.reader = nil
|
||||||
iter.Error = nil
|
|
||||||
iter.buf = input
|
iter.buf = input
|
||||||
iter.head = 0
|
iter.head = 0
|
||||||
iter.tail = len(input)
|
iter.tail = len(input)
|
||||||
@ -158,7 +172,7 @@ func (iter *Iterator) nextToken() byte {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (iter *Iterator) reportError(operation string, msg string) {
|
func (iter *Iterator) ReportError(operation string, msg string) {
|
||||||
if iter.Error != nil {
|
if iter.Error != nil {
|
||||||
if iter.Error != io.EOF {
|
if iter.Error != io.EOF {
|
||||||
return
|
return
|
||||||
@ -199,10 +213,16 @@ func (iter *Iterator) readByte() (ret byte) {
|
|||||||
func (iter *Iterator) loadMore() bool {
|
func (iter *Iterator) loadMore() bool {
|
||||||
if iter.reader == nil {
|
if iter.reader == nil {
|
||||||
if iter.Error == nil {
|
if iter.Error == nil {
|
||||||
|
iter.head = iter.tail
|
||||||
iter.Error = io.EOF
|
iter.Error = io.EOF
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
if iter.captureStartedAt != -1 {
|
||||||
|
iter.captured = append(iter.captured,
|
||||||
|
iter.buf[iter.captureStartedAt:iter.tail]...)
|
||||||
|
iter.captureStartedAt = 0
|
||||||
|
}
|
||||||
for {
|
for {
|
||||||
n, err := iter.reader.Read(iter.buf)
|
n, err := iter.reader.Read(iter.buf)
|
||||||
if n == 0 {
|
if n == 0 {
|
||||||
@ -222,7 +242,7 @@ func (iter *Iterator) loadMore() bool {
|
|||||||
|
|
||||||
func (iter *Iterator) unreadByte() {
|
func (iter *Iterator) unreadByte() {
|
||||||
if iter.head == 0 {
|
if iter.head == 0 {
|
||||||
iter.reportError("unreadByte", "unread too many bytes")
|
iter.ReportError("unreadByte", "unread too many bytes")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
iter.head--
|
iter.head--
|
||||||
@ -256,24 +276,7 @@ func (iter *Iterator) Read() interface{} {
|
|||||||
})
|
})
|
||||||
return obj
|
return obj
|
||||||
default:
|
default:
|
||||||
iter.reportError("Read", fmt.Sprintf("unexpected value type: %v", valueType))
|
iter.ReportError("Read", fmt.Sprintf("unexpected value type: %v", valueType))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadBase64 reads a json object as Base64 in byte slice
|
|
||||||
func (iter *Iterator) ReadBase64() (ret []byte) {
|
|
||||||
src := iter.ReadStringAsSlice()
|
|
||||||
if iter.Error != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
b64 := base64.StdEncoding
|
|
||||||
ret = make([]byte, b64.DecodedLen(len(src)))
|
|
||||||
n, err := b64.Decode(ret, src)
|
|
||||||
if err != nil {
|
|
||||||
iter.Error = err
|
|
||||||
return
|
|
||||||
}
|
|
||||||
return ret[:n]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@ -18,12 +18,11 @@ func (iter *Iterator) ReadArray() (ret bool) {
|
|||||||
case ',':
|
case ',':
|
||||||
return true
|
return true
|
||||||
default:
|
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
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (iter *Iterator) ReadArrayCB(callback func(*Iterator) bool) (ret bool) {
|
func (iter *Iterator) ReadArrayCB(callback func(*Iterator) bool) (ret bool) {
|
||||||
c := iter.nextToken()
|
c := iter.nextToken()
|
||||||
if c == '[' {
|
if c == '[' {
|
||||||
@ -46,6 +45,6 @@ func (iter *Iterator) ReadArrayCB(callback func(*Iterator) bool) (ret bool) {
|
|||||||
iter.skipFixedBytes(3)
|
iter.skipFixedBytes(3)
|
||||||
return true // null
|
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
|
return false
|
||||||
}
|
}
|
@ -2,11 +2,13 @@ package jsoniter
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
|
"math/big"
|
||||||
"strconv"
|
"strconv"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
var floatDigits []int8
|
var floatDigits []int8
|
||||||
|
|
||||||
const invalidCharForNumber = int8(-1)
|
const invalidCharForNumber = int8(-1)
|
||||||
const endOfNumber = int8(-2)
|
const endOfNumber = int8(-2)
|
||||||
const dotInNumber = int8(-3)
|
const dotInNumber = int8(-3)
|
||||||
@ -19,11 +21,45 @@ func init() {
|
|||||||
for i := int8('0'); i <= int8('9'); i++ {
|
for i := int8('0'); i <= int8('9'); i++ {
|
||||||
floatDigits[i] = i - int8('0')
|
floatDigits[i] = i - int8('0')
|
||||||
}
|
}
|
||||||
floatDigits[','] = endOfNumber;
|
floatDigits[','] = endOfNumber
|
||||||
floatDigits[']'] = endOfNumber;
|
floatDigits[']'] = endOfNumber
|
||||||
floatDigits['}'] = endOfNumber;
|
floatDigits['}'] = endOfNumber
|
||||||
floatDigits[' '] = endOfNumber;
|
floatDigits[' '] = endOfNumber
|
||||||
floatDigits['.'] = dotInNumber;
|
floatDigits['\t'] = endOfNumber
|
||||||
|
floatDigits['\n'] = endOfNumber
|
||||||
|
floatDigits['.'] = dotInNumber
|
||||||
|
}
|
||||||
|
|
||||||
|
func (iter *Iterator) ReadBigFloat() (ret *big.Float) {
|
||||||
|
str := iter.readNumberAsString()
|
||||||
|
if iter.Error != nil && iter.Error != io.EOF {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
prec := 64
|
||||||
|
if len(str) > prec {
|
||||||
|
prec = len(str)
|
||||||
|
}
|
||||||
|
val, _, err := big.ParseFloat(str, 10, uint(prec), big.ToZero)
|
||||||
|
if err != nil {
|
||||||
|
iter.Error = err
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
|
||||||
|
func (iter *Iterator) ReadBigInt() (ret *big.Int) {
|
||||||
|
str := iter.readNumberAsString()
|
||||||
|
if iter.Error != nil && iter.Error != io.EOF {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
ret = big.NewInt(0)
|
||||||
|
var success bool
|
||||||
|
ret, success = ret.SetString(str, 10)
|
||||||
|
if !success {
|
||||||
|
iter.ReportError("ReadBigInt", "invalid big int")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
func (iter *Iterator) ReadFloat32() (ret float32) {
|
func (iter *Iterator) ReadFloat32() (ret float32) {
|
||||||
@ -40,7 +76,7 @@ func (iter *Iterator) readPositiveFloat32() (ret float32) {
|
|||||||
value := uint64(0)
|
value := uint64(0)
|
||||||
c := byte(' ')
|
c := byte(' ')
|
||||||
i := iter.head
|
i := iter.head
|
||||||
non_decimal_loop:
|
non_decimal_loop:
|
||||||
for ; i < iter.tail; i++ {
|
for ; i < iter.tail; i++ {
|
||||||
c = iter.buf[i]
|
c = iter.buf[i]
|
||||||
ind := floatDigits[c]
|
ind := floatDigits[c]
|
||||||
@ -56,22 +92,22 @@ func (iter *Iterator) readPositiveFloat32() (ret float32) {
|
|||||||
if value > uint64SafeToMultiple10 {
|
if value > uint64SafeToMultiple10 {
|
||||||
return iter.readFloat32SlowPath()
|
return iter.readFloat32SlowPath()
|
||||||
}
|
}
|
||||||
value = (value << 3) + (value << 1) + uint64(ind); // value = value * 10 + ind;
|
value = (value << 3) + (value << 1) + uint64(ind) // value = value * 10 + ind;
|
||||||
}
|
}
|
||||||
if c == '.' {
|
if c == '.' {
|
||||||
i++
|
i++
|
||||||
decimalPlaces := 0;
|
decimalPlaces := 0
|
||||||
for ; i < iter.tail; i++ {
|
for ; i < iter.tail; i++ {
|
||||||
c = iter.buf[i]
|
c = iter.buf[i]
|
||||||
ind := floatDigits[c];
|
ind := floatDigits[c]
|
||||||
switch ind {
|
switch ind {
|
||||||
case endOfNumber:
|
case endOfNumber:
|
||||||
if decimalPlaces > 0 && decimalPlaces < len(POW10) {
|
if decimalPlaces > 0 && decimalPlaces < len(_POW10) {
|
||||||
iter.head = i
|
iter.head = i
|
||||||
return float32(float64(value) / float64(POW10[decimalPlaces]))
|
return float32(float64(value) / float64(_POW10[decimalPlaces]))
|
||||||
}
|
}
|
||||||
// too many decimal places
|
// too many decimal places
|
||||||
return iter.readFloat32SlowPath()
|
return iter.readFloat32SlowPath()
|
||||||
case invalidCharForNumber:
|
case invalidCharForNumber:
|
||||||
fallthrough
|
fallthrough
|
||||||
case dotInNumber:
|
case dotInNumber:
|
||||||
@ -87,10 +123,10 @@ func (iter *Iterator) readPositiveFloat32() (ret float32) {
|
|||||||
return iter.readFloat32SlowPath()
|
return iter.readFloat32SlowPath()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (iter *Iterator) readFloat32SlowPath() (ret float32) {
|
func (iter *Iterator) readNumberAsString() (ret string) {
|
||||||
strBuf := [16]byte{}
|
strBuf := [16]byte{}
|
||||||
str := strBuf[0:0]
|
str := strBuf[0:0]
|
||||||
load_loop:
|
load_loop:
|
||||||
for {
|
for {
|
||||||
for i := iter.head; i < iter.tail; i++ {
|
for i := iter.head; i < iter.tail; i++ {
|
||||||
c := iter.buf[i]
|
c := iter.buf[i]
|
||||||
@ -99,6 +135,7 @@ func (iter *Iterator) readFloat32SlowPath() (ret float32) {
|
|||||||
str = append(str, c)
|
str = append(str, c)
|
||||||
continue
|
continue
|
||||||
default:
|
default:
|
||||||
|
iter.head = i
|
||||||
break load_loop
|
break load_loop
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -109,7 +146,18 @@ func (iter *Iterator) readFloat32SlowPath() (ret float32) {
|
|||||||
if iter.Error != nil && iter.Error != io.EOF {
|
if iter.Error != nil && iter.Error != io.EOF {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
val, err := strconv.ParseFloat(*(*string)(unsafe.Pointer(&str)), 32)
|
if len(str) == 0 {
|
||||||
|
iter.ReportError("readNumberAsString", "invalid number")
|
||||||
|
}
|
||||||
|
return *(*string)(unsafe.Pointer(&str))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (iter *Iterator) readFloat32SlowPath() (ret float32) {
|
||||||
|
str := iter.readNumberAsString()
|
||||||
|
if iter.Error != nil && iter.Error != io.EOF {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
val, err := strconv.ParseFloat(str, 32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
iter.Error = err
|
iter.Error = err
|
||||||
return
|
return
|
||||||
@ -131,7 +179,7 @@ func (iter *Iterator) readPositiveFloat64() (ret float64) {
|
|||||||
value := uint64(0)
|
value := uint64(0)
|
||||||
c := byte(' ')
|
c := byte(' ')
|
||||||
i := iter.head
|
i := iter.head
|
||||||
non_decimal_loop:
|
non_decimal_loop:
|
||||||
for ; i < iter.tail; i++ {
|
for ; i < iter.tail; i++ {
|
||||||
c = iter.buf[i]
|
c = iter.buf[i]
|
||||||
ind := floatDigits[c]
|
ind := floatDigits[c]
|
||||||
@ -147,19 +195,19 @@ func (iter *Iterator) readPositiveFloat64() (ret float64) {
|
|||||||
if value > uint64SafeToMultiple10 {
|
if value > uint64SafeToMultiple10 {
|
||||||
return iter.readFloat64SlowPath()
|
return iter.readFloat64SlowPath()
|
||||||
}
|
}
|
||||||
value = (value << 3) + (value << 1) + uint64(ind); // value = value * 10 + ind;
|
value = (value << 3) + (value << 1) + uint64(ind) // value = value * 10 + ind;
|
||||||
}
|
}
|
||||||
if c == '.' {
|
if c == '.' {
|
||||||
i++
|
i++
|
||||||
decimalPlaces := 0;
|
decimalPlaces := 0
|
||||||
for ; i < iter.tail; i++ {
|
for ; i < iter.tail; i++ {
|
||||||
c = iter.buf[i]
|
c = iter.buf[i]
|
||||||
ind := floatDigits[c];
|
ind := floatDigits[c]
|
||||||
switch ind {
|
switch ind {
|
||||||
case endOfNumber:
|
case endOfNumber:
|
||||||
if decimalPlaces > 0 && decimalPlaces < len(POW10) {
|
if decimalPlaces > 0 && decimalPlaces < len(_POW10) {
|
||||||
iter.head = i
|
iter.head = i
|
||||||
return float64(value) / float64(POW10[decimalPlaces])
|
return float64(value) / float64(_POW10[decimalPlaces])
|
||||||
}
|
}
|
||||||
// too many decimal places
|
// too many decimal places
|
||||||
return iter.readFloat64SlowPath()
|
return iter.readFloat64SlowPath()
|
||||||
@ -179,28 +227,11 @@ func (iter *Iterator) readPositiveFloat64() (ret float64) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (iter *Iterator) readFloat64SlowPath() (ret float64) {
|
func (iter *Iterator) readFloat64SlowPath() (ret float64) {
|
||||||
strBuf := [16]byte{}
|
str := iter.readNumberAsString()
|
||||||
str := strBuf[0:0]
|
|
||||||
load_loop:
|
|
||||||
for {
|
|
||||||
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':
|
|
||||||
str = append(str, c)
|
|
||||||
continue
|
|
||||||
default:
|
|
||||||
break load_loop
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !iter.loadMore() {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if iter.Error != nil && iter.Error != io.EOF {
|
if iter.Error != nil && iter.Error != io.EOF {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
val, err := strconv.ParseFloat(*(*string)(unsafe.Pointer(&str)), 64)
|
val, err := strconv.ParseFloat(str, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
iter.Error = err
|
iter.Error = err
|
||||||
return
|
return
|
||||||
|
@ -1,23 +1,18 @@
|
|||||||
package jsoniter
|
package jsoniter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"math"
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
var intDigits []int8
|
var intDigits []int8
|
||||||
|
|
||||||
const uint32SafeToMultiply10 = uint32(0xffffffff) / 10 - 1
|
const uint32SafeToMultiply10 = uint32(0xffffffff)/10 - 1
|
||||||
const uint64SafeToMultiple10 = uint64(0xffffffffffffffff) / 10 - 1
|
const uint64SafeToMultiple10 = uint64(0xffffffffffffffff)/10 - 1
|
||||||
const int64Max = uint64(0x7fffffffffffffff)
|
|
||||||
const int32Max = uint32(0x7fffffff)
|
|
||||||
const int16Max = uint32(0x7fff)
|
|
||||||
const uint16Max = uint32(0xffff)
|
|
||||||
const int8Max = uint32(0x7fff)
|
|
||||||
const uint8Max = uint32(0xffff)
|
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
intDigits = make([]int8, 256)
|
intDigits = make([]int8, 256)
|
||||||
for i := 0; i < len(floatDigits); i++ {
|
for i := 0; i < len(intDigits); i++ {
|
||||||
intDigits[i] = invalidCharForNumber
|
intDigits[i] = invalidCharForNumber
|
||||||
}
|
}
|
||||||
for i := int8('0'); i <= int8('9'); i++ {
|
for i := int8('0'); i <= int8('9'); i++ {
|
||||||
@ -37,15 +32,15 @@ func (iter *Iterator) ReadInt8() (ret int8) {
|
|||||||
c := iter.nextToken()
|
c := iter.nextToken()
|
||||||
if c == '-' {
|
if c == '-' {
|
||||||
val := iter.readUint32(iter.readByte())
|
val := iter.readUint32(iter.readByte())
|
||||||
if val > int8Max + 1 {
|
if val > math.MaxInt8+1 {
|
||||||
iter.reportError("ReadInt8", "overflow: " + strconv.FormatInt(int64(val), 10))
|
iter.ReportError("ReadInt8", "overflow: "+strconv.FormatInt(int64(val), 10))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
return -int8(val)
|
return -int8(val)
|
||||||
} else {
|
} else {
|
||||||
val := iter.readUint32(c)
|
val := iter.readUint32(c)
|
||||||
if val > int8Max {
|
if val > math.MaxInt8 {
|
||||||
iter.reportError("ReadInt8", "overflow: " + strconv.FormatInt(int64(val), 10))
|
iter.ReportError("ReadInt8", "overflow: "+strconv.FormatInt(int64(val), 10))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
return int8(val)
|
return int8(val)
|
||||||
@ -54,8 +49,8 @@ func (iter *Iterator) ReadInt8() (ret int8) {
|
|||||||
|
|
||||||
func (iter *Iterator) ReadUint8() (ret uint8) {
|
func (iter *Iterator) ReadUint8() (ret uint8) {
|
||||||
val := iter.readUint32(iter.nextToken())
|
val := iter.readUint32(iter.nextToken())
|
||||||
if val > uint8Max {
|
if val > math.MaxUint8 {
|
||||||
iter.reportError("ReadUint8", "overflow: " + strconv.FormatInt(int64(val), 10))
|
iter.ReportError("ReadUint8", "overflow: "+strconv.FormatInt(int64(val), 10))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
return uint8(val)
|
return uint8(val)
|
||||||
@ -65,15 +60,15 @@ func (iter *Iterator) ReadInt16() (ret int16) {
|
|||||||
c := iter.nextToken()
|
c := iter.nextToken()
|
||||||
if c == '-' {
|
if c == '-' {
|
||||||
val := iter.readUint32(iter.readByte())
|
val := iter.readUint32(iter.readByte())
|
||||||
if val > int16Max + 1 {
|
if val > math.MaxInt16+1 {
|
||||||
iter.reportError("ReadInt16", "overflow: " + strconv.FormatInt(int64(val), 10))
|
iter.ReportError("ReadInt16", "overflow: "+strconv.FormatInt(int64(val), 10))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
return -int16(val)
|
return -int16(val)
|
||||||
} else {
|
} else {
|
||||||
val := iter.readUint32(c)
|
val := iter.readUint32(c)
|
||||||
if val > int16Max {
|
if val > math.MaxInt16 {
|
||||||
iter.reportError("ReadInt16", "overflow: " + strconv.FormatInt(int64(val), 10))
|
iter.ReportError("ReadInt16", "overflow: "+strconv.FormatInt(int64(val), 10))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
return int16(val)
|
return int16(val)
|
||||||
@ -82,8 +77,8 @@ func (iter *Iterator) ReadInt16() (ret int16) {
|
|||||||
|
|
||||||
func (iter *Iterator) ReadUint16() (ret uint16) {
|
func (iter *Iterator) ReadUint16() (ret uint16) {
|
||||||
val := iter.readUint32(iter.nextToken())
|
val := iter.readUint32(iter.nextToken())
|
||||||
if val > uint16Max {
|
if val > math.MaxUint16 {
|
||||||
iter.reportError("ReadUint16", "overflow: " + strconv.FormatInt(int64(val), 10))
|
iter.ReportError("ReadUint16", "overflow: "+strconv.FormatInt(int64(val), 10))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
return uint16(val)
|
return uint16(val)
|
||||||
@ -93,15 +88,15 @@ func (iter *Iterator) ReadInt32() (ret int32) {
|
|||||||
c := iter.nextToken()
|
c := iter.nextToken()
|
||||||
if c == '-' {
|
if c == '-' {
|
||||||
val := iter.readUint32(iter.readByte())
|
val := iter.readUint32(iter.readByte())
|
||||||
if val > int32Max + 1 {
|
if val > math.MaxInt32+1 {
|
||||||
iter.reportError("ReadInt32", "overflow: " + strconv.FormatInt(int64(val), 10))
|
iter.ReportError("ReadInt32", "overflow: "+strconv.FormatInt(int64(val), 10))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
return -int32(val)
|
return -int32(val)
|
||||||
} else {
|
} else {
|
||||||
val := iter.readUint32(c)
|
val := iter.readUint32(c)
|
||||||
if val > int32Max {
|
if val > math.MaxInt32 {
|
||||||
iter.reportError("ReadInt32", "overflow: " + strconv.FormatInt(int64(val), 10))
|
iter.ReportError("ReadInt32", "overflow: "+strconv.FormatInt(int64(val), 10))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
return int32(val)
|
return int32(val)
|
||||||
@ -118,11 +113,11 @@ func (iter *Iterator) readUint32(c byte) (ret uint32) {
|
|||||||
return 0 // single zero
|
return 0 // single zero
|
||||||
}
|
}
|
||||||
if ind == invalidCharForNumber {
|
if ind == invalidCharForNumber {
|
||||||
iter.reportError("readUint32", "unexpected character: " + string([]byte{byte(ind)}))
|
iter.ReportError("readUint32", "unexpected character: "+string([]byte{byte(ind)}))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
value := uint32(ind)
|
value := uint32(ind)
|
||||||
if iter.tail - iter.head > 10 {
|
if iter.tail-iter.head > 10 {
|
||||||
i := iter.head
|
i := iter.head
|
||||||
ind2 := intDigits[iter.buf[i]]
|
ind2 := intDigits[iter.buf[i]]
|
||||||
if ind2 == invalidCharForNumber {
|
if ind2 == invalidCharForNumber {
|
||||||
@ -133,7 +128,7 @@ func (iter *Iterator) readUint32(c byte) (ret uint32) {
|
|||||||
ind3 := intDigits[iter.buf[i]]
|
ind3 := intDigits[iter.buf[i]]
|
||||||
if ind3 == invalidCharForNumber {
|
if ind3 == invalidCharForNumber {
|
||||||
iter.head = i
|
iter.head = i
|
||||||
return value * 10 + uint32(ind2)
|
return value*10 + uint32(ind2)
|
||||||
}
|
}
|
||||||
//iter.head = i + 1
|
//iter.head = i + 1
|
||||||
//value = value * 100 + uint32(ind2) * 10 + uint32(ind3)
|
//value = value * 100 + uint32(ind2) * 10 + uint32(ind3)
|
||||||
@ -141,35 +136,35 @@ func (iter *Iterator) readUint32(c byte) (ret uint32) {
|
|||||||
ind4 := intDigits[iter.buf[i]]
|
ind4 := intDigits[iter.buf[i]]
|
||||||
if ind4 == invalidCharForNumber {
|
if ind4 == invalidCharForNumber {
|
||||||
iter.head = i
|
iter.head = i
|
||||||
return value * 100 + uint32(ind2) * 10 + uint32(ind3)
|
return value*100 + uint32(ind2)*10 + uint32(ind3)
|
||||||
}
|
}
|
||||||
i++
|
i++
|
||||||
ind5 := intDigits[iter.buf[i]]
|
ind5 := intDigits[iter.buf[i]]
|
||||||
if ind5 == invalidCharForNumber {
|
if ind5 == invalidCharForNumber {
|
||||||
iter.head = i
|
iter.head = i
|
||||||
return value * 1000 + uint32(ind2) * 100 + uint32(ind3) * 10 + uint32(ind4)
|
return value*1000 + uint32(ind2)*100 + uint32(ind3)*10 + uint32(ind4)
|
||||||
}
|
}
|
||||||
i++
|
i++
|
||||||
ind6 := intDigits[iter.buf[i]]
|
ind6 := intDigits[iter.buf[i]]
|
||||||
if ind6 == invalidCharForNumber {
|
if ind6 == invalidCharForNumber {
|
||||||
iter.head = i
|
iter.head = i
|
||||||
return value * 10000 + uint32(ind2) * 1000 + uint32(ind3) * 100 + uint32(ind4) * 10 + uint32(ind5)
|
return value*10000 + uint32(ind2)*1000 + uint32(ind3)*100 + uint32(ind4)*10 + uint32(ind5)
|
||||||
}
|
}
|
||||||
i++
|
i++
|
||||||
ind7 := intDigits[iter.buf[i]]
|
ind7 := intDigits[iter.buf[i]]
|
||||||
if ind7 == invalidCharForNumber {
|
if ind7 == invalidCharForNumber {
|
||||||
iter.head = i
|
iter.head = i
|
||||||
return value * 100000 + uint32(ind2) * 10000 + uint32(ind3) * 1000 + uint32(ind4) * 100 + uint32(ind5) * 10 + uint32(ind6)
|
return value*100000 + uint32(ind2)*10000 + uint32(ind3)*1000 + uint32(ind4)*100 + uint32(ind5)*10 + uint32(ind6)
|
||||||
}
|
}
|
||||||
i++
|
i++
|
||||||
ind8 := intDigits[iter.buf[i]]
|
ind8 := intDigits[iter.buf[i]]
|
||||||
if ind8 == invalidCharForNumber {
|
if ind8 == invalidCharForNumber {
|
||||||
iter.head = i
|
iter.head = i
|
||||||
return value * 1000000 + uint32(ind2) * 100000 + uint32(ind3) * 10000 + uint32(ind4) * 1000 + uint32(ind5) * 100 + uint32(ind6) * 10 + uint32(ind7)
|
return value*1000000 + uint32(ind2)*100000 + uint32(ind3)*10000 + uint32(ind4)*1000 + uint32(ind5)*100 + uint32(ind6)*10 + uint32(ind7)
|
||||||
}
|
}
|
||||||
i++
|
i++
|
||||||
ind9 := intDigits[iter.buf[i]]
|
ind9 := intDigits[iter.buf[i]]
|
||||||
value = value * 10000000 + uint32(ind2) * 1000000 + uint32(ind3) * 100000 + uint32(ind4) * 10000 + uint32(ind5) * 1000 + uint32(ind6) * 100 + uint32(ind7) * 10 + uint32(ind8)
|
value = value*10000000 + uint32(ind2)*1000000 + uint32(ind3)*100000 + uint32(ind4)*10000 + uint32(ind5)*1000 + uint32(ind6)*100 + uint32(ind7)*10 + uint32(ind8)
|
||||||
iter.head = i
|
iter.head = i
|
||||||
if ind9 == invalidCharForNumber {
|
if ind9 == invalidCharForNumber {
|
||||||
return value
|
return value
|
||||||
@ -185,7 +180,7 @@ func (iter *Iterator) readUint32(c byte) (ret uint32) {
|
|||||||
if value > uint32SafeToMultiply10 {
|
if value > uint32SafeToMultiply10 {
|
||||||
value2 := (value << 3) + (value << 1) + uint32(ind)
|
value2 := (value << 3) + (value << 1) + uint32(ind)
|
||||||
if value2 < value {
|
if value2 < value {
|
||||||
iter.reportError("readUint32", "overflow")
|
iter.ReportError("readUint32", "overflow")
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
value = value2
|
value = value2
|
||||||
@ -194,7 +189,7 @@ func (iter *Iterator) readUint32(c byte) (ret uint32) {
|
|||||||
}
|
}
|
||||||
value = (value << 3) + (value << 1) + uint32(ind)
|
value = (value << 3) + (value << 1) + uint32(ind)
|
||||||
}
|
}
|
||||||
if (!iter.loadMore()) {
|
if !iter.loadMore() {
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -204,15 +199,15 @@ func (iter *Iterator) ReadInt64() (ret int64) {
|
|||||||
c := iter.nextToken()
|
c := iter.nextToken()
|
||||||
if c == '-' {
|
if c == '-' {
|
||||||
val := iter.readUint64(iter.readByte())
|
val := iter.readUint64(iter.readByte())
|
||||||
if val > int64Max + 1 {
|
if val > math.MaxInt64+1 {
|
||||||
iter.reportError("ReadInt64", "overflow: " + strconv.FormatUint(uint64(val), 10))
|
iter.ReportError("ReadInt64", "overflow: "+strconv.FormatUint(uint64(val), 10))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
return -int64(val)
|
return -int64(val)
|
||||||
} else {
|
} else {
|
||||||
val := iter.readUint64(c)
|
val := iter.readUint64(c)
|
||||||
if val > int64Max {
|
if val > math.MaxInt64 {
|
||||||
iter.reportError("ReadInt64", "overflow: " + strconv.FormatUint(uint64(val), 10))
|
iter.ReportError("ReadInt64", "overflow: "+strconv.FormatUint(uint64(val), 10))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
return int64(val)
|
return int64(val)
|
||||||
@ -229,7 +224,7 @@ func (iter *Iterator) readUint64(c byte) (ret uint64) {
|
|||||||
return 0 // single zero
|
return 0 // single zero
|
||||||
}
|
}
|
||||||
if ind == invalidCharForNumber {
|
if ind == invalidCharForNumber {
|
||||||
iter.reportError("readUint64", "unexpected character: " + string([]byte{byte(ind)}))
|
iter.ReportError("readUint64", "unexpected character: "+string([]byte{byte(ind)}))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
value := uint64(ind)
|
value := uint64(ind)
|
||||||
@ -243,7 +238,7 @@ func (iter *Iterator) readUint64(c byte) (ret uint64) {
|
|||||||
if value > uint64SafeToMultiple10 {
|
if value > uint64SafeToMultiple10 {
|
||||||
value2 := (value << 3) + (value << 1) + uint64(ind)
|
value2 := (value << 3) + (value << 1) + uint64(ind)
|
||||||
if value2 < value {
|
if value2 < value {
|
||||||
iter.reportError("readUint64", "overflow")
|
iter.ReportError("readUint64", "overflow")
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
value = value2
|
value = value2
|
||||||
@ -252,7 +247,7 @@ func (iter *Iterator) readUint64(c byte) (ret uint64) {
|
|||||||
}
|
}
|
||||||
value = (value << 3) + (value << 1) + uint64(ind)
|
value = (value << 3) + (value << 1) + uint64(ind)
|
||||||
}
|
}
|
||||||
if (!iter.loadMore()) {
|
if !iter.loadMore() {
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,11 @@
|
|||||||
package jsoniter
|
package jsoniter
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"unicode"
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
func (iter *Iterator) ReadObject() (ret string) {
|
func (iter *Iterator) ReadObject() (ret string) {
|
||||||
c := iter.nextToken()
|
c := iter.nextToken()
|
||||||
switch c {
|
switch c {
|
||||||
@ -15,14 +21,14 @@ func (iter *Iterator) ReadObject() (ret string) {
|
|||||||
if c == '}' {
|
if c == '}' {
|
||||||
return "" // end of object
|
return "" // end of object
|
||||||
}
|
}
|
||||||
iter.reportError("ReadObject", `expect " after {`)
|
iter.ReportError("ReadObject", `expect " after {`)
|
||||||
return
|
return
|
||||||
case ',':
|
case ',':
|
||||||
return string(iter.readObjectFieldAsBytes())
|
return string(iter.readObjectFieldAsBytes())
|
||||||
case '}':
|
case '}':
|
||||||
return "" // end of object
|
return "" // end of object
|
||||||
default:
|
default:
|
||||||
iter.reportError("ReadObject", `expect { or , or } or n`)
|
iter.ReportError("ReadObject", fmt.Sprintf(`expect { or , or } or n, but found %s`, string([]byte{c})))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -35,11 +41,14 @@ func (iter *Iterator) readFieldHash() int32 {
|
|||||||
for i := iter.head; i < iter.tail; i++ {
|
for i := iter.head; i < iter.tail; i++ {
|
||||||
// require ascii string and no escape
|
// require ascii string and no escape
|
||||||
b := iter.buf[i]
|
b := iter.buf[i]
|
||||||
|
if 'A' <= b && b <= 'Z' {
|
||||||
|
b += 'a' - 'A'
|
||||||
|
}
|
||||||
if b == '"' {
|
if b == '"' {
|
||||||
iter.head = i+1
|
iter.head = i + 1
|
||||||
c = iter.nextToken()
|
c = iter.nextToken()
|
||||||
if c != ':' {
|
if c != ':' {
|
||||||
iter.reportError("readFieldHash", `expect :, but found ` + string([]byte{c}))
|
iter.ReportError("readFieldHash", `expect :, but found `+string([]byte{c}))
|
||||||
}
|
}
|
||||||
return int32(hash)
|
return int32(hash)
|
||||||
}
|
}
|
||||||
@ -47,19 +56,19 @@ func (iter *Iterator) readFieldHash() int32 {
|
|||||||
hash *= 0x1000193
|
hash *= 0x1000193
|
||||||
}
|
}
|
||||||
if !iter.loadMore() {
|
if !iter.loadMore() {
|
||||||
iter.reportError("readFieldHash", `incomplete field name`)
|
iter.ReportError("readFieldHash", `incomplete field name`)
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
iter.reportError("readFieldHash", `expect ", but found ` + string([]byte{c}))
|
iter.ReportError("readFieldHash", `expect ", but found `+string([]byte{c}))
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func calcHash(str string) int32 {
|
func calcHash(str string) int32 {
|
||||||
hash := int64(0x811c9dc5)
|
hash := int64(0x811c9dc5)
|
||||||
for _, b := range str {
|
for _, b := range str {
|
||||||
hash ^= int64(b)
|
hash ^= int64(unicode.ToLower(b))
|
||||||
hash *= 0x1000193
|
hash *= 0x1000193
|
||||||
}
|
}
|
||||||
return int32(hash)
|
return int32(hash)
|
||||||
@ -71,12 +80,52 @@ func (iter *Iterator) ReadObjectCB(callback func(*Iterator, string) bool) bool {
|
|||||||
c = iter.nextToken()
|
c = iter.nextToken()
|
||||||
if c == '"' {
|
if c == '"' {
|
||||||
iter.unreadByte()
|
iter.unreadByte()
|
||||||
field := string(iter.readObjectFieldAsBytes())
|
field := iter.readObjectFieldAsBytes()
|
||||||
|
if !callback(iter, *(*string)(unsafe.Pointer(&field))) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for iter.nextToken() == ',' {
|
||||||
|
field = iter.readObjectFieldAsBytes()
|
||||||
|
if !callback(iter, *(*string)(unsafe.Pointer(&field))) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if c == '}' {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
iter.ReportError("ReadObjectCB", `expect " after }`)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if c == 'n' {
|
||||||
|
iter.skipFixedBytes(3)
|
||||||
|
return true // null
|
||||||
|
}
|
||||||
|
iter.ReportError("ReadObjectCB", `expect { or n`)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (iter *Iterator) ReadMapCB(callback func(*Iterator, string) bool) bool {
|
||||||
|
c := iter.nextToken()
|
||||||
|
if c == '{' {
|
||||||
|
c = iter.nextToken()
|
||||||
|
if c == '"' {
|
||||||
|
iter.unreadByte()
|
||||||
|
field := iter.ReadString()
|
||||||
|
if iter.nextToken() != ':' {
|
||||||
|
iter.ReportError("ReadMapCB", "expect : after object field")
|
||||||
|
return false
|
||||||
|
}
|
||||||
if !callback(iter, field) {
|
if !callback(iter, field) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
for iter.nextToken() == ',' {
|
for iter.nextToken() == ',' {
|
||||||
field := string(iter.readObjectFieldAsBytes())
|
field = iter.ReadString()
|
||||||
|
if iter.nextToken() != ':' {
|
||||||
|
iter.ReportError("ReadMapCB", "expect : after object field")
|
||||||
|
return false
|
||||||
|
}
|
||||||
if !callback(iter, field) {
|
if !callback(iter, field) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@ -86,14 +135,14 @@ func (iter *Iterator) ReadObjectCB(callback func(*Iterator, string) bool) bool {
|
|||||||
if c == '}' {
|
if c == '}' {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
iter.reportError("ReadObjectCB", `expect " after }`)
|
iter.ReportError("ReadMapCB", `expect " after }`)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if c == 'n' {
|
if c == 'n' {
|
||||||
iter.skipFixedBytes(3)
|
iter.skipFixedBytes(3)
|
||||||
return true // null
|
return true // null
|
||||||
}
|
}
|
||||||
iter.reportError("ReadObjectCB", `expect { or n`)
|
iter.ReportError("ReadMapCB", `expect { or n`)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -106,8 +155,11 @@ func (iter *Iterator) readObjectStart() bool {
|
|||||||
}
|
}
|
||||||
iter.unreadByte()
|
iter.unreadByte()
|
||||||
return true
|
return true
|
||||||
|
} else if c == 'n' {
|
||||||
|
iter.skipFixedBytes(3)
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
iter.reportError("readObjectStart", "expect { ")
|
iter.ReportError("readObjectStart", "expect { or n")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -123,7 +175,7 @@ func (iter *Iterator) readObjectFieldAsBytes() (ret []byte) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if iter.buf[iter.head] != ':' {
|
if iter.buf[iter.head] != ':' {
|
||||||
iter.reportError("readObjectFieldAsBytes", "expect : after object field")
|
iter.ReportError("readObjectFieldAsBytes", "expect : after object field")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
iter.head++
|
iter.head++
|
||||||
|
@ -25,10 +25,44 @@ func (iter *Iterator) ReadBool() (ret bool) {
|
|||||||
iter.skipFixedBytes(4)
|
iter.skipFixedBytes(4)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
iter.reportError("ReadBool", "expect t or f")
|
iter.ReportError("ReadBool", "expect t or f")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (iter *Iterator) SkipAndReturnBytes() []byte {
|
||||||
|
iter.startCapture(iter.head)
|
||||||
|
iter.Skip()
|
||||||
|
return iter.stopCapture()
|
||||||
|
}
|
||||||
|
|
||||||
|
type captureBuffer struct {
|
||||||
|
startedAt int
|
||||||
|
captured []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func (iter *Iterator) startCapture(captureStartedAt int) {
|
||||||
|
if iter.captured != nil {
|
||||||
|
panic("already in capture mode")
|
||||||
|
}
|
||||||
|
iter.captureStartedAt = captureStartedAt
|
||||||
|
iter.captured = make([]byte, 0, 32)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (iter *Iterator) stopCapture() []byte {
|
||||||
|
if iter.captured == nil {
|
||||||
|
panic("not in capture mode")
|
||||||
|
}
|
||||||
|
captured := iter.captured
|
||||||
|
remaining := iter.buf[iter.captureStartedAt:iter.head]
|
||||||
|
iter.captureStartedAt = -1
|
||||||
|
iter.captured = nil
|
||||||
|
if len(captured) == 0 {
|
||||||
|
return remaining
|
||||||
|
} else {
|
||||||
|
captured = append(captured, remaining...)
|
||||||
|
return captured
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Skip skips a json object and positions to relatively the next json object
|
// Skip skips a json object and positions to relatively the next json object
|
||||||
func (iter *Iterator) Skip() {
|
func (iter *Iterator) Skip() {
|
||||||
@ -41,13 +75,13 @@ func (iter *Iterator) Skip() {
|
|||||||
case 'f':
|
case 'f':
|
||||||
iter.skipFixedBytes(4) // false
|
iter.skipFixedBytes(4) // false
|
||||||
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
|
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
|
||||||
iter.skipUntilBreak()
|
iter.skipNumber()
|
||||||
case '[':
|
case '[':
|
||||||
iter.skipArray()
|
iter.skipArray()
|
||||||
case '{':
|
case '{':
|
||||||
iter.skipObject()
|
iter.skipObject()
|
||||||
default:
|
default:
|
||||||
iter.reportError("Skip", fmt.Sprintf("do not know how to skip: %v", c))
|
iter.ReportError("Skip", fmt.Sprintf("do not know how to skip: %v", c))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -57,7 +91,7 @@ func (iter *Iterator) skipString() {
|
|||||||
end, escaped := iter.findStringEnd()
|
end, escaped := iter.findStringEnd()
|
||||||
if end == -1 {
|
if end == -1 {
|
||||||
if !iter.loadMore() {
|
if !iter.loadMore() {
|
||||||
iter.reportError("skipString", "incomplete string")
|
iter.ReportError("skipString", "incomplete string")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if escaped {
|
if escaped {
|
||||||
@ -141,7 +175,7 @@ func (iter *Iterator) skipArray() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !iter.loadMore() {
|
if !iter.loadMore() {
|
||||||
iter.reportError("skipObject", "incomplete array")
|
iter.ReportError("skipObject", "incomplete array")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -169,14 +203,13 @@ func (iter *Iterator) skipObject() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !iter.loadMore() {
|
if !iter.loadMore() {
|
||||||
iter.reportError("skipObject", "incomplete object")
|
iter.ReportError("skipObject", "incomplete object")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (iter *Iterator) skipUntilBreak() {
|
func (iter *Iterator) skipNumber() {
|
||||||
// true, false, null, number
|
|
||||||
for {
|
for {
|
||||||
for i := iter.head; i < iter.tail; i++ {
|
for i := iter.head; i < iter.tail; i++ {
|
||||||
c := iter.buf[i]
|
c := iter.buf[i]
|
||||||
@ -193,15 +226,15 @@ func (iter *Iterator) skipUntilBreak() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (iter *Iterator) skipFixedBytes(n int) {
|
func (iter *Iterator) skipFixedBytes(n int) {
|
||||||
iter.head += n;
|
iter.head += n
|
||||||
if (iter.head >= iter.tail) {
|
if iter.head >= iter.tail {
|
||||||
more := iter.head - iter.tail;
|
more := iter.head - iter.tail
|
||||||
if !iter.loadMore() {
|
if !iter.loadMore() {
|
||||||
if more > 0 {
|
if more > 0 {
|
||||||
iter.reportError("skipFixedBytes", "unexpected end");
|
iter.ReportError("skipFixedBytes", "unexpected end")
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
iter.head += more;
|
iter.head += more
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -2,64 +2,42 @@ package jsoniter
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"unicode/utf16"
|
"unicode/utf16"
|
||||||
"unsafe"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO: avoid append
|
|
||||||
func (iter *Iterator) ReadString() (ret string) {
|
func (iter *Iterator) ReadString() (ret string) {
|
||||||
c := iter.nextToken()
|
c := iter.nextToken()
|
||||||
if c == '"' {
|
if c == '"' {
|
||||||
copied := make([]byte, 32)
|
for i := iter.head; i < iter.tail; i++ {
|
||||||
j := 0
|
c := iter.buf[i]
|
||||||
fast_loop:
|
if c == '"' {
|
||||||
for {
|
ret = string(iter.buf[iter.head:i])
|
||||||
i := iter.head
|
iter.head = i + 1
|
||||||
for ; i < iter.tail && j < len(copied); i++ {
|
return ret
|
||||||
c := iter.buf[i]
|
} else if c == '\\' {
|
||||||
if c == '"' {
|
break
|
||||||
iter.head = i + 1
|
|
||||||
copied = copied[:j]
|
|
||||||
return *(*string)(unsafe.Pointer(&copied))
|
|
||||||
} else if c == '\\' {
|
|
||||||
iter.head = i
|
|
||||||
break fast_loop
|
|
||||||
}
|
|
||||||
copied[j] = c
|
|
||||||
j++
|
|
||||||
}
|
|
||||||
if i == iter.tail {
|
|
||||||
if iter.loadMore() {
|
|
||||||
i = iter.head
|
|
||||||
continue
|
|
||||||
} else {
|
|
||||||
iter.reportError("ReadString", "incomplete string")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
iter.head = i
|
|
||||||
if j == len(copied) {
|
|
||||||
newBuf := make([]byte, len(copied) * 2)
|
|
||||||
copy(newBuf, copied)
|
|
||||||
copied = newBuf
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return iter.readStringSlowPath(copied[:j])
|
return iter.readStringSlowPath()
|
||||||
|
} else if c == 'n' {
|
||||||
|
iter.skipFixedBytes(3)
|
||||||
|
return ""
|
||||||
}
|
}
|
||||||
iter.reportError("ReadString", `expects " or n`)
|
iter.ReportError("ReadString", `expects " or n`)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (iter *Iterator) readStringSlowPath(str []byte) (ret string) {
|
func (iter *Iterator) readStringSlowPath() (ret string) {
|
||||||
|
var str []byte
|
||||||
var c byte
|
var c byte
|
||||||
for iter.Error == nil {
|
for iter.Error == nil {
|
||||||
c = iter.readByte()
|
c = iter.readByte()
|
||||||
if c == '"' {
|
if c == '"' {
|
||||||
return *(*string)(unsafe.Pointer(&str))
|
return string(str)
|
||||||
}
|
}
|
||||||
if c == '\\' {
|
if c == '\\' {
|
||||||
c = iter.readByte()
|
c = iter.readByte()
|
||||||
switch c {
|
switch c {
|
||||||
case 'u':
|
case 'u', 'U':
|
||||||
r := iter.readU4()
|
r := iter.readU4()
|
||||||
if utf16.IsSurrogate(r) {
|
if utf16.IsSurrogate(r) {
|
||||||
c = iter.readByte()
|
c = iter.readByte()
|
||||||
@ -67,7 +45,7 @@ func (iter *Iterator) readStringSlowPath(str []byte) (ret string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if c != '\\' {
|
if c != '\\' {
|
||||||
iter.reportError("ReadString",
|
iter.ReportError("ReadString",
|
||||||
`expects \u after utf16 surrogate, but \ not found`)
|
`expects \u after utf16 surrogate, but \ not found`)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -75,8 +53,8 @@ func (iter *Iterator) readStringSlowPath(str []byte) (ret string) {
|
|||||||
if iter.Error != nil {
|
if iter.Error != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if c != 'u' {
|
if c != 'u' && c != 'U' {
|
||||||
iter.reportError("ReadString",
|
iter.ReportError("ReadString",
|
||||||
`expects \u after utf16 surrogate, but \u not found`)
|
`expects \u after utf16 surrogate, but \u not found`)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -106,7 +84,7 @@ func (iter *Iterator) readStringSlowPath(str []byte) (ret string) {
|
|||||||
case 't':
|
case 't':
|
||||||
str = append(str, '\t')
|
str = append(str, '\t')
|
||||||
default:
|
default:
|
||||||
iter.reportError("ReadString",
|
iter.ReportError("ReadString",
|
||||||
`invalid escape char after \`)
|
`invalid escape char after \`)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -114,6 +92,7 @@ func (iter *Iterator) readStringSlowPath(str []byte) (ret string) {
|
|||||||
str = append(str, c)
|
str = append(str, c)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
iter.ReportError("ReadString", "unexpected end of input")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -125,13 +104,13 @@ func (iter *Iterator) ReadStringAsSlice() (ret []byte) {
|
|||||||
// for: field name, base64, number
|
// for: field name, base64, number
|
||||||
if iter.buf[i] == '"' {
|
if iter.buf[i] == '"' {
|
||||||
// fast path: reuse the underlying buffer
|
// fast path: reuse the underlying buffer
|
||||||
ret = iter.buf[iter.head : i]
|
ret = iter.buf[iter.head:i]
|
||||||
iter.head = i + 1
|
iter.head = i + 1
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
readLen := iter.tail - iter.head
|
readLen := iter.tail - iter.head
|
||||||
copied := make([]byte, readLen, readLen * 2)
|
copied := make([]byte, readLen, readLen*2)
|
||||||
copy(copied, iter.buf[iter.head:iter.tail])
|
copy(copied, iter.buf[iter.head:iter.tail])
|
||||||
iter.head = iter.tail
|
iter.head = iter.tail
|
||||||
for iter.Error == nil {
|
for iter.Error == nil {
|
||||||
@ -143,7 +122,7 @@ func (iter *Iterator) ReadStringAsSlice() (ret []byte) {
|
|||||||
}
|
}
|
||||||
return copied
|
return copied
|
||||||
}
|
}
|
||||||
iter.reportError("ReadString", `expects " or n`)
|
iter.ReportError("ReadString", `expects " or n`)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -154,11 +133,13 @@ func (iter *Iterator) readU4() (ret rune) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if c >= '0' && c <= '9' {
|
if c >= '0' && c <= '9' {
|
||||||
ret = ret * 16 + rune(c - '0')
|
ret = ret*16 + rune(c-'0')
|
||||||
} else if c >= 'a' && c <= 'f' {
|
} else if c >= 'a' && c <= 'f' {
|
||||||
ret = ret * 16 + rune(c - 'a' + 10)
|
ret = ret*16 + rune(c-'a'+10)
|
||||||
|
} else if c >= 'A' && c <= 'F' {
|
||||||
|
ret = ret*16 + rune(c-'A'+10)
|
||||||
} else {
|
} else {
|
||||||
iter.reportError("readU4", "expects 0~9 or a~f")
|
iter.ReportError("readU4", "expects 0~9 or a~f")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -178,14 +159,14 @@ const (
|
|||||||
mask3 = 0x0F // 0000 1111
|
mask3 = 0x0F // 0000 1111
|
||||||
mask4 = 0x07 // 0000 0111
|
mask4 = 0x07 // 0000 0111
|
||||||
|
|
||||||
rune1Max = 1 << 7 - 1
|
rune1Max = 1<<7 - 1
|
||||||
rune2Max = 1 << 11 - 1
|
rune2Max = 1<<11 - 1
|
||||||
rune3Max = 1 << 16 - 1
|
rune3Max = 1<<16 - 1
|
||||||
|
|
||||||
surrogateMin = 0xD800
|
surrogateMin = 0xD800
|
||||||
surrogateMax = 0xDFFF
|
surrogateMax = 0xDFFF
|
||||||
|
|
||||||
maxRune = '\U0010FFFF' // Maximum valid Unicode code point.
|
maxRune = '\U0010FFFF' // Maximum valid Unicode code point.
|
||||||
runeError = '\uFFFD' // the "error" Rune or "Unicode replacement character"
|
runeError = '\uFFFD' // the "error" Rune or "Unicode replacement character"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -196,22 +177,22 @@ func appendRune(p []byte, r rune) []byte {
|
|||||||
p = append(p, byte(r))
|
p = append(p, byte(r))
|
||||||
return p
|
return p
|
||||||
case i <= rune2Max:
|
case i <= rune2Max:
|
||||||
p = append(p, t2 | byte(r >> 6))
|
p = append(p, t2|byte(r>>6))
|
||||||
p = append(p, tx | byte(r) & maskx)
|
p = append(p, tx|byte(r)&maskx)
|
||||||
return p
|
return p
|
||||||
case i > maxRune, surrogateMin <= i && i <= surrogateMax:
|
case i > maxRune, surrogateMin <= i && i <= surrogateMax:
|
||||||
r = runeError
|
r = runeError
|
||||||
fallthrough
|
fallthrough
|
||||||
case i <= rune3Max:
|
case i <= rune3Max:
|
||||||
p = append(p, t3 | byte(r >> 12))
|
p = append(p, t3|byte(r>>12))
|
||||||
p = append(p, tx | byte(r >> 6) & maskx)
|
p = append(p, tx|byte(r>>6)&maskx)
|
||||||
p = append(p, tx | byte(r) & maskx)
|
p = append(p, tx|byte(r)&maskx)
|
||||||
return p
|
return p
|
||||||
default:
|
default:
|
||||||
p = append(p, t4 | byte(r >> 18))
|
p = append(p, t4|byte(r>>18))
|
||||||
p = append(p, tx | byte(r >> 12) & maskx)
|
p = append(p, tx|byte(r>>12)&maskx)
|
||||||
p = append(p, tx | byte(r >> 6) & maskx)
|
p = append(p, tx|byte(r>>6)&maskx)
|
||||||
p = append(p, tx | byte(r) & maskx)
|
p = append(p, tx|byte(r)&maskx)
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
}
|
}
|
45
feature_pool.go
Normal file
45
feature_pool.go
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
package jsoniter
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (cfg *frozenConfig) BorrowStream(writer io.Writer) *Stream {
|
||||||
|
select {
|
||||||
|
case stream := <-cfg.streamPool:
|
||||||
|
stream.Reset(writer)
|
||||||
|
return stream
|
||||||
|
default:
|
||||||
|
return NewStream(cfg, writer, 512)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cfg *frozenConfig) ReturnStream(stream *Stream) {
|
||||||
|
stream.Error = nil
|
||||||
|
select {
|
||||||
|
case cfg.streamPool <- stream:
|
||||||
|
return
|
||||||
|
default:
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cfg *frozenConfig) BorrowIterator(data []byte) *Iterator {
|
||||||
|
select {
|
||||||
|
case iter := <-cfg.iteratorPool:
|
||||||
|
iter.ResetBytes(data)
|
||||||
|
return iter
|
||||||
|
default:
|
||||||
|
return ParseBytes(cfg, data)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cfg *frozenConfig) ReturnIterator(iter *Iterator) {
|
||||||
|
iter.Error = nil
|
||||||
|
select {
|
||||||
|
case cfg.iteratorPool <- iter:
|
||||||
|
return
|
||||||
|
default:
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
@ -1,275 +1,208 @@
|
|||||||
package jsoniter
|
package jsoniter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"sync/atomic"
|
"time"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
"errors"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
// ValDecoder is an internal type registered to cache as needed.
|
||||||
Reflection on type to create decoders, which is then cached
|
// Don't confuse jsoniter.ValDecoder with json.Decoder.
|
||||||
Reflection on value is avoided as we can, as the reflect.Value itself will allocate, with following exceptions
|
// For json.Decoder's adapter, refer to jsoniter.AdapterDecoder(todo link).
|
||||||
1. create instance of new value, for example *int will need a int to be allocated
|
//
|
||||||
2. append to slice, if the existing cap is not enough, allocate will be done using Reflect.New
|
// Reflection on type to create decoders, which is then cached
|
||||||
3. assignment to map, both key and value will be reflect.Value
|
// Reflection on value is avoided as we can, as the reflect.Value itself will allocate, with following exceptions
|
||||||
For a simple struct binding, it will be reflect.Value free and allocation free
|
// 1. create instance of new value, for example *int will need a int to be allocated
|
||||||
*/
|
// 2. append to slice, if the existing cap is not enough, allocate will be done using Reflect.New
|
||||||
|
// 3. assignment to map, both key and value will be reflect.Value
|
||||||
type Decoder interface {
|
// For a simple struct binding, it will be reflect.Value free and allocation free
|
||||||
decode(ptr unsafe.Pointer, iter *Iterator)
|
type ValDecoder interface {
|
||||||
|
Decode(ptr unsafe.Pointer, iter *Iterator)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Encoder interface {
|
// ValEncoder is an internal type registered to cache as needed.
|
||||||
encode(ptr unsafe.Pointer, stream *Stream)
|
// Don't confuse jsoniter.ValEncoder with json.Encoder.
|
||||||
encodeInterface(val interface{}, stream *Stream)
|
// For json.Encoder's adapter, refer to jsoniter.AdapterEncoder(todo godoc link).
|
||||||
|
type ValEncoder interface {
|
||||||
|
IsEmpty(ptr unsafe.Pointer) bool
|
||||||
|
Encode(ptr unsafe.Pointer, stream *Stream)
|
||||||
|
EncodeInterface(val interface{}, stream *Stream)
|
||||||
}
|
}
|
||||||
|
|
||||||
func WriteToStream(val interface{}, stream *Stream, encoder Encoder) {
|
type checkIsEmpty interface {
|
||||||
|
IsEmpty(ptr unsafe.Pointer) bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func WriteToStream(val interface{}, stream *Stream, encoder ValEncoder) {
|
||||||
e := (*emptyInterface)(unsafe.Pointer(&val))
|
e := (*emptyInterface)(unsafe.Pointer(&val))
|
||||||
|
if e.word == nil {
|
||||||
|
stream.WriteNil()
|
||||||
|
return
|
||||||
|
}
|
||||||
if reflect.TypeOf(val).Kind() == reflect.Ptr {
|
if reflect.TypeOf(val).Kind() == reflect.Ptr {
|
||||||
encoder.encode(unsafe.Pointer(&e.word), stream)
|
encoder.Encode(unsafe.Pointer(&e.word), stream)
|
||||||
} else {
|
} else {
|
||||||
encoder.encode(e.word, stream)
|
encoder.Encode(e.word, stream)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type DecoderFunc func(ptr unsafe.Pointer, iter *Iterator)
|
type DecoderFunc func(ptr unsafe.Pointer, iter *Iterator)
|
||||||
type EncoderFunc func(ptr unsafe.Pointer, stream *Stream)
|
type EncoderFunc func(ptr unsafe.Pointer, stream *Stream)
|
||||||
type ExtensionFunc func(typ reflect.Type, field *reflect.StructField) ([]string, DecoderFunc)
|
|
||||||
|
|
||||||
type funcDecoder struct {
|
var jsonNumberType reflect.Type
|
||||||
fun DecoderFunc
|
var jsonRawMessageType reflect.Type
|
||||||
}
|
var jsoniterRawMessageType reflect.Type
|
||||||
|
|
||||||
func (decoder *funcDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
|
|
||||||
decoder.fun(ptr, iter)
|
|
||||||
}
|
|
||||||
|
|
||||||
type funcEncoder struct {
|
|
||||||
fun EncoderFunc
|
|
||||||
}
|
|
||||||
|
|
||||||
func (encoder *funcEncoder) encode(ptr unsafe.Pointer, stream *Stream) {
|
|
||||||
encoder.fun(ptr, stream)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (encoder *funcEncoder) encodeInterface(val interface{}, stream *Stream) {
|
|
||||||
WriteToStream(val, stream, encoder)
|
|
||||||
}
|
|
||||||
|
|
||||||
var DECODERS unsafe.Pointer
|
|
||||||
var ENCODERS unsafe.Pointer
|
|
||||||
|
|
||||||
var typeDecoders map[string]Decoder
|
|
||||||
var fieldDecoders map[string]Decoder
|
|
||||||
var typeEncoders map[string]Encoder
|
|
||||||
var fieldEncoders map[string]Encoder
|
|
||||||
var extensions []ExtensionFunc
|
|
||||||
var anyType reflect.Type
|
var anyType reflect.Type
|
||||||
|
var marshalerType reflect.Type
|
||||||
|
var unmarshalerType reflect.Type
|
||||||
|
var textMarshalerType reflect.Type
|
||||||
|
var textUnmarshalerType reflect.Type
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
typeDecoders = map[string]Decoder{}
|
jsonNumberType = reflect.TypeOf((*json.Number)(nil)).Elem()
|
||||||
fieldDecoders = map[string]Decoder{}
|
jsonRawMessageType = reflect.TypeOf((*json.RawMessage)(nil)).Elem()
|
||||||
typeEncoders = map[string]Encoder{}
|
jsoniterRawMessageType = reflect.TypeOf((*RawMessage)(nil)).Elem()
|
||||||
fieldEncoders = map[string]Encoder{}
|
|
||||||
extensions = []ExtensionFunc{}
|
|
||||||
atomic.StorePointer(&DECODERS, unsafe.Pointer(&map[string]Decoder{}))
|
|
||||||
atomic.StorePointer(&ENCODERS, unsafe.Pointer(&map[string]Encoder{}))
|
|
||||||
anyType = reflect.TypeOf((*Any)(nil)).Elem()
|
anyType = reflect.TypeOf((*Any)(nil)).Elem()
|
||||||
}
|
marshalerType = reflect.TypeOf((*json.Marshaler)(nil)).Elem()
|
||||||
|
unmarshalerType = reflect.TypeOf((*json.Unmarshaler)(nil)).Elem()
|
||||||
func addDecoderToCache(cacheKey reflect.Type, decoder Decoder) {
|
textMarshalerType = reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem()
|
||||||
done := false
|
textUnmarshalerType = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem()
|
||||||
for !done {
|
|
||||||
ptr := atomic.LoadPointer(&DECODERS)
|
|
||||||
cache := *(*map[reflect.Type]Decoder)(ptr)
|
|
||||||
copied := map[reflect.Type]Decoder{}
|
|
||||||
for k, v := range cache {
|
|
||||||
copied[k] = v
|
|
||||||
}
|
|
||||||
copied[cacheKey] = decoder
|
|
||||||
done = atomic.CompareAndSwapPointer(&DECODERS, ptr, unsafe.Pointer(&copied))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func addEncoderToCache(cacheKey reflect.Type, encoder Encoder) {
|
|
||||||
done := false
|
|
||||||
for !done {
|
|
||||||
ptr := atomic.LoadPointer(&ENCODERS)
|
|
||||||
cache := *(*map[reflect.Type]Encoder)(ptr)
|
|
||||||
copied := map[reflect.Type]Encoder{}
|
|
||||||
for k, v := range cache {
|
|
||||||
copied[k] = v
|
|
||||||
}
|
|
||||||
copied[cacheKey] = encoder
|
|
||||||
done = atomic.CompareAndSwapPointer(&ENCODERS, ptr, unsafe.Pointer(&copied))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func getDecoderFromCache(cacheKey reflect.Type) Decoder {
|
|
||||||
ptr := atomic.LoadPointer(&DECODERS)
|
|
||||||
cache := *(*map[reflect.Type]Decoder)(ptr)
|
|
||||||
return cache[cacheKey]
|
|
||||||
}
|
|
||||||
|
|
||||||
func getEncoderFromCache(cacheKey reflect.Type) Encoder {
|
|
||||||
ptr := atomic.LoadPointer(&ENCODERS)
|
|
||||||
cache := *(*map[reflect.Type]Encoder)(ptr)
|
|
||||||
return cache[cacheKey]
|
|
||||||
}
|
|
||||||
|
|
||||||
// RegisterTypeDecoder can register a type for json object
|
|
||||||
func RegisterTypeDecoder(typ string, fun DecoderFunc) {
|
|
||||||
typeDecoders[typ] = &funcDecoder{fun}
|
|
||||||
}
|
|
||||||
|
|
||||||
// RegisterFieldDecoder can register a type for json field
|
|
||||||
func RegisterFieldDecoder(typ string, field string, fun DecoderFunc) {
|
|
||||||
fieldDecoders[fmt.Sprintf("%s/%s", typ, field)] = &funcDecoder{fun}
|
|
||||||
}
|
|
||||||
|
|
||||||
func RegisterTypeEncoder(typ string, fun EncoderFunc) {
|
|
||||||
typeEncoders[typ] = &funcEncoder{fun}
|
|
||||||
}
|
|
||||||
|
|
||||||
func RegisterFieldEncoder(typ string, field string, fun EncoderFunc) {
|
|
||||||
fieldEncoders[fmt.Sprintf("%s/%s", typ, field)] = &funcEncoder{fun}
|
|
||||||
}
|
|
||||||
|
|
||||||
// RegisterExtension can register a custom extension
|
|
||||||
func RegisterExtension(extension ExtensionFunc) {
|
|
||||||
extensions = append(extensions, extension)
|
|
||||||
}
|
|
||||||
|
|
||||||
// CleanDecoders cleans decoders registered
|
|
||||||
func CleanDecoders() {
|
|
||||||
typeDecoders = map[string]Decoder{}
|
|
||||||
fieldDecoders = map[string]Decoder{}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type optionalDecoder struct {
|
type optionalDecoder struct {
|
||||||
valueType reflect.Type
|
valueType reflect.Type
|
||||||
valueDecoder Decoder
|
valueDecoder ValDecoder
|
||||||
}
|
}
|
||||||
|
|
||||||
func (decoder *optionalDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
|
func (decoder *optionalDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
if iter.ReadNil() {
|
if iter.ReadNil() {
|
||||||
*((*unsafe.Pointer)(ptr)) = nil
|
*((*unsafe.Pointer)(ptr)) = nil
|
||||||
} else {
|
} else {
|
||||||
if *((*unsafe.Pointer)(ptr)) == nil {
|
if *((*unsafe.Pointer)(ptr)) == nil {
|
||||||
// pointer to null, we have to allocate memory to hold the value
|
//pointer to null, we have to allocate memory to hold the value
|
||||||
value := reflect.New(decoder.valueType)
|
value := reflect.New(decoder.valueType)
|
||||||
decoder.valueDecoder.decode(unsafe.Pointer(value.Pointer()), iter)
|
newPtr := extractInterface(value.Interface()).word
|
||||||
*((*uintptr)(ptr)) = value.Pointer()
|
decoder.valueDecoder.Decode(newPtr, iter)
|
||||||
|
*((*uintptr)(ptr)) = uintptr(newPtr)
|
||||||
} else {
|
} else {
|
||||||
// reuse existing instance
|
//reuse existing instance
|
||||||
decoder.valueDecoder.decode(*((*unsafe.Pointer)(ptr)), iter)
|
decoder.valueDecoder.Decode(*((*unsafe.Pointer)(ptr)), iter)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type deferenceDecoder struct {
|
||||||
|
// only to deference a pointer
|
||||||
|
valueType reflect.Type
|
||||||
|
valueDecoder ValDecoder
|
||||||
|
}
|
||||||
|
|
||||||
|
func (decoder *deferenceDecoder) 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 {
|
type optionalEncoder struct {
|
||||||
valueType reflect.Type
|
valueEncoder ValEncoder
|
||||||
valueEncoder Encoder
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (encoder *optionalEncoder) encode(ptr unsafe.Pointer, stream *Stream) {
|
func (encoder *optionalEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||||
if *((*unsafe.Pointer)(ptr)) == nil {
|
if *((*unsafe.Pointer)(ptr)) == nil {
|
||||||
stream.WriteNil()
|
stream.WriteNil()
|
||||||
} else {
|
} else {
|
||||||
encoder.valueEncoder.encode(*((*unsafe.Pointer)(ptr)), stream)
|
encoder.valueEncoder.Encode(*((*unsafe.Pointer)(ptr)), stream)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (encoder *optionalEncoder) encodeInterface(val interface{}, stream *Stream) {
|
func (encoder *optionalEncoder) EncodeInterface(val interface{}, stream *Stream) {
|
||||||
WriteToStream(val, stream, encoder)
|
WriteToStream(val, stream, encoder)
|
||||||
}
|
}
|
||||||
|
|
||||||
type mapDecoder struct {
|
func (encoder *optionalEncoder) IsEmpty(ptr unsafe.Pointer) bool {
|
||||||
mapType reflect.Type
|
if *((*unsafe.Pointer)(ptr)) == nil {
|
||||||
elemType reflect.Type
|
return true
|
||||||
elemDecoder Decoder
|
} else {
|
||||||
mapInterface emptyInterface
|
return false
|
||||||
}
|
|
||||||
|
|
||||||
func (decoder *mapDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
|
|
||||||
// dark magic to cast unsafe.Pointer back to interface{} using reflect.Type
|
|
||||||
mapInterface := decoder.mapInterface
|
|
||||||
mapInterface.word = ptr
|
|
||||||
realInterface := (*interface{})(unsafe.Pointer(&mapInterface))
|
|
||||||
realVal := reflect.ValueOf(*realInterface).Elem()
|
|
||||||
|
|
||||||
for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
|
|
||||||
elem := reflect.New(decoder.elemType)
|
|
||||||
decoder.elemDecoder.decode(unsafe.Pointer(elem.Pointer()), iter)
|
|
||||||
// to put into map, we have to use reflection
|
|
||||||
realVal.SetMapIndex(reflect.ValueOf(string([]byte(field))), elem.Elem())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type mapEncoder struct {
|
type placeholderEncoder struct {
|
||||||
mapType reflect.Type
|
cfg *frozenConfig
|
||||||
elemType reflect.Type
|
cacheKey reflect.Type
|
||||||
elemEncoder Encoder
|
|
||||||
mapInterface emptyInterface
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (encoder *mapEncoder) encode(ptr unsafe.Pointer, stream *Stream) {
|
func (encoder *placeholderEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||||
mapInterface := encoder.mapInterface
|
encoder.getRealEncoder().Encode(ptr, stream)
|
||||||
mapInterface.word = ptr
|
}
|
||||||
realInterface := (*interface{})(unsafe.Pointer(&mapInterface))
|
|
||||||
realVal := reflect.ValueOf(*realInterface)
|
|
||||||
|
|
||||||
stream.WriteObjectStart()
|
func (encoder *placeholderEncoder) EncodeInterface(val interface{}, stream *Stream) {
|
||||||
for i, key := range realVal.MapKeys() {
|
WriteToStream(val, stream, encoder)
|
||||||
if i != 0 {
|
}
|
||||||
stream.WriteMore()
|
|
||||||
|
func (encoder *placeholderEncoder) IsEmpty(ptr unsafe.Pointer) bool {
|
||||||
|
return encoder.getRealEncoder().IsEmpty(ptr)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *placeholderEncoder) getRealEncoder() ValEncoder {
|
||||||
|
for i := 0; i < 30; i++ {
|
||||||
|
realDecoder := encoder.cfg.getEncoderFromCache(encoder.cacheKey)
|
||||||
|
_, isPlaceholder := realDecoder.(*placeholderEncoder)
|
||||||
|
if isPlaceholder {
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
} else {
|
||||||
|
return realDecoder
|
||||||
}
|
}
|
||||||
stream.WriteObjectField(key.String())
|
|
||||||
val := realVal.MapIndex(key).Interface()
|
|
||||||
encoder.elemEncoder.encodeInterface(val, stream)
|
|
||||||
}
|
}
|
||||||
stream.WriteObjectEnd()
|
panic(fmt.Sprintf("real encoder not found for cache key: %v", encoder.cacheKey))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (encoder *mapEncoder) encodeInterface(val interface{}, stream *Stream) {
|
type placeholderDecoder struct {
|
||||||
WriteToStream(val, stream, encoder)
|
cfg *frozenConfig
|
||||||
|
cacheKey reflect.Type
|
||||||
}
|
}
|
||||||
|
|
||||||
type mapInterfaceEncoder struct {
|
func (decoder *placeholderDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
mapType reflect.Type
|
for i := 0; i < 30; i++ {
|
||||||
elemType reflect.Type
|
realDecoder := decoder.cfg.getDecoderFromCache(decoder.cacheKey)
|
||||||
elemEncoder Encoder
|
_, isPlaceholder := realDecoder.(*placeholderDecoder)
|
||||||
mapInterface emptyInterface
|
if isPlaceholder {
|
||||||
}
|
time.Sleep(time.Second)
|
||||||
|
} else {
|
||||||
func (encoder *mapInterfaceEncoder) encode(ptr unsafe.Pointer, stream *Stream) {
|
realDecoder.Decode(ptr, iter)
|
||||||
mapInterface := encoder.mapInterface
|
return
|
||||||
mapInterface.word = ptr
|
|
||||||
realInterface := (*interface{})(unsafe.Pointer(&mapInterface))
|
|
||||||
realVal := reflect.ValueOf(*realInterface)
|
|
||||||
|
|
||||||
stream.WriteObjectStart()
|
|
||||||
for i, key := range realVal.MapKeys() {
|
|
||||||
if i != 0 {
|
|
||||||
stream.WriteMore()
|
|
||||||
}
|
}
|
||||||
stream.WriteObjectField(key.String())
|
|
||||||
val := realVal.MapIndex(key).Interface()
|
|
||||||
encoder.elemEncoder.encode(unsafe.Pointer(&val), stream)
|
|
||||||
}
|
}
|
||||||
stream.WriteObjectEnd()
|
panic(fmt.Sprintf("real decoder not found for cache key: %v", decoder.cacheKey))
|
||||||
}
|
|
||||||
|
|
||||||
func (encoder *mapInterfaceEncoder) encodeInterface(val interface{}, stream *Stream) {
|
|
||||||
WriteToStream(val, stream, encoder)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// emptyInterface is the header for an interface{} value.
|
// emptyInterface is the header for an interface{} value.
|
||||||
type emptyInterface struct {
|
type emptyInterface struct {
|
||||||
typ *struct{}
|
typ unsafe.Pointer
|
||||||
|
word unsafe.Pointer
|
||||||
|
}
|
||||||
|
|
||||||
|
// emptyInterface is the header for an interface with method (not interface{})
|
||||||
|
type nonEmptyInterface struct {
|
||||||
|
// see ../runtime/iface.go:/Itab
|
||||||
|
itab *struct {
|
||||||
|
ityp unsafe.Pointer // static interface type
|
||||||
|
typ unsafe.Pointer // dynamic concrete type
|
||||||
|
link unsafe.Pointer
|
||||||
|
bad int32
|
||||||
|
unused int32
|
||||||
|
fun [100000]unsafe.Pointer // method table
|
||||||
|
}
|
||||||
word unsafe.Pointer
|
word unsafe.Pointer
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -277,21 +210,15 @@ type emptyInterface struct {
|
|||||||
func (iter *Iterator) ReadVal(obj interface{}) {
|
func (iter *Iterator) ReadVal(obj interface{}) {
|
||||||
typ := reflect.TypeOf(obj)
|
typ := reflect.TypeOf(obj)
|
||||||
cacheKey := typ.Elem()
|
cacheKey := typ.Elem()
|
||||||
cachedDecoder := getDecoderFromCache(cacheKey)
|
decoder, err := decoderOfType(iter.cfg, cacheKey)
|
||||||
if cachedDecoder == nil {
|
if err != nil {
|
||||||
decoder, err := decoderOfType(cacheKey)
|
iter.Error = err
|
||||||
if err != nil {
|
return
|
||||||
iter.Error = err
|
|
||||||
return
|
|
||||||
}
|
|
||||||
cachedDecoder = decoder
|
|
||||||
addDecoderToCache(cacheKey, decoder)
|
|
||||||
}
|
}
|
||||||
e := (*emptyInterface)(unsafe.Pointer(&obj))
|
e := (*emptyInterface)(unsafe.Pointer(&obj))
|
||||||
cachedDecoder.decode(e.word, iter)
|
decoder.Decode(e.word, iter)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (stream *Stream) WriteVal(val interface{}) {
|
func (stream *Stream) WriteVal(val interface{}) {
|
||||||
if nil == val {
|
if nil == val {
|
||||||
stream.WriteNil()
|
stream.WriteNil()
|
||||||
@ -299,42 +226,264 @@ func (stream *Stream) WriteVal(val interface{}) {
|
|||||||
}
|
}
|
||||||
typ := reflect.TypeOf(val)
|
typ := reflect.TypeOf(val)
|
||||||
cacheKey := typ
|
cacheKey := typ
|
||||||
cachedEncoder := getEncoderFromCache(cacheKey)
|
encoder, err := encoderOfType(stream.cfg, cacheKey)
|
||||||
if cachedEncoder == nil {
|
if err != nil {
|
||||||
encoder, err := encoderOfType(cacheKey)
|
stream.Error = err
|
||||||
if err != nil {
|
return
|
||||||
stream.Error = err
|
|
||||||
return
|
|
||||||
}
|
|
||||||
cachedEncoder = encoder
|
|
||||||
addEncoderToCache(cacheKey, encoder)
|
|
||||||
}
|
}
|
||||||
cachedEncoder.encodeInterface(val, stream)
|
encoder.EncodeInterface(val, stream)
|
||||||
}
|
}
|
||||||
|
|
||||||
type prefix string
|
type prefix string
|
||||||
|
|
||||||
func (p prefix) addToDecoder(decoder Decoder, err error) (Decoder, error) {
|
func (p prefix) addToDecoder(decoder ValDecoder, err error) (ValDecoder, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("%s: %s", p, err.Error())
|
return nil, fmt.Errorf("%s: %s", p, err.Error())
|
||||||
}
|
}
|
||||||
return decoder, err
|
return decoder, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p prefix) addToEncoder(encoder Encoder, err error) (Encoder, error) {
|
func (p prefix) addToEncoder(encoder ValEncoder, err error) (ValEncoder, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("%s: %s", p, err.Error())
|
return nil, fmt.Errorf("%s: %s", p, err.Error())
|
||||||
}
|
}
|
||||||
return encoder, err
|
return encoder, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func decoderOfType(typ reflect.Type) (Decoder, error) {
|
func decoderOfType(cfg *frozenConfig, typ reflect.Type) (ValDecoder, error) {
|
||||||
|
cacheKey := typ
|
||||||
|
decoder := cfg.getDecoderFromCache(cacheKey)
|
||||||
|
if decoder != nil {
|
||||||
|
return decoder, nil
|
||||||
|
}
|
||||||
|
decoder = getTypeDecoderFromExtension(typ)
|
||||||
|
if decoder != nil {
|
||||||
|
cfg.addDecoderToCache(cacheKey, decoder)
|
||||||
|
return decoder, nil
|
||||||
|
}
|
||||||
|
decoder = &placeholderDecoder{cfg: cfg, cacheKey: cacheKey}
|
||||||
|
cfg.addDecoderToCache(cacheKey, decoder)
|
||||||
|
decoder, err := createDecoderOfType(cfg, typ)
|
||||||
|
for _, extension := range extensions {
|
||||||
|
decoder = extension.DecorateDecoder(typ, decoder)
|
||||||
|
}
|
||||||
|
cfg.addDecoderToCache(cacheKey, decoder)
|
||||||
|
return decoder, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func createDecoderOfType(cfg *frozenConfig, typ reflect.Type) (ValDecoder, error) {
|
||||||
typeName := typ.String()
|
typeName := typ.String()
|
||||||
typeDecoder := typeDecoders[typeName]
|
if typ == jsonRawMessageType {
|
||||||
if typeDecoder != nil {
|
return &jsonRawMessageCodec{}, nil
|
||||||
return typeDecoder, nil
|
}
|
||||||
|
if typ == jsoniterRawMessageType {
|
||||||
|
return &jsoniterRawMessageCodec{}, nil
|
||||||
|
}
|
||||||
|
if typ.AssignableTo(jsonNumberType) {
|
||||||
|
return &jsonNumberCodec{}, nil
|
||||||
|
}
|
||||||
|
if typ.Kind() == reflect.Slice && typ.Elem().Kind() == reflect.Uint8 {
|
||||||
|
return &base64Codec{}, nil
|
||||||
|
}
|
||||||
|
if typ.Implements(unmarshalerType) {
|
||||||
|
templateInterface := reflect.New(typ).Elem().Interface()
|
||||||
|
var decoder ValDecoder = &unmarshalerDecoder{extractInterface(templateInterface)}
|
||||||
|
if typ.Kind() == reflect.Ptr {
|
||||||
|
decoder = &optionalDecoder{typ.Elem(), decoder}
|
||||||
|
}
|
||||||
|
return decoder, nil
|
||||||
|
}
|
||||||
|
if reflect.PtrTo(typ).Implements(unmarshalerType) {
|
||||||
|
templateInterface := reflect.New(typ).Interface()
|
||||||
|
var decoder ValDecoder = &unmarshalerDecoder{extractInterface(templateInterface)}
|
||||||
|
return decoder, nil
|
||||||
|
}
|
||||||
|
if typ.Implements(textUnmarshalerType) {
|
||||||
|
templateInterface := reflect.New(typ).Elem().Interface()
|
||||||
|
var decoder ValDecoder = &textUnmarshalerDecoder{extractInterface(templateInterface)}
|
||||||
|
if typ.Kind() == reflect.Ptr {
|
||||||
|
decoder = &optionalDecoder{typ.Elem(), decoder}
|
||||||
|
}
|
||||||
|
return decoder, nil
|
||||||
|
}
|
||||||
|
if reflect.PtrTo(typ).Implements(textUnmarshalerType) {
|
||||||
|
templateInterface := reflect.New(typ).Interface()
|
||||||
|
var decoder ValDecoder = &textUnmarshalerDecoder{extractInterface(templateInterface)}
|
||||||
|
return decoder, nil
|
||||||
|
}
|
||||||
|
if typ.Implements(anyType) {
|
||||||
|
return &anyCodec{}, nil
|
||||||
}
|
}
|
||||||
switch typ.Kind() {
|
switch typ.Kind() {
|
||||||
|
case reflect.String:
|
||||||
|
if typeName != "string" {
|
||||||
|
return decoderOfType(cfg, reflect.TypeOf((*string)(nil)).Elem())
|
||||||
|
}
|
||||||
|
return &stringCodec{}, nil
|
||||||
|
case reflect.Int:
|
||||||
|
if typeName != "int" {
|
||||||
|
return decoderOfType(cfg, reflect.TypeOf((*int)(nil)).Elem())
|
||||||
|
}
|
||||||
|
return &intCodec{}, nil
|
||||||
|
case reflect.Int8:
|
||||||
|
if typeName != "int8" {
|
||||||
|
return decoderOfType(cfg, reflect.TypeOf((*int8)(nil)).Elem())
|
||||||
|
}
|
||||||
|
return &int8Codec{}, nil
|
||||||
|
case reflect.Int16:
|
||||||
|
if typeName != "int16" {
|
||||||
|
return decoderOfType(cfg, reflect.TypeOf((*int16)(nil)).Elem())
|
||||||
|
}
|
||||||
|
return &int16Codec{}, nil
|
||||||
|
case reflect.Int32:
|
||||||
|
if typeName != "int32" {
|
||||||
|
return decoderOfType(cfg, reflect.TypeOf((*int32)(nil)).Elem())
|
||||||
|
}
|
||||||
|
return &int32Codec{}, nil
|
||||||
|
case reflect.Int64:
|
||||||
|
if typeName != "int64" {
|
||||||
|
return decoderOfType(cfg, reflect.TypeOf((*int64)(nil)).Elem())
|
||||||
|
}
|
||||||
|
return &int64Codec{}, nil
|
||||||
|
case reflect.Uint:
|
||||||
|
if typeName != "uint" {
|
||||||
|
return decoderOfType(cfg, reflect.TypeOf((*uint)(nil)).Elem())
|
||||||
|
}
|
||||||
|
return &uintCodec{}, nil
|
||||||
|
case reflect.Uint8:
|
||||||
|
if typeName != "uint8" {
|
||||||
|
return decoderOfType(cfg, reflect.TypeOf((*uint8)(nil)).Elem())
|
||||||
|
}
|
||||||
|
return &uint8Codec{}, nil
|
||||||
|
case reflect.Uint16:
|
||||||
|
if typeName != "uint16" {
|
||||||
|
return decoderOfType(cfg, reflect.TypeOf((*uint16)(nil)).Elem())
|
||||||
|
}
|
||||||
|
return &uint16Codec{}, nil
|
||||||
|
case reflect.Uint32:
|
||||||
|
if typeName != "uint32" {
|
||||||
|
return decoderOfType(cfg, reflect.TypeOf((*uint32)(nil)).Elem())
|
||||||
|
}
|
||||||
|
return &uint32Codec{}, nil
|
||||||
|
case reflect.Uintptr:
|
||||||
|
if typeName != "uintptr" {
|
||||||
|
return decoderOfType(cfg, reflect.TypeOf((*uintptr)(nil)).Elem())
|
||||||
|
}
|
||||||
|
return &uintptrCodec{}, nil
|
||||||
|
case reflect.Uint64:
|
||||||
|
if typeName != "uint64" {
|
||||||
|
return decoderOfType(cfg, reflect.TypeOf((*uint64)(nil)).Elem())
|
||||||
|
}
|
||||||
|
return &uint64Codec{}, nil
|
||||||
|
case reflect.Float32:
|
||||||
|
if typeName != "float32" {
|
||||||
|
return decoderOfType(cfg, reflect.TypeOf((*float32)(nil)).Elem())
|
||||||
|
}
|
||||||
|
return &float32Codec{}, nil
|
||||||
|
case reflect.Float64:
|
||||||
|
if typeName != "float64" {
|
||||||
|
return decoderOfType(cfg, reflect.TypeOf((*float64)(nil)).Elem())
|
||||||
|
}
|
||||||
|
return &float64Codec{}, nil
|
||||||
|
case reflect.Bool:
|
||||||
|
if typeName != "bool" {
|
||||||
|
return decoderOfType(cfg, reflect.TypeOf((*bool)(nil)).Elem())
|
||||||
|
}
|
||||||
|
return &boolCodec{}, nil
|
||||||
|
case reflect.Interface:
|
||||||
|
if typ.NumMethod() == 0 {
|
||||||
|
return &emptyInterfaceCodec{}, nil
|
||||||
|
} else {
|
||||||
|
return &nonEmptyInterfaceCodec{}, nil
|
||||||
|
}
|
||||||
|
case reflect.Struct:
|
||||||
|
return prefix(fmt.Sprintf("[%s]", typeName)).addToDecoder(decoderOfStruct(cfg, typ))
|
||||||
|
case reflect.Array:
|
||||||
|
return prefix("[array]").addToDecoder(decoderOfArray(cfg, typ))
|
||||||
|
case reflect.Slice:
|
||||||
|
return prefix("[slice]").addToDecoder(decoderOfSlice(cfg, typ))
|
||||||
|
case reflect.Map:
|
||||||
|
return prefix("[map]").addToDecoder(decoderOfMap(cfg, typ))
|
||||||
|
case reflect.Ptr:
|
||||||
|
return prefix("[optional]").addToDecoder(decoderOfOptional(cfg, typ))
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("unsupported type: %v", typ)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func encoderOfType(cfg *frozenConfig, typ reflect.Type) (ValEncoder, error) {
|
||||||
|
cacheKey := typ
|
||||||
|
encoder := cfg.getEncoderFromCache(cacheKey)
|
||||||
|
if encoder != nil {
|
||||||
|
return encoder, nil
|
||||||
|
}
|
||||||
|
encoder = getTypeEncoderFromExtension(typ)
|
||||||
|
if encoder != nil {
|
||||||
|
cfg.addEncoderToCache(cacheKey, encoder)
|
||||||
|
return encoder, nil
|
||||||
|
}
|
||||||
|
encoder = &placeholderEncoder{cfg: cfg, cacheKey: cacheKey}
|
||||||
|
cfg.addEncoderToCache(cacheKey, encoder)
|
||||||
|
encoder, err := createEncoderOfType(cfg, typ)
|
||||||
|
for _, extension := range extensions {
|
||||||
|
encoder = extension.DecorateEncoder(typ, encoder)
|
||||||
|
}
|
||||||
|
cfg.addEncoderToCache(cacheKey, encoder)
|
||||||
|
return encoder, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func createEncoderOfType(cfg *frozenConfig, typ reflect.Type) (ValEncoder, error) {
|
||||||
|
if typ == jsonRawMessageType {
|
||||||
|
return &jsonRawMessageCodec{}, nil
|
||||||
|
}
|
||||||
|
if typ == jsoniterRawMessageType {
|
||||||
|
return &jsoniterRawMessageCodec{}, nil
|
||||||
|
}
|
||||||
|
if typ.AssignableTo(jsonNumberType) {
|
||||||
|
return &jsonNumberCodec{}, nil
|
||||||
|
}
|
||||||
|
if typ.Kind() == reflect.Slice && typ.Elem().Kind() == reflect.Uint8 {
|
||||||
|
return &base64Codec{typ}, nil
|
||||||
|
}
|
||||||
|
if typ.Implements(marshalerType) {
|
||||||
|
checkIsEmpty, err := createCheckIsEmpty(typ)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
templateInterface := reflect.New(typ).Elem().Interface()
|
||||||
|
var encoder ValEncoder = &marshalerEncoder{
|
||||||
|
templateInterface: extractInterface(templateInterface),
|
||||||
|
checkIsEmpty: checkIsEmpty,
|
||||||
|
}
|
||||||
|
if typ.Kind() == reflect.Ptr {
|
||||||
|
encoder = &optionalEncoder{encoder}
|
||||||
|
}
|
||||||
|
return encoder, nil
|
||||||
|
}
|
||||||
|
if typ.Implements(textMarshalerType) {
|
||||||
|
checkIsEmpty, err := createCheckIsEmpty(typ)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
templateInterface := reflect.New(typ).Elem().Interface()
|
||||||
|
var encoder ValEncoder = &textMarshalerEncoder{
|
||||||
|
templateInterface: extractInterface(templateInterface),
|
||||||
|
checkIsEmpty: checkIsEmpty,
|
||||||
|
}
|
||||||
|
if typ.Kind() == reflect.Ptr {
|
||||||
|
encoder = &optionalEncoder{encoder}
|
||||||
|
}
|
||||||
|
return encoder, nil
|
||||||
|
}
|
||||||
|
if typ.Implements(anyType) {
|
||||||
|
return &anyCodec{}, nil
|
||||||
|
}
|
||||||
|
return createEncoderOfSimpleType(cfg, typ)
|
||||||
|
}
|
||||||
|
|
||||||
|
func createCheckIsEmpty(typ reflect.Type) (checkIsEmpty, error) {
|
||||||
|
kind := typ.Kind()
|
||||||
|
switch kind {
|
||||||
case reflect.String:
|
case reflect.String:
|
||||||
return &stringCodec{}, nil
|
return &stringCodec{}, nil
|
||||||
case reflect.Int:
|
case reflect.Int:
|
||||||
@ -355,6 +504,8 @@ func decoderOfType(typ reflect.Type) (Decoder, error) {
|
|||||||
return &uint16Codec{}, nil
|
return &uint16Codec{}, nil
|
||||||
case reflect.Uint32:
|
case reflect.Uint32:
|
||||||
return &uint32Codec{}, nil
|
return &uint32Codec{}, nil
|
||||||
|
case reflect.Uintptr:
|
||||||
|
return &uintptrCodec{}, nil
|
||||||
case reflect.Uint64:
|
case reflect.Uint64:
|
||||||
return &uint64Codec{}, nil
|
return &uint64Codec{}, nil
|
||||||
case reflect.Float32:
|
case reflect.Float32:
|
||||||
@ -365,112 +516,169 @@ func decoderOfType(typ reflect.Type) (Decoder, error) {
|
|||||||
return &boolCodec{}, nil
|
return &boolCodec{}, nil
|
||||||
case reflect.Interface:
|
case reflect.Interface:
|
||||||
if typ.NumMethod() == 0 {
|
if typ.NumMethod() == 0 {
|
||||||
return &interfaceCodec{}, nil
|
return &emptyInterfaceCodec{}, nil
|
||||||
} else {
|
} else {
|
||||||
return nil, errors.New("unsupportd type: " + typ.String())
|
return &nonEmptyInterfaceCodec{}, nil
|
||||||
}
|
}
|
||||||
case reflect.Struct:
|
case reflect.Struct:
|
||||||
return prefix(fmt.Sprintf("[%s]", typeName)).addToDecoder(decoderOfStruct(typ))
|
return &structEncoder{}, nil
|
||||||
|
case reflect.Array:
|
||||||
|
return &arrayEncoder{}, nil
|
||||||
case reflect.Slice:
|
case reflect.Slice:
|
||||||
return prefix("[slice]").addToDecoder(decoderOfSlice(typ))
|
return &sliceEncoder{}, nil
|
||||||
case reflect.Map:
|
case reflect.Map:
|
||||||
return prefix("[map]").addToDecoder(decoderOfMap(typ))
|
return &mapEncoder{}, nil
|
||||||
case reflect.Ptr:
|
case reflect.Ptr:
|
||||||
return prefix("[optional]").addToDecoder(decoderOfOptional(typ))
|
return &optionalEncoder{}, nil
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("unsupported type: %v", typ)
|
return nil, fmt.Errorf("unsupported type: %v", typ)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func encoderOfType(typ reflect.Type) (Encoder, error) {
|
func createEncoderOfSimpleType(cfg *frozenConfig, typ reflect.Type) (ValEncoder, error) {
|
||||||
if typ.ConvertibleTo(anyType) {
|
|
||||||
return &anyCodec{}, nil
|
|
||||||
}
|
|
||||||
typeName := typ.String()
|
typeName := typ.String()
|
||||||
typeEncoder := typeEncoders[typeName]
|
kind := typ.Kind()
|
||||||
if typeEncoder != nil {
|
switch kind {
|
||||||
return typeEncoder, nil
|
|
||||||
}
|
|
||||||
switch typ.Kind() {
|
|
||||||
case reflect.String:
|
case reflect.String:
|
||||||
|
if typeName != "string" {
|
||||||
|
return encoderOfType(cfg, reflect.TypeOf((*string)(nil)).Elem())
|
||||||
|
}
|
||||||
return &stringCodec{}, nil
|
return &stringCodec{}, nil
|
||||||
case reflect.Int:
|
case reflect.Int:
|
||||||
|
if typeName != "int" {
|
||||||
|
return encoderOfType(cfg, reflect.TypeOf((*int)(nil)).Elem())
|
||||||
|
}
|
||||||
return &intCodec{}, nil
|
return &intCodec{}, nil
|
||||||
case reflect.Int8:
|
case reflect.Int8:
|
||||||
|
if typeName != "int8" {
|
||||||
|
return encoderOfType(cfg, reflect.TypeOf((*int8)(nil)).Elem())
|
||||||
|
}
|
||||||
return &int8Codec{}, nil
|
return &int8Codec{}, nil
|
||||||
case reflect.Int16:
|
case reflect.Int16:
|
||||||
|
if typeName != "int16" {
|
||||||
|
return encoderOfType(cfg, reflect.TypeOf((*int16)(nil)).Elem())
|
||||||
|
}
|
||||||
return &int16Codec{}, nil
|
return &int16Codec{}, nil
|
||||||
case reflect.Int32:
|
case reflect.Int32:
|
||||||
|
if typeName != "int32" {
|
||||||
|
return encoderOfType(cfg, reflect.TypeOf((*int32)(nil)).Elem())
|
||||||
|
}
|
||||||
return &int32Codec{}, nil
|
return &int32Codec{}, nil
|
||||||
case reflect.Int64:
|
case reflect.Int64:
|
||||||
|
if typeName != "int64" {
|
||||||
|
return encoderOfType(cfg, reflect.TypeOf((*int64)(nil)).Elem())
|
||||||
|
}
|
||||||
return &int64Codec{}, nil
|
return &int64Codec{}, nil
|
||||||
case reflect.Uint:
|
case reflect.Uint:
|
||||||
|
if typeName != "uint" {
|
||||||
|
return encoderOfType(cfg, reflect.TypeOf((*uint)(nil)).Elem())
|
||||||
|
}
|
||||||
return &uintCodec{}, nil
|
return &uintCodec{}, nil
|
||||||
case reflect.Uint8:
|
case reflect.Uint8:
|
||||||
|
if typeName != "uint8" {
|
||||||
|
return encoderOfType(cfg, reflect.TypeOf((*uint8)(nil)).Elem())
|
||||||
|
}
|
||||||
return &uint8Codec{}, nil
|
return &uint8Codec{}, nil
|
||||||
case reflect.Uint16:
|
case reflect.Uint16:
|
||||||
|
if typeName != "uint16" {
|
||||||
|
return encoderOfType(cfg, reflect.TypeOf((*uint16)(nil)).Elem())
|
||||||
|
}
|
||||||
return &uint16Codec{}, nil
|
return &uint16Codec{}, nil
|
||||||
case reflect.Uint32:
|
case reflect.Uint32:
|
||||||
|
if typeName != "uint32" {
|
||||||
|
return encoderOfType(cfg, reflect.TypeOf((*uint32)(nil)).Elem())
|
||||||
|
}
|
||||||
return &uint32Codec{}, nil
|
return &uint32Codec{}, nil
|
||||||
|
case reflect.Uintptr:
|
||||||
|
if typeName != "uintptr" {
|
||||||
|
return encoderOfType(cfg, reflect.TypeOf((*uintptr)(nil)).Elem())
|
||||||
|
}
|
||||||
|
return &uintptrCodec{}, nil
|
||||||
case reflect.Uint64:
|
case reflect.Uint64:
|
||||||
|
if typeName != "uint64" {
|
||||||
|
return encoderOfType(cfg, reflect.TypeOf((*uint64)(nil)).Elem())
|
||||||
|
}
|
||||||
return &uint64Codec{}, nil
|
return &uint64Codec{}, nil
|
||||||
case reflect.Float32:
|
case reflect.Float32:
|
||||||
|
if typeName != "float32" {
|
||||||
|
return encoderOfType(cfg, reflect.TypeOf((*float32)(nil)).Elem())
|
||||||
|
}
|
||||||
return &float32Codec{}, nil
|
return &float32Codec{}, nil
|
||||||
case reflect.Float64:
|
case reflect.Float64:
|
||||||
|
if typeName != "float64" {
|
||||||
|
return encoderOfType(cfg, reflect.TypeOf((*float64)(nil)).Elem())
|
||||||
|
}
|
||||||
return &float64Codec{}, nil
|
return &float64Codec{}, nil
|
||||||
case reflect.Bool:
|
case reflect.Bool:
|
||||||
|
if typeName != "bool" {
|
||||||
|
return encoderOfType(cfg, reflect.TypeOf((*bool)(nil)).Elem())
|
||||||
|
}
|
||||||
return &boolCodec{}, nil
|
return &boolCodec{}, nil
|
||||||
case reflect.Interface:
|
case reflect.Interface:
|
||||||
return &interfaceCodec{}, nil
|
if typ.NumMethod() == 0 {
|
||||||
|
return &emptyInterfaceCodec{}, nil
|
||||||
|
} else {
|
||||||
|
return &nonEmptyInterfaceCodec{}, nil
|
||||||
|
}
|
||||||
case reflect.Struct:
|
case reflect.Struct:
|
||||||
return prefix(fmt.Sprintf("[%s]", typeName)).addToEncoder(encoderOfStruct(typ))
|
return prefix(fmt.Sprintf("[%s]", typeName)).addToEncoder(encoderOfStruct(cfg, typ))
|
||||||
|
case reflect.Array:
|
||||||
|
return prefix("[array]").addToEncoder(encoderOfArray(cfg, typ))
|
||||||
case reflect.Slice:
|
case reflect.Slice:
|
||||||
return prefix("[slice]").addToEncoder(encoderOfSlice(typ))
|
return prefix("[slice]").addToEncoder(encoderOfSlice(cfg, typ))
|
||||||
case reflect.Map:
|
case reflect.Map:
|
||||||
return prefix("[map]").addToEncoder(encoderOfMap(typ))
|
return prefix("[map]").addToEncoder(encoderOfMap(cfg, typ))
|
||||||
case reflect.Ptr:
|
case reflect.Ptr:
|
||||||
return prefix("[optional]").addToEncoder(encoderOfOptional(typ))
|
return prefix("[optional]").addToEncoder(encoderOfOptional(cfg, typ))
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("unsupported type: %v", typ)
|
return nil, fmt.Errorf("unsupported type: %v", typ)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func decoderOfOptional(typ reflect.Type) (Decoder, error) {
|
func decoderOfOptional(cfg *frozenConfig, typ reflect.Type) (ValDecoder, error) {
|
||||||
elemType := typ.Elem()
|
elemType := typ.Elem()
|
||||||
decoder, err := decoderOfType(elemType)
|
decoder, err := decoderOfType(cfg, elemType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &optionalDecoder{elemType, decoder}, nil
|
return &optionalDecoder{elemType, decoder}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func encoderOfOptional(typ reflect.Type) (Encoder, error) {
|
func encoderOfOptional(cfg *frozenConfig, typ reflect.Type) (ValEncoder, error) {
|
||||||
elemType := typ.Elem()
|
elemType := typ.Elem()
|
||||||
decoder, err := encoderOfType(elemType)
|
elemEncoder, err := encoderOfType(cfg, elemType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &optionalEncoder{elemType, decoder}, nil
|
encoder := &optionalEncoder{elemEncoder}
|
||||||
|
if elemType.Kind() == reflect.Map {
|
||||||
|
encoder = &optionalEncoder{encoder}
|
||||||
|
}
|
||||||
|
return encoder, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func decoderOfMap(typ reflect.Type) (Decoder, error) {
|
func decoderOfMap(cfg *frozenConfig, typ reflect.Type) (ValDecoder, error) {
|
||||||
decoder, err := decoderOfType(typ.Elem())
|
decoder, err := decoderOfType(cfg, typ.Elem())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
mapInterface := reflect.New(typ).Interface()
|
mapInterface := reflect.New(typ).Interface()
|
||||||
return &mapDecoder{typ, typ.Elem(), decoder, *((*emptyInterface)(unsafe.Pointer(&mapInterface)))}, nil
|
return &mapDecoder{typ, typ.Key(), typ.Elem(), decoder, extractInterface(mapInterface)}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func encoderOfMap(typ reflect.Type) (Encoder, error) {
|
func extractInterface(val interface{}) emptyInterface {
|
||||||
|
return *((*emptyInterface)(unsafe.Pointer(&val)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func encoderOfMap(cfg *frozenConfig, typ reflect.Type) (ValEncoder, error) {
|
||||||
elemType := typ.Elem()
|
elemType := typ.Elem()
|
||||||
encoder, err := encoderOfType(elemType)
|
encoder, err := encoderOfType(cfg, elemType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
mapInterface := reflect.New(typ).Elem().Interface()
|
mapInterface := reflect.New(typ).Elem().Interface()
|
||||||
if elemType.Kind() == reflect.Interface && elemType.NumMethod() == 0 {
|
if cfg.sortMapKeys {
|
||||||
return &mapInterfaceEncoder{typ, elemType, encoder, *((*emptyInterface)(unsafe.Pointer(&mapInterface)))}, nil
|
return &sortKeysMapEncoder{typ, elemType, encoder, *((*emptyInterface)(unsafe.Pointer(&mapInterface)))}, nil
|
||||||
} else {
|
} else {
|
||||||
return &mapEncoder{typ, elemType, encoder, *((*emptyInterface)(unsafe.Pointer(&mapInterface)))}, nil
|
return &mapEncoder{typ, elemType, encoder, *((*emptyInterface)(unsafe.Pointer(&mapInterface)))}, nil
|
||||||
}
|
}
|
||||||
|
@ -1,150 +1,97 @@
|
|||||||
package jsoniter
|
package jsoniter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"unsafe"
|
|
||||||
"reflect"
|
|
||||||
"io"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"reflect"
|
||||||
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
func decoderOfSlice(typ reflect.Type) (Decoder, error) {
|
func decoderOfArray(cfg *frozenConfig, typ reflect.Type) (ValDecoder, error) {
|
||||||
decoder, err := decoderOfType(typ.Elem())
|
decoder, err := decoderOfType(cfg, typ.Elem())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &sliceDecoder{typ, typ.Elem(), decoder}, nil
|
return &arrayDecoder{typ, typ.Elem(), decoder}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func encoderOfSlice(typ reflect.Type) (Encoder, error) {
|
func encoderOfArray(cfg *frozenConfig, typ reflect.Type) (ValEncoder, error) {
|
||||||
encoder, err := encoderOfType(typ.Elem())
|
encoder, err := encoderOfType(cfg, typ.Elem())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &sliceEncoder{typ, typ.Elem(), encoder}, nil
|
if typ.Elem().Kind() == reflect.Map {
|
||||||
|
encoder = &optionalEncoder{encoder}
|
||||||
|
}
|
||||||
|
return &arrayEncoder{typ, typ.Elem(), encoder}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type sliceEncoder struct {
|
type arrayEncoder struct {
|
||||||
sliceType reflect.Type
|
arrayType reflect.Type
|
||||||
elemType reflect.Type
|
elemType reflect.Type
|
||||||
elemEncoder Encoder
|
elemEncoder ValEncoder
|
||||||
}
|
}
|
||||||
|
|
||||||
func (encoder *sliceEncoder) encode(ptr unsafe.Pointer, stream *Stream) {
|
func (encoder *arrayEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||||
slice := (*sliceHeader)(ptr)
|
|
||||||
if slice.Len == 0 {
|
|
||||||
stream.WriteEmptyArray()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
stream.WriteArrayStart()
|
stream.WriteArrayStart()
|
||||||
elemPtr := uintptr(slice.Data)
|
elemPtr := uintptr(ptr)
|
||||||
encoder.elemEncoder.encode(unsafe.Pointer(elemPtr), stream)
|
encoder.elemEncoder.Encode(unsafe.Pointer(elemPtr), stream)
|
||||||
for i := 1; i < slice.Len; i++ {
|
for i := 1; i < encoder.arrayType.Len(); i++ {
|
||||||
stream.WriteMore()
|
stream.WriteMore()
|
||||||
elemPtr += encoder.elemType.Size()
|
elemPtr += encoder.elemType.Size()
|
||||||
encoder.elemEncoder.encode(unsafe.Pointer(elemPtr), stream)
|
encoder.elemEncoder.Encode(unsafe.Pointer(elemPtr), stream)
|
||||||
}
|
}
|
||||||
stream.WriteArrayEnd()
|
stream.WriteArrayEnd()
|
||||||
if stream.Error != nil && stream.Error != io.EOF {
|
if stream.Error != nil && stream.Error != io.EOF {
|
||||||
stream.Error = fmt.Errorf("%v: %s", encoder.sliceType, stream.Error.Error())
|
stream.Error = fmt.Errorf("%v: %s", encoder.arrayType, stream.Error.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (encoder *sliceEncoder) encodeInterface(val interface{}, stream *Stream) {
|
func (encoder *arrayEncoder) EncodeInterface(val interface{}, stream *Stream) {
|
||||||
WriteToStream(val, stream, encoder)
|
// special optimization for interface{}
|
||||||
|
e := (*emptyInterface)(unsafe.Pointer(&val))
|
||||||
|
if e.word == nil {
|
||||||
|
stream.WriteArrayStart()
|
||||||
|
stream.WriteNil()
|
||||||
|
stream.WriteArrayEnd()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
elemType := encoder.arrayType.Elem()
|
||||||
|
if encoder.arrayType.Len() == 1 && (elemType.Kind() == reflect.Ptr || elemType.Kind() == reflect.Map) {
|
||||||
|
ptr := uintptr(e.word)
|
||||||
|
e.word = unsafe.Pointer(&ptr)
|
||||||
|
}
|
||||||
|
if reflect.TypeOf(val).Kind() == reflect.Ptr {
|
||||||
|
encoder.Encode(unsafe.Pointer(&e.word), stream)
|
||||||
|
} else {
|
||||||
|
encoder.Encode(e.word, stream)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type sliceDecoder struct {
|
func (encoder *arrayEncoder) IsEmpty(ptr unsafe.Pointer) bool {
|
||||||
sliceType reflect.Type
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
type arrayDecoder struct {
|
||||||
|
arrayType reflect.Type
|
||||||
elemType reflect.Type
|
elemType reflect.Type
|
||||||
elemDecoder Decoder
|
elemDecoder ValDecoder
|
||||||
}
|
}
|
||||||
|
|
||||||
// sliceHeader is a safe version of SliceHeader used within this package.
|
func (decoder *arrayDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
type sliceHeader struct {
|
|
||||||
Data unsafe.Pointer
|
|
||||||
Len int
|
|
||||||
Cap int
|
|
||||||
}
|
|
||||||
|
|
||||||
func (decoder *sliceDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
|
|
||||||
decoder.doDecode(ptr, iter)
|
decoder.doDecode(ptr, iter)
|
||||||
if iter.Error != nil && iter.Error != io.EOF {
|
if iter.Error != nil && iter.Error != io.EOF {
|
||||||
iter.Error = fmt.Errorf("%v: %s", decoder.sliceType, iter.Error.Error())
|
iter.Error = fmt.Errorf("%v: %s", decoder.arrayType, iter.Error.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (decoder *sliceDecoder) doDecode(ptr unsafe.Pointer, iter *Iterator) {
|
func (decoder *arrayDecoder) doDecode(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
slice := (*sliceHeader)(ptr)
|
|
||||||
reuseSlice(slice, decoder.sliceType, 4)
|
|
||||||
if !iter.ReadArray() {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
offset := uintptr(0)
|
offset := uintptr(0)
|
||||||
decoder.elemDecoder.decode(unsafe.Pointer(uintptr(slice.Data) + offset), iter)
|
for ; iter.ReadArray(); offset += decoder.elemType.Size() {
|
||||||
if !iter.ReadArray() {
|
if offset < decoder.arrayType.Size() {
|
||||||
slice.Len = 1
|
decoder.elemDecoder.Decode(unsafe.Pointer(uintptr(ptr)+offset), iter)
|
||||||
return
|
} else {
|
||||||
}
|
iter.Skip()
|
||||||
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() {
|
|
||||||
growOne(slice, decoder.sliceType, decoder.elemType)
|
|
||||||
offset += decoder.elemType.Size()
|
|
||||||
decoder.elemDecoder.decode(unsafe.Pointer(uintptr(slice.Data) + offset), iter)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// grow grows the slice s so that it can hold extra more values, allocating
|
|
||||||
// more capacity if needed. It also returns the old and new slice lengths.
|
|
||||||
func growOne(slice *sliceHeader, sliceType reflect.Type, elementType reflect.Type) {
|
|
||||||
newLen := slice.Len + 1
|
|
||||||
if newLen <= slice.Cap {
|
|
||||||
slice.Len = newLen
|
|
||||||
return
|
|
||||||
}
|
|
||||||
newCap := slice.Cap
|
|
||||||
if newCap == 0 {
|
|
||||||
newCap = 1
|
|
||||||
} else {
|
|
||||||
for newCap < newLen {
|
|
||||||
if slice.Len < 1024 {
|
|
||||||
newCap += newCap
|
|
||||||
} else {
|
|
||||||
newCap += newCap / 4
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
dst := unsafe.Pointer(reflect.MakeSlice(sliceType, newLen, newCap).Pointer())
|
|
||||||
// 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]
|
|
||||||
}
|
|
||||||
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
|
|
||||||
slice.Data = dst
|
|
||||||
}
|
}
|
382
feature_reflect_extension.go
Normal file
382
feature_reflect_extension.go
Normal file
@ -0,0 +1,382 @@
|
|||||||
|
package jsoniter
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
"unicode"
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
var typeDecoders = map[string]ValDecoder{}
|
||||||
|
var fieldDecoders = map[string]ValDecoder{}
|
||||||
|
var typeEncoders = map[string]ValEncoder{}
|
||||||
|
var fieldEncoders = map[string]ValEncoder{}
|
||||||
|
var extensions = []Extension{}
|
||||||
|
|
||||||
|
type StructDescriptor struct {
|
||||||
|
onePtrEmbedded bool
|
||||||
|
onePtrOptimization bool
|
||||||
|
Type reflect.Type
|
||||||
|
Fields []*Binding
|
||||||
|
}
|
||||||
|
|
||||||
|
func (structDescriptor *StructDescriptor) GetField(fieldName string) *Binding {
|
||||||
|
for _, binding := range structDescriptor.Fields {
|
||||||
|
if binding.Field.Name == fieldName {
|
||||||
|
return binding
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type Binding struct {
|
||||||
|
levels []int
|
||||||
|
Field *reflect.StructField
|
||||||
|
FromNames []string
|
||||||
|
ToNames []string
|
||||||
|
Encoder ValEncoder
|
||||||
|
Decoder ValDecoder
|
||||||
|
}
|
||||||
|
|
||||||
|
type Extension interface {
|
||||||
|
UpdateStructDescriptor(structDescriptor *StructDescriptor)
|
||||||
|
CreateDecoder(typ reflect.Type) ValDecoder
|
||||||
|
CreateEncoder(typ reflect.Type) ValEncoder
|
||||||
|
DecorateDecoder(typ reflect.Type, decoder ValDecoder) ValDecoder
|
||||||
|
DecorateEncoder(typ reflect.Type, encoder ValEncoder) ValEncoder
|
||||||
|
}
|
||||||
|
|
||||||
|
type DummyExtension struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (extension *DummyExtension) UpdateStructDescriptor(structDescriptor *StructDescriptor) {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (extension *DummyExtension) CreateDecoder(typ reflect.Type) ValDecoder {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (extension *DummyExtension) CreateEncoder(typ reflect.Type) ValEncoder {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (extension *DummyExtension) DecorateDecoder(typ reflect.Type, decoder ValDecoder) ValDecoder {
|
||||||
|
return decoder
|
||||||
|
}
|
||||||
|
|
||||||
|
func (extension *DummyExtension) DecorateEncoder(typ reflect.Type, encoder ValEncoder) ValEncoder {
|
||||||
|
return encoder
|
||||||
|
}
|
||||||
|
|
||||||
|
type funcDecoder struct {
|
||||||
|
fun DecoderFunc
|
||||||
|
}
|
||||||
|
|
||||||
|
func (decoder *funcDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
|
decoder.fun(ptr, iter)
|
||||||
|
}
|
||||||
|
|
||||||
|
type funcEncoder struct {
|
||||||
|
fun EncoderFunc
|
||||||
|
isEmptyFunc func(ptr unsafe.Pointer) bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *funcEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||||
|
encoder.fun(ptr, stream)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *funcEncoder) EncodeInterface(val interface{}, stream *Stream) {
|
||||||
|
WriteToStream(val, stream, encoder)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *funcEncoder) IsEmpty(ptr unsafe.Pointer) bool {
|
||||||
|
if encoder.isEmptyFunc == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return encoder.isEmptyFunc(ptr)
|
||||||
|
}
|
||||||
|
|
||||||
|
func RegisterTypeDecoderFunc(typ string, fun DecoderFunc) {
|
||||||
|
typeDecoders[typ] = &funcDecoder{fun}
|
||||||
|
}
|
||||||
|
|
||||||
|
func RegisterTypeDecoder(typ string, decoder ValDecoder) {
|
||||||
|
typeDecoders[typ] = decoder
|
||||||
|
}
|
||||||
|
|
||||||
|
func RegisterFieldDecoderFunc(typ string, field string, fun DecoderFunc) {
|
||||||
|
RegisterFieldDecoder(typ, field, &funcDecoder{fun})
|
||||||
|
}
|
||||||
|
|
||||||
|
func RegisterFieldDecoder(typ string, field string, decoder ValDecoder) {
|
||||||
|
fieldDecoders[fmt.Sprintf("%s/%s", typ, field)] = decoder
|
||||||
|
}
|
||||||
|
|
||||||
|
func RegisterTypeEncoderFunc(typ string, fun EncoderFunc, isEmptyFunc func(unsafe.Pointer) bool) {
|
||||||
|
typeEncoders[typ] = &funcEncoder{fun, isEmptyFunc}
|
||||||
|
}
|
||||||
|
|
||||||
|
func RegisterTypeEncoder(typ string, encoder ValEncoder) {
|
||||||
|
typeEncoders[typ] = encoder
|
||||||
|
}
|
||||||
|
|
||||||
|
func RegisterFieldEncoderFunc(typ string, field string, fun EncoderFunc, isEmptyFunc func(unsafe.Pointer) bool) {
|
||||||
|
RegisterFieldEncoder(typ, field, &funcEncoder{fun, isEmptyFunc})
|
||||||
|
}
|
||||||
|
|
||||||
|
func RegisterFieldEncoder(typ string, field string, encoder ValEncoder) {
|
||||||
|
fieldEncoders[fmt.Sprintf("%s/%s", typ, field)] = encoder
|
||||||
|
}
|
||||||
|
|
||||||
|
func RegisterExtension(extension Extension) {
|
||||||
|
extensions = append(extensions, extension)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getTypeDecoderFromExtension(typ reflect.Type) ValDecoder {
|
||||||
|
decoder := _getTypeDecoderFromExtension(typ)
|
||||||
|
if decoder != nil {
|
||||||
|
for _, extension := range extensions {
|
||||||
|
decoder = extension.DecorateDecoder(typ, decoder)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return decoder
|
||||||
|
}
|
||||||
|
func _getTypeDecoderFromExtension(typ reflect.Type) ValDecoder {
|
||||||
|
for _, extension := range extensions {
|
||||||
|
decoder := extension.CreateDecoder(typ)
|
||||||
|
if decoder != nil {
|
||||||
|
return decoder
|
||||||
|
}
|
||||||
|
}
|
||||||
|
typeName := typ.String()
|
||||||
|
decoder := typeDecoders[typeName]
|
||||||
|
if decoder != nil {
|
||||||
|
return decoder
|
||||||
|
}
|
||||||
|
if typ.Kind() == reflect.Ptr {
|
||||||
|
decoder := typeDecoders[typ.Elem().String()]
|
||||||
|
if decoder != nil {
|
||||||
|
return &optionalDecoder{typ.Elem(), decoder}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getTypeEncoderFromExtension(typ reflect.Type) ValEncoder {
|
||||||
|
encoder := _getTypeEncoderFromExtension(typ)
|
||||||
|
if encoder != nil {
|
||||||
|
for _, extension := range extensions {
|
||||||
|
encoder = extension.DecorateEncoder(typ, encoder)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return encoder
|
||||||
|
}
|
||||||
|
|
||||||
|
func _getTypeEncoderFromExtension(typ reflect.Type) ValEncoder {
|
||||||
|
for _, extension := range extensions {
|
||||||
|
encoder := extension.CreateEncoder(typ)
|
||||||
|
if encoder != nil {
|
||||||
|
return encoder
|
||||||
|
}
|
||||||
|
}
|
||||||
|
typeName := typ.String()
|
||||||
|
encoder := typeEncoders[typeName]
|
||||||
|
if encoder != nil {
|
||||||
|
return encoder
|
||||||
|
}
|
||||||
|
if typ.Kind() == reflect.Ptr {
|
||||||
|
encoder := typeEncoders[typ.Elem().String()]
|
||||||
|
if encoder != nil {
|
||||||
|
return &optionalEncoder{encoder}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func describeStruct(cfg *frozenConfig, typ reflect.Type) (*StructDescriptor, error) {
|
||||||
|
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] == "") {
|
||||||
|
if field.Type.Kind() == reflect.Struct {
|
||||||
|
structDescriptor, err := describeStruct(cfg, field.Type)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for _, binding := range structDescriptor.Fields {
|
||||||
|
binding.levels = append([]int{i}, binding.levels...)
|
||||||
|
omitempty := binding.Encoder.(*structFieldEncoder).omitempty
|
||||||
|
binding.Encoder = &structFieldEncoder{&field, binding.Encoder, omitempty}
|
||||||
|
binding.Decoder = &structFieldDecoder{&field, binding.Decoder}
|
||||||
|
embeddedBindings = append(embeddedBindings, binding)
|
||||||
|
}
|
||||||
|
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
|
||||||
|
}
|
||||||
|
for _, binding := range structDescriptor.Fields {
|
||||||
|
binding.levels = append([]int{i}, binding.levels...)
|
||||||
|
omitempty := binding.Encoder.(*structFieldEncoder).omitempty
|
||||||
|
binding.Encoder = &optionalEncoder{binding.Encoder}
|
||||||
|
binding.Encoder = &structFieldEncoder{&field, binding.Encoder, omitempty}
|
||||||
|
binding.Decoder = &deferenceDecoder{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")))
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
binding := &Binding{
|
||||||
|
Field: &field,
|
||||||
|
FromNames: fieldNames,
|
||||||
|
ToNames: fieldNames,
|
||||||
|
Decoder: decoder,
|
||||||
|
Encoder: encoder,
|
||||||
|
}
|
||||||
|
binding.levels = []int{i}
|
||||||
|
bindings = append(bindings, binding)
|
||||||
|
}
|
||||||
|
return createStructDescriptor(cfg, typ, bindings, embeddedBindings), nil
|
||||||
|
}
|
||||||
|
func createStructDescriptor(cfg *frozenConfig, typ reflect.Type, bindings []*Binding, embeddedBindings []*Binding) *StructDescriptor {
|
||||||
|
onePtrEmbedded := false
|
||||||
|
onePtrOptimization := false
|
||||||
|
if typ.NumField() == 1 {
|
||||||
|
firstField := typ.Field(0)
|
||||||
|
switch firstField.Type.Kind() {
|
||||||
|
case reflect.Ptr:
|
||||||
|
if firstField.Anonymous && firstField.Type.Elem().Kind() == reflect.Struct {
|
||||||
|
onePtrEmbedded = true
|
||||||
|
}
|
||||||
|
fallthrough
|
||||||
|
case reflect.Map:
|
||||||
|
onePtrOptimization = true
|
||||||
|
case reflect.Struct:
|
||||||
|
onePtrOptimization = isStructOnePtr(firstField.Type)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
structDescriptor := &StructDescriptor{
|
||||||
|
onePtrEmbedded: onePtrEmbedded,
|
||||||
|
onePtrOptimization: onePtrOptimization,
|
||||||
|
Type: typ,
|
||||||
|
Fields: bindings,
|
||||||
|
}
|
||||||
|
for _, extension := range extensions {
|
||||||
|
extension.UpdateStructDescriptor(structDescriptor)
|
||||||
|
}
|
||||||
|
processTags(structDescriptor, cfg)
|
||||||
|
// merge normal & embedded bindings & sort with original order
|
||||||
|
allBindings := sortableBindings(append(embeddedBindings, structDescriptor.Fields...))
|
||||||
|
sort.Sort(allBindings)
|
||||||
|
structDescriptor.Fields = allBindings
|
||||||
|
return structDescriptor
|
||||||
|
}
|
||||||
|
|
||||||
|
func isStructOnePtr(typ reflect.Type) bool {
|
||||||
|
if typ.NumField() == 1 {
|
||||||
|
firstField := typ.Field(0)
|
||||||
|
switch firstField.Type.Kind() {
|
||||||
|
case reflect.Ptr:
|
||||||
|
return true
|
||||||
|
case reflect.Map:
|
||||||
|
return true
|
||||||
|
case reflect.Struct:
|
||||||
|
return isStructOnePtr(firstField.Type)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
type sortableBindings []*Binding
|
||||||
|
|
||||||
|
func (bindings sortableBindings) Len() int {
|
||||||
|
return len(bindings)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bindings sortableBindings) Less(i, j int) bool {
|
||||||
|
left := bindings[i].levels
|
||||||
|
right := bindings[j].levels
|
||||||
|
k := 0
|
||||||
|
for {
|
||||||
|
if left[k] < right[k] {
|
||||||
|
return true
|
||||||
|
} else if left[k] > right[k] {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
k++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bindings sortableBindings) Swap(i, j int) {
|
||||||
|
bindings[i], bindings[j] = bindings[j], bindings[i]
|
||||||
|
}
|
||||||
|
|
||||||
|
func processTags(structDescriptor *StructDescriptor, cfg *frozenConfig) {
|
||||||
|
for _, binding := range structDescriptor.Fields {
|
||||||
|
shouldOmitEmpty := false
|
||||||
|
tagParts := strings.Split(binding.Field.Tag.Get("json"), ",")
|
||||||
|
for _, tagPart := range tagParts[1:] {
|
||||||
|
if tagPart == "omitempty" {
|
||||||
|
shouldOmitEmpty = true
|
||||||
|
} else if tagPart == "string" {
|
||||||
|
if binding.Field.Type.Kind() == reflect.String {
|
||||||
|
binding.Decoder = &stringModeStringDecoder{binding.Decoder, cfg}
|
||||||
|
binding.Encoder = &stringModeStringEncoder{binding.Encoder, cfg}
|
||||||
|
} else {
|
||||||
|
binding.Decoder = &stringModeNumberDecoder{binding.Decoder}
|
||||||
|
binding.Encoder = &stringModeNumberEncoder{binding.Encoder}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
binding.Decoder = &structFieldDecoder{binding.Field, binding.Decoder}
|
||||||
|
binding.Encoder = &structFieldEncoder{binding.Field, binding.Encoder, shouldOmitEmpty}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func calcFieldNames(originalFieldName string, tagProvidedFieldName string, wholeTag string) []string {
|
||||||
|
// ignore?
|
||||||
|
if wholeTag == "-" {
|
||||||
|
return []string{}
|
||||||
|
}
|
||||||
|
// rename?
|
||||||
|
var fieldNames []string
|
||||||
|
if tagProvidedFieldName == "" {
|
||||||
|
fieldNames = []string{originalFieldName}
|
||||||
|
} else {
|
||||||
|
fieldNames = []string{tagProvidedFieldName}
|
||||||
|
}
|
||||||
|
// private?
|
||||||
|
isNotExported := unicode.IsLower(rune(originalFieldName[0]))
|
||||||
|
if isNotExported {
|
||||||
|
fieldNames = []string{}
|
||||||
|
}
|
||||||
|
return fieldNames
|
||||||
|
}
|
236
feature_reflect_map.go
Normal file
236
feature_reflect_map.go
Normal file
@ -0,0 +1,236 @@
|
|||||||
|
package jsoniter
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding"
|
||||||
|
"encoding/json"
|
||||||
|
"reflect"
|
||||||
|
"sort"
|
||||||
|
"strconv"
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
type mapDecoder struct {
|
||||||
|
mapType reflect.Type
|
||||||
|
keyType reflect.Type
|
||||||
|
elemType reflect.Type
|
||||||
|
elemDecoder ValDecoder
|
||||||
|
mapInterface emptyInterface
|
||||||
|
}
|
||||||
|
|
||||||
|
func (decoder *mapDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
|
// dark magic to cast unsafe.Pointer back to interface{} using reflect.Type
|
||||||
|
mapInterface := decoder.mapInterface
|
||||||
|
mapInterface.word = ptr
|
||||||
|
realInterface := (*interface{})(unsafe.Pointer(&mapInterface))
|
||||||
|
realVal := reflect.ValueOf(*realInterface).Elem()
|
||||||
|
if iter.ReadNil() {
|
||||||
|
realVal.Set(reflect.Zero(decoder.mapType))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if realVal.IsNil() {
|
||||||
|
realVal.Set(reflect.MakeMap(realVal.Type()))
|
||||||
|
}
|
||||||
|
iter.ReadMapCB(func(iter *Iterator, keyStr string) bool {
|
||||||
|
elem := reflect.New(decoder.elemType)
|
||||||
|
decoder.elemDecoder.Decode(unsafe.Pointer(elem.Pointer()), iter)
|
||||||
|
// to put into map, we have to use reflection
|
||||||
|
keyType := decoder.keyType
|
||||||
|
// TODO: remove this from loop
|
||||||
|
switch {
|
||||||
|
case keyType.Kind() == reflect.String:
|
||||||
|
realVal.SetMapIndex(reflect.ValueOf(keyStr).Convert(keyType), elem.Elem())
|
||||||
|
return true
|
||||||
|
case keyType.Implements(textUnmarshalerType):
|
||||||
|
textUnmarshaler := reflect.New(keyType.Elem()).Interface().(encoding.TextUnmarshaler)
|
||||||
|
err := textUnmarshaler.UnmarshalText([]byte(keyStr))
|
||||||
|
if err != nil {
|
||||||
|
iter.ReportError("read map key as TextUnmarshaler", err.Error())
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
realVal.SetMapIndex(reflect.ValueOf(textUnmarshaler), elem.Elem())
|
||||||
|
return true
|
||||||
|
case reflect.PtrTo(keyType).Implements(textUnmarshalerType):
|
||||||
|
textUnmarshaler := reflect.New(keyType).Interface().(encoding.TextUnmarshaler)
|
||||||
|
err := textUnmarshaler.UnmarshalText([]byte(keyStr))
|
||||||
|
if err != nil {
|
||||||
|
iter.ReportError("read map key as TextUnmarshaler", err.Error())
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
realVal.SetMapIndex(reflect.ValueOf(textUnmarshaler).Elem(), elem.Elem())
|
||||||
|
return true
|
||||||
|
default:
|
||||||
|
switch keyType.Kind() {
|
||||||
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||||
|
n, err := strconv.ParseInt(keyStr, 10, 64)
|
||||||
|
if err != nil || reflect.Zero(keyType).OverflowInt(n) {
|
||||||
|
iter.ReportError("read map key as int64", "read int64 failed")
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
realVal.SetMapIndex(reflect.ValueOf(n).Convert(keyType), elem.Elem())
|
||||||
|
return true
|
||||||
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
||||||
|
n, err := strconv.ParseUint(keyStr, 10, 64)
|
||||||
|
if err != nil || reflect.Zero(keyType).OverflowUint(n) {
|
||||||
|
iter.ReportError("read map key as uint64", "read uint64 failed")
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
realVal.SetMapIndex(reflect.ValueOf(n).Convert(keyType), elem.Elem())
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
iter.ReportError("read map key", "unexpected map key type "+keyType.String())
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
type mapEncoder struct {
|
||||||
|
mapType reflect.Type
|
||||||
|
elemType reflect.Type
|
||||||
|
elemEncoder ValEncoder
|
||||||
|
mapInterface emptyInterface
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *mapEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||||
|
mapInterface := encoder.mapInterface
|
||||||
|
mapInterface.word = ptr
|
||||||
|
realInterface := (*interface{})(unsafe.Pointer(&mapInterface))
|
||||||
|
realVal := reflect.ValueOf(*realInterface)
|
||||||
|
stream.WriteObjectStart()
|
||||||
|
for i, key := range realVal.MapKeys() {
|
||||||
|
if i != 0 {
|
||||||
|
stream.WriteMore()
|
||||||
|
}
|
||||||
|
encodeMapKey(key, stream)
|
||||||
|
stream.writeByte(':')
|
||||||
|
val := realVal.MapIndex(key).Interface()
|
||||||
|
encoder.elemEncoder.EncodeInterface(val, stream)
|
||||||
|
}
|
||||||
|
stream.WriteObjectEnd()
|
||||||
|
}
|
||||||
|
|
||||||
|
func encodeMapKey(key reflect.Value, stream *Stream) {
|
||||||
|
if key.Kind() == reflect.String {
|
||||||
|
stream.WriteString(key.String())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if tm, ok := key.Interface().(encoding.TextMarshaler); ok {
|
||||||
|
buf, err := tm.MarshalText()
|
||||||
|
if err != nil {
|
||||||
|
stream.Error = err
|
||||||
|
return
|
||||||
|
}
|
||||||
|
stream.writeByte('"')
|
||||||
|
stream.Write(buf)
|
||||||
|
stream.writeByte('"')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
switch key.Kind() {
|
||||||
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||||
|
stream.writeByte('"')
|
||||||
|
stream.WriteInt64(key.Int())
|
||||||
|
stream.writeByte('"')
|
||||||
|
return
|
||||||
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
||||||
|
stream.writeByte('"')
|
||||||
|
stream.WriteUint64(key.Uint())
|
||||||
|
stream.writeByte('"')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
stream.Error = &json.UnsupportedTypeError{key.Type()}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *mapEncoder) EncodeInterface(val interface{}, stream *Stream) {
|
||||||
|
WriteToStream(val, stream, encoder)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *mapEncoder) IsEmpty(ptr unsafe.Pointer) bool {
|
||||||
|
mapInterface := encoder.mapInterface
|
||||||
|
mapInterface.word = ptr
|
||||||
|
realInterface := (*interface{})(unsafe.Pointer(&mapInterface))
|
||||||
|
realVal := reflect.ValueOf(*realInterface)
|
||||||
|
return realVal.Len() == 0
|
||||||
|
}
|
||||||
|
|
||||||
|
type sortKeysMapEncoder struct {
|
||||||
|
mapType reflect.Type
|
||||||
|
elemType reflect.Type
|
||||||
|
elemEncoder ValEncoder
|
||||||
|
mapInterface emptyInterface
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *sortKeysMapEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||||
|
mapInterface := encoder.mapInterface
|
||||||
|
mapInterface.word = ptr
|
||||||
|
realInterface := (*interface{})(unsafe.Pointer(&mapInterface))
|
||||||
|
realVal := reflect.ValueOf(*realInterface)
|
||||||
|
|
||||||
|
// Extract and sort the keys.
|
||||||
|
keys := realVal.MapKeys()
|
||||||
|
sv := stringValues(make([]reflectWithString, len(keys)))
|
||||||
|
for i, v := range keys {
|
||||||
|
sv[i].v = v
|
||||||
|
if err := sv[i].resolve(); err != nil {
|
||||||
|
stream.Error = err
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sort.Sort(sv)
|
||||||
|
|
||||||
|
stream.WriteObjectStart()
|
||||||
|
for i, key := range sv {
|
||||||
|
if i != 0 {
|
||||||
|
stream.WriteMore()
|
||||||
|
}
|
||||||
|
stream.WriteVal(key.s) // might need html escape, so can not WriteString directly
|
||||||
|
stream.writeByte(':')
|
||||||
|
val := realVal.MapIndex(key.v).Interface()
|
||||||
|
encoder.elemEncoder.EncodeInterface(val, stream)
|
||||||
|
}
|
||||||
|
stream.WriteObjectEnd()
|
||||||
|
}
|
||||||
|
|
||||||
|
// stringValues is a slice of reflect.Value holding *reflect.StringValue.
|
||||||
|
// It implements the methods to sort by string.
|
||||||
|
type stringValues []reflectWithString
|
||||||
|
|
||||||
|
type reflectWithString struct {
|
||||||
|
v reflect.Value
|
||||||
|
s string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *reflectWithString) resolve() error {
|
||||||
|
if w.v.Kind() == reflect.String {
|
||||||
|
w.s = w.v.String()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if tm, ok := w.v.Interface().(encoding.TextMarshaler); ok {
|
||||||
|
buf, err := tm.MarshalText()
|
||||||
|
w.s = string(buf)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
switch w.v.Kind() {
|
||||||
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||||
|
w.s = strconv.FormatInt(w.v.Int(), 10)
|
||||||
|
return nil
|
||||||
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
||||||
|
w.s = strconv.FormatUint(w.v.Uint(), 10)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return &json.UnsupportedTypeError{w.v.Type()}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sv stringValues) Len() int { return len(sv) }
|
||||||
|
func (sv stringValues) Swap(i, j int) { sv[i], sv[j] = sv[j], sv[i] }
|
||||||
|
func (sv stringValues) Less(i, j int) bool { return sv[i].s < sv[j].s }
|
||||||
|
|
||||||
|
func (encoder *sortKeysMapEncoder) EncodeInterface(val interface{}, stream *Stream) {
|
||||||
|
WriteToStream(val, stream, encoder)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *sortKeysMapEncoder) IsEmpty(ptr unsafe.Pointer) bool {
|
||||||
|
mapInterface := encoder.mapInterface
|
||||||
|
mapInterface.word = ptr
|
||||||
|
realInterface := (*interface{})(unsafe.Pointer(&mapInterface))
|
||||||
|
realVal := reflect.ValueOf(*realInterface)
|
||||||
|
return realVal.Len() == 0
|
||||||
|
}
|
@ -1,266 +1,647 @@
|
|||||||
package jsoniter
|
package jsoniter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding"
|
||||||
|
"encoding/base64"
|
||||||
|
"encoding/json"
|
||||||
|
"reflect"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
type stringCodec struct {
|
type stringCodec struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (codec *stringCodec) decode(ptr unsafe.Pointer, iter *Iterator) {
|
func (codec *stringCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
*((*string)(ptr)) = iter.ReadString()
|
*((*string)(ptr)) = iter.ReadString()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (codec *stringCodec) encode(ptr unsafe.Pointer, stream *Stream) {
|
func (codec *stringCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||||
stream.WriteString(*((*string)(ptr)))
|
str := *((*string)(ptr))
|
||||||
|
stream.WriteString(str)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (encoder *stringCodec) encodeInterface(val interface{}, stream *Stream) {
|
func (codec *stringCodec) EncodeInterface(val interface{}, stream *Stream) {
|
||||||
WriteToStream(val, stream, encoder)
|
WriteToStream(val, stream, codec)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (codec *stringCodec) IsEmpty(ptr unsafe.Pointer) bool {
|
||||||
|
return *((*string)(ptr)) == ""
|
||||||
}
|
}
|
||||||
|
|
||||||
type intCodec struct {
|
type intCodec struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (codec *intCodec) decode(ptr unsafe.Pointer, iter *Iterator) {
|
func (codec *intCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
*((*int)(ptr)) = iter.ReadInt()
|
*((*int)(ptr)) = iter.ReadInt()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (codec *intCodec) encode(ptr unsafe.Pointer, stream *Stream) {
|
func (codec *intCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||||
stream.WriteInt(*((*int)(ptr)))
|
stream.WriteInt(*((*int)(ptr)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (encoder *intCodec) encodeInterface(val interface{}, stream *Stream) {
|
func (encoder *intCodec) EncodeInterface(val interface{}, stream *Stream) {
|
||||||
WriteToStream(val, stream, encoder)
|
WriteToStream(val, stream, encoder)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (codec *intCodec) IsEmpty(ptr unsafe.Pointer) bool {
|
||||||
|
return *((*int)(ptr)) == 0
|
||||||
|
}
|
||||||
|
|
||||||
|
type uintptrCodec struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (codec *uintptrCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
|
*((*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) IsEmpty(ptr unsafe.Pointer) bool {
|
||||||
|
return *((*uintptr)(ptr)) == 0
|
||||||
|
}
|
||||||
|
|
||||||
type int8Codec struct {
|
type int8Codec struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (codec *int8Codec) decode(ptr unsafe.Pointer, iter *Iterator) {
|
func (codec *int8Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
*((*int8)(ptr)) = iter.ReadInt8()
|
*((*int8)(ptr)) = iter.ReadInt8()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (codec *int8Codec) encode(ptr unsafe.Pointer, stream *Stream) {
|
func (codec *int8Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||||
stream.WriteInt8(*((*int8)(ptr)))
|
stream.WriteInt8(*((*int8)(ptr)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (encoder *int8Codec) encodeInterface(val interface{}, stream *Stream) {
|
func (encoder *int8Codec) EncodeInterface(val interface{}, stream *Stream) {
|
||||||
WriteToStream(val, stream, encoder)
|
WriteToStream(val, stream, encoder)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (codec *int8Codec) IsEmpty(ptr unsafe.Pointer) bool {
|
||||||
|
return *((*int8)(ptr)) == 0
|
||||||
|
}
|
||||||
|
|
||||||
type int16Codec struct {
|
type int16Codec struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (codec *int16Codec) decode(ptr unsafe.Pointer, iter *Iterator) {
|
func (codec *int16Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
*((*int16)(ptr)) = iter.ReadInt16()
|
*((*int16)(ptr)) = iter.ReadInt16()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (codec *int16Codec) encode(ptr unsafe.Pointer, stream *Stream) {
|
func (codec *int16Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||||
stream.WriteInt16(*((*int16)(ptr)))
|
stream.WriteInt16(*((*int16)(ptr)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (encoder *int16Codec) encodeInterface(val interface{}, stream *Stream) {
|
func (encoder *int16Codec) EncodeInterface(val interface{}, stream *Stream) {
|
||||||
WriteToStream(val, stream, encoder)
|
WriteToStream(val, stream, encoder)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (codec *int16Codec) IsEmpty(ptr unsafe.Pointer) bool {
|
||||||
|
return *((*int16)(ptr)) == 0
|
||||||
|
}
|
||||||
|
|
||||||
type int32Codec struct {
|
type int32Codec struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (codec *int32Codec) decode(ptr unsafe.Pointer, iter *Iterator) {
|
func (codec *int32Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
*((*int32)(ptr)) = iter.ReadInt32()
|
*((*int32)(ptr)) = iter.ReadInt32()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (codec *int32Codec) encode(ptr unsafe.Pointer, stream *Stream) {
|
func (codec *int32Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||||
stream.WriteInt32(*((*int32)(ptr)))
|
stream.WriteInt32(*((*int32)(ptr)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (encoder *int32Codec) encodeInterface(val interface{}, stream *Stream) {
|
func (encoder *int32Codec) EncodeInterface(val interface{}, stream *Stream) {
|
||||||
WriteToStream(val, stream, encoder)
|
WriteToStream(val, stream, encoder)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (codec *int32Codec) IsEmpty(ptr unsafe.Pointer) bool {
|
||||||
|
return *((*int32)(ptr)) == 0
|
||||||
|
}
|
||||||
|
|
||||||
type int64Codec struct {
|
type int64Codec struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (codec *int64Codec) decode(ptr unsafe.Pointer, iter *Iterator) {
|
func (codec *int64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
*((*int64)(ptr)) = iter.ReadInt64()
|
*((*int64)(ptr)) = iter.ReadInt64()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (codec *int64Codec) encode(ptr unsafe.Pointer, stream *Stream) {
|
func (codec *int64Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||||
stream.WriteInt64(*((*int64)(ptr)))
|
stream.WriteInt64(*((*int64)(ptr)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (encoder *int64Codec) encodeInterface(val interface{}, stream *Stream) {
|
func (encoder *int64Codec) EncodeInterface(val interface{}, stream *Stream) {
|
||||||
WriteToStream(val, stream, encoder)
|
WriteToStream(val, stream, encoder)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (codec *int64Codec) IsEmpty(ptr unsafe.Pointer) bool {
|
||||||
|
return *((*int64)(ptr)) == 0
|
||||||
|
}
|
||||||
|
|
||||||
type uintCodec struct {
|
type uintCodec struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (codec *uintCodec) decode(ptr unsafe.Pointer, iter *Iterator) {
|
func (codec *uintCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
*((*uint)(ptr)) = iter.ReadUint()
|
*((*uint)(ptr)) = iter.ReadUint()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (codec *uintCodec) encode(ptr unsafe.Pointer, stream *Stream) {
|
func (codec *uintCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||||
stream.WriteUint(*((*uint)(ptr)))
|
stream.WriteUint(*((*uint)(ptr)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (encoder *uintCodec) encodeInterface(val interface{}, stream *Stream) {
|
func (encoder *uintCodec) EncodeInterface(val interface{}, stream *Stream) {
|
||||||
WriteToStream(val, stream, encoder)
|
WriteToStream(val, stream, encoder)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (codec *uintCodec) IsEmpty(ptr unsafe.Pointer) bool {
|
||||||
|
return *((*uint)(ptr)) == 0
|
||||||
|
}
|
||||||
|
|
||||||
type uint8Codec struct {
|
type uint8Codec struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (codec *uint8Codec) decode(ptr unsafe.Pointer, iter *Iterator) {
|
func (codec *uint8Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
*((*uint8)(ptr)) = iter.ReadUint8()
|
*((*uint8)(ptr)) = iter.ReadUint8()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (codec *uint8Codec) encode(ptr unsafe.Pointer, stream *Stream) {
|
func (codec *uint8Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||||
stream.WriteUint8(*((*uint8)(ptr)))
|
stream.WriteUint8(*((*uint8)(ptr)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (encoder *uint8Codec) encodeInterface(val interface{}, stream *Stream) {
|
func (encoder *uint8Codec) EncodeInterface(val interface{}, stream *Stream) {
|
||||||
WriteToStream(val, stream, encoder)
|
WriteToStream(val, stream, encoder)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (codec *uint8Codec) IsEmpty(ptr unsafe.Pointer) bool {
|
||||||
|
return *((*uint8)(ptr)) == 0
|
||||||
|
}
|
||||||
|
|
||||||
type uint16Codec struct {
|
type uint16Codec struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (decoder *uint16Codec) decode(ptr unsafe.Pointer, iter *Iterator) {
|
func (decoder *uint16Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
*((*uint16)(ptr)) = iter.ReadUint16()
|
*((*uint16)(ptr)) = iter.ReadUint16()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (codec *uint16Codec) encode(ptr unsafe.Pointer, stream *Stream) {
|
func (codec *uint16Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||||
stream.WriteUint16(*((*uint16)(ptr)))
|
stream.WriteUint16(*((*uint16)(ptr)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (encoder *uint16Codec) encodeInterface(val interface{}, stream *Stream) {
|
func (encoder *uint16Codec) EncodeInterface(val interface{}, stream *Stream) {
|
||||||
WriteToStream(val, stream, encoder)
|
WriteToStream(val, stream, encoder)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (codec *uint16Codec) IsEmpty(ptr unsafe.Pointer) bool {
|
||||||
|
return *((*uint16)(ptr)) == 0
|
||||||
|
}
|
||||||
|
|
||||||
type uint32Codec struct {
|
type uint32Codec struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (codec *uint32Codec) decode(ptr unsafe.Pointer, iter *Iterator) {
|
func (codec *uint32Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
*((*uint32)(ptr)) = iter.ReadUint32()
|
*((*uint32)(ptr)) = iter.ReadUint32()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (codec *uint32Codec) encode(ptr unsafe.Pointer, stream *Stream) {
|
func (codec *uint32Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||||
stream.WriteUint32(*((*uint32)(ptr)))
|
stream.WriteUint32(*((*uint32)(ptr)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (encoder *uint32Codec) encodeInterface(val interface{}, stream *Stream) {
|
func (encoder *uint32Codec) EncodeInterface(val interface{}, stream *Stream) {
|
||||||
WriteToStream(val, stream, encoder)
|
WriteToStream(val, stream, encoder)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (codec *uint32Codec) IsEmpty(ptr unsafe.Pointer) bool {
|
||||||
|
return *((*uint32)(ptr)) == 0
|
||||||
|
}
|
||||||
|
|
||||||
type uint64Codec struct {
|
type uint64Codec struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (codec *uint64Codec) decode(ptr unsafe.Pointer, iter *Iterator) {
|
func (codec *uint64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
*((*uint64)(ptr)) = iter.ReadUint64()
|
*((*uint64)(ptr)) = iter.ReadUint64()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (codec *uint64Codec) encode(ptr unsafe.Pointer, stream *Stream) {
|
func (codec *uint64Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||||
stream.WriteUint64(*((*uint64)(ptr)))
|
stream.WriteUint64(*((*uint64)(ptr)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (encoder *uint64Codec) encodeInterface(val interface{}, stream *Stream) {
|
func (encoder *uint64Codec) EncodeInterface(val interface{}, stream *Stream) {
|
||||||
WriteToStream(val, stream, encoder)
|
WriteToStream(val, stream, encoder)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (codec *uint64Codec) IsEmpty(ptr unsafe.Pointer) bool {
|
||||||
|
return *((*uint64)(ptr)) == 0
|
||||||
|
}
|
||||||
|
|
||||||
type float32Codec struct {
|
type float32Codec struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (codec *float32Codec) decode(ptr unsafe.Pointer, iter *Iterator) {
|
func (codec *float32Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
*((*float32)(ptr)) = iter.ReadFloat32()
|
*((*float32)(ptr)) = iter.ReadFloat32()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (codec *float32Codec) encode(ptr unsafe.Pointer, stream *Stream) {
|
func (codec *float32Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||||
stream.WriteFloat32(*((*float32)(ptr)))
|
stream.WriteFloat32(*((*float32)(ptr)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (encoder *float32Codec) encodeInterface(val interface{}, stream *Stream) {
|
func (encoder *float32Codec) EncodeInterface(val interface{}, stream *Stream) {
|
||||||
WriteToStream(val, stream, encoder)
|
WriteToStream(val, stream, encoder)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (codec *float32Codec) IsEmpty(ptr unsafe.Pointer) bool {
|
||||||
|
return *((*float32)(ptr)) == 0
|
||||||
|
}
|
||||||
|
|
||||||
type float64Codec struct {
|
type float64Codec struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (codec *float64Codec) decode(ptr unsafe.Pointer, iter *Iterator) {
|
func (codec *float64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
*((*float64)(ptr)) = iter.ReadFloat64()
|
*((*float64)(ptr)) = iter.ReadFloat64()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (codec *float64Codec) encode(ptr unsafe.Pointer, stream *Stream) {
|
func (codec *float64Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||||
stream.WriteFloat64(*((*float64)(ptr)))
|
stream.WriteFloat64(*((*float64)(ptr)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (encoder *float64Codec) encodeInterface(val interface{}, stream *Stream) {
|
func (encoder *float64Codec) EncodeInterface(val interface{}, stream *Stream) {
|
||||||
WriteToStream(val, stream, encoder)
|
WriteToStream(val, stream, encoder)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (codec *float64Codec) IsEmpty(ptr unsafe.Pointer) bool {
|
||||||
|
return *((*float64)(ptr)) == 0
|
||||||
|
}
|
||||||
|
|
||||||
type boolCodec struct {
|
type boolCodec struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (codec *boolCodec) decode(ptr unsafe.Pointer, iter *Iterator) {
|
func (codec *boolCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
*((*bool)(ptr)) = iter.ReadBool()
|
*((*bool)(ptr)) = iter.ReadBool()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (codec *boolCodec) encode(ptr unsafe.Pointer, stream *Stream) {
|
func (codec *boolCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||||
stream.WriteBool(*((*bool)(ptr)))
|
stream.WriteBool(*((*bool)(ptr)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (encoder *boolCodec) encodeInterface(val interface{}, stream *Stream) {
|
func (encoder *boolCodec) EncodeInterface(val interface{}, stream *Stream) {
|
||||||
WriteToStream(val, stream, encoder)
|
WriteToStream(val, stream, encoder)
|
||||||
}
|
}
|
||||||
|
|
||||||
type interfaceCodec struct {
|
func (codec *boolCodec) IsEmpty(ptr unsafe.Pointer) bool {
|
||||||
|
return !(*((*bool)(ptr)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (codec *interfaceCodec) decode(ptr unsafe.Pointer, iter *Iterator) {
|
type emptyInterfaceCodec struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (codec *emptyInterfaceCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
*((*interface{})(ptr)) = iter.Read()
|
*((*interface{})(ptr)) = iter.Read()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (codec *interfaceCodec) encode(ptr unsafe.Pointer, stream *Stream) {
|
func (codec *emptyInterfaceCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||||
stream.WriteVal(*((*interface{})(ptr)))
|
stream.WriteVal(*((*interface{})(ptr)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (encoder *interfaceCodec) encodeInterface(val interface{}, stream *Stream) {
|
func (encoder *emptyInterfaceCodec) EncodeInterface(val interface{}, stream *Stream) {
|
||||||
stream.WriteVal(val)
|
stream.WriteVal(val)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (codec *emptyInterfaceCodec) IsEmpty(ptr unsafe.Pointer) bool {
|
||||||
|
return ptr == nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type nonEmptyInterfaceCodec struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (codec *nonEmptyInterfaceCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
|
nonEmptyInterface := (*nonEmptyInterface)(ptr)
|
||||||
|
if nonEmptyInterface.itab == nil {
|
||||||
|
iter.ReportError("read non-empty interface", "do not know which concrete type to decode to")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var i interface{}
|
||||||
|
e := (*emptyInterface)(unsafe.Pointer(&i))
|
||||||
|
e.typ = nonEmptyInterface.itab.typ
|
||||||
|
e.word = nonEmptyInterface.word
|
||||||
|
iter.ReadVal(&i)
|
||||||
|
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
|
||||||
|
stream.WriteVal(i)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *nonEmptyInterfaceCodec) EncodeInterface(val interface{}, stream *Stream) {
|
||||||
|
stream.WriteVal(val)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (codec *nonEmptyInterfaceCodec) IsEmpty(ptr unsafe.Pointer) bool {
|
||||||
|
nonEmptyInterface := (*nonEmptyInterface)(ptr)
|
||||||
|
return nonEmptyInterface.word == nil
|
||||||
|
}
|
||||||
|
|
||||||
type anyCodec struct {
|
type anyCodec struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (codec *anyCodec) decode(ptr unsafe.Pointer, iter *Iterator) {
|
func (codec *anyCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
*((*Any)(ptr)) = iter.ReadAny()
|
*((*Any)(ptr)) = iter.ReadAny()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (codec *anyCodec) encode(ptr unsafe.Pointer, stream *Stream) {
|
func (codec *anyCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||||
(*((*Any)(ptr))).WriteTo(stream)
|
(*((*Any)(ptr))).WriteTo(stream)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (encoder *anyCodec) encodeInterface(val interface{}, stream *Stream) {
|
func (encoder *anyCodec) EncodeInterface(val interface{}, stream *Stream) {
|
||||||
(val.(Any)).WriteTo(stream)
|
(val.(Any)).WriteTo(stream)
|
||||||
}
|
}
|
||||||
|
|
||||||
type stringNumberDecoder struct {
|
func (encoder *anyCodec) IsEmpty(ptr unsafe.Pointer) bool {
|
||||||
elemDecoder Decoder
|
return (*((*Any)(ptr))).Size() == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (decoder *stringNumberDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
|
type jsonNumberCodec struct {
|
||||||
c := iter.nextToken()
|
}
|
||||||
if c != '"' {
|
|
||||||
iter.reportError("stringNumberDecoder", `expect "`)
|
func (codec *jsonNumberCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
|
*((*json.Number)(ptr)) = json.Number([]byte(iter.readNumberAsString()))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (codec *jsonNumberCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||||
|
stream.WriteRaw(string(*((*json.Number)(ptr))))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *jsonNumberCodec) EncodeInterface(val interface{}, stream *Stream) {
|
||||||
|
stream.WriteRaw(string(val.(json.Number)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *jsonNumberCodec) IsEmpty(ptr unsafe.Pointer) bool {
|
||||||
|
return len(*((*json.Number)(ptr))) == 0
|
||||||
|
}
|
||||||
|
|
||||||
|
type jsonRawMessageCodec struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (codec *jsonRawMessageCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
|
*((*json.RawMessage)(ptr)) = json.RawMessage(iter.SkipAndReturnBytes())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (codec *jsonRawMessageCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||||
|
stream.WriteRaw(string(*((*json.RawMessage)(ptr))))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *jsonRawMessageCodec) EncodeInterface(val interface{}, stream *Stream) {
|
||||||
|
stream.WriteRaw(string(val.(json.RawMessage)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *jsonRawMessageCodec) IsEmpty(ptr unsafe.Pointer) bool {
|
||||||
|
return len(*((*json.RawMessage)(ptr))) == 0
|
||||||
|
}
|
||||||
|
|
||||||
|
type jsoniterRawMessageCodec struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (codec *jsoniterRawMessageCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
|
*((*RawMessage)(ptr)) = RawMessage(iter.SkipAndReturnBytes())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (codec *jsoniterRawMessageCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||||
|
stream.WriteRaw(string(*((*RawMessage)(ptr))))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *jsoniterRawMessageCodec) EncodeInterface(val interface{}, stream *Stream) {
|
||||||
|
stream.WriteRaw(string(val.(RawMessage)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *jsoniterRawMessageCodec) IsEmpty(ptr unsafe.Pointer) bool {
|
||||||
|
return len(*((*RawMessage)(ptr))) == 0
|
||||||
|
}
|
||||||
|
|
||||||
|
type base64Codec struct {
|
||||||
|
actualType reflect.Type
|
||||||
|
}
|
||||||
|
|
||||||
|
func (codec *base64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
|
if iter.ReadNil() {
|
||||||
|
ptrSlice := (*sliceHeader)(ptr)
|
||||||
|
ptrSlice.Len = 0
|
||||||
|
ptrSlice.Cap = 0
|
||||||
|
ptrSlice.Data = nil
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
decoder.elemDecoder.decode(ptr, iter)
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (codec *base64Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||||
|
src := *((*[]byte)(ptr))
|
||||||
|
if len(src) == 0 {
|
||||||
|
stream.WriteNil()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
encoding := base64.StdEncoding
|
||||||
|
stream.writeByte('"')
|
||||||
|
toGrow := encoding.EncodedLen(len(src))
|
||||||
|
stream.ensure(toGrow)
|
||||||
|
encoding.Encode(stream.buf[stream.n:], src)
|
||||||
|
stream.n += toGrow
|
||||||
|
stream.writeByte('"')
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *base64Codec) EncodeInterface(val interface{}, stream *Stream) {
|
||||||
|
ptr := extractInterface(val).word
|
||||||
|
src := *((*[]byte)(ptr))
|
||||||
|
if len(src) == 0 {
|
||||||
|
stream.WriteNil()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
encoding := base64.StdEncoding
|
||||||
|
stream.writeByte('"')
|
||||||
|
toGrow := encoding.EncodedLen(len(src))
|
||||||
|
stream.ensure(toGrow)
|
||||||
|
encoding.Encode(stream.buf[stream.n:], src)
|
||||||
|
stream.n += toGrow
|
||||||
|
stream.writeByte('"')
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *base64Codec) IsEmpty(ptr unsafe.Pointer) bool {
|
||||||
|
return len(*((*[]byte)(ptr))) == 0
|
||||||
|
}
|
||||||
|
|
||||||
|
type stringModeNumberDecoder struct {
|
||||||
|
elemDecoder ValDecoder
|
||||||
|
}
|
||||||
|
|
||||||
|
func (decoder *stringModeNumberDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
|
c := iter.nextToken()
|
||||||
|
if c != '"' {
|
||||||
|
iter.ReportError("stringModeNumberDecoder", `expect "`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
decoder.elemDecoder.Decode(ptr, iter)
|
||||||
if iter.Error != nil {
|
if iter.Error != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c = iter.readByte()
|
c = iter.readByte()
|
||||||
if c != '"' {
|
if c != '"' {
|
||||||
iter.reportError("stringNumberDecoder", `expect "`)
|
iter.ReportError("stringModeNumberDecoder", `expect "`)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type stringModeStringDecoder struct {
|
||||||
|
elemDecoder ValDecoder
|
||||||
|
cfg *frozenConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
func (decoder *stringModeStringDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
|
decoder.elemDecoder.Decode(ptr, iter)
|
||||||
|
str := *((*string)(ptr))
|
||||||
|
tempIter := decoder.cfg.BorrowIterator([]byte(str))
|
||||||
|
defer decoder.cfg.ReturnIterator(tempIter)
|
||||||
|
*((*string)(ptr)) = tempIter.ReadString()
|
||||||
|
}
|
||||||
|
|
||||||
|
type stringModeNumberEncoder struct {
|
||||||
|
elemEncoder ValEncoder
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *stringModeNumberEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||||
|
stream.writeByte('"')
|
||||||
|
encoder.elemEncoder.Encode(ptr, stream)
|
||||||
|
stream.writeByte('"')
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *stringModeNumberEncoder) EncodeInterface(val interface{}, stream *Stream) {
|
||||||
|
WriteToStream(val, stream, encoder)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *stringModeNumberEncoder) IsEmpty(ptr unsafe.Pointer) bool {
|
||||||
|
return encoder.elemEncoder.IsEmpty(ptr)
|
||||||
|
}
|
||||||
|
|
||||||
|
type stringModeStringEncoder struct {
|
||||||
|
elemEncoder ValEncoder
|
||||||
|
cfg *frozenConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *stringModeStringEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||||
|
tempStream := encoder.cfg.BorrowStream(nil)
|
||||||
|
defer encoder.cfg.ReturnStream(tempStream)
|
||||||
|
encoder.elemEncoder.Encode(ptr, tempStream)
|
||||||
|
stream.WriteString(string(tempStream.Buffer()))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *stringModeStringEncoder) EncodeInterface(val interface{}, stream *Stream) {
|
||||||
|
WriteToStream(val, stream, encoder)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *stringModeStringEncoder) IsEmpty(ptr unsafe.Pointer) bool {
|
||||||
|
return encoder.elemEncoder.IsEmpty(ptr)
|
||||||
|
}
|
||||||
|
|
||||||
|
type marshalerEncoder struct {
|
||||||
|
templateInterface emptyInterface
|
||||||
|
checkIsEmpty checkIsEmpty
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *marshalerEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||||
|
templateInterface := encoder.templateInterface
|
||||||
|
templateInterface.word = ptr
|
||||||
|
realInterface := (*interface{})(unsafe.Pointer(&templateInterface))
|
||||||
|
marshaler := (*realInterface).(json.Marshaler)
|
||||||
|
bytes, err := marshaler.MarshalJSON()
|
||||||
|
if err != nil {
|
||||||
|
stream.Error = err
|
||||||
|
} else {
|
||||||
|
stream.Write(bytes)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func (encoder *marshalerEncoder) EncodeInterface(val interface{}, stream *Stream) {
|
||||||
|
WriteToStream(val, stream, encoder)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *marshalerEncoder) IsEmpty(ptr unsafe.Pointer) bool {
|
||||||
|
return encoder.checkIsEmpty.IsEmpty(ptr)
|
||||||
|
}
|
||||||
|
|
||||||
|
type textMarshalerEncoder struct {
|
||||||
|
templateInterface emptyInterface
|
||||||
|
checkIsEmpty checkIsEmpty
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *textMarshalerEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||||
|
templateInterface := encoder.templateInterface
|
||||||
|
templateInterface.word = ptr
|
||||||
|
realInterface := (*interface{})(unsafe.Pointer(&templateInterface))
|
||||||
|
marshaler := (*realInterface).(encoding.TextMarshaler)
|
||||||
|
bytes, err := marshaler.MarshalText()
|
||||||
|
if err != nil {
|
||||||
|
stream.Error = err
|
||||||
|
} else {
|
||||||
|
stream.WriteString(string(bytes))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *textMarshalerEncoder) EncodeInterface(val interface{}, stream *Stream) {
|
||||||
|
WriteToStream(val, stream, encoder)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *textMarshalerEncoder) IsEmpty(ptr unsafe.Pointer) bool {
|
||||||
|
return encoder.checkIsEmpty.IsEmpty(ptr)
|
||||||
|
}
|
||||||
|
|
||||||
|
type unmarshalerDecoder struct {
|
||||||
|
templateInterface emptyInterface
|
||||||
|
}
|
||||||
|
|
||||||
|
func (decoder *unmarshalerDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
|
templateInterface := decoder.templateInterface
|
||||||
|
templateInterface.word = ptr
|
||||||
|
realInterface := (*interface{})(unsafe.Pointer(&templateInterface))
|
||||||
|
unmarshaler := (*realInterface).(json.Unmarshaler)
|
||||||
|
iter.nextToken()
|
||||||
|
iter.unreadByte() // skip spaces
|
||||||
|
bytes := iter.SkipAndReturnBytes()
|
||||||
|
err := unmarshaler.UnmarshalJSON(bytes)
|
||||||
|
if err != nil {
|
||||||
|
iter.ReportError("unmarshalerDecoder", err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type textUnmarshalerDecoder struct {
|
||||||
|
templateInterface emptyInterface
|
||||||
|
}
|
||||||
|
|
||||||
|
func (decoder *textUnmarshalerDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
|
templateInterface := decoder.templateInterface
|
||||||
|
templateInterface.word = ptr
|
||||||
|
realInterface := (*interface{})(unsafe.Pointer(&templateInterface))
|
||||||
|
unmarshaler := (*realInterface).(encoding.TextUnmarshaler)
|
||||||
|
str := iter.ReadString()
|
||||||
|
err := unmarshaler.UnmarshalText([]byte(str))
|
||||||
|
if err != nil {
|
||||||
|
iter.ReportError("textUnmarshalerDecoder", err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,110 +1,115 @@
|
|||||||
package jsoniter
|
package jsoniter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"reflect"
|
"reflect"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func encoderOfStruct(cfg *frozenConfig, typ reflect.Type) (ValEncoder, error) {
|
||||||
func encoderOfStruct(typ reflect.Type) (Encoder, error) {
|
type bindingTo struct {
|
||||||
structEncoder_ := &structEncoder{}
|
binding *Binding
|
||||||
for i := 0; i < typ.NumField(); i++ {
|
toName string
|
||||||
field := typ.Field(i)
|
ignored bool
|
||||||
var fieldNames []string
|
}
|
||||||
for _, extension := range extensions {
|
orderedBindings := []*bindingTo{}
|
||||||
alternativeFieldNames, _ := extension(typ, &field)
|
structDescriptor, err := describeStruct(cfg, typ)
|
||||||
if alternativeFieldNames != nil {
|
if err != nil {
|
||||||
fieldNames = alternativeFieldNames
|
return nil, err
|
||||||
|
}
|
||||||
|
for _, binding := range structDescriptor.Fields {
|
||||||
|
for _, toName := range binding.ToNames {
|
||||||
|
new := &bindingTo{
|
||||||
|
binding: binding,
|
||||||
|
toName: toName,
|
||||||
}
|
}
|
||||||
}
|
for _, old := range orderedBindings {
|
||||||
tagParts := strings.Split(field.Tag.Get("json"), ",")
|
if old.toName != toName {
|
||||||
// if fieldNames set by extension, use theirs, otherwise try tags
|
continue
|
||||||
if fieldNames == nil {
|
}
|
||||||
/// tagParts[0] always present, even if no tags
|
old.ignored, new.ignored = resolveConflictBinding(old.binding, new.binding)
|
||||||
switch tagParts[0] {
|
|
||||||
case "":
|
|
||||||
fieldNames = []string{field.Name}
|
|
||||||
case "-":
|
|
||||||
fieldNames = []string{}
|
|
||||||
default:
|
|
||||||
fieldNames = []string{tagParts[0]}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
encoder, err := encoderOfType(field.Type)
|
|
||||||
if err != nil {
|
|
||||||
return prefix(fmt.Sprintf("{%s}", field.Name)).addToEncoder(encoder, err)
|
|
||||||
}
|
|
||||||
// map is stored as pointer in the struct
|
|
||||||
// but if struct only has one map, it is inlined
|
|
||||||
if field.Type.Kind() == reflect.Map && typ.NumField() > 1 {
|
|
||||||
encoder = &optionalEncoder{field.Type, encoder}
|
|
||||||
}
|
|
||||||
for _, fieldName := range fieldNames {
|
|
||||||
if structEncoder_.firstField == nil {
|
|
||||||
structEncoder_.firstField = &structFieldEncoder{&field, fieldName, encoder}
|
|
||||||
} else {
|
|
||||||
structEncoder_.fields = append(structEncoder_.fields, &structFieldEncoder{&field, fieldName, encoder})
|
|
||||||
}
|
}
|
||||||
|
orderedBindings = append(orderedBindings, new)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if structEncoder_.firstField == nil {
|
if len(orderedBindings) == 0 {
|
||||||
return &emptyStructEncoder{}, nil
|
return &emptyStructEncoder{}, nil
|
||||||
}
|
}
|
||||||
return structEncoder_, nil
|
finalOrderedFields := []structFieldTo{}
|
||||||
|
for _, bindingTo := range orderedBindings {
|
||||||
|
if !bindingTo.ignored {
|
||||||
|
finalOrderedFields = append(finalOrderedFields, structFieldTo{
|
||||||
|
encoder: bindingTo.binding.Encoder.(*structFieldEncoder),
|
||||||
|
toName: bindingTo.toName,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return &structEncoder{structDescriptor.onePtrEmbedded, structDescriptor.onePtrOptimization, finalOrderedFields}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func decoderOfStruct(typ reflect.Type) (Decoder, error) {
|
func resolveConflictBinding(old, new *Binding) (ignoreOld, ignoreNew bool) {
|
||||||
|
newTagged := new.Field.Tag.Get("json") != ""
|
||||||
|
oldTagged := old.Field.Tag.Get("json") != ""
|
||||||
|
if newTagged {
|
||||||
|
if oldTagged {
|
||||||
|
if len(old.levels) > len(new.levels) {
|
||||||
|
return true, false
|
||||||
|
} else if len(new.levels) > len(old.levels) {
|
||||||
|
return false, true
|
||||||
|
} else {
|
||||||
|
return true, true
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return true, false
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if oldTagged {
|
||||||
|
return true, false
|
||||||
|
} else {
|
||||||
|
if len(old.levels) > len(new.levels) {
|
||||||
|
return true, false
|
||||||
|
} else if len(new.levels) > len(old.levels) {
|
||||||
|
return false, true
|
||||||
|
} else {
|
||||||
|
return true, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func decoderOfStruct(cfg *frozenConfig, typ reflect.Type) (ValDecoder, error) {
|
||||||
|
bindings := map[string]*Binding{}
|
||||||
|
structDescriptor, err := describeStruct(cfg, typ)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for _, binding := range structDescriptor.Fields {
|
||||||
|
for _, fromName := range binding.FromNames {
|
||||||
|
old := bindings[fromName]
|
||||||
|
if old == nil {
|
||||||
|
bindings[fromName] = binding
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
ignoreOld, ignoreNew := resolveConflictBinding(old, binding)
|
||||||
|
if ignoreOld {
|
||||||
|
delete(bindings, fromName)
|
||||||
|
}
|
||||||
|
if !ignoreNew {
|
||||||
|
bindings[fromName] = binding
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
fields := map[string]*structFieldDecoder{}
|
fields := map[string]*structFieldDecoder{}
|
||||||
for i := 0; i < typ.NumField(); i++ {
|
for k, binding := range bindings {
|
||||||
field := typ.Field(i)
|
fields[k] = binding.Decoder.(*structFieldDecoder)
|
||||||
fieldDecoderKey := fmt.Sprintf("%s/%s", typ.String(), field.Name)
|
|
||||||
var fieldNames []string
|
|
||||||
for _, extension := range extensions {
|
|
||||||
alternativeFieldNames, fun := extension(typ, &field)
|
|
||||||
if alternativeFieldNames != nil {
|
|
||||||
fieldNames = alternativeFieldNames
|
|
||||||
}
|
|
||||||
if fun != nil {
|
|
||||||
fieldDecoders[fieldDecoderKey] = &funcDecoder{fun}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
decoder := fieldDecoders[fieldDecoderKey]
|
|
||||||
tagParts := strings.Split(field.Tag.Get("json"), ",")
|
|
||||||
// if fieldNames set by extension, use theirs, otherwise try tags
|
|
||||||
if fieldNames == nil {
|
|
||||||
/// tagParts[0] always present, even if no tags
|
|
||||||
switch tagParts[0] {
|
|
||||||
case "":
|
|
||||||
fieldNames = []string{field.Name}
|
|
||||||
case "-":
|
|
||||||
fieldNames = []string{}
|
|
||||||
default:
|
|
||||||
fieldNames = []string{tagParts[0]}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if decoder == nil {
|
|
||||||
var err error
|
|
||||||
decoder, err = decoderOfType(field.Type)
|
|
||||||
if err != nil {
|
|
||||||
return prefix(fmt.Sprintf("{%s}", field.Name)).addToDecoder(decoder, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(tagParts) > 1 && tagParts[1] == "string" {
|
|
||||||
decoder = &stringNumberDecoder{decoder}
|
|
||||||
}
|
|
||||||
for _, fieldName := range fieldNames {
|
|
||||||
fields[fieldName] = &structFieldDecoder{&field, decoder}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return createStructDecoder(typ, fields)
|
return createStructDecoder(typ, fields)
|
||||||
}
|
}
|
||||||
|
|
||||||
func createStructDecoder(typ reflect.Type, fields map[string]*structFieldDecoder) (Decoder, error) {
|
func createStructDecoder(typ reflect.Type, fields map[string]*structFieldDecoder) (ValDecoder, error) {
|
||||||
knownHash := map[int32]struct{}{
|
knownHash := map[int32]struct{}{
|
||||||
0: struct{}{},
|
0: {},
|
||||||
}
|
}
|
||||||
switch len(fields) {
|
switch len(fields) {
|
||||||
case 0:
|
case 0:
|
||||||
@ -527,7 +532,7 @@ type generalStructDecoder struct {
|
|||||||
fields map[string]*structFieldDecoder
|
fields map[string]*structFieldDecoder
|
||||||
}
|
}
|
||||||
|
|
||||||
func (decoder *generalStructDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
|
func (decoder *generalStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
if !iter.readObjectStart() {
|
if !iter.readObjectStart() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -537,7 +542,7 @@ func (decoder *generalStructDecoder) decode(ptr unsafe.Pointer, iter *Iterator)
|
|||||||
if fieldDecoder == nil {
|
if fieldDecoder == nil {
|
||||||
iter.Skip()
|
iter.Skip()
|
||||||
} else {
|
} else {
|
||||||
fieldDecoder.decode(ptr, iter)
|
fieldDecoder.Decode(ptr, iter)
|
||||||
}
|
}
|
||||||
for iter.nextToken() == ',' {
|
for iter.nextToken() == ',' {
|
||||||
fieldBytes = iter.readObjectFieldAsBytes()
|
fieldBytes = iter.readObjectFieldAsBytes()
|
||||||
@ -546,7 +551,7 @@ func (decoder *generalStructDecoder) decode(ptr unsafe.Pointer, iter *Iterator)
|
|||||||
if fieldDecoder == nil {
|
if fieldDecoder == nil {
|
||||||
iter.Skip()
|
iter.Skip()
|
||||||
} else {
|
} else {
|
||||||
fieldDecoder.decode(ptr, iter)
|
fieldDecoder.Decode(ptr, iter)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if iter.Error != nil && iter.Error != io.EOF {
|
if iter.Error != nil && iter.Error != io.EOF {
|
||||||
@ -558,7 +563,7 @@ type skipDecoder struct {
|
|||||||
typ reflect.Type
|
typ reflect.Type
|
||||||
}
|
}
|
||||||
|
|
||||||
func (decoder *skipDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
|
func (decoder *skipDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
iter.Skip()
|
iter.Skip()
|
||||||
if iter.Error != nil && iter.Error != io.EOF {
|
if iter.Error != nil && iter.Error != io.EOF {
|
||||||
iter.Error = fmt.Errorf("%v: %s", decoder.typ, iter.Error.Error())
|
iter.Error = fmt.Errorf("%v: %s", decoder.typ, iter.Error.Error())
|
||||||
@ -571,13 +576,13 @@ type oneFieldStructDecoder struct {
|
|||||||
fieldDecoder *structFieldDecoder
|
fieldDecoder *structFieldDecoder
|
||||||
}
|
}
|
||||||
|
|
||||||
func (decoder *oneFieldStructDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
|
func (decoder *oneFieldStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
if !iter.readObjectStart() {
|
if !iter.readObjectStart() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for {
|
for {
|
||||||
if iter.readFieldHash() == decoder.fieldHash {
|
if iter.readFieldHash() == decoder.fieldHash {
|
||||||
decoder.fieldDecoder.decode(ptr, iter)
|
decoder.fieldDecoder.Decode(ptr, iter)
|
||||||
} else {
|
} else {
|
||||||
iter.Skip()
|
iter.Skip()
|
||||||
}
|
}
|
||||||
@ -591,23 +596,23 @@ func (decoder *oneFieldStructDecoder) decode(ptr unsafe.Pointer, iter *Iterator)
|
|||||||
}
|
}
|
||||||
|
|
||||||
type twoFieldsStructDecoder struct {
|
type twoFieldsStructDecoder struct {
|
||||||
typ reflect.Type
|
typ reflect.Type
|
||||||
fieldHash1 int32
|
fieldHash1 int32
|
||||||
fieldDecoder1 *structFieldDecoder
|
fieldDecoder1 *structFieldDecoder
|
||||||
fieldHash2 int32
|
fieldHash2 int32
|
||||||
fieldDecoder2 *structFieldDecoder
|
fieldDecoder2 *structFieldDecoder
|
||||||
}
|
}
|
||||||
|
|
||||||
func (decoder *twoFieldsStructDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
|
func (decoder *twoFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
if !iter.readObjectStart() {
|
if !iter.readObjectStart() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for {
|
for {
|
||||||
switch iter.readFieldHash() {
|
switch iter.readFieldHash() {
|
||||||
case decoder.fieldHash1:
|
case decoder.fieldHash1:
|
||||||
decoder.fieldDecoder1.decode(ptr, iter)
|
decoder.fieldDecoder1.Decode(ptr, iter)
|
||||||
case decoder.fieldHash2:
|
case decoder.fieldHash2:
|
||||||
decoder.fieldDecoder2.decode(ptr, iter)
|
decoder.fieldDecoder2.Decode(ptr, iter)
|
||||||
default:
|
default:
|
||||||
iter.Skip()
|
iter.Skip()
|
||||||
}
|
}
|
||||||
@ -621,27 +626,27 @@ func (decoder *twoFieldsStructDecoder) decode(ptr unsafe.Pointer, iter *Iterator
|
|||||||
}
|
}
|
||||||
|
|
||||||
type threeFieldsStructDecoder struct {
|
type threeFieldsStructDecoder struct {
|
||||||
typ reflect.Type
|
typ reflect.Type
|
||||||
fieldHash1 int32
|
fieldHash1 int32
|
||||||
fieldDecoder1 *structFieldDecoder
|
fieldDecoder1 *structFieldDecoder
|
||||||
fieldHash2 int32
|
fieldHash2 int32
|
||||||
fieldDecoder2 *structFieldDecoder
|
fieldDecoder2 *structFieldDecoder
|
||||||
fieldHash3 int32
|
fieldHash3 int32
|
||||||
fieldDecoder3 *structFieldDecoder
|
fieldDecoder3 *structFieldDecoder
|
||||||
}
|
}
|
||||||
|
|
||||||
func (decoder *threeFieldsStructDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
|
func (decoder *threeFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
if !iter.readObjectStart() {
|
if !iter.readObjectStart() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for {
|
for {
|
||||||
switch iter.readFieldHash() {
|
switch iter.readFieldHash() {
|
||||||
case decoder.fieldHash1:
|
case decoder.fieldHash1:
|
||||||
decoder.fieldDecoder1.decode(ptr, iter)
|
decoder.fieldDecoder1.Decode(ptr, iter)
|
||||||
case decoder.fieldHash2:
|
case decoder.fieldHash2:
|
||||||
decoder.fieldDecoder2.decode(ptr, iter)
|
decoder.fieldDecoder2.Decode(ptr, iter)
|
||||||
case decoder.fieldHash3:
|
case decoder.fieldHash3:
|
||||||
decoder.fieldDecoder3.decode(ptr, iter)
|
decoder.fieldDecoder3.Decode(ptr, iter)
|
||||||
default:
|
default:
|
||||||
iter.Skip()
|
iter.Skip()
|
||||||
}
|
}
|
||||||
@ -655,31 +660,31 @@ func (decoder *threeFieldsStructDecoder) decode(ptr unsafe.Pointer, iter *Iterat
|
|||||||
}
|
}
|
||||||
|
|
||||||
type fourFieldsStructDecoder struct {
|
type fourFieldsStructDecoder struct {
|
||||||
typ reflect.Type
|
typ reflect.Type
|
||||||
fieldHash1 int32
|
fieldHash1 int32
|
||||||
fieldDecoder1 *structFieldDecoder
|
fieldDecoder1 *structFieldDecoder
|
||||||
fieldHash2 int32
|
fieldHash2 int32
|
||||||
fieldDecoder2 *structFieldDecoder
|
fieldDecoder2 *structFieldDecoder
|
||||||
fieldHash3 int32
|
fieldHash3 int32
|
||||||
fieldDecoder3 *structFieldDecoder
|
fieldDecoder3 *structFieldDecoder
|
||||||
fieldHash4 int32
|
fieldHash4 int32
|
||||||
fieldDecoder4 *structFieldDecoder
|
fieldDecoder4 *structFieldDecoder
|
||||||
}
|
}
|
||||||
|
|
||||||
func (decoder *fourFieldsStructDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
|
func (decoder *fourFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
if !iter.readObjectStart() {
|
if !iter.readObjectStart() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for {
|
for {
|
||||||
switch iter.readFieldHash() {
|
switch iter.readFieldHash() {
|
||||||
case decoder.fieldHash1:
|
case decoder.fieldHash1:
|
||||||
decoder.fieldDecoder1.decode(ptr, iter)
|
decoder.fieldDecoder1.Decode(ptr, iter)
|
||||||
case decoder.fieldHash2:
|
case decoder.fieldHash2:
|
||||||
decoder.fieldDecoder2.decode(ptr, iter)
|
decoder.fieldDecoder2.Decode(ptr, iter)
|
||||||
case decoder.fieldHash3:
|
case decoder.fieldHash3:
|
||||||
decoder.fieldDecoder3.decode(ptr, iter)
|
decoder.fieldDecoder3.Decode(ptr, iter)
|
||||||
case decoder.fieldHash4:
|
case decoder.fieldHash4:
|
||||||
decoder.fieldDecoder4.decode(ptr, iter)
|
decoder.fieldDecoder4.Decode(ptr, iter)
|
||||||
default:
|
default:
|
||||||
iter.Skip()
|
iter.Skip()
|
||||||
}
|
}
|
||||||
@ -693,35 +698,35 @@ func (decoder *fourFieldsStructDecoder) decode(ptr unsafe.Pointer, iter *Iterato
|
|||||||
}
|
}
|
||||||
|
|
||||||
type fiveFieldsStructDecoder struct {
|
type fiveFieldsStructDecoder struct {
|
||||||
typ reflect.Type
|
typ reflect.Type
|
||||||
fieldHash1 int32
|
fieldHash1 int32
|
||||||
fieldDecoder1 *structFieldDecoder
|
fieldDecoder1 *structFieldDecoder
|
||||||
fieldHash2 int32
|
fieldHash2 int32
|
||||||
fieldDecoder2 *structFieldDecoder
|
fieldDecoder2 *structFieldDecoder
|
||||||
fieldHash3 int32
|
fieldHash3 int32
|
||||||
fieldDecoder3 *structFieldDecoder
|
fieldDecoder3 *structFieldDecoder
|
||||||
fieldHash4 int32
|
fieldHash4 int32
|
||||||
fieldDecoder4 *structFieldDecoder
|
fieldDecoder4 *structFieldDecoder
|
||||||
fieldHash5 int32
|
fieldHash5 int32
|
||||||
fieldDecoder5 *structFieldDecoder
|
fieldDecoder5 *structFieldDecoder
|
||||||
}
|
}
|
||||||
|
|
||||||
func (decoder *fiveFieldsStructDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
|
func (decoder *fiveFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
if !iter.readObjectStart() {
|
if !iter.readObjectStart() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for {
|
for {
|
||||||
switch iter.readFieldHash() {
|
switch iter.readFieldHash() {
|
||||||
case decoder.fieldHash1:
|
case decoder.fieldHash1:
|
||||||
decoder.fieldDecoder1.decode(ptr, iter)
|
decoder.fieldDecoder1.Decode(ptr, iter)
|
||||||
case decoder.fieldHash2:
|
case decoder.fieldHash2:
|
||||||
decoder.fieldDecoder2.decode(ptr, iter)
|
decoder.fieldDecoder2.Decode(ptr, iter)
|
||||||
case decoder.fieldHash3:
|
case decoder.fieldHash3:
|
||||||
decoder.fieldDecoder3.decode(ptr, iter)
|
decoder.fieldDecoder3.Decode(ptr, iter)
|
||||||
case decoder.fieldHash4:
|
case decoder.fieldHash4:
|
||||||
decoder.fieldDecoder4.decode(ptr, iter)
|
decoder.fieldDecoder4.Decode(ptr, iter)
|
||||||
case decoder.fieldHash5:
|
case decoder.fieldHash5:
|
||||||
decoder.fieldDecoder5.decode(ptr, iter)
|
decoder.fieldDecoder5.Decode(ptr, iter)
|
||||||
default:
|
default:
|
||||||
iter.Skip()
|
iter.Skip()
|
||||||
}
|
}
|
||||||
@ -735,39 +740,39 @@ func (decoder *fiveFieldsStructDecoder) decode(ptr unsafe.Pointer, iter *Iterato
|
|||||||
}
|
}
|
||||||
|
|
||||||
type sixFieldsStructDecoder struct {
|
type sixFieldsStructDecoder struct {
|
||||||
typ reflect.Type
|
typ reflect.Type
|
||||||
fieldHash1 int32
|
fieldHash1 int32
|
||||||
fieldDecoder1 *structFieldDecoder
|
fieldDecoder1 *structFieldDecoder
|
||||||
fieldHash2 int32
|
fieldHash2 int32
|
||||||
fieldDecoder2 *structFieldDecoder
|
fieldDecoder2 *structFieldDecoder
|
||||||
fieldHash3 int32
|
fieldHash3 int32
|
||||||
fieldDecoder3 *structFieldDecoder
|
fieldDecoder3 *structFieldDecoder
|
||||||
fieldHash4 int32
|
fieldHash4 int32
|
||||||
fieldDecoder4 *structFieldDecoder
|
fieldDecoder4 *structFieldDecoder
|
||||||
fieldHash5 int32
|
fieldHash5 int32
|
||||||
fieldDecoder5 *structFieldDecoder
|
fieldDecoder5 *structFieldDecoder
|
||||||
fieldHash6 int32
|
fieldHash6 int32
|
||||||
fieldDecoder6 *structFieldDecoder
|
fieldDecoder6 *structFieldDecoder
|
||||||
}
|
}
|
||||||
|
|
||||||
func (decoder *sixFieldsStructDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
|
func (decoder *sixFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
if !iter.readObjectStart() {
|
if !iter.readObjectStart() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for {
|
for {
|
||||||
switch iter.readFieldHash() {
|
switch iter.readFieldHash() {
|
||||||
case decoder.fieldHash1:
|
case decoder.fieldHash1:
|
||||||
decoder.fieldDecoder1.decode(ptr, iter)
|
decoder.fieldDecoder1.Decode(ptr, iter)
|
||||||
case decoder.fieldHash2:
|
case decoder.fieldHash2:
|
||||||
decoder.fieldDecoder2.decode(ptr, iter)
|
decoder.fieldDecoder2.Decode(ptr, iter)
|
||||||
case decoder.fieldHash3:
|
case decoder.fieldHash3:
|
||||||
decoder.fieldDecoder3.decode(ptr, iter)
|
decoder.fieldDecoder3.Decode(ptr, iter)
|
||||||
case decoder.fieldHash4:
|
case decoder.fieldHash4:
|
||||||
decoder.fieldDecoder4.decode(ptr, iter)
|
decoder.fieldDecoder4.Decode(ptr, iter)
|
||||||
case decoder.fieldHash5:
|
case decoder.fieldHash5:
|
||||||
decoder.fieldDecoder5.decode(ptr, iter)
|
decoder.fieldDecoder5.Decode(ptr, iter)
|
||||||
case decoder.fieldHash6:
|
case decoder.fieldHash6:
|
||||||
decoder.fieldDecoder6.decode(ptr, iter)
|
decoder.fieldDecoder6.Decode(ptr, iter)
|
||||||
default:
|
default:
|
||||||
iter.Skip()
|
iter.Skip()
|
||||||
}
|
}
|
||||||
@ -781,43 +786,43 @@ func (decoder *sixFieldsStructDecoder) decode(ptr unsafe.Pointer, iter *Iterator
|
|||||||
}
|
}
|
||||||
|
|
||||||
type sevenFieldsStructDecoder struct {
|
type sevenFieldsStructDecoder struct {
|
||||||
typ reflect.Type
|
typ reflect.Type
|
||||||
fieldHash1 int32
|
fieldHash1 int32
|
||||||
fieldDecoder1 *structFieldDecoder
|
fieldDecoder1 *structFieldDecoder
|
||||||
fieldHash2 int32
|
fieldHash2 int32
|
||||||
fieldDecoder2 *structFieldDecoder
|
fieldDecoder2 *structFieldDecoder
|
||||||
fieldHash3 int32
|
fieldHash3 int32
|
||||||
fieldDecoder3 *structFieldDecoder
|
fieldDecoder3 *structFieldDecoder
|
||||||
fieldHash4 int32
|
fieldHash4 int32
|
||||||
fieldDecoder4 *structFieldDecoder
|
fieldDecoder4 *structFieldDecoder
|
||||||
fieldHash5 int32
|
fieldHash5 int32
|
||||||
fieldDecoder5 *structFieldDecoder
|
fieldDecoder5 *structFieldDecoder
|
||||||
fieldHash6 int32
|
fieldHash6 int32
|
||||||
fieldDecoder6 *structFieldDecoder
|
fieldDecoder6 *structFieldDecoder
|
||||||
fieldHash7 int32
|
fieldHash7 int32
|
||||||
fieldDecoder7 *structFieldDecoder
|
fieldDecoder7 *structFieldDecoder
|
||||||
}
|
}
|
||||||
|
|
||||||
func (decoder *sevenFieldsStructDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
|
func (decoder *sevenFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
if !iter.readObjectStart() {
|
if !iter.readObjectStart() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for {
|
for {
|
||||||
switch iter.readFieldHash() {
|
switch iter.readFieldHash() {
|
||||||
case decoder.fieldHash1:
|
case decoder.fieldHash1:
|
||||||
decoder.fieldDecoder1.decode(ptr, iter)
|
decoder.fieldDecoder1.Decode(ptr, iter)
|
||||||
case decoder.fieldHash2:
|
case decoder.fieldHash2:
|
||||||
decoder.fieldDecoder2.decode(ptr, iter)
|
decoder.fieldDecoder2.Decode(ptr, iter)
|
||||||
case decoder.fieldHash3:
|
case decoder.fieldHash3:
|
||||||
decoder.fieldDecoder3.decode(ptr, iter)
|
decoder.fieldDecoder3.Decode(ptr, iter)
|
||||||
case decoder.fieldHash4:
|
case decoder.fieldHash4:
|
||||||
decoder.fieldDecoder4.decode(ptr, iter)
|
decoder.fieldDecoder4.Decode(ptr, iter)
|
||||||
case decoder.fieldHash5:
|
case decoder.fieldHash5:
|
||||||
decoder.fieldDecoder5.decode(ptr, iter)
|
decoder.fieldDecoder5.Decode(ptr, iter)
|
||||||
case decoder.fieldHash6:
|
case decoder.fieldHash6:
|
||||||
decoder.fieldDecoder6.decode(ptr, iter)
|
decoder.fieldDecoder6.Decode(ptr, iter)
|
||||||
case decoder.fieldHash7:
|
case decoder.fieldHash7:
|
||||||
decoder.fieldDecoder7.decode(ptr, iter)
|
decoder.fieldDecoder7.Decode(ptr, iter)
|
||||||
default:
|
default:
|
||||||
iter.Skip()
|
iter.Skip()
|
||||||
}
|
}
|
||||||
@ -831,47 +836,47 @@ func (decoder *sevenFieldsStructDecoder) decode(ptr unsafe.Pointer, iter *Iterat
|
|||||||
}
|
}
|
||||||
|
|
||||||
type eightFieldsStructDecoder struct {
|
type eightFieldsStructDecoder struct {
|
||||||
typ reflect.Type
|
typ reflect.Type
|
||||||
fieldHash1 int32
|
fieldHash1 int32
|
||||||
fieldDecoder1 *structFieldDecoder
|
fieldDecoder1 *structFieldDecoder
|
||||||
fieldHash2 int32
|
fieldHash2 int32
|
||||||
fieldDecoder2 *structFieldDecoder
|
fieldDecoder2 *structFieldDecoder
|
||||||
fieldHash3 int32
|
fieldHash3 int32
|
||||||
fieldDecoder3 *structFieldDecoder
|
fieldDecoder3 *structFieldDecoder
|
||||||
fieldHash4 int32
|
fieldHash4 int32
|
||||||
fieldDecoder4 *structFieldDecoder
|
fieldDecoder4 *structFieldDecoder
|
||||||
fieldHash5 int32
|
fieldHash5 int32
|
||||||
fieldDecoder5 *structFieldDecoder
|
fieldDecoder5 *structFieldDecoder
|
||||||
fieldHash6 int32
|
fieldHash6 int32
|
||||||
fieldDecoder6 *structFieldDecoder
|
fieldDecoder6 *structFieldDecoder
|
||||||
fieldHash7 int32
|
fieldHash7 int32
|
||||||
fieldDecoder7 *structFieldDecoder
|
fieldDecoder7 *structFieldDecoder
|
||||||
fieldHash8 int32
|
fieldHash8 int32
|
||||||
fieldDecoder8 *structFieldDecoder
|
fieldDecoder8 *structFieldDecoder
|
||||||
}
|
}
|
||||||
|
|
||||||
func (decoder *eightFieldsStructDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
|
func (decoder *eightFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
if !iter.readObjectStart() {
|
if !iter.readObjectStart() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for {
|
for {
|
||||||
switch iter.readFieldHash() {
|
switch iter.readFieldHash() {
|
||||||
case decoder.fieldHash1:
|
case decoder.fieldHash1:
|
||||||
decoder.fieldDecoder1.decode(ptr, iter)
|
decoder.fieldDecoder1.Decode(ptr, iter)
|
||||||
case decoder.fieldHash2:
|
case decoder.fieldHash2:
|
||||||
decoder.fieldDecoder2.decode(ptr, iter)
|
decoder.fieldDecoder2.Decode(ptr, iter)
|
||||||
case decoder.fieldHash3:
|
case decoder.fieldHash3:
|
||||||
decoder.fieldDecoder3.decode(ptr, iter)
|
decoder.fieldDecoder3.Decode(ptr, iter)
|
||||||
case decoder.fieldHash4:
|
case decoder.fieldHash4:
|
||||||
decoder.fieldDecoder4.decode(ptr, iter)
|
decoder.fieldDecoder4.Decode(ptr, iter)
|
||||||
case decoder.fieldHash5:
|
case decoder.fieldHash5:
|
||||||
decoder.fieldDecoder5.decode(ptr, iter)
|
decoder.fieldDecoder5.Decode(ptr, iter)
|
||||||
case decoder.fieldHash6:
|
case decoder.fieldHash6:
|
||||||
decoder.fieldDecoder6.decode(ptr, iter)
|
decoder.fieldDecoder6.Decode(ptr, iter)
|
||||||
case decoder.fieldHash7:
|
case decoder.fieldHash7:
|
||||||
decoder.fieldDecoder7.decode(ptr, iter)
|
decoder.fieldDecoder7.Decode(ptr, iter)
|
||||||
case decoder.fieldHash8:
|
case decoder.fieldHash8:
|
||||||
decoder.fieldDecoder8.decode(ptr, iter)
|
decoder.fieldDecoder8.Decode(ptr, iter)
|
||||||
default:
|
default:
|
||||||
iter.Skip()
|
iter.Skip()
|
||||||
}
|
}
|
||||||
@ -885,51 +890,51 @@ func (decoder *eightFieldsStructDecoder) decode(ptr unsafe.Pointer, iter *Iterat
|
|||||||
}
|
}
|
||||||
|
|
||||||
type nineFieldsStructDecoder struct {
|
type nineFieldsStructDecoder struct {
|
||||||
typ reflect.Type
|
typ reflect.Type
|
||||||
fieldHash1 int32
|
fieldHash1 int32
|
||||||
fieldDecoder1 *structFieldDecoder
|
fieldDecoder1 *structFieldDecoder
|
||||||
fieldHash2 int32
|
fieldHash2 int32
|
||||||
fieldDecoder2 *structFieldDecoder
|
fieldDecoder2 *structFieldDecoder
|
||||||
fieldHash3 int32
|
fieldHash3 int32
|
||||||
fieldDecoder3 *structFieldDecoder
|
fieldDecoder3 *structFieldDecoder
|
||||||
fieldHash4 int32
|
fieldHash4 int32
|
||||||
fieldDecoder4 *structFieldDecoder
|
fieldDecoder4 *structFieldDecoder
|
||||||
fieldHash5 int32
|
fieldHash5 int32
|
||||||
fieldDecoder5 *structFieldDecoder
|
fieldDecoder5 *structFieldDecoder
|
||||||
fieldHash6 int32
|
fieldHash6 int32
|
||||||
fieldDecoder6 *structFieldDecoder
|
fieldDecoder6 *structFieldDecoder
|
||||||
fieldHash7 int32
|
fieldHash7 int32
|
||||||
fieldDecoder7 *structFieldDecoder
|
fieldDecoder7 *structFieldDecoder
|
||||||
fieldHash8 int32
|
fieldHash8 int32
|
||||||
fieldDecoder8 *structFieldDecoder
|
fieldDecoder8 *structFieldDecoder
|
||||||
fieldHash9 int32
|
fieldHash9 int32
|
||||||
fieldDecoder9 *structFieldDecoder
|
fieldDecoder9 *structFieldDecoder
|
||||||
}
|
}
|
||||||
|
|
||||||
func (decoder *nineFieldsStructDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
|
func (decoder *nineFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
if !iter.readObjectStart() {
|
if !iter.readObjectStart() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for {
|
for {
|
||||||
switch iter.readFieldHash() {
|
switch iter.readFieldHash() {
|
||||||
case decoder.fieldHash1:
|
case decoder.fieldHash1:
|
||||||
decoder.fieldDecoder1.decode(ptr, iter)
|
decoder.fieldDecoder1.Decode(ptr, iter)
|
||||||
case decoder.fieldHash2:
|
case decoder.fieldHash2:
|
||||||
decoder.fieldDecoder2.decode(ptr, iter)
|
decoder.fieldDecoder2.Decode(ptr, iter)
|
||||||
case decoder.fieldHash3:
|
case decoder.fieldHash3:
|
||||||
decoder.fieldDecoder3.decode(ptr, iter)
|
decoder.fieldDecoder3.Decode(ptr, iter)
|
||||||
case decoder.fieldHash4:
|
case decoder.fieldHash4:
|
||||||
decoder.fieldDecoder4.decode(ptr, iter)
|
decoder.fieldDecoder4.Decode(ptr, iter)
|
||||||
case decoder.fieldHash5:
|
case decoder.fieldHash5:
|
||||||
decoder.fieldDecoder5.decode(ptr, iter)
|
decoder.fieldDecoder5.Decode(ptr, iter)
|
||||||
case decoder.fieldHash6:
|
case decoder.fieldHash6:
|
||||||
decoder.fieldDecoder6.decode(ptr, iter)
|
decoder.fieldDecoder6.Decode(ptr, iter)
|
||||||
case decoder.fieldHash7:
|
case decoder.fieldHash7:
|
||||||
decoder.fieldDecoder7.decode(ptr, iter)
|
decoder.fieldDecoder7.Decode(ptr, iter)
|
||||||
case decoder.fieldHash8:
|
case decoder.fieldHash8:
|
||||||
decoder.fieldDecoder8.decode(ptr, iter)
|
decoder.fieldDecoder8.Decode(ptr, iter)
|
||||||
case decoder.fieldHash9:
|
case decoder.fieldHash9:
|
||||||
decoder.fieldDecoder9.decode(ptr, iter)
|
decoder.fieldDecoder9.Decode(ptr, iter)
|
||||||
default:
|
default:
|
||||||
iter.Skip()
|
iter.Skip()
|
||||||
}
|
}
|
||||||
@ -966,32 +971,32 @@ type tenFieldsStructDecoder struct {
|
|||||||
fieldDecoder10 *structFieldDecoder
|
fieldDecoder10 *structFieldDecoder
|
||||||
}
|
}
|
||||||
|
|
||||||
func (decoder *tenFieldsStructDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
|
func (decoder *tenFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
if !iter.readObjectStart() {
|
if !iter.readObjectStart() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for {
|
for {
|
||||||
switch iter.readFieldHash() {
|
switch iter.readFieldHash() {
|
||||||
case decoder.fieldHash1:
|
case decoder.fieldHash1:
|
||||||
decoder.fieldDecoder1.decode(ptr, iter)
|
decoder.fieldDecoder1.Decode(ptr, iter)
|
||||||
case decoder.fieldHash2:
|
case decoder.fieldHash2:
|
||||||
decoder.fieldDecoder2.decode(ptr, iter)
|
decoder.fieldDecoder2.Decode(ptr, iter)
|
||||||
case decoder.fieldHash3:
|
case decoder.fieldHash3:
|
||||||
decoder.fieldDecoder3.decode(ptr, iter)
|
decoder.fieldDecoder3.Decode(ptr, iter)
|
||||||
case decoder.fieldHash4:
|
case decoder.fieldHash4:
|
||||||
decoder.fieldDecoder4.decode(ptr, iter)
|
decoder.fieldDecoder4.Decode(ptr, iter)
|
||||||
case decoder.fieldHash5:
|
case decoder.fieldHash5:
|
||||||
decoder.fieldDecoder5.decode(ptr, iter)
|
decoder.fieldDecoder5.Decode(ptr, iter)
|
||||||
case decoder.fieldHash6:
|
case decoder.fieldHash6:
|
||||||
decoder.fieldDecoder6.decode(ptr, iter)
|
decoder.fieldDecoder6.Decode(ptr, iter)
|
||||||
case decoder.fieldHash7:
|
case decoder.fieldHash7:
|
||||||
decoder.fieldDecoder7.decode(ptr, iter)
|
decoder.fieldDecoder7.Decode(ptr, iter)
|
||||||
case decoder.fieldHash8:
|
case decoder.fieldHash8:
|
||||||
decoder.fieldDecoder8.decode(ptr, iter)
|
decoder.fieldDecoder8.Decode(ptr, iter)
|
||||||
case decoder.fieldHash9:
|
case decoder.fieldHash9:
|
||||||
decoder.fieldDecoder9.decode(ptr, iter)
|
decoder.fieldDecoder9.Decode(ptr, iter)
|
||||||
case decoder.fieldHash10:
|
case decoder.fieldHash10:
|
||||||
decoder.fieldDecoder10.decode(ptr, iter)
|
decoder.fieldDecoder10.Decode(ptr, iter)
|
||||||
default:
|
default:
|
||||||
iter.Skip()
|
iter.Skip()
|
||||||
}
|
}
|
||||||
@ -1006,12 +1011,12 @@ func (decoder *tenFieldsStructDecoder) decode(ptr unsafe.Pointer, iter *Iterator
|
|||||||
|
|
||||||
type structFieldDecoder struct {
|
type structFieldDecoder struct {
|
||||||
field *reflect.StructField
|
field *reflect.StructField
|
||||||
fieldDecoder Decoder
|
fieldDecoder ValDecoder
|
||||||
}
|
}
|
||||||
|
|
||||||
func (decoder *structFieldDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
|
func (decoder *structFieldDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
fieldPtr := uintptr(ptr) + decoder.field.Offset
|
fieldPtr := uintptr(ptr) + decoder.field.Offset
|
||||||
decoder.fieldDecoder.decode(unsafe.Pointer(fieldPtr), iter)
|
decoder.fieldDecoder.Decode(unsafe.Pointer(fieldPtr), iter)
|
||||||
if iter.Error != nil && iter.Error != io.EOF {
|
if iter.Error != nil && iter.Error != io.EOF {
|
||||||
iter.Error = fmt.Errorf("%s: %s", decoder.field.Name, iter.Error.Error())
|
iter.Error = fmt.Errorf("%s: %s", decoder.field.Name, iter.Error.Error())
|
||||||
}
|
}
|
||||||
@ -1019,51 +1024,88 @@ func (decoder *structFieldDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
|
|||||||
|
|
||||||
type structFieldEncoder struct {
|
type structFieldEncoder struct {
|
||||||
field *reflect.StructField
|
field *reflect.StructField
|
||||||
fieldName string
|
fieldEncoder ValEncoder
|
||||||
fieldEncoder Encoder
|
omitempty bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (encoder *structFieldEncoder) encode(ptr unsafe.Pointer, stream *Stream) {
|
func (encoder *structFieldEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||||
fieldPtr := uintptr(ptr) + encoder.field.Offset
|
fieldPtr := uintptr(ptr) + encoder.field.Offset
|
||||||
stream.WriteObjectField(encoder.fieldName)
|
encoder.fieldEncoder.Encode(unsafe.Pointer(fieldPtr), stream)
|
||||||
encoder.fieldEncoder.encode(unsafe.Pointer(fieldPtr), stream)
|
|
||||||
if stream.Error != nil && stream.Error != io.EOF {
|
if stream.Error != nil && stream.Error != io.EOF {
|
||||||
stream.Error = fmt.Errorf("%s: %s", encoder.field.Name, stream.Error.Error())
|
stream.Error = fmt.Errorf("%s: %s", encoder.field.Name, stream.Error.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (encoder *structFieldEncoder) encodeInterface(val interface{}, stream *Stream) {
|
func (encoder *structFieldEncoder) EncodeInterface(val interface{}, stream *Stream) {
|
||||||
WriteToStream(val, stream, encoder)
|
WriteToStream(val, stream, encoder)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (encoder *structFieldEncoder) IsEmpty(ptr unsafe.Pointer) bool {
|
||||||
type structEncoder struct {
|
fieldPtr := uintptr(ptr) + encoder.field.Offset
|
||||||
firstField *structFieldEncoder
|
return encoder.fieldEncoder.IsEmpty(unsafe.Pointer(fieldPtr))
|
||||||
fields []*structFieldEncoder
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (encoder *structEncoder) encode(ptr unsafe.Pointer, stream *Stream) {
|
type structEncoder struct {
|
||||||
|
onePtrEmbedded bool
|
||||||
|
onePtrOptimization bool
|
||||||
|
fields []structFieldTo
|
||||||
|
}
|
||||||
|
|
||||||
|
type structFieldTo struct {
|
||||||
|
encoder *structFieldEncoder
|
||||||
|
toName string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *structEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||||
stream.WriteObjectStart()
|
stream.WriteObjectStart()
|
||||||
encoder.firstField.encode(ptr, stream)
|
isNotFirst := false
|
||||||
for _, field := range encoder.fields {
|
for _, field := range encoder.fields {
|
||||||
stream.WriteMore()
|
if field.encoder.omitempty && field.encoder.IsEmpty(ptr) {
|
||||||
field.encode(ptr, stream)
|
continue
|
||||||
|
}
|
||||||
|
if isNotFirst {
|
||||||
|
stream.WriteMore()
|
||||||
|
}
|
||||||
|
stream.WriteObjectField(field.toName)
|
||||||
|
field.encoder.Encode(ptr, stream)
|
||||||
|
isNotFirst = true
|
||||||
}
|
}
|
||||||
stream.WriteObjectEnd()
|
stream.WriteObjectEnd()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (encoder *structEncoder) encodeInterface(val interface{}, stream *Stream) {
|
func (encoder *structEncoder) EncodeInterface(val interface{}, stream *Stream) {
|
||||||
WriteToStream(val, stream, encoder)
|
e := (*emptyInterface)(unsafe.Pointer(&val))
|
||||||
|
if encoder.onePtrOptimization {
|
||||||
|
if e.word == nil && encoder.onePtrEmbedded {
|
||||||
|
stream.WriteObjectStart()
|
||||||
|
stream.WriteObjectEnd()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ptr := uintptr(e.word)
|
||||||
|
e.word = unsafe.Pointer(&ptr)
|
||||||
|
}
|
||||||
|
if reflect.TypeOf(val).Kind() == reflect.Ptr {
|
||||||
|
encoder.Encode(unsafe.Pointer(&e.word), stream)
|
||||||
|
} else {
|
||||||
|
encoder.Encode(e.word, stream)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (encoder *structEncoder) IsEmpty(ptr unsafe.Pointer) bool {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
type emptyStructEncoder struct {
|
type emptyStructEncoder struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (encoder *emptyStructEncoder) encode(ptr unsafe.Pointer, stream *Stream) {
|
func (encoder *emptyStructEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||||
stream.WriteEmptyObject()
|
stream.WriteEmptyObject()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (encoder *emptyStructEncoder) encodeInterface(val interface{}, stream *Stream) {
|
func (encoder *emptyStructEncoder) EncodeInterface(val interface{}, stream *Stream) {
|
||||||
WriteToStream(val, stream, encoder)
|
WriteToStream(val, stream, encoder)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (encoder *emptyStructEncoder) IsEmpty(ptr unsafe.Pointer) bool {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
168
feature_reflect_slice.go
Normal file
168
feature_reflect_slice.go
Normal file
@ -0,0 +1,168 @@
|
|||||||
|
package jsoniter
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"reflect"
|
||||||
|
"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 encoderOfSlice(cfg *frozenConfig, typ reflect.Type) (ValEncoder, error) {
|
||||||
|
encoder, err := encoderOfType(cfg, typ.Elem())
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if typ.Elem().Kind() == reflect.Map {
|
||||||
|
encoder = &optionalEncoder{encoder}
|
||||||
|
}
|
||||||
|
return &sliceEncoder{typ, typ.Elem(), encoder}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type sliceEncoder struct {
|
||||||
|
sliceType reflect.Type
|
||||||
|
elemType reflect.Type
|
||||||
|
elemEncoder ValEncoder
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *sliceEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||||
|
slice := (*sliceHeader)(ptr)
|
||||||
|
if slice.Data == nil {
|
||||||
|
stream.WriteNil()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if slice.Len == 0 {
|
||||||
|
stream.WriteEmptyArray()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
stream.WriteArrayStart()
|
||||||
|
elemPtr := uintptr(slice.Data)
|
||||||
|
encoder.elemEncoder.Encode(unsafe.Pointer(elemPtr), stream)
|
||||||
|
for i := 1; i < slice.Len; i++ {
|
||||||
|
stream.WriteMore()
|
||||||
|
elemPtr += encoder.elemType.Size()
|
||||||
|
encoder.elemEncoder.Encode(unsafe.Pointer(elemPtr), stream)
|
||||||
|
}
|
||||||
|
stream.WriteArrayEnd()
|
||||||
|
if stream.Error != nil && stream.Error != io.EOF {
|
||||||
|
stream.Error = fmt.Errorf("%v: %s", encoder.sliceType, stream.Error.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *sliceEncoder) EncodeInterface(val interface{}, stream *Stream) {
|
||||||
|
WriteToStream(val, stream, encoder)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *sliceEncoder) IsEmpty(ptr unsafe.Pointer) bool {
|
||||||
|
slice := (*sliceHeader)(ptr)
|
||||||
|
return slice.Len == 0
|
||||||
|
}
|
||||||
|
|
||||||
|
type sliceDecoder struct {
|
||||||
|
sliceType reflect.Type
|
||||||
|
elemType reflect.Type
|
||||||
|
elemDecoder ValDecoder
|
||||||
|
}
|
||||||
|
|
||||||
|
// sliceHeader is a safe version of SliceHeader used within this package.
|
||||||
|
type sliceHeader struct {
|
||||||
|
Data unsafe.Pointer
|
||||||
|
Len int
|
||||||
|
Cap int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (decoder *sliceDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
|
decoder.doDecode(ptr, iter)
|
||||||
|
if iter.Error != nil && iter.Error != io.EOF {
|
||||||
|
iter.Error = fmt.Errorf("%v: %s", decoder.sliceType, iter.Error.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (decoder *sliceDecoder) doDecode(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
|
slice := (*sliceHeader)(ptr)
|
||||||
|
if iter.ReadNil() {
|
||||||
|
slice.Len = 0
|
||||||
|
slice.Cap = 0
|
||||||
|
slice.Data = nil
|
||||||
|
return
|
||||||
|
}
|
||||||
|
reuseSlice(slice, decoder.sliceType, 4)
|
||||||
|
if !iter.ReadArray() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
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() {
|
||||||
|
growOne(slice, decoder.sliceType, decoder.elemType)
|
||||||
|
offset += decoder.elemType.Size()
|
||||||
|
decoder.elemDecoder.Decode(unsafe.Pointer(uintptr(slice.Data)+offset), iter)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// grow grows the slice s so that it can hold extra more values, allocating
|
||||||
|
// more capacity if needed. It also returns the old and new slice lengths.
|
||||||
|
func growOne(slice *sliceHeader, sliceType reflect.Type, elementType reflect.Type) {
|
||||||
|
newLen := slice.Len + 1
|
||||||
|
if newLen <= slice.Cap {
|
||||||
|
slice.Len = newLen
|
||||||
|
return
|
||||||
|
}
|
||||||
|
newCap := slice.Cap
|
||||||
|
if newCap == 0 {
|
||||||
|
newCap = 1
|
||||||
|
} else {
|
||||||
|
for newCap < newLen {
|
||||||
|
if slice.Len < 1024 {
|
||||||
|
newCap += newCap
|
||||||
|
} else {
|
||||||
|
newCap += newCap / 4
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dst := unsafe.Pointer(reflect.MakeSlice(sliceType, newLen, newCap).Pointer())
|
||||||
|
// 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]
|
||||||
|
}
|
||||||
|
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
|
||||||
|
slice.Data = dst
|
||||||
|
}
|
@ -5,16 +5,23 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Stream struct {
|
type Stream struct {
|
||||||
out io.Writer
|
cfg *frozenConfig
|
||||||
buf []byte
|
out io.Writer
|
||||||
n int
|
buf []byte
|
||||||
Error error
|
n int
|
||||||
indention int
|
Error error
|
||||||
IndentionStep int
|
indention int
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewStream(out io.Writer, bufSize int) *Stream {
|
func NewStream(cfg *frozenConfig, out io.Writer, bufSize int) *Stream {
|
||||||
return &Stream{out, make([]byte, bufSize), 0, nil, 0, 0}
|
return &Stream{
|
||||||
|
cfg: cfg,
|
||||||
|
out: out,
|
||||||
|
buf: make([]byte, bufSize),
|
||||||
|
n: 0,
|
||||||
|
Error: nil,
|
||||||
|
indention: 0,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Stream) Reset(out io.Writer) {
|
func (b *Stream) Reset(out io.Writer) {
|
||||||
@ -32,24 +39,32 @@ func (b *Stream) Buffered() int {
|
|||||||
return b.n
|
return b.n
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *Stream) Buffer() []byte {
|
||||||
|
return b.buf[:b.n]
|
||||||
|
}
|
||||||
|
|
||||||
// Write writes the contents of p into the buffer.
|
// Write writes the contents of p into the buffer.
|
||||||
// It returns the number of bytes written.
|
// It returns the number of bytes written.
|
||||||
// If nn < len(p), it also returns an error explaining
|
// If nn < len(p), it also returns an error explaining
|
||||||
// why the write is short.
|
// why the write is short.
|
||||||
func (b *Stream) Write(p []byte) (nn int, err error) {
|
func (b *Stream) Write(p []byte) (nn int, err error) {
|
||||||
for len(p) > b.Available() && b.Error == nil {
|
for len(p) > b.Available() && b.Error == nil {
|
||||||
var n int
|
if b.out == nil {
|
||||||
if b.Buffered() == 0 {
|
b.growAtLeast(len(p))
|
||||||
// Large write, empty buffer.
|
|
||||||
// Write directly from p to avoid copy.
|
|
||||||
n, b.Error = b.out.Write(p)
|
|
||||||
} else {
|
} else {
|
||||||
n = copy(b.buf[b.n:], p)
|
var n int
|
||||||
b.n += n
|
if b.Buffered() == 0 {
|
||||||
b.Flush()
|
// Large write, empty buffer.
|
||||||
|
// Write directly from p to avoid copy.
|
||||||
|
n, b.Error = b.out.Write(p)
|
||||||
|
} else {
|
||||||
|
n = copy(b.buf[b.n:], p)
|
||||||
|
b.n += n
|
||||||
|
b.Flush()
|
||||||
|
}
|
||||||
|
nn += n
|
||||||
|
p = p[n:]
|
||||||
}
|
}
|
||||||
nn += n
|
|
||||||
p = p[n:]
|
|
||||||
}
|
}
|
||||||
if b.Error != nil {
|
if b.Error != nil {
|
||||||
return nn, b.Error
|
return nn, b.Error
|
||||||
@ -60,14 +75,13 @@ func (b *Stream) Write(p []byte) (nn int, err error) {
|
|||||||
return nn, nil
|
return nn, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// WriteByte writes a single byte.
|
// WriteByte writes a single byte.
|
||||||
func (b *Stream) writeByte(c byte) {
|
func (b *Stream) writeByte(c byte) {
|
||||||
if b.Error != nil {
|
if b.Error != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if b.Available() <= 0 && b.Flush() != nil {
|
if b.Available() < 1 {
|
||||||
return
|
b.growAtLeast(1)
|
||||||
}
|
}
|
||||||
b.buf[b.n] = c
|
b.buf[b.n] = c
|
||||||
b.n++
|
b.n++
|
||||||
@ -77,11 +91,11 @@ func (b *Stream) writeTwoBytes(c1 byte, c2 byte) {
|
|||||||
if b.Error != nil {
|
if b.Error != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if b.Available() <= 1 && b.Flush() != nil {
|
if b.Available() < 2 {
|
||||||
return
|
b.growAtLeast(2)
|
||||||
}
|
}
|
||||||
b.buf[b.n] = c1
|
b.buf[b.n] = c1
|
||||||
b.buf[b.n + 1] = c2
|
b.buf[b.n+1] = c2
|
||||||
b.n += 2
|
b.n += 2
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -89,12 +103,12 @@ func (b *Stream) writeThreeBytes(c1 byte, c2 byte, c3 byte) {
|
|||||||
if b.Error != nil {
|
if b.Error != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if b.Available() <= 2 && b.Flush() != nil {
|
if b.Available() < 3 {
|
||||||
return
|
b.growAtLeast(3)
|
||||||
}
|
}
|
||||||
b.buf[b.n] = c1
|
b.buf[b.n] = c1
|
||||||
b.buf[b.n + 1] = c2
|
b.buf[b.n+1] = c2
|
||||||
b.buf[b.n + 2] = c3
|
b.buf[b.n+2] = c3
|
||||||
b.n += 3
|
b.n += 3
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -102,13 +116,13 @@ func (b *Stream) writeFourBytes(c1 byte, c2 byte, c3 byte, c4 byte) {
|
|||||||
if b.Error != nil {
|
if b.Error != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if b.Available() <= 3 && b.Flush() != nil {
|
if b.Available() < 4 {
|
||||||
return
|
b.growAtLeast(4)
|
||||||
}
|
}
|
||||||
b.buf[b.n] = c1
|
b.buf[b.n] = c1
|
||||||
b.buf[b.n + 1] = c2
|
b.buf[b.n+1] = c2
|
||||||
b.buf[b.n + 2] = c3
|
b.buf[b.n+2] = c3
|
||||||
b.buf[b.n + 3] = c4
|
b.buf[b.n+3] = c4
|
||||||
b.n += 4
|
b.n += 4
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -116,19 +130,22 @@ func (b *Stream) writeFiveBytes(c1 byte, c2 byte, c3 byte, c4 byte, c5 byte) {
|
|||||||
if b.Error != nil {
|
if b.Error != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if b.Available() <= 3 && b.Flush() != nil {
|
if b.Available() < 5 {
|
||||||
return
|
b.growAtLeast(5)
|
||||||
}
|
}
|
||||||
b.buf[b.n] = c1
|
b.buf[b.n] = c1
|
||||||
b.buf[b.n + 1] = c2
|
b.buf[b.n+1] = c2
|
||||||
b.buf[b.n + 2] = c3
|
b.buf[b.n+2] = c3
|
||||||
b.buf[b.n + 3] = c4
|
b.buf[b.n+3] = c4
|
||||||
b.buf[b.n + 4] = c5
|
b.buf[b.n+4] = c5
|
||||||
b.n += 5
|
b.n += 5
|
||||||
}
|
}
|
||||||
|
|
||||||
// Flush writes any buffered data to the underlying io.Writer.
|
// Flush writes any buffered data to the underlying io.Writer.
|
||||||
func (b *Stream) Flush() error {
|
func (b *Stream) Flush() error {
|
||||||
|
if b.out == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
if b.Error != nil {
|
if b.Error != nil {
|
||||||
return b.Error
|
return b.Error
|
||||||
}
|
}
|
||||||
@ -141,7 +158,7 @@ func (b *Stream) Flush() error {
|
|||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if n > 0 && n < b.n {
|
if n > 0 && n < b.n {
|
||||||
copy(b.buf[0:b.n - n], b.buf[n:b.n])
|
copy(b.buf[0:b.n-n], b.buf[n:b.n])
|
||||||
}
|
}
|
||||||
b.n -= n
|
b.n -= n
|
||||||
b.Error = err
|
b.Error = err
|
||||||
@ -151,13 +168,28 @@ func (b *Stream) Flush() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Stream) WriteRaw(s string) {
|
func (b *Stream) ensure(minimal int) {
|
||||||
for len(s) > b.Available() && b.Error == nil {
|
available := b.Available()
|
||||||
n := copy(b.buf[b.n:], s)
|
if available < minimal {
|
||||||
b.n += n
|
if b.n > 1024 {
|
||||||
s = s[n:]
|
b.Flush()
|
||||||
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 {
|
if b.Error != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -165,69 +197,6 @@ func (b *Stream) WriteRaw(s string) {
|
|||||||
b.n += n
|
b.n += n
|
||||||
}
|
}
|
||||||
|
|
||||||
func (stream *Stream) WriteString(s string) {
|
|
||||||
valLen := len(s)
|
|
||||||
toWriteLen := valLen
|
|
||||||
bufLengthMinusTwo := len(stream.buf) - 2 // make room for the quotes
|
|
||||||
if stream.n + toWriteLen > bufLengthMinusTwo {
|
|
||||||
toWriteLen = bufLengthMinusTwo - stream.n
|
|
||||||
}
|
|
||||||
if toWriteLen < 0 {
|
|
||||||
stream.Flush()
|
|
||||||
if stream.n + toWriteLen > bufLengthMinusTwo {
|
|
||||||
toWriteLen = bufLengthMinusTwo - stream.n
|
|
||||||
}
|
|
||||||
}
|
|
||||||
n := stream.n
|
|
||||||
stream.buf[n] = '"'
|
|
||||||
n++
|
|
||||||
// write string, the fast path, without utf8 and escape support
|
|
||||||
i := 0
|
|
||||||
for ; i < toWriteLen; i++ {
|
|
||||||
c := s[i]
|
|
||||||
if c > 31 && c != '"' && c != '\\' {
|
|
||||||
stream.buf[n] = c
|
|
||||||
n++
|
|
||||||
} else {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if i == valLen {
|
|
||||||
stream.buf[n] = '"'
|
|
||||||
n++
|
|
||||||
stream.n = n
|
|
||||||
return
|
|
||||||
}
|
|
||||||
stream.n = n
|
|
||||||
// for the remaining parts, we process them char by char
|
|
||||||
stream.writeStringSlowPath(s, i, valLen);
|
|
||||||
stream.writeByte('"')
|
|
||||||
}
|
|
||||||
|
|
||||||
func (stream *Stream) writeStringSlowPath(s string, i int, valLen int) {
|
|
||||||
for ; i < valLen; i++ {
|
|
||||||
c := s[i]
|
|
||||||
switch (c) {
|
|
||||||
case '"':
|
|
||||||
stream.writeTwoBytes('\\', '"')
|
|
||||||
case '\\':
|
|
||||||
stream.writeTwoBytes('\\', '\\')
|
|
||||||
case '\b':
|
|
||||||
stream.writeTwoBytes('\\', 'b')
|
|
||||||
case '\f':
|
|
||||||
stream.writeTwoBytes('\\', 'f')
|
|
||||||
case '\n':
|
|
||||||
stream.writeTwoBytes('\\', 'n')
|
|
||||||
case '\r':
|
|
||||||
stream.writeTwoBytes('\\', 'r')
|
|
||||||
case '\t':
|
|
||||||
stream.writeTwoBytes('\\', 't')
|
|
||||||
default:
|
|
||||||
stream.writeByte(c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (stream *Stream) WriteNil() {
|
func (stream *Stream) WriteNil() {
|
||||||
stream.writeFourBytes('n', 'u', 'l', 'l')
|
stream.writeFourBytes('n', 'u', 'l', 'l')
|
||||||
}
|
}
|
||||||
@ -249,19 +218,23 @@ func (stream *Stream) WriteBool(val bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (stream *Stream) WriteObjectStart() {
|
func (stream *Stream) WriteObjectStart() {
|
||||||
stream.indention += stream.IndentionStep
|
stream.indention += stream.cfg.indentionStep
|
||||||
stream.writeByte('{')
|
stream.writeByte('{')
|
||||||
stream.writeIndention(0)
|
stream.writeIndention(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (stream *Stream) WriteObjectField(field string) {
|
func (stream *Stream) WriteObjectField(field string) {
|
||||||
stream.WriteString(field)
|
stream.WriteString(field)
|
||||||
stream.writeByte(':')
|
if stream.indention > 0 {
|
||||||
|
stream.writeTwoBytes(':', ' ')
|
||||||
|
} else {
|
||||||
|
stream.writeByte(':')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (stream *Stream) WriteObjectEnd() {
|
func (stream *Stream) WriteObjectEnd() {
|
||||||
stream.writeIndention(stream.IndentionStep)
|
stream.writeIndention(stream.cfg.indentionStep)
|
||||||
stream.indention -= stream.IndentionStep
|
stream.indention -= stream.cfg.indentionStep
|
||||||
stream.writeByte('}')
|
stream.writeByte('}')
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -276,7 +249,7 @@ func (stream *Stream) WriteMore() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (stream *Stream) WriteArrayStart() {
|
func (stream *Stream) WriteArrayStart() {
|
||||||
stream.indention += stream.IndentionStep
|
stream.indention += stream.cfg.indentionStep
|
||||||
stream.writeByte('[')
|
stream.writeByte('[')
|
||||||
stream.writeIndention(0)
|
stream.writeIndention(0)
|
||||||
}
|
}
|
||||||
@ -287,27 +260,20 @@ func (stream *Stream) WriteEmptyArray() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (stream *Stream) WriteArrayEnd() {
|
func (stream *Stream) WriteArrayEnd() {
|
||||||
stream.writeIndention(stream.IndentionStep)
|
stream.writeIndention(stream.cfg.indentionStep)
|
||||||
stream.indention -= stream.IndentionStep
|
stream.indention -= stream.cfg.indentionStep
|
||||||
stream.writeByte(']')
|
stream.writeByte(']')
|
||||||
}
|
}
|
||||||
|
|
||||||
func (stream *Stream) writeIndention(delta int) {
|
func (stream *Stream) writeIndention(delta int) {
|
||||||
if (stream.indention == 0) {
|
if stream.indention == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
stream.writeByte('\n')
|
stream.writeByte('\n')
|
||||||
toWrite := stream.indention - delta
|
toWrite := stream.indention - delta
|
||||||
i := 0
|
stream.ensure(toWrite)
|
||||||
for {
|
for i := 0; i < toWrite && stream.n < len(stream.buf); i++ {
|
||||||
for ; i < toWrite && stream.n < len(stream.buf); i++ {
|
stream.buf[stream.n] = ' '
|
||||||
stream.buf[stream.n] = ' '
|
stream.n++
|
||||||
stream.n ++
|
|
||||||
}
|
|
||||||
if i == toWrite {
|
|
||||||
break;
|
|
||||||
} else {
|
|
||||||
stream.Flush()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -2,70 +2,91 @@ package jsoniter
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"math"
|
||||||
)
|
)
|
||||||
|
|
||||||
var POW10 []uint64
|
var _POW10 []uint64
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
POW10 = []uint64{1, 10, 100, 1000, 10000, 100000, 1000000}
|
_POW10 = []uint64{1, 10, 100, 1000, 10000, 100000, 1000000}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (stream *Stream) WriteFloat32(val float32) {
|
func (stream *Stream) WriteFloat32(val float32) {
|
||||||
|
abs := math.Abs(float64(val))
|
||||||
|
fmt := byte('f')
|
||||||
|
// Note: Must use float32 comparisons for underlying float32 value to get precise cutoffs right.
|
||||||
|
if abs != 0 {
|
||||||
|
if float32(abs) < 1e-6 || float32(abs) >= 1e21 {
|
||||||
|
fmt = 'e'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
stream.WriteRaw(strconv.FormatFloat(float64(val), fmt, -1, 32))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (stream *Stream) WriteFloat32Lossy(val float32) {
|
||||||
if val < 0 {
|
if val < 0 {
|
||||||
stream.writeByte('-')
|
stream.writeByte('-')
|
||||||
val = -val
|
val = -val
|
||||||
}
|
}
|
||||||
if val > 0x4ffffff {
|
if val > 0x4ffffff {
|
||||||
stream.WriteRaw(strconv.FormatFloat(float64(val), 'f', -1, 32));
|
stream.WriteFloat32(val)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
precision := 6
|
precision := 6
|
||||||
exp := uint64(1000000) // 6
|
exp := uint64(1000000) // 6
|
||||||
lval := uint64(float64(val) * float64(exp) + 0.5)
|
lval := uint64(float64(val)*float64(exp) + 0.5)
|
||||||
stream.WriteUint64(lval / exp)
|
stream.WriteUint64(lval / exp)
|
||||||
fval := lval % exp
|
fval := lval % exp
|
||||||
if fval == 0 {
|
if fval == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
stream.writeByte('.')
|
stream.writeByte('.')
|
||||||
if stream.Available() < 10 {
|
stream.ensure(10)
|
||||||
stream.Flush()
|
for p := precision - 1; p > 0 && fval < _POW10[p]; p-- {
|
||||||
}
|
|
||||||
for p := precision - 1; p > 0 && fval < POW10[p]; p-- {
|
|
||||||
stream.writeByte('0')
|
stream.writeByte('0')
|
||||||
}
|
}
|
||||||
stream.WriteUint64(fval);
|
stream.WriteUint64(fval)
|
||||||
for stream.buf[stream.n - 1] == '0' {
|
for stream.buf[stream.n-1] == '0' {
|
||||||
stream.n--;
|
stream.n--
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (stream *Stream) WriteFloat64(val float64) {
|
func (stream *Stream) WriteFloat64(val float64) {
|
||||||
|
abs := math.Abs(val)
|
||||||
|
fmt := byte('f')
|
||||||
|
// Note: Must use float32 comparisons for underlying float32 value to get precise cutoffs right.
|
||||||
|
if abs != 0 {
|
||||||
|
if abs < 1e-6 || abs >= 1e21 {
|
||||||
|
fmt = 'e'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
stream.WriteRaw(strconv.FormatFloat(float64(val), fmt, -1, 64))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (stream *Stream) WriteFloat64Lossy(val float64) {
|
||||||
if val < 0 {
|
if val < 0 {
|
||||||
stream.writeByte('-')
|
stream.writeByte('-')
|
||||||
val = -val
|
val = -val
|
||||||
}
|
}
|
||||||
if val > 0x4ffffff {
|
if val > 0x4ffffff {
|
||||||
stream.WriteRaw(strconv.FormatFloat(val, 'f', -1, 64));
|
stream.WriteFloat64(val)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
precision := 6
|
precision := 6
|
||||||
exp := uint64(1000000) // 6
|
exp := uint64(1000000) // 6
|
||||||
lval := uint64(val * float64(exp) + 0.5)
|
lval := uint64(val*float64(exp) + 0.5)
|
||||||
stream.WriteUint64(lval / exp)
|
stream.WriteUint64(lval / exp)
|
||||||
fval := lval % exp
|
fval := lval % exp
|
||||||
if fval == 0 {
|
if fval == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
stream.writeByte('.')
|
stream.writeByte('.')
|
||||||
if stream.Available() < 10 {
|
stream.ensure(10)
|
||||||
stream.Flush()
|
for p := precision - 1; p > 0 && fval < _POW10[p]; p-- {
|
||||||
}
|
|
||||||
for p := precision - 1; p > 0 && fval < POW10[p]; p-- {
|
|
||||||
stream.writeByte('0')
|
stream.writeByte('0')
|
||||||
}
|
}
|
||||||
stream.WriteUint64(fval);
|
stream.WriteUint64(fval)
|
||||||
for stream.buf[stream.n - 1] == '0' {
|
for stream.buf[stream.n-1] == '0' {
|
||||||
stream.n--;
|
stream.n--
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,50 +1,15 @@
|
|||||||
package jsoniter
|
package jsoniter
|
||||||
|
|
||||||
var digits []uint8
|
var _DIGITS []uint32
|
||||||
var digitTens []uint8
|
|
||||||
var digitOnes []uint8
|
|
||||||
var DIGITS []uint32
|
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
digits = []uint8{
|
_DIGITS = make([]uint32, 1000)
|
||||||
'0', '1', '2', '3', '4', '5',
|
|
||||||
'6', '7', '8', '9', 'a', 'b',
|
|
||||||
'c', 'd', 'e', 'f', 'g', 'h',
|
|
||||||
'i', 'j', 'k', 'l', 'm', 'n',
|
|
||||||
'o', 'p', 'q', 'r', 's', 't',
|
|
||||||
'u', 'v', 'w', 'x', 'y', 'z',
|
|
||||||
}
|
|
||||||
digitTens = []uint8{
|
|
||||||
'0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
|
|
||||||
'1', '1', '1', '1', '1', '1', '1', '1', '1', '1',
|
|
||||||
'2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
|
|
||||||
'3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
|
|
||||||
'4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
|
|
||||||
'5', '5', '5', '5', '5', '5', '5', '5', '5', '5',
|
|
||||||
'6', '6', '6', '6', '6', '6', '6', '6', '6', '6',
|
|
||||||
'7', '7', '7', '7', '7', '7', '7', '7', '7', '7',
|
|
||||||
'8', '8', '8', '8', '8', '8', '8', '8', '8', '8',
|
|
||||||
'9', '9', '9', '9', '9', '9', '9', '9', '9', '9',
|
|
||||||
}
|
|
||||||
digitOnes = []uint8{
|
|
||||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
|
||||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
|
||||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
|
||||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
|
||||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
|
||||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
|
||||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
|
||||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
|
||||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
|
||||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
|
||||||
}
|
|
||||||
DIGITS = make([]uint32, 1000)
|
|
||||||
for i := uint32(0); i < 1000; i++ {
|
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 {
|
if i < 10 {
|
||||||
DIGITS[i] += 2 << 24
|
_DIGITS[i] += 2 << 24
|
||||||
} else if i < 100 {
|
} else if i < 100 {
|
||||||
DIGITS[i] += 1 << 24
|
_DIGITS[i] += 1 << 24
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -67,56 +32,48 @@ func writeFirstBuf(buf []byte, v uint32, n int) int {
|
|||||||
|
|
||||||
func writeBuf(buf []byte, v uint32, n int) {
|
func writeBuf(buf []byte, v uint32, n int) {
|
||||||
buf[n] = byte(v >> 16)
|
buf[n] = byte(v >> 16)
|
||||||
buf[n + 1] = byte(v >> 8)
|
buf[n+1] = byte(v >> 8)
|
||||||
buf[n + 2] = byte(v)
|
buf[n+2] = byte(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (stream *Stream) WriteUint8(val uint8) {
|
func (stream *Stream) WriteUint8(val uint8) {
|
||||||
if stream.Available() < 3 {
|
stream.ensure(3)
|
||||||
stream.Flush()
|
stream.n = writeFirstBuf(stream.buf, _DIGITS[val], stream.n)
|
||||||
}
|
|
||||||
stream.n = writeFirstBuf(stream.buf, DIGITS[val], stream.n)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (stream *Stream) WriteInt8(nval int8) {
|
func (stream *Stream) WriteInt8(nval int8) {
|
||||||
if stream.Available() < 4 {
|
stream.ensure(4)
|
||||||
stream.Flush()
|
|
||||||
}
|
|
||||||
n := stream.n
|
n := stream.n
|
||||||
var val uint8
|
var val uint8
|
||||||
if (nval < 0) {
|
if nval < 0 {
|
||||||
val = uint8(-nval)
|
val = uint8(-nval)
|
||||||
stream.buf[n] = '-'
|
stream.buf[n] = '-'
|
||||||
n++
|
n++
|
||||||
} else {
|
} else {
|
||||||
val = uint8(nval)
|
val = uint8(nval)
|
||||||
}
|
}
|
||||||
stream.n = writeFirstBuf(stream.buf, DIGITS[val], n)
|
stream.n = writeFirstBuf(stream.buf, _DIGITS[val], n)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (stream *Stream) WriteUint16(val uint16) {
|
func (stream *Stream) WriteUint16(val uint16) {
|
||||||
if stream.Available() < 5 {
|
stream.ensure(5)
|
||||||
stream.Flush()
|
|
||||||
}
|
|
||||||
q1 := val / 1000
|
q1 := val / 1000
|
||||||
if q1 == 0 {
|
if q1 == 0 {
|
||||||
stream.n = writeFirstBuf(stream.buf, DIGITS[val], stream.n)
|
stream.n = writeFirstBuf(stream.buf, _DIGITS[val], stream.n)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
r1 := val - q1 * 1000;
|
r1 := val - q1*1000
|
||||||
n := writeFirstBuf(stream.buf, DIGITS[q1], stream.n)
|
n := writeFirstBuf(stream.buf, _DIGITS[q1], stream.n)
|
||||||
writeBuf(stream.buf, DIGITS[r1], n)
|
writeBuf(stream.buf, _DIGITS[r1], n)
|
||||||
stream.n = n + 3
|
stream.n = n + 3
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (stream *Stream) WriteInt16(nval int16) {
|
func (stream *Stream) WriteInt16(nval int16) {
|
||||||
if stream.Available() < 6 {
|
stream.ensure(6)
|
||||||
stream.Flush()
|
|
||||||
}
|
|
||||||
n := stream.n
|
n := stream.n
|
||||||
var val uint16
|
var val uint16
|
||||||
if (nval < 0) {
|
if nval < 0 {
|
||||||
val = uint16(-nval)
|
val = uint16(-nval)
|
||||||
stream.buf[n] = '-'
|
stream.buf[n] = '-'
|
||||||
n++
|
n++
|
||||||
@ -125,57 +82,53 @@ func (stream *Stream) WriteInt16(nval int16) {
|
|||||||
}
|
}
|
||||||
q1 := val / 1000
|
q1 := val / 1000
|
||||||
if q1 == 0 {
|
if q1 == 0 {
|
||||||
stream.n = writeFirstBuf(stream.buf, DIGITS[val], n)
|
stream.n = writeFirstBuf(stream.buf, _DIGITS[val], n)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
r1 := val - q1 * 1000;
|
r1 := val - q1*1000
|
||||||
n = writeFirstBuf(stream.buf, DIGITS[q1], n)
|
n = writeFirstBuf(stream.buf, _DIGITS[q1], n)
|
||||||
writeBuf(stream.buf, DIGITS[r1], n)
|
writeBuf(stream.buf, _DIGITS[r1], n)
|
||||||
stream.n = n + 3
|
stream.n = n + 3
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (stream *Stream) WriteUint32(val uint32) {
|
func (stream *Stream) WriteUint32(val uint32) {
|
||||||
if stream.Available() < 10 {
|
stream.ensure(10)
|
||||||
stream.Flush()
|
|
||||||
}
|
|
||||||
n := stream.n
|
n := stream.n
|
||||||
q1 := val / 1000
|
q1 := val / 1000
|
||||||
if q1 == 0 {
|
if q1 == 0 {
|
||||||
stream.n = writeFirstBuf(stream.buf, DIGITS[val], n)
|
stream.n = writeFirstBuf(stream.buf, _DIGITS[val], n)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
r1 := val - q1 * 1000;
|
r1 := val - q1*1000
|
||||||
q2 := q1 / 1000
|
q2 := q1 / 1000
|
||||||
if q2 == 0 {
|
if q2 == 0 {
|
||||||
n := writeFirstBuf(stream.buf, DIGITS[q1], n)
|
n := writeFirstBuf(stream.buf, _DIGITS[q1], n)
|
||||||
writeBuf(stream.buf, DIGITS[r1], n)
|
writeBuf(stream.buf, _DIGITS[r1], n)
|
||||||
stream.n = n + 3
|
stream.n = n + 3
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
r2 := q1 - q2 * 1000
|
r2 := q1 - q2*1000
|
||||||
q3 := q2 / 1000
|
q3 := q2 / 1000
|
||||||
if q3 == 0 {
|
if q3 == 0 {
|
||||||
n = writeFirstBuf(stream.buf, DIGITS[q2], n)
|
n = writeFirstBuf(stream.buf, _DIGITS[q2], n)
|
||||||
} else {
|
} else {
|
||||||
r3 := q2 - q3 * 1000
|
r3 := q2 - q3*1000
|
||||||
stream.buf[n] = byte(q3 + '0')
|
stream.buf[n] = byte(q3 + '0')
|
||||||
n++
|
n++
|
||||||
writeBuf(stream.buf, DIGITS[r3], n)
|
writeBuf(stream.buf, _DIGITS[r3], n)
|
||||||
n += 3
|
n += 3
|
||||||
}
|
}
|
||||||
writeBuf(stream.buf, DIGITS[r2], n)
|
writeBuf(stream.buf, _DIGITS[r2], n)
|
||||||
writeBuf(stream.buf, DIGITS[r1], n + 3)
|
writeBuf(stream.buf, _DIGITS[r1], n+3)
|
||||||
stream.n = n + 6
|
stream.n = n + 6
|
||||||
}
|
}
|
||||||
|
|
||||||
func (stream *Stream) WriteInt32(nval int32) {
|
func (stream *Stream) WriteInt32(nval int32) {
|
||||||
if stream.Available() < 11 {
|
stream.ensure(11)
|
||||||
stream.Flush()
|
|
||||||
}
|
|
||||||
n := stream.n
|
n := stream.n
|
||||||
var val uint32
|
var val uint32
|
||||||
if (nval < 0) {
|
if nval < 0 {
|
||||||
val = uint32(-nval)
|
val = uint32(-nval)
|
||||||
stream.buf[n] = '-'
|
stream.buf[n] = '-'
|
||||||
n++
|
n++
|
||||||
@ -184,106 +137,102 @@ func (stream *Stream) WriteInt32(nval int32) {
|
|||||||
}
|
}
|
||||||
q1 := val / 1000
|
q1 := val / 1000
|
||||||
if q1 == 0 {
|
if q1 == 0 {
|
||||||
stream.n = writeFirstBuf(stream.buf, DIGITS[val], n)
|
stream.n = writeFirstBuf(stream.buf, _DIGITS[val], n)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
r1 := val - q1 * 1000;
|
r1 := val - q1*1000
|
||||||
q2 := q1 / 1000
|
q2 := q1 / 1000
|
||||||
if q2 == 0 {
|
if q2 == 0 {
|
||||||
n := writeFirstBuf(stream.buf, DIGITS[q1], n)
|
n := writeFirstBuf(stream.buf, _DIGITS[q1], n)
|
||||||
writeBuf(stream.buf, DIGITS[r1], n)
|
writeBuf(stream.buf, _DIGITS[r1], n)
|
||||||
stream.n = n + 3
|
stream.n = n + 3
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
r2 := q1 - q2 * 1000
|
r2 := q1 - q2*1000
|
||||||
q3 := q2 / 1000
|
q3 := q2 / 1000
|
||||||
if q3 == 0 {
|
if q3 == 0 {
|
||||||
n = writeFirstBuf(stream.buf, DIGITS[q2], n)
|
n = writeFirstBuf(stream.buf, _DIGITS[q2], n)
|
||||||
} else {
|
} else {
|
||||||
r3 := q2 - q3 * 1000
|
r3 := q2 - q3*1000
|
||||||
stream.buf[n] = byte(q3 + '0')
|
stream.buf[n] = byte(q3 + '0')
|
||||||
n++
|
n++
|
||||||
writeBuf(stream.buf, DIGITS[r3], n)
|
writeBuf(stream.buf, _DIGITS[r3], n)
|
||||||
n += 3
|
n += 3
|
||||||
}
|
}
|
||||||
writeBuf(stream.buf, DIGITS[r2], n)
|
writeBuf(stream.buf, _DIGITS[r2], n)
|
||||||
writeBuf(stream.buf, DIGITS[r1], n + 3)
|
writeBuf(stream.buf, _DIGITS[r1], n+3)
|
||||||
stream.n = n + 6
|
stream.n = n + 6
|
||||||
}
|
}
|
||||||
|
|
||||||
func (stream *Stream) WriteUint64(val uint64) {
|
func (stream *Stream) WriteUint64(val uint64) {
|
||||||
if stream.Available() < 20 {
|
stream.ensure(20)
|
||||||
stream.Flush()
|
|
||||||
}
|
|
||||||
n := stream.n
|
n := stream.n
|
||||||
q1 := val / 1000
|
q1 := val / 1000
|
||||||
if q1 == 0 {
|
if q1 == 0 {
|
||||||
stream.n = writeFirstBuf(stream.buf, DIGITS[val], n)
|
stream.n = writeFirstBuf(stream.buf, _DIGITS[val], n)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
r1 := val - q1 * 1000;
|
r1 := val - q1*1000
|
||||||
q2 := q1 / 1000
|
q2 := q1 / 1000
|
||||||
if q2 == 0 {
|
if q2 == 0 {
|
||||||
n := writeFirstBuf(stream.buf, DIGITS[q1], n)
|
n := writeFirstBuf(stream.buf, _DIGITS[q1], n)
|
||||||
writeBuf(stream.buf, DIGITS[r1], n)
|
writeBuf(stream.buf, _DIGITS[r1], n)
|
||||||
stream.n = n + 3
|
stream.n = n + 3
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
r2 := q1 - q2 * 1000
|
r2 := q1 - q2*1000
|
||||||
q3 := q2 / 1000
|
q3 := q2 / 1000
|
||||||
if q3 == 0 {
|
if q3 == 0 {
|
||||||
n = writeFirstBuf(stream.buf, DIGITS[q2], n)
|
n = writeFirstBuf(stream.buf, _DIGITS[q2], n)
|
||||||
writeBuf(stream.buf, DIGITS[r2], n)
|
writeBuf(stream.buf, _DIGITS[r2], n)
|
||||||
writeBuf(stream.buf, DIGITS[r1], n + 3)
|
writeBuf(stream.buf, _DIGITS[r1], n+3)
|
||||||
stream.n = n + 6
|
stream.n = n + 6
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
r3 := q2 - q3 * 1000
|
r3 := q2 - q3*1000
|
||||||
q4 := q3 / 1000
|
q4 := q3 / 1000
|
||||||
if q4 == 0 {
|
if q4 == 0 {
|
||||||
n = writeFirstBuf(stream.buf, DIGITS[q3], n)
|
n = writeFirstBuf(stream.buf, _DIGITS[q3], n)
|
||||||
writeBuf(stream.buf, DIGITS[r3], n)
|
writeBuf(stream.buf, _DIGITS[r3], n)
|
||||||
writeBuf(stream.buf, DIGITS[r2], n + 3)
|
writeBuf(stream.buf, _DIGITS[r2], n+3)
|
||||||
writeBuf(stream.buf, DIGITS[r1], n + 6)
|
writeBuf(stream.buf, _DIGITS[r1], n+6)
|
||||||
stream.n = n + 9
|
stream.n = n + 9
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
r4 := q3 - q4 * 1000
|
r4 := q3 - q4*1000
|
||||||
q5 := q4 / 1000
|
q5 := q4 / 1000
|
||||||
if q5 == 0 {
|
if q5 == 0 {
|
||||||
n = writeFirstBuf(stream.buf, DIGITS[q4], n)
|
n = writeFirstBuf(stream.buf, _DIGITS[q4], n)
|
||||||
writeBuf(stream.buf, DIGITS[r4], n)
|
writeBuf(stream.buf, _DIGITS[r4], n)
|
||||||
writeBuf(stream.buf, DIGITS[r3], n + 3)
|
writeBuf(stream.buf, _DIGITS[r3], n+3)
|
||||||
writeBuf(stream.buf, DIGITS[r2], n + 6)
|
writeBuf(stream.buf, _DIGITS[r2], n+6)
|
||||||
writeBuf(stream.buf, DIGITS[r1], n + 9)
|
writeBuf(stream.buf, _DIGITS[r1], n+9)
|
||||||
stream.n = n + 12
|
stream.n = n + 12
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
r5 := q4 - q5 * 1000
|
r5 := q4 - q5*1000
|
||||||
q6 := q5 / 1000
|
q6 := q5 / 1000
|
||||||
if q6 == 0 {
|
if q6 == 0 {
|
||||||
n = writeFirstBuf(stream.buf, DIGITS[q5], n)
|
n = writeFirstBuf(stream.buf, _DIGITS[q5], n)
|
||||||
} else {
|
} else {
|
||||||
n = writeFirstBuf(stream.buf, DIGITS[q6], n)
|
n = writeFirstBuf(stream.buf, _DIGITS[q6], n)
|
||||||
r6 := q5 - q6 * 1000
|
r6 := q5 - q6*1000
|
||||||
writeBuf(stream.buf, DIGITS[r6], n)
|
writeBuf(stream.buf, _DIGITS[r6], n)
|
||||||
n += 3
|
n += 3
|
||||||
}
|
}
|
||||||
writeBuf(stream.buf, DIGITS[r5], n)
|
writeBuf(stream.buf, _DIGITS[r5], n)
|
||||||
writeBuf(stream.buf, DIGITS[r4], n + 3)
|
writeBuf(stream.buf, _DIGITS[r4], n+3)
|
||||||
writeBuf(stream.buf, DIGITS[r3], n + 6)
|
writeBuf(stream.buf, _DIGITS[r3], n+6)
|
||||||
writeBuf(stream.buf, DIGITS[r2], n + 9)
|
writeBuf(stream.buf, _DIGITS[r2], n+9)
|
||||||
writeBuf(stream.buf, DIGITS[r1], n + 12)
|
writeBuf(stream.buf, _DIGITS[r1], n+12)
|
||||||
stream.n = n + 15
|
stream.n = n + 15
|
||||||
}
|
}
|
||||||
|
|
||||||
func (stream *Stream) WriteInt64(nval int64) {
|
func (stream *Stream) WriteInt64(nval int64) {
|
||||||
if stream.Available() < 20 {
|
stream.ensure(20)
|
||||||
stream.Flush()
|
|
||||||
}
|
|
||||||
n := stream.n
|
n := stream.n
|
||||||
var val uint64
|
var val uint64
|
||||||
if (nval < 0) {
|
if nval < 0 {
|
||||||
val = uint64(-nval)
|
val = uint64(-nval)
|
||||||
stream.buf[n] = '-'
|
stream.buf[n] = '-'
|
||||||
n++
|
n++
|
||||||
@ -292,63 +241,63 @@ func (stream *Stream) WriteInt64(nval int64) {
|
|||||||
}
|
}
|
||||||
q1 := val / 1000
|
q1 := val / 1000
|
||||||
if q1 == 0 {
|
if q1 == 0 {
|
||||||
stream.n = writeFirstBuf(stream.buf, DIGITS[val], n)
|
stream.n = writeFirstBuf(stream.buf, _DIGITS[val], n)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
r1 := val - q1 * 1000;
|
r1 := val - q1*1000
|
||||||
q2 := q1 / 1000
|
q2 := q1 / 1000
|
||||||
if q2 == 0 {
|
if q2 == 0 {
|
||||||
n := writeFirstBuf(stream.buf, DIGITS[q1], n)
|
n := writeFirstBuf(stream.buf, _DIGITS[q1], n)
|
||||||
writeBuf(stream.buf, DIGITS[r1], n)
|
writeBuf(stream.buf, _DIGITS[r1], n)
|
||||||
stream.n = n + 3
|
stream.n = n + 3
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
r2 := q1 - q2 * 1000
|
r2 := q1 - q2*1000
|
||||||
q3 := q2 / 1000
|
q3 := q2 / 1000
|
||||||
if q3 == 0 {
|
if q3 == 0 {
|
||||||
n = writeFirstBuf(stream.buf, DIGITS[q2], n)
|
n = writeFirstBuf(stream.buf, _DIGITS[q2], n)
|
||||||
writeBuf(stream.buf, DIGITS[r2], n)
|
writeBuf(stream.buf, _DIGITS[r2], n)
|
||||||
writeBuf(stream.buf, DIGITS[r1], n + 3)
|
writeBuf(stream.buf, _DIGITS[r1], n+3)
|
||||||
stream.n = n + 6
|
stream.n = n + 6
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
r3 := q2 - q3 * 1000
|
r3 := q2 - q3*1000
|
||||||
q4 := q3 / 1000
|
q4 := q3 / 1000
|
||||||
if q4 == 0 {
|
if q4 == 0 {
|
||||||
n = writeFirstBuf(stream.buf, DIGITS[q3], n)
|
n = writeFirstBuf(stream.buf, _DIGITS[q3], n)
|
||||||
writeBuf(stream.buf, DIGITS[r3], n)
|
writeBuf(stream.buf, _DIGITS[r3], n)
|
||||||
writeBuf(stream.buf, DIGITS[r2], n + 3)
|
writeBuf(stream.buf, _DIGITS[r2], n+3)
|
||||||
writeBuf(stream.buf, DIGITS[r1], n + 6)
|
writeBuf(stream.buf, _DIGITS[r1], n+6)
|
||||||
stream.n = n + 9
|
stream.n = n + 9
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
r4 := q3 - q4 * 1000
|
r4 := q3 - q4*1000
|
||||||
q5 := q4 / 1000
|
q5 := q4 / 1000
|
||||||
if q5 == 0 {
|
if q5 == 0 {
|
||||||
n = writeFirstBuf(stream.buf, DIGITS[q4], n)
|
n = writeFirstBuf(stream.buf, _DIGITS[q4], n)
|
||||||
writeBuf(stream.buf, DIGITS[r4], n)
|
writeBuf(stream.buf, _DIGITS[r4], n)
|
||||||
writeBuf(stream.buf, DIGITS[r3], n + 3)
|
writeBuf(stream.buf, _DIGITS[r3], n+3)
|
||||||
writeBuf(stream.buf, DIGITS[r2], n + 6)
|
writeBuf(stream.buf, _DIGITS[r2], n+6)
|
||||||
writeBuf(stream.buf, DIGITS[r1], n + 9)
|
writeBuf(stream.buf, _DIGITS[r1], n+9)
|
||||||
stream.n = n + 12
|
stream.n = n + 12
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
r5 := q4 - q5 * 1000
|
r5 := q4 - q5*1000
|
||||||
q6 := q5 / 1000
|
q6 := q5 / 1000
|
||||||
if q6 == 0 {
|
if q6 == 0 {
|
||||||
n = writeFirstBuf(stream.buf, DIGITS[q5], n)
|
n = writeFirstBuf(stream.buf, _DIGITS[q5], n)
|
||||||
} else {
|
} else {
|
||||||
stream.buf[n] = byte(q6 + '0')
|
stream.buf[n] = byte(q6 + '0')
|
||||||
n++
|
n++
|
||||||
r6 := q5 - q6 * 1000
|
r6 := q5 - q6*1000
|
||||||
writeBuf(stream.buf, DIGITS[r6], n)
|
writeBuf(stream.buf, _DIGITS[r6], n)
|
||||||
n += 3
|
n += 3
|
||||||
}
|
}
|
||||||
writeBuf(stream.buf, DIGITS[r5], n)
|
writeBuf(stream.buf, _DIGITS[r5], n)
|
||||||
writeBuf(stream.buf, DIGITS[r4], n + 3)
|
writeBuf(stream.buf, _DIGITS[r4], n+3)
|
||||||
writeBuf(stream.buf, DIGITS[r3], n + 6)
|
writeBuf(stream.buf, _DIGITS[r3], n+6)
|
||||||
writeBuf(stream.buf, DIGITS[r2], n + 9)
|
writeBuf(stream.buf, _DIGITS[r2], n+9)
|
||||||
writeBuf(stream.buf, DIGITS[r1], n + 12)
|
writeBuf(stream.buf, _DIGITS[r1], n+12)
|
||||||
stream.n = n + 15
|
stream.n = n + 15
|
||||||
}
|
}
|
||||||
|
|
||||||
|
392
feature_stream_string.go
Normal file
392
feature_stream_string.go
Normal file
@ -0,0 +1,392 @@
|
|||||||
|
package jsoniter
|
||||||
|
|
||||||
|
import (
|
||||||
|
"unicode/utf8"
|
||||||
|
)
|
||||||
|
|
||||||
|
// htmlSafeSet holds the value true if the ASCII character with the given
|
||||||
|
// array position can be safely represented inside a JSON string, embedded
|
||||||
|
// inside of HTML <script> tags, without any additional escaping.
|
||||||
|
//
|
||||||
|
// All values are true except for the ASCII control characters (0-31), the
|
||||||
|
// double quote ("), the backslash character ("\"), HTML opening and closing
|
||||||
|
// tags ("<" and ">"), and the ampersand ("&").
|
||||||
|
var htmlSafeSet = [utf8.RuneSelf]bool{
|
||||||
|
' ': true,
|
||||||
|
'!': true,
|
||||||
|
'"': false,
|
||||||
|
'#': true,
|
||||||
|
'$': true,
|
||||||
|
'%': true,
|
||||||
|
'&': false,
|
||||||
|
'\'': true,
|
||||||
|
'(': true,
|
||||||
|
')': true,
|
||||||
|
'*': true,
|
||||||
|
'+': true,
|
||||||
|
',': true,
|
||||||
|
'-': true,
|
||||||
|
'.': true,
|
||||||
|
'/': true,
|
||||||
|
'0': true,
|
||||||
|
'1': true,
|
||||||
|
'2': true,
|
||||||
|
'3': true,
|
||||||
|
'4': true,
|
||||||
|
'5': true,
|
||||||
|
'6': true,
|
||||||
|
'7': true,
|
||||||
|
'8': true,
|
||||||
|
'9': true,
|
||||||
|
':': true,
|
||||||
|
';': true,
|
||||||
|
'<': false,
|
||||||
|
'=': true,
|
||||||
|
'>': false,
|
||||||
|
'?': true,
|
||||||
|
'@': true,
|
||||||
|
'A': true,
|
||||||
|
'B': true,
|
||||||
|
'C': true,
|
||||||
|
'D': true,
|
||||||
|
'E': true,
|
||||||
|
'F': true,
|
||||||
|
'G': true,
|
||||||
|
'H': true,
|
||||||
|
'I': true,
|
||||||
|
'J': true,
|
||||||
|
'K': true,
|
||||||
|
'L': true,
|
||||||
|
'M': true,
|
||||||
|
'N': true,
|
||||||
|
'O': true,
|
||||||
|
'P': true,
|
||||||
|
'Q': true,
|
||||||
|
'R': true,
|
||||||
|
'S': true,
|
||||||
|
'T': true,
|
||||||
|
'U': true,
|
||||||
|
'V': true,
|
||||||
|
'W': true,
|
||||||
|
'X': true,
|
||||||
|
'Y': true,
|
||||||
|
'Z': true,
|
||||||
|
'[': true,
|
||||||
|
'\\': false,
|
||||||
|
']': true,
|
||||||
|
'^': true,
|
||||||
|
'_': true,
|
||||||
|
'`': true,
|
||||||
|
'a': true,
|
||||||
|
'b': true,
|
||||||
|
'c': true,
|
||||||
|
'd': true,
|
||||||
|
'e': true,
|
||||||
|
'f': true,
|
||||||
|
'g': true,
|
||||||
|
'h': true,
|
||||||
|
'i': true,
|
||||||
|
'j': true,
|
||||||
|
'k': true,
|
||||||
|
'l': true,
|
||||||
|
'm': true,
|
||||||
|
'n': true,
|
||||||
|
'o': true,
|
||||||
|
'p': true,
|
||||||
|
'q': true,
|
||||||
|
'r': true,
|
||||||
|
's': true,
|
||||||
|
't': true,
|
||||||
|
'u': true,
|
||||||
|
'v': true,
|
||||||
|
'w': true,
|
||||||
|
'x': true,
|
||||||
|
'y': true,
|
||||||
|
'z': true,
|
||||||
|
'{': true,
|
||||||
|
'|': true,
|
||||||
|
'}': true,
|
||||||
|
'~': true,
|
||||||
|
'\u007f': true,
|
||||||
|
}
|
||||||
|
|
||||||
|
// safeSet holds the value true if the ASCII character with the given array
|
||||||
|
// position can be represented inside a JSON string without any further
|
||||||
|
// escaping.
|
||||||
|
//
|
||||||
|
// All values are true except for the ASCII control characters (0-31), the
|
||||||
|
// double quote ("), and the backslash character ("\").
|
||||||
|
var safeSet = [utf8.RuneSelf]bool{
|
||||||
|
' ': true,
|
||||||
|
'!': true,
|
||||||
|
'"': false,
|
||||||
|
'#': true,
|
||||||
|
'$': true,
|
||||||
|
'%': true,
|
||||||
|
'&': true,
|
||||||
|
'\'': true,
|
||||||
|
'(': true,
|
||||||
|
')': true,
|
||||||
|
'*': true,
|
||||||
|
'+': true,
|
||||||
|
',': true,
|
||||||
|
'-': true,
|
||||||
|
'.': true,
|
||||||
|
'/': true,
|
||||||
|
'0': true,
|
||||||
|
'1': true,
|
||||||
|
'2': true,
|
||||||
|
'3': true,
|
||||||
|
'4': true,
|
||||||
|
'5': true,
|
||||||
|
'6': true,
|
||||||
|
'7': true,
|
||||||
|
'8': true,
|
||||||
|
'9': true,
|
||||||
|
':': true,
|
||||||
|
';': true,
|
||||||
|
'<': true,
|
||||||
|
'=': true,
|
||||||
|
'>': true,
|
||||||
|
'?': true,
|
||||||
|
'@': true,
|
||||||
|
'A': true,
|
||||||
|
'B': true,
|
||||||
|
'C': true,
|
||||||
|
'D': true,
|
||||||
|
'E': true,
|
||||||
|
'F': true,
|
||||||
|
'G': true,
|
||||||
|
'H': true,
|
||||||
|
'I': true,
|
||||||
|
'J': true,
|
||||||
|
'K': true,
|
||||||
|
'L': true,
|
||||||
|
'M': true,
|
||||||
|
'N': true,
|
||||||
|
'O': true,
|
||||||
|
'P': true,
|
||||||
|
'Q': true,
|
||||||
|
'R': true,
|
||||||
|
'S': true,
|
||||||
|
'T': true,
|
||||||
|
'U': true,
|
||||||
|
'V': true,
|
||||||
|
'W': true,
|
||||||
|
'X': true,
|
||||||
|
'Y': true,
|
||||||
|
'Z': true,
|
||||||
|
'[': true,
|
||||||
|
'\\': false,
|
||||||
|
']': true,
|
||||||
|
'^': true,
|
||||||
|
'_': true,
|
||||||
|
'`': true,
|
||||||
|
'a': true,
|
||||||
|
'b': true,
|
||||||
|
'c': true,
|
||||||
|
'd': true,
|
||||||
|
'e': true,
|
||||||
|
'f': true,
|
||||||
|
'g': true,
|
||||||
|
'h': true,
|
||||||
|
'i': true,
|
||||||
|
'j': true,
|
||||||
|
'k': true,
|
||||||
|
'l': true,
|
||||||
|
'm': true,
|
||||||
|
'n': true,
|
||||||
|
'o': true,
|
||||||
|
'p': true,
|
||||||
|
'q': true,
|
||||||
|
'r': true,
|
||||||
|
's': true,
|
||||||
|
't': true,
|
||||||
|
'u': true,
|
||||||
|
'v': true,
|
||||||
|
'w': true,
|
||||||
|
'x': true,
|
||||||
|
'y': true,
|
||||||
|
'z': true,
|
||||||
|
'{': true,
|
||||||
|
'|': true,
|
||||||
|
'}': true,
|
||||||
|
'~': true,
|
||||||
|
'\u007f': true,
|
||||||
|
}
|
||||||
|
|
||||||
|
var hex = "0123456789abcdef"
|
||||||
|
|
||||||
|
func (stream *Stream) WriteStringWithHtmlEscaped(s string) {
|
||||||
|
stream.ensure(32)
|
||||||
|
valLen := len(s)
|
||||||
|
toWriteLen := valLen
|
||||||
|
bufLengthMinusTwo := len(stream.buf) - 2 // make room for the quotes
|
||||||
|
if stream.n+toWriteLen > bufLengthMinusTwo {
|
||||||
|
toWriteLen = bufLengthMinusTwo - stream.n
|
||||||
|
}
|
||||||
|
n := stream.n
|
||||||
|
stream.buf[n] = '"'
|
||||||
|
n++
|
||||||
|
// write string, the fast path, without utf8 and escape support
|
||||||
|
i := 0
|
||||||
|
for ; i < toWriteLen; i++ {
|
||||||
|
c := s[i]
|
||||||
|
if c < utf8.RuneSelf && htmlSafeSet[c] {
|
||||||
|
stream.buf[n] = c
|
||||||
|
n++
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if i == valLen {
|
||||||
|
stream.buf[n] = '"'
|
||||||
|
n++
|
||||||
|
stream.n = n
|
||||||
|
return
|
||||||
|
}
|
||||||
|
stream.n = n
|
||||||
|
writeStringSlowPathWithHtmlEscaped(stream, i, s, valLen)
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
if b := s[i]; b < utf8.RuneSelf {
|
||||||
|
if htmlSafeSet[b] {
|
||||||
|
i++
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if start < i {
|
||||||
|
stream.WriteRaw(s[start:i])
|
||||||
|
}
|
||||||
|
switch b {
|
||||||
|
case '\\', '"':
|
||||||
|
stream.writeTwoBytes('\\', b)
|
||||||
|
case '\n':
|
||||||
|
stream.writeTwoBytes('\\', 'n')
|
||||||
|
case '\r':
|
||||||
|
stream.writeTwoBytes('\\', 'r')
|
||||||
|
case '\t':
|
||||||
|
stream.writeTwoBytes('\\', 't')
|
||||||
|
default:
|
||||||
|
// This encodes bytes < 0x20 except for \t, \n and \r.
|
||||||
|
// If escapeHTML is set, it also escapes <, >, and &
|
||||||
|
// because they can lead to security holes when
|
||||||
|
// user-controlled strings are rendered into JSON
|
||||||
|
// and served to some browsers.
|
||||||
|
stream.WriteRaw(`\u00`)
|
||||||
|
stream.writeTwoBytes(hex[b>>4], hex[b&0xF])
|
||||||
|
}
|
||||||
|
i++
|
||||||
|
start = i
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
c, size := utf8.DecodeRuneInString(s[i:])
|
||||||
|
if c == utf8.RuneError && size == 1 {
|
||||||
|
if start < i {
|
||||||
|
stream.WriteRaw(s[start:i])
|
||||||
|
}
|
||||||
|
start = i
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// U+2028 is LINE SEPARATOR.
|
||||||
|
// U+2029 is PARAGRAPH SEPARATOR.
|
||||||
|
// They are both technically valid characters in JSON strings,
|
||||||
|
// but don't work in JSONP, which has to be evaluated as JavaScript,
|
||||||
|
// and can lead to security holes there. It is valid JSON to
|
||||||
|
// escape them, so we do so unconditionally.
|
||||||
|
// See http://timelessrepo.com/json-isnt-a-javascript-subset for discussion.
|
||||||
|
if c == '\u2028' || c == '\u2029' {
|
||||||
|
if start < i {
|
||||||
|
stream.WriteRaw(s[start:i])
|
||||||
|
}
|
||||||
|
stream.WriteRaw(`\u202`)
|
||||||
|
stream.writeByte(hex[c&0xF])
|
||||||
|
i += size
|
||||||
|
start = i
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
i += size
|
||||||
|
}
|
||||||
|
if start < len(s) {
|
||||||
|
stream.WriteRaw(s[start:])
|
||||||
|
}
|
||||||
|
stream.writeByte('"')
|
||||||
|
}
|
||||||
|
|
||||||
|
func (stream *Stream) WriteString(s string) {
|
||||||
|
stream.ensure(32)
|
||||||
|
valLen := len(s)
|
||||||
|
toWriteLen := valLen
|
||||||
|
bufLengthMinusTwo := len(stream.buf) - 2 // make room for the quotes
|
||||||
|
if stream.n+toWriteLen > bufLengthMinusTwo {
|
||||||
|
toWriteLen = bufLengthMinusTwo - stream.n
|
||||||
|
}
|
||||||
|
n := stream.n
|
||||||
|
stream.buf[n] = '"'
|
||||||
|
n++
|
||||||
|
// write string, the fast path, without utf8 and escape support
|
||||||
|
i := 0
|
||||||
|
for ; i < toWriteLen; i++ {
|
||||||
|
c := s[i]
|
||||||
|
if c > 31 && c != '"' && c != '\\' {
|
||||||
|
stream.buf[n] = c
|
||||||
|
n++
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if i == valLen {
|
||||||
|
stream.buf[n] = '"'
|
||||||
|
n++
|
||||||
|
stream.n = n
|
||||||
|
return
|
||||||
|
}
|
||||||
|
stream.n = n
|
||||||
|
writeStringSlowPath(stream, i, s, valLen)
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeStringSlowPath(stream *Stream, i int, s string, valLen int) {
|
||||||
|
start := i
|
||||||
|
// for the remaining parts, we process them char by char
|
||||||
|
for i < valLen {
|
||||||
|
if b := s[i]; b < utf8.RuneSelf {
|
||||||
|
if safeSet[b] {
|
||||||
|
i++
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if start < i {
|
||||||
|
stream.WriteRaw(s[start:i])
|
||||||
|
}
|
||||||
|
switch b {
|
||||||
|
case '\\', '"':
|
||||||
|
stream.writeTwoBytes('\\', b)
|
||||||
|
case '\n':
|
||||||
|
stream.writeTwoBytes('\\', 'n')
|
||||||
|
case '\r':
|
||||||
|
stream.writeTwoBytes('\\', 'r')
|
||||||
|
case '\t':
|
||||||
|
stream.writeTwoBytes('\\', 't')
|
||||||
|
default:
|
||||||
|
// This encodes bytes < 0x20 except for \t, \n and \r.
|
||||||
|
// If escapeHTML is set, it also escapes <, >, and &
|
||||||
|
// because they can lead to security holes when
|
||||||
|
// user-controlled strings are rendered into JSON
|
||||||
|
// and served to some browsers.
|
||||||
|
stream.WriteRaw(`\u00`)
|
||||||
|
stream.writeTwoBytes(hex[b>>4], hex[b&0xF])
|
||||||
|
}
|
||||||
|
i++
|
||||||
|
start = i
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
i++
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if start < len(s) {
|
||||||
|
stream.WriteRaw(s[start:])
|
||||||
|
}
|
||||||
|
stream.writeByte('"')
|
||||||
|
}
|
44
jsoniter_1dot8_only_test.go
Normal file
44
jsoniter_1dot8_only_test.go
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
// +build go1.8
|
||||||
|
|
||||||
|
package jsoniter
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"encoding/json"
|
||||||
|
"github.com/json-iterator/go/require"
|
||||||
|
"bytes"
|
||||||
|
"unicode/utf8"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_new_encoder(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
buf1 := &bytes.Buffer{}
|
||||||
|
encoder1 := json.NewEncoder(buf1)
|
||||||
|
encoder1.SetEscapeHTML(false)
|
||||||
|
encoder1.Encode([]int{1})
|
||||||
|
should.Equal("[1]\n", buf1.String())
|
||||||
|
buf2 := &bytes.Buffer{}
|
||||||
|
encoder2 := NewEncoder(buf2)
|
||||||
|
encoder2.SetEscapeHTML(false)
|
||||||
|
encoder2.Encode([]int{1})
|
||||||
|
should.Equal("[1]", buf2.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_string_encode_with_std_without_html_escape(t *testing.T) {
|
||||||
|
api := Config{EscapeHtml: false}.Froze()
|
||||||
|
should := require.New(t)
|
||||||
|
for i := 0; i < utf8.RuneSelf; i++ {
|
||||||
|
input := string([]byte{byte(i)})
|
||||||
|
buf := &bytes.Buffer{}
|
||||||
|
encoder := json.NewEncoder(buf)
|
||||||
|
encoder.SetEscapeHTML(false)
|
||||||
|
err := encoder.Encode(input)
|
||||||
|
should.Nil(err)
|
||||||
|
stdOutput := buf.String()
|
||||||
|
stdOutput = stdOutput[:len(stdOutput)-1]
|
||||||
|
jsoniterOutputBytes, err := api.Marshal(input)
|
||||||
|
should.Nil(err)
|
||||||
|
jsoniterOutput := string(jsoniterOutputBytes)
|
||||||
|
should.Equal(stdOutput, jsoniterOutput)
|
||||||
|
}
|
||||||
|
}
|
70
jsoniter_adapter_test.go
Normal file
70
jsoniter_adapter_test.go
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
package jsoniter
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"github.com/json-iterator/go/require"
|
||||||
|
"io/ioutil"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_new_decoder(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
decoder1 := json.NewDecoder(bytes.NewBufferString(`[1][2]`))
|
||||||
|
decoder2 := NewDecoder(bytes.NewBufferString(`[1][2]`))
|
||||||
|
arr1 := []int{}
|
||||||
|
should.Nil(decoder1.Decode(&arr1))
|
||||||
|
should.Equal([]int{1}, arr1)
|
||||||
|
arr2 := []int{}
|
||||||
|
should.True(decoder1.More())
|
||||||
|
buffered, _ := ioutil.ReadAll(decoder1.Buffered())
|
||||||
|
should.Equal("[2]", string(buffered))
|
||||||
|
should.Nil(decoder2.Decode(&arr2))
|
||||||
|
should.Equal([]int{1}, arr2)
|
||||||
|
should.True(decoder2.More())
|
||||||
|
buffered, _ = ioutil.ReadAll(decoder2.Buffered())
|
||||||
|
should.Equal("[2]", string(buffered))
|
||||||
|
|
||||||
|
should.Nil(decoder1.Decode(&arr1))
|
||||||
|
should.Equal([]int{2}, arr1)
|
||||||
|
should.False(decoder1.More())
|
||||||
|
should.Nil(decoder2.Decode(&arr2))
|
||||||
|
should.Equal([]int{2}, arr2)
|
||||||
|
should.False(decoder2.More())
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_use_number(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
decoder1 := json.NewDecoder(bytes.NewBufferString(`123`))
|
||||||
|
decoder1.UseNumber()
|
||||||
|
decoder2 := NewDecoder(bytes.NewBufferString(`123`))
|
||||||
|
decoder2.UseNumber()
|
||||||
|
var obj1 interface{}
|
||||||
|
should.Nil(decoder1.Decode(&obj1))
|
||||||
|
should.Equal(json.Number("123"), obj1)
|
||||||
|
var obj2 interface{}
|
||||||
|
should.Nil(decoder2.Decode(&obj2))
|
||||||
|
should.Equal(json.Number("123"), obj2)
|
||||||
|
}
|
||||||
|
|
||||||
|
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))
|
||||||
|
}
|
96
jsoniter_any_array_test.go
Normal file
96
jsoniter_any_array_test.go
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
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())
|
||||||
|
}
|
12
jsoniter_any_bool_test.go
Normal file
12
jsoniter_any_bool_test.go
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
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())
|
||||||
|
}
|
14
jsoniter_any_float_test.go
Normal file
14
jsoniter_any_float_test.go
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
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())
|
||||||
|
}
|
22
jsoniter_any_int_test.go
Normal file
22
jsoniter_any_int_test.go
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
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())
|
||||||
|
}
|
14
jsoniter_any_map_test.go
Normal file
14
jsoniter_any_map_test.go
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
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())
|
||||||
|
}
|
15
jsoniter_any_null_test.go
Normal file
15
jsoniter_any_null_test.go
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
package jsoniter
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/json-iterator/go/require"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_read_null_as_any(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
any := Get([]byte(`null`))
|
||||||
|
should.Equal(0, any.ToInt())
|
||||||
|
should.Equal(float64(0), any.ToFloat64())
|
||||||
|
should.Equal("", any.ToString())
|
||||||
|
should.False(any.ToBool())
|
||||||
|
}
|
75
jsoniter_any_object_test.go
Normal file
75
jsoniter_any_object_test.go
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
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())
|
||||||
|
}
|
25
jsoniter_any_string_test.go
Normal file
25
jsoniter_any_string_test.go
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
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,19 +1,18 @@
|
|||||||
package jsoniter
|
package jsoniter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"testing"
|
|
||||||
"github.com/json-iterator/go/require"
|
|
||||||
"bytes"
|
"bytes"
|
||||||
"io"
|
"encoding/json"
|
||||||
|
"github.com/json-iterator/go/require"
|
||||||
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_empty_array(t *testing.T) {
|
func Test_empty_array(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(`[]`)
|
iter := ParseString(ConfigDefault, `[]`)
|
||||||
cont := iter.ReadArray()
|
cont := iter.ReadArray()
|
||||||
should.False(cont)
|
should.False(cont)
|
||||||
iter = ParseString(`[]`)
|
iter = ParseString(ConfigDefault, `[]`)
|
||||||
iter.ReadArrayCB(func(iter *Iterator) bool {
|
iter.ReadArrayCB(func(iter *Iterator) bool {
|
||||||
should.FailNow("should not call")
|
should.FailNow("should not call")
|
||||||
return true
|
return true
|
||||||
@ -22,11 +21,11 @@ func Test_empty_array(t *testing.T) {
|
|||||||
|
|
||||||
func Test_one_element(t *testing.T) {
|
func Test_one_element(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(`[1]`)
|
iter := ParseString(ConfigDefault, `[1]`)
|
||||||
should.True(iter.ReadArray())
|
should.True(iter.ReadArray())
|
||||||
should.Equal(1, iter.ReadInt())
|
should.Equal(1, iter.ReadInt())
|
||||||
should.False(iter.ReadArray())
|
should.False(iter.ReadArray())
|
||||||
iter = ParseString(`[1]`)
|
iter = ParseString(ConfigDefault, `[1]`)
|
||||||
iter.ReadArrayCB(func(iter *Iterator) bool {
|
iter.ReadArrayCB(func(iter *Iterator) bool {
|
||||||
should.Equal(1, iter.ReadInt())
|
should.Equal(1, iter.ReadInt())
|
||||||
return true
|
return true
|
||||||
@ -35,124 +34,18 @@ func Test_one_element(t *testing.T) {
|
|||||||
|
|
||||||
func Test_two_elements(t *testing.T) {
|
func Test_two_elements(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(`[1,2]`)
|
iter := ParseString(ConfigDefault, `[1,2]`)
|
||||||
should.True(iter.ReadArray())
|
should.True(iter.ReadArray())
|
||||||
should.Equal(int64(1), iter.ReadInt64())
|
should.Equal(int64(1), iter.ReadInt64())
|
||||||
should.True(iter.ReadArray())
|
should.True(iter.ReadArray())
|
||||||
should.Equal(int64(2), iter.ReadInt64())
|
should.Equal(int64(2), iter.ReadInt64())
|
||||||
should.False(iter.ReadArray())
|
should.False(iter.ReadArray())
|
||||||
iter = ParseString(`[1,2]`)
|
iter = ParseString(ConfigDefault, `[1,2]`)
|
||||||
should.Equal([]interface{}{float64(1), float64(2)}, iter.Read())
|
should.Equal([]interface{}{float64(1), float64(2)}, iter.Read())
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_read_empty_array_as_any(t *testing.T) {
|
|
||||||
should := require.New(t)
|
|
||||||
any, err := UnmarshalAnyFromString("[]")
|
|
||||||
should.Nil(err)
|
|
||||||
should.Equal(0, any.Size())
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_read_one_element_array_as_any(t *testing.T) {
|
|
||||||
should := require.New(t)
|
|
||||||
any, err := UnmarshalAnyFromString("[1]")
|
|
||||||
should.Nil(err)
|
|
||||||
should.Equal(1, any.Size())
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_read_two_element_array_as_any(t *testing.T) {
|
|
||||||
should := require.New(t)
|
|
||||||
any, err := UnmarshalAnyFromString("[1,2]")
|
|
||||||
should.Nil(err)
|
|
||||||
should.Equal(1, any.Get(0).ToInt())
|
|
||||||
should.Equal(2, any.Size())
|
|
||||||
should.True(any.ToBool())
|
|
||||||
should.Equal(1, any.ToInt())
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_read_array_with_any_iterator(t *testing.T) {
|
|
||||||
should := require.New(t)
|
|
||||||
any, err := UnmarshalAnyFromString("[1,2]")
|
|
||||||
should.Nil(err)
|
|
||||||
var element Any
|
|
||||||
var elements []int
|
|
||||||
for next, hasNext := any.IterateArray(); hasNext; {
|
|
||||||
element, hasNext = next()
|
|
||||||
elements = append(elements, element.ToInt())
|
|
||||||
}
|
|
||||||
should.Equal([]int{1, 2}, elements)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_wrap_array(t *testing.T) {
|
|
||||||
should := require.New(t)
|
|
||||||
any := Wrap([]int{1,2,3})
|
|
||||||
should.Equal("[1,2,3]", any.ToString())
|
|
||||||
var element Any
|
|
||||||
var elements []int
|
|
||||||
for next, hasNext := any.IterateArray(); hasNext; {
|
|
||||||
element, hasNext = next()
|
|
||||||
elements = append(elements, element.ToInt())
|
|
||||||
}
|
|
||||||
should.Equal([]int{1, 2, 3}, elements)
|
|
||||||
any = Wrap([]int{1,2,3})
|
|
||||||
should.Equal(3, any.Size())
|
|
||||||
any = Wrap([]int{1,2,3})
|
|
||||||
should.Equal(2, any.Get(1).ToInt())
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_array_lazy_any_get(t *testing.T) {
|
|
||||||
should := require.New(t)
|
|
||||||
any, err := UnmarshalAnyFromString("[1,[2,3],4]")
|
|
||||||
should.Nil(err)
|
|
||||||
should.Equal(3, any.Get(1,1).ToInt())
|
|
||||||
should.Equal("[1,[2,3],4]", any.ToString())
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_array_lazy_any_get_all(t *testing.T) {
|
|
||||||
should := require.New(t)
|
|
||||||
any, err := UnmarshalAnyFromString("[[1],[2],[3,4]]")
|
|
||||||
should.Nil(err)
|
|
||||||
should.Equal("[1,2,3]", any.Get('*',0).ToString())
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_array_wrapper_any_get_all(t *testing.T) {
|
|
||||||
should := require.New(t)
|
|
||||||
any := wrapArray([][]int{
|
|
||||||
[]int{1, 2},
|
|
||||||
[]int{3, 4},
|
|
||||||
[]int{5, 6},
|
|
||||||
})
|
|
||||||
should.Equal("[1,3,5]", any.Get('*',0).ToString())
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_array_lazy_any_get_invalid(t *testing.T) {
|
|
||||||
should := require.New(t)
|
|
||||||
any, err := UnmarshalAnyFromString("[]")
|
|
||||||
should.Nil(err)
|
|
||||||
should.Equal(Invalid, any.Get(1,1).ValueType())
|
|
||||||
should.NotNil(any.Get(1,1).LastError())
|
|
||||||
should.Equal(Invalid, any.Get("1").ValueType())
|
|
||||||
should.NotNil(any.Get("1").LastError())
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_array_lazy_any_set(t *testing.T) {
|
|
||||||
should := require.New(t)
|
|
||||||
any, err := UnmarshalAnyFromString("[1,[2,3],4]")
|
|
||||||
should.Nil(err)
|
|
||||||
any.GetArray()[0] = WrapInt64(2)
|
|
||||||
str, err := MarshalToString(any)
|
|
||||||
should.Nil(err)
|
|
||||||
should.Equal("[2,[2,3],4]", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_invalid_array(t *testing.T) {
|
|
||||||
_, err := UnmarshalAnyFromString("[")
|
|
||||||
if err == nil || err == io.EOF {
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_whitespace_in_head(t *testing.T) {
|
func Test_whitespace_in_head(t *testing.T) {
|
||||||
iter := ParseString(` [1]`)
|
iter := ParseString(ConfigDefault, ` [1]`)
|
||||||
cont := iter.ReadArray()
|
cont := iter.ReadArray()
|
||||||
if cont != true {
|
if cont != true {
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
@ -163,7 +56,7 @@ func Test_whitespace_in_head(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_whitespace_after_array_start(t *testing.T) {
|
func Test_whitespace_after_array_start(t *testing.T) {
|
||||||
iter := ParseString(`[ 1]`)
|
iter := ParseString(ConfigDefault, `[ 1]`)
|
||||||
cont := iter.ReadArray()
|
cont := iter.ReadArray()
|
||||||
if cont != true {
|
if cont != true {
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
@ -174,7 +67,7 @@ func Test_whitespace_after_array_start(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_whitespace_before_array_end(t *testing.T) {
|
func Test_whitespace_before_array_end(t *testing.T) {
|
||||||
iter := ParseString(`[1 ]`)
|
iter := ParseString(ConfigDefault, `[1 ]`)
|
||||||
cont := iter.ReadArray()
|
cont := iter.ReadArray()
|
||||||
if cont != true {
|
if cont != true {
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
@ -189,7 +82,7 @@ func Test_whitespace_before_array_end(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_whitespace_before_comma(t *testing.T) {
|
func Test_whitespace_before_comma(t *testing.T) {
|
||||||
iter := ParseString(`[1 ,2]`)
|
iter := ParseString(ConfigDefault, `[1 ,2]`)
|
||||||
cont := iter.ReadArray()
|
cont := iter.ReadArray()
|
||||||
if cont != true {
|
if cont != true {
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
@ -213,8 +106,7 @@ func Test_whitespace_before_comma(t *testing.T) {
|
|||||||
func Test_write_array(t *testing.T) {
|
func Test_write_array(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(Config{IndentionStep: 2}.Froze(), buf, 4096)
|
||||||
stream.IndentionStep = 2
|
|
||||||
stream.WriteArrayStart()
|
stream.WriteArrayStart()
|
||||||
stream.WriteInt(1)
|
stream.WriteInt(1)
|
||||||
stream.WriteMore()
|
stream.WriteMore()
|
||||||
@ -228,7 +120,7 @@ func Test_write_array(t *testing.T) {
|
|||||||
func Test_write_val_array(t *testing.T) {
|
func Test_write_val_array(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
val := []int{1, 2, 3}
|
val := []int{1, 2, 3}
|
||||||
str, err := MarshalToString(val)
|
str, err := MarshalToString(&val)
|
||||||
should.Nil(err)
|
should.Nil(err)
|
||||||
should.Equal("[1,2,3]", str)
|
should.Equal("[1,2,3]", str)
|
||||||
}
|
}
|
||||||
@ -244,19 +136,55 @@ func Test_write_val_empty_array(t *testing.T) {
|
|||||||
func Test_write_array_of_interface_in_struct(t *testing.T) {
|
func Test_write_array_of_interface_in_struct(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
type TestObject struct {
|
type TestObject struct {
|
||||||
Field []interface{}
|
Field []interface{}
|
||||||
Field2 string
|
Field2 string
|
||||||
}
|
}
|
||||||
val := TestObject{[]interface{}{1, 2}, ""}
|
val := TestObject{[]interface{}{1, 2}, ""}
|
||||||
str, err := MarshalToString(val)
|
str, err := MarshalToString(val)
|
||||||
should.Nil(err)
|
should.Nil(err)
|
||||||
should.Equal(`{"Field":[1,2],"Field2":""}`, str)
|
should.Contains(str, `"Field":[1,2]`)
|
||||||
|
should.Contains(str, `"Field2":""`)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_encode_byte_array(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
bytes, err := json.Marshal([]byte{1, 2, 3})
|
||||||
|
should.Nil(err)
|
||||||
|
should.Equal(`"AQID"`, string(bytes))
|
||||||
|
bytes, err = Marshal([]byte{1, 2, 3})
|
||||||
|
should.Nil(err)
|
||||||
|
should.Equal(`"AQID"`, string(bytes))
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_decode_byte_array(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
data := []byte{}
|
||||||
|
err := json.Unmarshal([]byte(`"AQID"`), &data)
|
||||||
|
should.Nil(err)
|
||||||
|
should.Equal([]byte{1, 2, 3}, data)
|
||||||
|
err = Unmarshal([]byte(`"AQID"`), &data)
|
||||||
|
should.Nil(err)
|
||||||
|
should.Equal([]byte{1, 2, 3}, data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_decode_slice(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
slice := make([]string, 0, 5)
|
||||||
|
UnmarshalFromString(`["hello", "world"]`, &slice)
|
||||||
|
should.Equal([]string{"hello", "world"}, slice)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_decode_large_slice(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
slice := make([]int, 0, 1)
|
||||||
|
UnmarshalFromString(`[1,2,3,4,5,6,7,8,9]`, &slice)
|
||||||
|
should.Equal([]int{1, 2, 3, 4, 5, 6, 7, 8, 9}, slice)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Benchmark_jsoniter_array(b *testing.B) {
|
func Benchmark_jsoniter_array(b *testing.B) {
|
||||||
b.ReportAllocs()
|
b.ReportAllocs()
|
||||||
input := []byte(`[1,2,3,4,5,6,7,8,9]`)
|
input := []byte(`[1,2,3,4,5,6,7,8,9]`)
|
||||||
iter := ParseBytes(input)
|
iter := ParseBytes(ConfigDefault, input)
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
for n := 0; n < b.N; n++ {
|
for n := 0; n < b.N; n++ {
|
||||||
iter.ResetBytes(input)
|
iter.ResetBytes(input)
|
||||||
|
@ -1,36 +1,30 @@
|
|||||||
package jsoniter
|
package jsoniter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
"github.com/json-iterator/go/require"
|
"github.com/json-iterator/go/require"
|
||||||
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_true(t *testing.T) {
|
func Test_true(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(`true`)
|
iter := ParseString(ConfigDefault, `true`)
|
||||||
should.True(iter.ReadBool())
|
should.True(iter.ReadBool())
|
||||||
iter = ParseString(`true`)
|
iter = ParseString(ConfigDefault, `true`)
|
||||||
should.Equal(true, iter.Read())
|
should.Equal(true, iter.Read())
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_false(t *testing.T) {
|
func Test_false(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(`false`)
|
iter := ParseString(ConfigDefault, `false`)
|
||||||
should.False(iter.ReadBool())
|
should.False(iter.ReadBool())
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_read_bool_as_any(t *testing.T) {
|
|
||||||
should := require.New(t)
|
|
||||||
any, err := UnmarshalAnyFromString("true")
|
|
||||||
should.Nil(err)
|
|
||||||
should.True(any.ToBool())
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_write_true_false(t *testing.T) {
|
func Test_write_true_false(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(ConfigDefault, buf, 4096)
|
||||||
stream.WriteTrue()
|
stream.WriteTrue()
|
||||||
stream.WriteFalse()
|
stream.WriteFalse()
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
@ -38,13 +32,49 @@ func Test_write_true_false(t *testing.T) {
|
|||||||
should.Equal("truefalse", buf.String())
|
should.Equal("truefalse", buf.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func Test_write_val_bool(t *testing.T) {
|
func Test_write_val_bool(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(ConfigDefault, buf, 4096)
|
||||||
stream.WriteVal(true)
|
stream.WriteVal(true)
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
should.Equal("true", buf.String())
|
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,7 +1,8 @@
|
|||||||
package jsoniter
|
package jsoniter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"reflect"
|
"encoding/json"
|
||||||
|
"github.com/json-iterator/go/require"
|
||||||
"strconv"
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@ -9,7 +10,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func Test_customize_type_decoder(t *testing.T) {
|
func Test_customize_type_decoder(t *testing.T) {
|
||||||
RegisterTypeDecoder("time.Time", func(ptr unsafe.Pointer, iter *Iterator) {
|
RegisterTypeDecoderFunc("time.Time", func(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
t, err := time.ParseInLocation("2006-01-02 15:04:05", iter.ReadString(), time.UTC)
|
t, err := time.ParseInLocation("2006-01-02 15:04:05", iter.ReadString(), time.UTC)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
iter.Error = err
|
iter.Error = err
|
||||||
@ -17,7 +18,7 @@ func Test_customize_type_decoder(t *testing.T) {
|
|||||||
}
|
}
|
||||||
*((*time.Time)(ptr)) = t
|
*((*time.Time)(ptr)) = t
|
||||||
})
|
})
|
||||||
defer CleanDecoders()
|
defer ConfigDefault.cleanDecoders()
|
||||||
val := time.Time{}
|
val := time.Time{}
|
||||||
err := Unmarshal([]byte(`"2016-12-05 08:43:28"`), &val)
|
err := Unmarshal([]byte(`"2016-12-05 08:43:28"`), &val)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -29,15 +30,50 @@ func Test_customize_type_decoder(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 {
|
type Tom struct {
|
||||||
field1 string
|
field1 string
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_customize_field_decoder(t *testing.T) {
|
func Test_customize_field_decoder(t *testing.T) {
|
||||||
RegisterFieldDecoder("jsoniter.Tom", "field1", func(ptr unsafe.Pointer, iter *Iterator) {
|
RegisterFieldDecoderFunc("jsoniter.Tom", "field1", func(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
*((*string)(ptr)) = strconv.Itoa(iter.ReadInt())
|
*((*string)(ptr)) = strconv.Itoa(iter.ReadInt())
|
||||||
})
|
})
|
||||||
defer CleanDecoders()
|
defer ConfigDefault.cleanDecoders()
|
||||||
tom := Tom{}
|
tom := Tom{}
|
||||||
err := Unmarshal([]byte(`{"field1": 100}`), &tom)
|
err := Unmarshal([]byte(`{"field1": 100}`), &tom)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -49,21 +85,203 @@ type TestObject1 struct {
|
|||||||
field1 string
|
field1 string
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_customize_field_by_extension(t *testing.T) {
|
type testExtension struct {
|
||||||
RegisterExtension(func(type_ reflect.Type, field *reflect.StructField) ([]string, DecoderFunc) {
|
DummyExtension
|
||||||
if type_.String() == "jsoniter.TestObject1" && field.Name == "field1" {
|
}
|
||||||
return []string{"field-1"}, func(ptr unsafe.Pointer, iter *Iterator) {
|
|
||||||
*((*string)(ptr)) = strconv.Itoa(iter.ReadInt())
|
func (extension *testExtension) UpdateStructDescriptor(structDescriptor *StructDescriptor) {
|
||||||
}
|
if structDescriptor.Type.String() != "jsoniter.TestObject1" {
|
||||||
}
|
return
|
||||||
return nil, nil
|
}
|
||||||
})
|
binding := structDescriptor.GetField("field1")
|
||||||
obj := TestObject1{}
|
binding.Encoder = &funcEncoder{fun: func(ptr unsafe.Pointer, stream *Stream) {
|
||||||
err := Unmarshal([]byte(`{"field-1": 100}`), &obj)
|
str := *((*string)(ptr))
|
||||||
if err != nil {
|
val, _ := strconv.Atoi(str)
|
||||||
t.Fatal(err)
|
stream.WriteInt(val)
|
||||||
}
|
}}
|
||||||
if obj.field1 != "100" {
|
binding.Decoder = &funcDecoder{func(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
t.Fatal(obj.field1)
|
*((*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,19 +1,22 @@
|
|||||||
package jsoniter
|
package jsoniter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/json-iterator/go/require"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_bind_api_demo(t *testing.T) {
|
func Test_bind_api_demo(t *testing.T) {
|
||||||
iter := ParseString(`[0,1,2,3]`)
|
should := require.New(t)
|
||||||
val := []int{}
|
val := []int{}
|
||||||
iter.ReadVal(&val)
|
err := UnmarshalFromString(`[0,1,2,3] `, &val)
|
||||||
fmt.Println(val[3])
|
should.Nil(err)
|
||||||
|
should.Equal([]int{0, 1, 2, 3}, val)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_iterator_api_demo(t *testing.T) {
|
func Test_iterator_api_demo(t *testing.T) {
|
||||||
iter := ParseString(`[0,1,2,3]`)
|
iter := ParseString(ConfigDefault, `[0,1,2,3]`)
|
||||||
total := 0
|
total := 0
|
||||||
for iter.ReadArray() {
|
for iter.ReadArray() {
|
||||||
total += iter.ReadInt()
|
total += iter.ReadInt()
|
||||||
@ -21,19 +24,63 @@ func Test_iterator_api_demo(t *testing.T) {
|
|||||||
fmt.Println(total)
|
fmt.Println(total)
|
||||||
}
|
}
|
||||||
|
|
||||||
type User struct {
|
type People struct {
|
||||||
userID int
|
Name string
|
||||||
name string
|
Gender string
|
||||||
tags []string
|
Age int
|
||||||
|
Address string
|
||||||
|
Mobile string
|
||||||
|
Country string
|
||||||
|
Height int
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_iterator_and_bind_api(t *testing.T) {
|
func jsoniterMarshal(p *People) error {
|
||||||
iter := ParseString(`[123, {"name": "taowen", "tags": ["crazy", "hacker"]}]`)
|
_, err := Marshal(p)
|
||||||
user := User{}
|
if nil != err {
|
||||||
iter.ReadArray()
|
return err
|
||||||
user.userID = iter.ReadInt()
|
}
|
||||||
iter.ReadArray()
|
return nil
|
||||||
iter.ReadVal(&user)
|
}
|
||||||
iter.ReadArray() // array end
|
func stdMarshal(p *People) error {
|
||||||
fmt.Println(user)
|
_, 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,62 +1,62 @@
|
|||||||
package jsoniter
|
package jsoniter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/json-iterator/go/require"
|
||||||
"io"
|
"io"
|
||||||
"testing"
|
"testing"
|
||||||
"github.com/json-iterator/go/require"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_string_end(t *testing.T) {
|
func Test_string_end(t *testing.T) {
|
||||||
end, escaped := ParseString(`abc"`).findStringEnd()
|
end, escaped := ParseString(ConfigDefault, `abc"`).findStringEnd()
|
||||||
if end != 4 {
|
if end != 4 {
|
||||||
t.Fatal(end)
|
t.Fatal(end)
|
||||||
}
|
}
|
||||||
if escaped != false {
|
if escaped != false {
|
||||||
t.Fatal(escaped)
|
t.Fatal(escaped)
|
||||||
}
|
}
|
||||||
end, escaped = ParseString(`abc\\"`).findStringEnd()
|
end, escaped = ParseString(ConfigDefault, `abc\\"`).findStringEnd()
|
||||||
if end != 6 {
|
if end != 6 {
|
||||||
t.Fatal(end)
|
t.Fatal(end)
|
||||||
}
|
}
|
||||||
if escaped != true {
|
if escaped != true {
|
||||||
t.Fatal(escaped)
|
t.Fatal(escaped)
|
||||||
}
|
}
|
||||||
end, escaped = ParseString(`abc\\\\"`).findStringEnd()
|
end, escaped = ParseString(ConfigDefault, `abc\\\\"`).findStringEnd()
|
||||||
if end != 8 {
|
if end != 8 {
|
||||||
t.Fatal(end)
|
t.Fatal(end)
|
||||||
}
|
}
|
||||||
if escaped != true {
|
if escaped != true {
|
||||||
t.Fatal(escaped)
|
t.Fatal(escaped)
|
||||||
}
|
}
|
||||||
end, escaped = ParseString(`abc\"`).findStringEnd()
|
end, escaped = ParseString(ConfigDefault, `abc\"`).findStringEnd()
|
||||||
if end != -1 {
|
if end != -1 {
|
||||||
t.Fatal(end)
|
t.Fatal(end)
|
||||||
}
|
}
|
||||||
if escaped != false {
|
if escaped != false {
|
||||||
t.Fatal(escaped)
|
t.Fatal(escaped)
|
||||||
}
|
}
|
||||||
end, escaped = ParseString(`abc\`).findStringEnd()
|
end, escaped = ParseString(ConfigDefault, `abc\`).findStringEnd()
|
||||||
if end != -1 {
|
if end != -1 {
|
||||||
t.Fatal(end)
|
t.Fatal(end)
|
||||||
}
|
}
|
||||||
if escaped != true {
|
if escaped != true {
|
||||||
t.Fatal(escaped)
|
t.Fatal(escaped)
|
||||||
}
|
}
|
||||||
end, escaped = ParseString(`abc\\`).findStringEnd()
|
end, escaped = ParseString(ConfigDefault, `abc\\`).findStringEnd()
|
||||||
if end != -1 {
|
if end != -1 {
|
||||||
t.Fatal(end)
|
t.Fatal(end)
|
||||||
}
|
}
|
||||||
if escaped != false {
|
if escaped != false {
|
||||||
t.Fatal(escaped)
|
t.Fatal(escaped)
|
||||||
}
|
}
|
||||||
end, escaped = ParseString(`\\`).findStringEnd()
|
end, escaped = ParseString(ConfigDefault, `\\`).findStringEnd()
|
||||||
if end != -1 {
|
if end != -1 {
|
||||||
t.Fatal(end)
|
t.Fatal(end)
|
||||||
}
|
}
|
||||||
if escaped != false {
|
if escaped != false {
|
||||||
t.Fatal(escaped)
|
t.Fatal(escaped)
|
||||||
}
|
}
|
||||||
end, escaped = ParseString(`\`).findStringEnd()
|
end, escaped = ParseString(ConfigDefault, `\`).findStringEnd()
|
||||||
if end != -1 {
|
if end != -1 {
|
||||||
t.Fatal(end)
|
t.Fatal(end)
|
||||||
}
|
}
|
||||||
@ -91,54 +91,54 @@ func (reader *StagedReader) Read(p []byte) (n int, err error) {
|
|||||||
|
|
||||||
func Test_skip_string(t *testing.T) {
|
func Test_skip_string(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(`"abc`)
|
iter := ParseString(ConfigDefault, `"abc`)
|
||||||
iter.skipString()
|
iter.skipString()
|
||||||
should.Equal(1, iter.head)
|
should.Equal(1, iter.head)
|
||||||
iter = ParseString(`\""abc`)
|
iter = ParseString(ConfigDefault, `\""abc`)
|
||||||
iter.skipString()
|
iter.skipString()
|
||||||
should.Equal(3, iter.head)
|
should.Equal(3, iter.head)
|
||||||
reader := &StagedReader{
|
reader := &StagedReader{
|
||||||
r1: `abc`,
|
r1: `abc`,
|
||||||
r2: `"`,
|
r2: `"`,
|
||||||
}
|
}
|
||||||
iter = Parse(reader, 4096)
|
iter = Parse(ConfigDefault, reader, 4096)
|
||||||
iter.skipString()
|
iter.skipString()
|
||||||
should.Equal(1, iter.head)
|
should.Equal(1, iter.head)
|
||||||
reader = &StagedReader{
|
reader = &StagedReader{
|
||||||
r1: `abc`,
|
r1: `abc`,
|
||||||
r2: `1"`,
|
r2: `1"`,
|
||||||
}
|
}
|
||||||
iter = Parse(reader, 4096)
|
iter = Parse(ConfigDefault, reader, 4096)
|
||||||
iter.skipString()
|
iter.skipString()
|
||||||
should.Equal(2, iter.head)
|
should.Equal(2, iter.head)
|
||||||
reader = &StagedReader{
|
reader = &StagedReader{
|
||||||
r1: `abc\`,
|
r1: `abc\`,
|
||||||
r2: `"`,
|
r2: `"`,
|
||||||
}
|
}
|
||||||
iter = Parse(reader, 4096)
|
iter = Parse(ConfigDefault, reader, 4096)
|
||||||
iter.skipString()
|
iter.skipString()
|
||||||
should.NotNil(iter.Error)
|
should.NotNil(iter.Error)
|
||||||
reader = &StagedReader{
|
reader = &StagedReader{
|
||||||
r1: `abc\`,
|
r1: `abc\`,
|
||||||
r2: `""`,
|
r2: `""`,
|
||||||
}
|
}
|
||||||
iter = Parse(reader, 4096)
|
iter = Parse(ConfigDefault, reader, 4096)
|
||||||
iter.skipString()
|
iter.skipString()
|
||||||
should.Equal(2, iter.head)
|
should.Equal(2, iter.head)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_skip_object(t *testing.T) {
|
func Test_skip_object(t *testing.T) {
|
||||||
iter := ParseString(`}`)
|
iter := ParseString(ConfigDefault, `}`)
|
||||||
iter.skipObject()
|
iter.skipObject()
|
||||||
if iter.head != 1 {
|
if iter.head != 1 {
|
||||||
t.Fatal(iter.head)
|
t.Fatal(iter.head)
|
||||||
}
|
}
|
||||||
iter = ParseString(`a}`)
|
iter = ParseString(ConfigDefault, `a}`)
|
||||||
iter.skipObject()
|
iter.skipObject()
|
||||||
if iter.head != 2 {
|
if iter.head != 2 {
|
||||||
t.Fatal(iter.head)
|
t.Fatal(iter.head)
|
||||||
}
|
}
|
||||||
iter = ParseString(`{}}a`)
|
iter = ParseString(ConfigDefault, `{}}a`)
|
||||||
iter.skipObject()
|
iter.skipObject()
|
||||||
if iter.head != 3 {
|
if iter.head != 3 {
|
||||||
t.Fatal(iter.head)
|
t.Fatal(iter.head)
|
||||||
@ -147,12 +147,12 @@ func Test_skip_object(t *testing.T) {
|
|||||||
r1: `{`,
|
r1: `{`,
|
||||||
r2: `}}a`,
|
r2: `}}a`,
|
||||||
}
|
}
|
||||||
iter = Parse(reader, 4096)
|
iter = Parse(ConfigDefault, reader, 4096)
|
||||||
iter.skipObject()
|
iter.skipObject()
|
||||||
if iter.head != 2 {
|
if iter.head != 2 {
|
||||||
t.Fatal(iter.head)
|
t.Fatal(iter.head)
|
||||||
}
|
}
|
||||||
iter = ParseString(`"}"}a`)
|
iter = ParseString(ConfigDefault, `"}"}a`)
|
||||||
iter.skipObject()
|
iter.skipObject()
|
||||||
if iter.head != 4 {
|
if iter.head != 4 {
|
||||||
t.Fatal(iter.head)
|
t.Fatal(iter.head)
|
||||||
|
37
jsoniter_fixed_array_test.go
Normal file
37
jsoniter_fixed_array_test.go
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
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,28 +1,46 @@
|
|||||||
|
// +build go1.8
|
||||||
|
|
||||||
package jsoniter
|
package jsoniter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
|
||||||
"github.com/json-iterator/go/require"
|
"github.com/json-iterator/go/require"
|
||||||
"bytes"
|
|
||||||
"strconv"
|
"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) {
|
func Test_read_float(t *testing.T) {
|
||||||
inputs := []string{`1.1`, `1000`, `9223372036854775807`, `12.3`, `-12.3`, `720368.54775807`, `720368.547758075`}
|
inputs := []string{`1.1`, `1000`, `9223372036854775807`, `12.3`, `-12.3`, `720368.54775807`, `720368.547758075`}
|
||||||
for _, input := range inputs {
|
for _, input := range inputs {
|
||||||
// non-streaming
|
// non-streaming
|
||||||
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(input + ",")
|
iter := ParseString(ConfigDefault, input+",")
|
||||||
expected, err := strconv.ParseFloat(input, 32)
|
expected, err := strconv.ParseFloat(input, 32)
|
||||||
should.Nil(err)
|
should.Nil(err)
|
||||||
should.Equal(float32(expected), iter.ReadFloat32())
|
should.Equal(float32(expected), iter.ReadFloat32())
|
||||||
})
|
})
|
||||||
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(input + ",")
|
iter := ParseString(ConfigDefault, input+",")
|
||||||
expected, err := strconv.ParseFloat(input, 64)
|
expected, err := strconv.ParseFloat(input, 64)
|
||||||
should.Nil(err)
|
should.Nil(err)
|
||||||
should.Equal(expected, iter.ReadFloat64())
|
should.Equal(expected, iter.ReadFloat64())
|
||||||
@ -30,14 +48,14 @@ func Test_read_float(t *testing.T) {
|
|||||||
// streaming
|
// streaming
|
||||||
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := Parse(bytes.NewBufferString(input + ","), 2)
|
iter := Parse(ConfigDefault, bytes.NewBufferString(input+","), 2)
|
||||||
expected, err := strconv.ParseFloat(input, 32)
|
expected, err := strconv.ParseFloat(input, 32)
|
||||||
should.Nil(err)
|
should.Nil(err)
|
||||||
should.Equal(float32(expected), iter.ReadFloat32())
|
should.Equal(float32(expected), iter.ReadFloat32())
|
||||||
})
|
})
|
||||||
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := Parse(bytes.NewBufferString(input + ","), 2)
|
iter := Parse(ConfigDefault, bytes.NewBufferString(input+","), 2)
|
||||||
expected, err := strconv.ParseFloat(input, 64)
|
expected, err := strconv.ParseFloat(input, 64)
|
||||||
should.Nil(err)
|
should.Nil(err)
|
||||||
should.Equal(expected, iter.ReadFloat64())
|
should.Equal(expected, iter.ReadFloat64())
|
||||||
@ -47,19 +65,10 @@ func Test_read_float(t *testing.T) {
|
|||||||
|
|
||||||
func Test_read_float_as_interface(t *testing.T) {
|
func Test_read_float_as_interface(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(`12.3`)
|
iter := ParseString(ConfigDefault, `12.3`)
|
||||||
should.Equal(float64(12.3), iter.Read())
|
should.Equal(float64(12.3), iter.Read())
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_read_float_as_any(t *testing.T) {
|
|
||||||
should := require.New(t)
|
|
||||||
any, err := UnmarshalAnyFromString("12.3")
|
|
||||||
should.Nil(err)
|
|
||||||
should.Equal(float64(12.3), any.ToFloat64())
|
|
||||||
should.Equal("12.3", any.ToString())
|
|
||||||
should.True(any.ToBool())
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_wrap_float(t *testing.T) {
|
func Test_wrap_float(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
str, err := MarshalToString(WrapFloat64(12.3))
|
str, err := MarshalToString(WrapFloat64(12.3))
|
||||||
@ -69,32 +78,36 @@ func Test_wrap_float(t *testing.T) {
|
|||||||
|
|
||||||
func Test_write_float32(t *testing.T) {
|
func Test_write_float32(t *testing.T) {
|
||||||
vals := []float32{0, 1, -1, 99, 0xff, 0xfff, 0xffff, 0xfffff, 0xffffff, 0x4ffffff, 0xfffffff,
|
vals := []float32{0, 1, -1, 99, 0xff, 0xfff, 0xffff, 0xfffff, 0xffffff, 0x4ffffff, 0xfffffff,
|
||||||
-0x4ffffff, -0xfffffff, 1.2345, 1.23456, 1.234567, 1.001}
|
-0x4ffffff, -0xfffffff, 1.2345, 1.23456, 1.234567, 1.001}
|
||||||
for _, val := range vals {
|
for _, val := range vals {
|
||||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(ConfigDefault, buf, 4096)
|
||||||
stream.WriteFloat32(val)
|
stream.WriteFloat32Lossy(val)
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
should.Equal(strconv.FormatFloat(float64(val), 'f', -1, 32), buf.String())
|
output, err := json.Marshal(val)
|
||||||
|
should.Nil(err)
|
||||||
|
should.Equal(string(output), buf.String())
|
||||||
})
|
})
|
||||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(ConfigDefault, buf, 4096)
|
||||||
stream.WriteVal(val)
|
stream.WriteVal(val)
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
should.Equal(strconv.FormatFloat(float64(val), 'f', -1, 32), buf.String())
|
output, err := json.Marshal(val)
|
||||||
|
should.Nil(err)
|
||||||
|
should.Equal(string(output), buf.String())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 10)
|
stream := NewStream(ConfigDefault, buf, 10)
|
||||||
stream.WriteRaw("abcdefg")
|
stream.WriteRaw("abcdefg")
|
||||||
stream.WriteFloat32(1.123456)
|
stream.WriteFloat32Lossy(1.123456)
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
should.Equal("abcdefg1.123456", buf.String())
|
should.Equal("abcdefg1.123456", buf.String())
|
||||||
@ -102,13 +115,13 @@ func Test_write_float32(t *testing.T) {
|
|||||||
|
|
||||||
func Test_write_float64(t *testing.T) {
|
func Test_write_float64(t *testing.T) {
|
||||||
vals := []float64{0, 1, -1, 99, 0xff, 0xfff, 0xffff, 0xfffff, 0xffffff, 0x4ffffff, 0xfffffff,
|
vals := []float64{0, 1, -1, 99, 0xff, 0xfff, 0xffff, 0xfffff, 0xffffff, 0x4ffffff, 0xfffffff,
|
||||||
-0x4ffffff, -0xfffffff, 1.2345, 1.23456, 1.234567, 1.001}
|
-0x4ffffff, -0xfffffff, 1.2345, 1.23456, 1.234567, 1.001}
|
||||||
for _, val := range vals {
|
for _, val := range vals {
|
||||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(ConfigDefault, buf, 4096)
|
||||||
stream.WriteFloat64(val)
|
stream.WriteFloat64Lossy(val)
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
should.Equal(strconv.FormatFloat(val, 'f', -1, 64), buf.String())
|
should.Equal(strconv.FormatFloat(val, 'f', -1, 64), buf.String())
|
||||||
@ -116,7 +129,7 @@ func Test_write_float64(t *testing.T) {
|
|||||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(ConfigDefault, buf, 4096)
|
||||||
stream.WriteVal(val)
|
stream.WriteVal(val)
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
@ -125,18 +138,51 @@ func Test_write_float64(t *testing.T) {
|
|||||||
}
|
}
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 10)
|
stream := NewStream(ConfigDefault, buf, 10)
|
||||||
stream.WriteRaw("abcdefg")
|
stream.WriteRaw("abcdefg")
|
||||||
stream.WriteFloat64(1.123456)
|
stream.WriteFloat64Lossy(1.123456)
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
should.Equal("abcdefg1.123456", buf.String())
|
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) {
|
func Benchmark_jsoniter_float(b *testing.B) {
|
||||||
b.ReportAllocs()
|
b.ReportAllocs()
|
||||||
input := []byte(`1.1123,`)
|
input := []byte(`1.1123,`)
|
||||||
iter := NewIterator()
|
iter := NewIterator(ConfigDefault)
|
||||||
for n := 0; n < b.N; n++ {
|
for n := 0; n < b.N; n++ {
|
||||||
iter.ResetBytes(input)
|
iter.ResetBytes(input)
|
||||||
iter.ReadFloat64()
|
iter.ReadFloat64()
|
||||||
|
@ -1,19 +1,20 @@
|
|||||||
|
// +build go1.8
|
||||||
|
|
||||||
package jsoniter
|
package jsoniter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"testing"
|
|
||||||
"github.com/json-iterator/go/require"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"github.com/json-iterator/go/require"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"io"
|
"strconv"
|
||||||
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_read_uint64_invalid(t *testing.T) {
|
func Test_read_uint64_invalid(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(",")
|
iter := ParseString(ConfigDefault, ",")
|
||||||
iter.ReadUint64()
|
iter.ReadUint64()
|
||||||
should.NotNil(iter.Error)
|
should.NotNil(iter.Error)
|
||||||
}
|
}
|
||||||
@ -23,7 +24,7 @@ func Test_read_int8(t *testing.T) {
|
|||||||
for _, input := range inputs {
|
for _, input := range inputs {
|
||||||
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(input)
|
iter := ParseString(ConfigDefault, input)
|
||||||
expected, err := strconv.ParseInt(input, 10, 8)
|
expected, err := strconv.ParseInt(input, 10, 8)
|
||||||
should.Nil(err)
|
should.Nil(err)
|
||||||
should.Equal(int8(expected), iter.ReadInt8())
|
should.Equal(int8(expected), iter.ReadInt8())
|
||||||
@ -36,7 +37,7 @@ func Test_read_int16(t *testing.T) {
|
|||||||
for _, input := range inputs {
|
for _, input := range inputs {
|
||||||
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(input)
|
iter := ParseString(ConfigDefault, input)
|
||||||
expected, err := strconv.ParseInt(input, 10, 16)
|
expected, err := strconv.ParseInt(input, 10, 16)
|
||||||
should.Nil(err)
|
should.Nil(err)
|
||||||
should.Equal(int16(expected), iter.ReadInt16())
|
should.Equal(int16(expected), iter.ReadInt16())
|
||||||
@ -49,14 +50,14 @@ func Test_read_int32(t *testing.T) {
|
|||||||
for _, input := range inputs {
|
for _, input := range inputs {
|
||||||
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(input)
|
iter := ParseString(ConfigDefault, input)
|
||||||
expected, err := strconv.ParseInt(input, 10, 32)
|
expected, err := strconv.ParseInt(input, 10, 32)
|
||||||
should.Nil(err)
|
should.Nil(err)
|
||||||
should.Equal(int32(expected), iter.ReadInt32())
|
should.Equal(int32(expected), iter.ReadInt32())
|
||||||
})
|
})
|
||||||
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := Parse(bytes.NewBufferString(input), 2)
|
iter := Parse(ConfigDefault, bytes.NewBufferString(input), 2)
|
||||||
expected, err := strconv.ParseInt(input, 10, 32)
|
expected, err := strconv.ParseInt(input, 10, 32)
|
||||||
should.Nil(err)
|
should.Nil(err)
|
||||||
should.Equal(int32(expected), iter.ReadInt32())
|
should.Equal(int32(expected), iter.ReadInt32())
|
||||||
@ -83,7 +84,7 @@ func Test_read_int64_array(t *testing.T) {
|
|||||||
func Test_read_int32_overflow(t *testing.T) {
|
func Test_read_int32_overflow(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
input := "123456789123456789,"
|
input := "123456789123456789,"
|
||||||
iter := ParseString(input)
|
iter := ParseString(ConfigDefault, input)
|
||||||
iter.ReadInt32()
|
iter.ReadInt32()
|
||||||
should.NotNil(iter.Error)
|
should.NotNil(iter.Error)
|
||||||
}
|
}
|
||||||
@ -93,14 +94,14 @@ func Test_read_int64(t *testing.T) {
|
|||||||
for _, input := range inputs {
|
for _, input := range inputs {
|
||||||
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(input)
|
iter := ParseString(ConfigDefault, input)
|
||||||
expected, err := strconv.ParseInt(input, 10, 64)
|
expected, err := strconv.ParseInt(input, 10, 64)
|
||||||
should.Nil(err)
|
should.Nil(err)
|
||||||
should.Equal(expected, iter.ReadInt64())
|
should.Equal(expected, iter.ReadInt64())
|
||||||
})
|
})
|
||||||
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := Parse(bytes.NewBufferString(input), 2)
|
iter := Parse(ConfigDefault, bytes.NewBufferString(input), 2)
|
||||||
expected, err := strconv.ParseInt(input, 10, 64)
|
expected, err := strconv.ParseInt(input, 10, 64)
|
||||||
should.Nil(err)
|
should.Nil(err)
|
||||||
should.Equal(expected, iter.ReadInt64())
|
should.Equal(expected, iter.ReadInt64())
|
||||||
@ -111,28 +112,11 @@ func Test_read_int64(t *testing.T) {
|
|||||||
func Test_read_int64_overflow(t *testing.T) {
|
func Test_read_int64_overflow(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
input := "123456789123456789123456789123456789,"
|
input := "123456789123456789123456789123456789,"
|
||||||
iter := ParseString(input)
|
iter := ParseString(ConfigDefault, input)
|
||||||
iter.ReadInt64()
|
iter.ReadInt64()
|
||||||
should.NotNil(iter.Error)
|
should.NotNil(iter.Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_read_int64_as_any(t *testing.T) {
|
|
||||||
should := require.New(t)
|
|
||||||
any, err := UnmarshalAnyFromString("1234")
|
|
||||||
should.Nil(err)
|
|
||||||
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, err := UnmarshalAnyFromString("1234")
|
|
||||||
should.Nil(err)
|
|
||||||
should.Equal(Invalid, any.Get(1, "2").ValueType())
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_wrap_int(t *testing.T) {
|
func Test_wrap_int(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
str, err := MarshalToString(WrapInt64(100))
|
str, err := MarshalToString(WrapInt64(100))
|
||||||
@ -146,7 +130,7 @@ func Test_write_uint8(t *testing.T) {
|
|||||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(ConfigDefault, buf, 4096)
|
||||||
stream.WriteUint8(val)
|
stream.WriteUint8(val)
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
@ -155,7 +139,7 @@ func Test_write_uint8(t *testing.T) {
|
|||||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(ConfigDefault, buf, 4096)
|
||||||
stream.WriteVal(val)
|
stream.WriteVal(val)
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
@ -164,7 +148,7 @@ func Test_write_uint8(t *testing.T) {
|
|||||||
}
|
}
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 3)
|
stream := NewStream(ConfigDefault, buf, 3)
|
||||||
stream.WriteRaw("a")
|
stream.WriteRaw("a")
|
||||||
stream.WriteUint8(100) // should clear buffer
|
stream.WriteUint8(100) // should clear buffer
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
@ -178,7 +162,7 @@ func Test_write_int8(t *testing.T) {
|
|||||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(ConfigDefault, buf, 4096)
|
||||||
stream.WriteInt8(val)
|
stream.WriteInt8(val)
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
@ -187,7 +171,7 @@ func Test_write_int8(t *testing.T) {
|
|||||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(ConfigDefault, buf, 4096)
|
||||||
stream.WriteVal(val)
|
stream.WriteVal(val)
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
@ -196,7 +180,7 @@ func Test_write_int8(t *testing.T) {
|
|||||||
}
|
}
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4)
|
stream := NewStream(ConfigDefault, buf, 4)
|
||||||
stream.WriteRaw("a")
|
stream.WriteRaw("a")
|
||||||
stream.WriteInt8(-100) // should clear buffer
|
stream.WriteInt8(-100) // should clear buffer
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
@ -210,7 +194,7 @@ func Test_write_uint16(t *testing.T) {
|
|||||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(ConfigDefault, buf, 4096)
|
||||||
stream.WriteUint16(val)
|
stream.WriteUint16(val)
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
@ -219,7 +203,7 @@ func Test_write_uint16(t *testing.T) {
|
|||||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(ConfigDefault, buf, 4096)
|
||||||
stream.WriteVal(val)
|
stream.WriteVal(val)
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
@ -228,7 +212,7 @@ func Test_write_uint16(t *testing.T) {
|
|||||||
}
|
}
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 5)
|
stream := NewStream(ConfigDefault, buf, 5)
|
||||||
stream.WriteRaw("a")
|
stream.WriteRaw("a")
|
||||||
stream.WriteUint16(10000) // should clear buffer
|
stream.WriteUint16(10000) // should clear buffer
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
@ -242,7 +226,7 @@ func Test_write_int16(t *testing.T) {
|
|||||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(ConfigDefault, buf, 4096)
|
||||||
stream.WriteInt16(val)
|
stream.WriteInt16(val)
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
@ -251,7 +235,7 @@ func Test_write_int16(t *testing.T) {
|
|||||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(ConfigDefault, buf, 4096)
|
||||||
stream.WriteVal(val)
|
stream.WriteVal(val)
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
@ -260,7 +244,7 @@ func Test_write_int16(t *testing.T) {
|
|||||||
}
|
}
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 6)
|
stream := NewStream(ConfigDefault, buf, 6)
|
||||||
stream.WriteRaw("a")
|
stream.WriteRaw("a")
|
||||||
stream.WriteInt16(-10000) // should clear buffer
|
stream.WriteInt16(-10000) // should clear buffer
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
@ -274,7 +258,7 @@ func Test_write_uint32(t *testing.T) {
|
|||||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(ConfigDefault, buf, 4096)
|
||||||
stream.WriteUint32(val)
|
stream.WriteUint32(val)
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
@ -283,7 +267,7 @@ func Test_write_uint32(t *testing.T) {
|
|||||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(ConfigDefault, buf, 4096)
|
||||||
stream.WriteVal(val)
|
stream.WriteVal(val)
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
@ -292,7 +276,7 @@ func Test_write_uint32(t *testing.T) {
|
|||||||
}
|
}
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 10)
|
stream := NewStream(ConfigDefault, buf, 10)
|
||||||
stream.WriteRaw("a")
|
stream.WriteRaw("a")
|
||||||
stream.WriteUint32(0xffffffff) // should clear buffer
|
stream.WriteUint32(0xffffffff) // should clear buffer
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
@ -306,7 +290,7 @@ func Test_write_int32(t *testing.T) {
|
|||||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(ConfigDefault, buf, 4096)
|
||||||
stream.WriteInt32(val)
|
stream.WriteInt32(val)
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
@ -315,7 +299,7 @@ func Test_write_int32(t *testing.T) {
|
|||||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(ConfigDefault, buf, 4096)
|
||||||
stream.WriteVal(val)
|
stream.WriteVal(val)
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
@ -324,7 +308,7 @@ func Test_write_int32(t *testing.T) {
|
|||||||
}
|
}
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 11)
|
stream := NewStream(ConfigDefault, buf, 11)
|
||||||
stream.WriteRaw("a")
|
stream.WriteRaw("a")
|
||||||
stream.WriteInt32(-0x7fffffff) // should clear buffer
|
stream.WriteInt32(-0x7fffffff) // should clear buffer
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
@ -340,7 +324,7 @@ func Test_write_uint64(t *testing.T) {
|
|||||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(ConfigDefault, buf, 4096)
|
||||||
stream.WriteUint64(val)
|
stream.WriteUint64(val)
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
@ -349,7 +333,7 @@ func Test_write_uint64(t *testing.T) {
|
|||||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(ConfigDefault, buf, 4096)
|
||||||
stream.WriteVal(val)
|
stream.WriteVal(val)
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
@ -358,7 +342,7 @@ func Test_write_uint64(t *testing.T) {
|
|||||||
}
|
}
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 10)
|
stream := NewStream(ConfigDefault, buf, 10)
|
||||||
stream.WriteRaw("a")
|
stream.WriteRaw("a")
|
||||||
stream.WriteUint64(0xffffffff) // should clear buffer
|
stream.WriteUint64(0xffffffff) // should clear buffer
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
@ -374,7 +358,7 @@ func Test_write_int64(t *testing.T) {
|
|||||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(ConfigDefault, buf, 4096)
|
||||||
stream.WriteInt64(val)
|
stream.WriteInt64(val)
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
@ -383,7 +367,7 @@ func Test_write_int64(t *testing.T) {
|
|||||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(ConfigDefault, buf, 4096)
|
||||||
stream.WriteVal(val)
|
stream.WriteVal(val)
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
@ -392,7 +376,7 @@ func Test_write_int64(t *testing.T) {
|
|||||||
}
|
}
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 10)
|
stream := NewStream(ConfigDefault, buf, 10)
|
||||||
stream.WriteRaw("a")
|
stream.WriteRaw("a")
|
||||||
stream.WriteInt64(0xffffffff) // should clear buffer
|
stream.WriteInt64(0xffffffff) // should clear buffer
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
@ -403,7 +387,7 @@ func Test_write_int64(t *testing.T) {
|
|||||||
func Test_write_val_int(t *testing.T) {
|
func Test_write_val_int(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(ConfigDefault, buf, 4096)
|
||||||
stream.WriteVal(1001)
|
stream.WriteVal(1001)
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
@ -413,7 +397,7 @@ func Test_write_val_int(t *testing.T) {
|
|||||||
func Test_write_val_int_ptr(t *testing.T) {
|
func Test_write_val_int_ptr(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(ConfigDefault, buf, 4096)
|
||||||
val := 1001
|
val := 1001
|
||||||
stream.WriteVal(&val)
|
stream.WriteVal(&val)
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
@ -421,8 +405,19 @@ func Test_write_val_int_ptr(t *testing.T) {
|
|||||||
should.Equal("1001", buf.String())
|
should.Equal("1001", buf.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_json_number(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
var arr []json.Number
|
||||||
|
err := Unmarshal([]byte(`[1]`), &arr)
|
||||||
|
should.Nil(err)
|
||||||
|
should.Equal(json.Number("1"), arr[0])
|
||||||
|
str, err := MarshalToString(arr)
|
||||||
|
should.Nil(err)
|
||||||
|
should.Equal(`[1]`, str)
|
||||||
|
}
|
||||||
|
|
||||||
func Benchmark_jsoniter_encode_int(b *testing.B) {
|
func Benchmark_jsoniter_encode_int(b *testing.B) {
|
||||||
stream := NewStream(ioutil.Discard, 64)
|
stream := NewStream(ConfigDefault, ioutil.Discard, 64)
|
||||||
for n := 0; n < b.N; n++ {
|
for n := 0; n < b.N; n++ {
|
||||||
stream.n = 0
|
stream.n = 0
|
||||||
stream.WriteUint64(0xffffffff)
|
stream.WriteUint64(0xffffffff)
|
||||||
@ -436,7 +431,7 @@ func Benchmark_itoa(b *testing.B) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Benchmark_jsoniter_int(b *testing.B) {
|
func Benchmark_jsoniter_int(b *testing.B) {
|
||||||
iter := NewIterator()
|
iter := NewIterator(ConfigDefault)
|
||||||
input := []byte(`100`)
|
input := []byte(`100`)
|
||||||
for n := 0; n < b.N; n++ {
|
for n := 0; n < b.N; n++ {
|
||||||
iter.ResetBytes(input)
|
iter.ResetBytes(input)
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
package jsoniter
|
package jsoniter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"encoding/json"
|
||||||
"github.com/json-iterator/go/require"
|
"github.com/json-iterator/go/require"
|
||||||
|
"testing"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -16,7 +17,7 @@ func Test_write_array_of_interface(t *testing.T) {
|
|||||||
|
|
||||||
func Test_write_map_of_interface(t *testing.T) {
|
func Test_write_map_of_interface(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
val := map[string]interface{}{"hello":"world"}
|
val := map[string]interface{}{"hello": "world"}
|
||||||
str, err := MarshalToString(val)
|
str, err := MarshalToString(val)
|
||||||
should.Nil(err)
|
should.Nil(err)
|
||||||
should.Equal(`{"hello":"world"}`, str)
|
should.Equal(`{"hello":"world"}`, str)
|
||||||
@ -27,7 +28,7 @@ func Test_write_map_of_interface_in_struct(t *testing.T) {
|
|||||||
Field map[string]interface{}
|
Field map[string]interface{}
|
||||||
}
|
}
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
val := TestObject{map[string]interface{}{"hello":"world"}}
|
val := TestObject{map[string]interface{}{"hello": "world"}}
|
||||||
str, err := MarshalToString(val)
|
str, err := MarshalToString(val)
|
||||||
should.Nil(err)
|
should.Nil(err)
|
||||||
should.Equal(`{"Field":{"hello":"world"}}`, str)
|
should.Equal(`{"Field":{"hello":"world"}}`, str)
|
||||||
@ -35,14 +36,14 @@ func Test_write_map_of_interface_in_struct(t *testing.T) {
|
|||||||
|
|
||||||
func Test_write_map_of_interface_in_struct_with_two_fields(t *testing.T) {
|
func Test_write_map_of_interface_in_struct_with_two_fields(t *testing.T) {
|
||||||
type TestObject struct {
|
type TestObject struct {
|
||||||
Field map[string]interface{}
|
Field map[string]interface{}
|
||||||
Field2 string
|
Field2 string
|
||||||
}
|
}
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
val := TestObject{map[string]interface{}{"hello":"world"}, ""}
|
val := TestObject{map[string]interface{}{"hello": "world"}, ""}
|
||||||
str, err := MarshalToString(val)
|
str, err := MarshalToString(val)
|
||||||
should.Nil(err)
|
should.Nil(err)
|
||||||
should.Equal(`{"Field":{"hello":"world"},"Field2":""}`, str)
|
should.Contains(str, `"Field":{"hello":"world"}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
type MyInterface interface {
|
type MyInterface interface {
|
||||||
@ -59,7 +60,7 @@ func Test_write_map_of_custom_interface(t *testing.T) {
|
|||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
myStr := MyString("world")
|
myStr := MyString("world")
|
||||||
should.Equal("world", myStr.Hello())
|
should.Equal("world", myStr.Hello())
|
||||||
val := map[string]MyInterface{"hello":myStr}
|
val := map[string]MyInterface{"hello": myStr}
|
||||||
str, err := MarshalToString(val)
|
str, err := MarshalToString(val)
|
||||||
should.Nil(err)
|
should.Nil(err)
|
||||||
should.Equal(`{"hello":"world"}`, str)
|
should.Equal(`{"hello":"world"}`, str)
|
||||||
@ -85,10 +86,205 @@ func Test_read_interface(t *testing.T) {
|
|||||||
func Test_read_custom_interface(t *testing.T) {
|
func Test_read_custom_interface(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
var val MyInterface
|
var val MyInterface
|
||||||
RegisterTypeDecoder("jsoniter.MyInterface", func(ptr unsafe.Pointer, iter *Iterator) {
|
RegisterTypeDecoderFunc("jsoniter.MyInterface", func(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
*((*MyInterface)(ptr)) = MyString(iter.ReadString())
|
*((*MyInterface)(ptr)) = MyString(iter.ReadString())
|
||||||
})
|
})
|
||||||
err := UnmarshalFromString(`"hello"`, &val)
|
err := UnmarshalFromString(`"hello"`, &val)
|
||||||
should.Nil(err)
|
should.Nil(err)
|
||||||
should.Equal("hello", val.Hello())
|
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)
|
||||||
|
}
|
||||||
|
@ -7,7 +7,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func Test_read_by_one(t *testing.T) {
|
func Test_read_by_one(t *testing.T) {
|
||||||
iter := Parse(bytes.NewBufferString("abc"), 1)
|
iter := Parse(ConfigDefault, bytes.NewBufferString("abc"), 1)
|
||||||
b := iter.readByte()
|
b := iter.readByte()
|
||||||
if iter.Error != nil {
|
if iter.Error != nil {
|
||||||
t.Fatal(iter.Error)
|
t.Fatal(iter.Error)
|
||||||
@ -34,7 +34,7 @@ func Test_read_by_one(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_read_by_two(t *testing.T) {
|
func Test_read_by_two(t *testing.T) {
|
||||||
iter := Parse(bytes.NewBufferString("abc"), 2)
|
iter := Parse(ConfigDefault, bytes.NewBufferString("abc"), 2)
|
||||||
b := iter.readByte()
|
b := iter.readByte()
|
||||||
if iter.Error != nil {
|
if iter.Error != nil {
|
||||||
t.Fatal(iter.Error)
|
t.Fatal(iter.Error)
|
||||||
@ -67,7 +67,7 @@ func Test_read_by_two(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_read_until_eof(t *testing.T) {
|
func Test_read_until_eof(t *testing.T) {
|
||||||
iter := Parse(bytes.NewBufferString("abc"), 2)
|
iter := Parse(ConfigDefault, bytes.NewBufferString("abc"), 2)
|
||||||
iter.readByte()
|
iter.readByte()
|
||||||
iter.readByte()
|
iter.readByte()
|
||||||
b := iter.readByte()
|
b := iter.readByte()
|
||||||
|
File diff suppressed because one or more lines are too long
@ -27,7 +27,7 @@ func Benchmark_jsoniter_large_file(b *testing.B) {
|
|||||||
b.ReportAllocs()
|
b.ReportAllocs()
|
||||||
for n := 0; n < b.N; n++ {
|
for n := 0; n < b.N; n++ {
|
||||||
file, _ := os.Open("/tmp/large-file.json")
|
file, _ := os.Open("/tmp/large-file.json")
|
||||||
iter := Parse(file, 4096)
|
iter := Parse(ConfigDefault, file, 4096)
|
||||||
count := 0
|
count := 0
|
||||||
for iter.ReadArray() {
|
for iter.ReadArray() {
|
||||||
iter.Skip()
|
iter.Skip()
|
||||||
|
@ -1,13 +1,15 @@
|
|||||||
package jsoniter
|
package jsoniter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"encoding/json"
|
||||||
"github.com/json-iterator/go/require"
|
"github.com/json-iterator/go/require"
|
||||||
|
"math/big"
|
||||||
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_read_map(t *testing.T) {
|
func Test_read_map(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(`{"hello": "world"}`)
|
iter := ParseString(ConfigDefault, `{"hello": "world"}`)
|
||||||
m := map[string]string{"1": "2"}
|
m := map[string]string{"1": "2"}
|
||||||
iter.ReadVal(&m)
|
iter.ReadVal(&m)
|
||||||
copy(iter.buf, []byte{0, 0, 0, 0, 0, 0})
|
copy(iter.buf, []byte{0, 0, 0, 0, 0, 0})
|
||||||
@ -16,36 +18,17 @@ func Test_read_map(t *testing.T) {
|
|||||||
|
|
||||||
func Test_read_map_of_interface(t *testing.T) {
|
func Test_read_map_of_interface(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(`{"hello": "world"}`)
|
iter := ParseString(ConfigDefault, `{"hello": "world"}`)
|
||||||
m := map[string]interface{}{"1": "2"}
|
m := map[string]interface{}{"1": "2"}
|
||||||
iter.ReadVal(&m)
|
iter.ReadVal(&m)
|
||||||
should.Equal(map[string]interface{}{"1": "2", "hello": "world"}, m)
|
should.Equal(map[string]interface{}{"1": "2", "hello": "world"}, m)
|
||||||
iter = ParseString(`{"hello": "world"}`)
|
iter = ParseString(ConfigDefault, `{"hello": "world"}`)
|
||||||
should.Equal(map[string]interface{}{"hello": "world"}, iter.Read())
|
should.Equal(map[string]interface{}{"hello": "world"}, iter.Read())
|
||||||
}
|
}
|
||||||
|
|
||||||
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())
|
|
||||||
any = Wrap(map[string]string{"Field1": "hello"})
|
|
||||||
vals := map[string]string{}
|
|
||||||
var k string
|
|
||||||
var v Any
|
|
||||||
for next, hasNext := any.IterateObject(); hasNext; {
|
|
||||||
k, v, hasNext = next()
|
|
||||||
if v.ValueType() == String {
|
|
||||||
vals[k] = v.ToString()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
should.Equal(map[string]string{"Field1":"hello"}, vals)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_map_wrapper_any_get_all(t *testing.T) {
|
func Test_map_wrapper_any_get_all(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
any := Wrap(map[string][]int{"Field1": []int{1, 2}})
|
any := Wrap(map[string][]int{"Field1": {1, 2}})
|
||||||
should.Equal(`{"Field1":1}`, any.Get('*', 0).ToString())
|
should.Equal(`{"Field1":1}`, any.Get('*', 0).ToString())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,3 +39,84 @@ func Test_write_val_map(t *testing.T) {
|
|||||||
should.Nil(err)
|
should.Nil(err)
|
||||||
should.Equal(`{"1":"2"}`, str)
|
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)
|
||||||
|
}
|
||||||
|
@ -15,7 +15,7 @@ type Level2 struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_nested(t *testing.T) {
|
func Test_nested(t *testing.T) {
|
||||||
iter := ParseString(`{"hello": [{"world": "value1"}, {"world": "value2"}]}`)
|
iter := ParseString(ConfigDefault, `{"hello": [{"world": "value1"}, {"world": "value2"}]}`)
|
||||||
l1 := Level1{}
|
l1 := Level1{}
|
||||||
for l1Field := iter.ReadObject(); l1Field != ""; l1Field = iter.ReadObject() {
|
for l1Field := iter.ReadObject(); l1Field != ""; l1Field = iter.ReadObject() {
|
||||||
switch l1Field {
|
switch l1Field {
|
||||||
@ -28,14 +28,14 @@ func Test_nested(t *testing.T) {
|
|||||||
case "world":
|
case "world":
|
||||||
l2.World = iter.ReadString()
|
l2.World = iter.ReadString()
|
||||||
default:
|
default:
|
||||||
iter.reportError("bind l2", "unexpected field: "+l2Field)
|
iter.ReportError("bind l2", "unexpected field: "+l2Field)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
l2Array = append(l2Array, l2)
|
l2Array = append(l2Array, l2)
|
||||||
}
|
}
|
||||||
l1.Hello = l2Array
|
l1.Hello = l2Array
|
||||||
default:
|
default:
|
||||||
iter.reportError("bind l1", "unexpected field: "+l1Field)
|
iter.ReportError("bind l1", "unexpected field: "+l1Field)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !reflect.DeepEqual(l1, Level1{
|
if !reflect.DeepEqual(l1, Level1{
|
||||||
@ -50,7 +50,7 @@ func Test_nested(t *testing.T) {
|
|||||||
|
|
||||||
func Benchmark_jsoniter_nested(b *testing.B) {
|
func Benchmark_jsoniter_nested(b *testing.B) {
|
||||||
for n := 0; n < b.N; n++ {
|
for n := 0; n < b.N; n++ {
|
||||||
iter := ParseString(`{"hello": [{"world": "value1"}, {"world": "value2"}]}`)
|
iter := ParseString(ConfigDefault, `{"hello": [{"world": "value1"}, {"world": "value2"}]}`)
|
||||||
l1 := Level1{}
|
l1 := Level1{}
|
||||||
for l1Field := iter.ReadObject(); l1Field != ""; l1Field = iter.ReadObject() {
|
for l1Field := iter.ReadObject(); l1Field != ""; l1Field = iter.ReadObject() {
|
||||||
switch l1Field {
|
switch l1Field {
|
||||||
|
@ -1,30 +1,24 @@
|
|||||||
package jsoniter
|
package jsoniter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
|
||||||
"github.com/json-iterator/go/require"
|
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"github.com/json-iterator/go/require"
|
||||||
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_read_null(t *testing.T) {
|
func Test_read_null(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(`null`)
|
iter := ParseString(ConfigDefault, `null`)
|
||||||
should.True(iter.ReadNil())
|
should.True(iter.ReadNil())
|
||||||
iter = ParseString(`null`)
|
iter = ParseString(ConfigDefault, `null`)
|
||||||
should.Nil(iter.Read())
|
should.Nil(iter.Read())
|
||||||
iter = ParseString(`null`)
|
|
||||||
any, err := UnmarshalAnyFromString(`null`)
|
|
||||||
should.Nil(err)
|
|
||||||
should.Equal(0, any.ToInt())
|
|
||||||
should.Equal(float64(0), any.ToFloat64())
|
|
||||||
should.Equal("", any.ToString())
|
|
||||||
should.False(any.ToBool())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_write_null(t *testing.T) {
|
func Test_write_null(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(ConfigDefault, buf, 4096)
|
||||||
stream.WriteNil()
|
stream.WriteNil()
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
@ -38,8 +32,9 @@ func Test_encode_null(t *testing.T) {
|
|||||||
should.Equal("null", str)
|
should.Equal("null", str)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_decode_null_object(t *testing.T) {
|
func Test_decode_null_object_field(t *testing.T) {
|
||||||
iter := ParseString(`[null,"a"]`)
|
should := require.New(t)
|
||||||
|
iter := ParseString(ConfigDefault, `[null,"a"]`)
|
||||||
iter.ReadArray()
|
iter.ReadArray()
|
||||||
if iter.ReadObject() != "" {
|
if iter.ReadObject() != "" {
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
@ -48,31 +43,48 @@ func Test_decode_null_object(t *testing.T) {
|
|||||||
if iter.ReadString() != "a" {
|
if iter.ReadString() != "a" {
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
|
type TestObject struct {
|
||||||
|
Field string
|
||||||
|
}
|
||||||
|
objs := []TestObject{}
|
||||||
|
should.Nil(UnmarshalFromString("[null]", &objs))
|
||||||
|
should.Len(objs, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_decode_null_array(t *testing.T) {
|
func Test_decode_null_array_element(t *testing.T) {
|
||||||
iter := ParseString(`[null,"a"]`)
|
|
||||||
iter.ReadArray()
|
|
||||||
if iter.ReadArray() != false {
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
iter.ReadArray()
|
|
||||||
if iter.ReadString() != "a" {
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_decode_null_string(t *testing.T) {
|
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(`[null,"a"]`)
|
iter := ParseString(ConfigDefault, `[null,"a"]`)
|
||||||
should.True(iter.ReadArray())
|
should.True(iter.ReadArray())
|
||||||
should.True(iter.ReadNil())
|
should.True(iter.ReadNil())
|
||||||
should.True(iter.ReadArray())
|
should.True(iter.ReadArray())
|
||||||
should.Equal("a", iter.ReadString())
|
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) {
|
func Test_decode_null_skip(t *testing.T) {
|
||||||
iter := ParseString(`[null,"a"]`)
|
iter := ParseString(ConfigDefault, `[null,"a"]`)
|
||||||
iter.ReadArray()
|
iter.ReadArray()
|
||||||
iter.Skip()
|
iter.Skip()
|
||||||
iter.ReadArray()
|
iter.ReadArray()
|
||||||
@ -80,3 +92,39 @@ func Test_decode_null_skip(t *testing.T) {
|
|||||||
t.FailNow()
|
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,18 +1,18 @@
|
|||||||
package jsoniter
|
package jsoniter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"testing"
|
|
||||||
"github.com/json-iterator/go/require"
|
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"github.com/json-iterator/go/require"
|
||||||
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_empty_object(t *testing.T) {
|
func Test_empty_object(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(`{}`)
|
iter := ParseString(ConfigDefault, `{}`)
|
||||||
field := iter.ReadObject()
|
field := iter.ReadObject()
|
||||||
should.Equal("", field)
|
should.Equal("", field)
|
||||||
iter = ParseString(`{}`)
|
iter = ParseString(ConfigDefault, `{}`)
|
||||||
iter.ReadObjectCB(func(iter *Iterator, field string) bool {
|
iter.ReadObjectCB(func(iter *Iterator, field string) bool {
|
||||||
should.FailNow("should not call")
|
should.FailNow("should not call")
|
||||||
return true
|
return true
|
||||||
@ -21,14 +21,14 @@ func Test_empty_object(t *testing.T) {
|
|||||||
|
|
||||||
func Test_one_field(t *testing.T) {
|
func Test_one_field(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(`{"a": "b"}`)
|
iter := ParseString(ConfigDefault, `{"a": "b"}`)
|
||||||
field := iter.ReadObject()
|
field := iter.ReadObject()
|
||||||
should.Equal("a", field)
|
should.Equal("a", field)
|
||||||
value := iter.ReadString()
|
value := iter.ReadString()
|
||||||
should.Equal("b", value)
|
should.Equal("b", value)
|
||||||
field = iter.ReadObject()
|
field = iter.ReadObject()
|
||||||
should.Equal("", field)
|
should.Equal("", field)
|
||||||
iter = ParseString(`{"a": "b"}`)
|
iter = ParseString(ConfigDefault, `{"a": "b"}`)
|
||||||
should.True(iter.ReadObjectCB(func(iter *Iterator, field string) bool {
|
should.True(iter.ReadObjectCB(func(iter *Iterator, field string) bool {
|
||||||
should.Equal("a", field)
|
should.Equal("a", field)
|
||||||
return true
|
return true
|
||||||
@ -37,7 +37,7 @@ func Test_one_field(t *testing.T) {
|
|||||||
|
|
||||||
func Test_two_field(t *testing.T) {
|
func Test_two_field(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(`{ "a": "b" , "c": "d" }`)
|
iter := ParseString(ConfigDefault, `{ "a": "b" , "c": "d" }`)
|
||||||
field := iter.ReadObject()
|
field := iter.ReadObject()
|
||||||
should.Equal("a", field)
|
should.Equal("a", field)
|
||||||
value := iter.ReadString()
|
value := iter.ReadString()
|
||||||
@ -48,7 +48,7 @@ func Test_two_field(t *testing.T) {
|
|||||||
should.Equal("d", value)
|
should.Equal("d", value)
|
||||||
field = iter.ReadObject()
|
field = iter.ReadObject()
|
||||||
should.Equal("", field)
|
should.Equal("", field)
|
||||||
iter = ParseString(`{"field1": "1", "field2": 2}`)
|
iter = ParseString(ConfigDefault, `{"field1": "1", "field2": 2}`)
|
||||||
for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
|
for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
|
||||||
switch field {
|
switch field {
|
||||||
case "field1":
|
case "field1":
|
||||||
@ -56,144 +56,11 @@ func Test_two_field(t *testing.T) {
|
|||||||
case "field2":
|
case "field2":
|
||||||
iter.ReadInt64()
|
iter.ReadInt64()
|
||||||
default:
|
default:
|
||||||
iter.reportError("bind object", "unexpected field")
|
iter.ReportError("bind object", "unexpected field")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_read_object_as_any(t *testing.T) {
|
|
||||||
should := require.New(t)
|
|
||||||
any, err := UnmarshalAnyFromString(`{"a":"b","c":"d"}`)
|
|
||||||
should.Nil(err)
|
|
||||||
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, err = UnmarshalAnyFromString(`{"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())
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_object_any_lazy_iterator(t *testing.T) {
|
|
||||||
should := require.New(t)
|
|
||||||
any, err := UnmarshalAnyFromString(`{"a":"b","c":"d"}`)
|
|
||||||
should.Nil(err)
|
|
||||||
// iterator parse
|
|
||||||
vals := map[string]string{}
|
|
||||||
var k string
|
|
||||||
var v Any
|
|
||||||
next, hasNext := any.IterateObject()
|
|
||||||
should.True(hasNext)
|
|
||||||
|
|
||||||
k, v, hasNext = next()
|
|
||||||
should.True(hasNext)
|
|
||||||
vals[k] = v.ToString()
|
|
||||||
|
|
||||||
// trigger full parse
|
|
||||||
should.Equal(2, len(any.Keys()))
|
|
||||||
|
|
||||||
k, v, hasNext = next()
|
|
||||||
should.False(hasNext)
|
|
||||||
vals[k] = v.ToString()
|
|
||||||
|
|
||||||
should.Equal(map[string]string{"a":"b", "c":"d"}, vals)
|
|
||||||
vals = map[string]string{}
|
|
||||||
for next, hasNext := any.IterateObject(); hasNext; k, v, hasNext = next() {
|
|
||||||
vals[k] = v.ToString()
|
|
||||||
}
|
|
||||||
should.Equal(map[string]string{"a":"b", "c":"d"}, vals)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_object_any_with_two_lazy_iterators(t *testing.T) {
|
|
||||||
should := require.New(t)
|
|
||||||
any, err := UnmarshalAnyFromString(`{"a":"b","c":"d","e":"f"}`)
|
|
||||||
should.Nil(err)
|
|
||||||
var k string
|
|
||||||
var v Any
|
|
||||||
next1, hasNext1 := any.IterateObject()
|
|
||||||
next2, hasNext2 := any.IterateObject()
|
|
||||||
should.True(hasNext1)
|
|
||||||
k, v, hasNext1 = next1()
|
|
||||||
should.True(hasNext1)
|
|
||||||
should.Equal("a", k)
|
|
||||||
should.Equal("b", v.ToString())
|
|
||||||
|
|
||||||
should.True(hasNext2)
|
|
||||||
k, v, hasNext2 = next2()
|
|
||||||
should.True(hasNext2)
|
|
||||||
should.Equal("a", k)
|
|
||||||
should.Equal("b", v.ToString())
|
|
||||||
|
|
||||||
k, v, hasNext1 = next1()
|
|
||||||
should.True(hasNext1)
|
|
||||||
should.Equal("c", k)
|
|
||||||
should.Equal("d", v.ToString())
|
|
||||||
|
|
||||||
k, v, hasNext2 = next2()
|
|
||||||
should.True(hasNext2)
|
|
||||||
should.Equal("c", k)
|
|
||||||
should.Equal("d", v.ToString())
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_object_lazy_any_get(t *testing.T) {
|
|
||||||
should := require.New(t)
|
|
||||||
any, err := UnmarshalAnyFromString(`{"a":{"b":{"c":"d"}}}`)
|
|
||||||
should.Nil(err)
|
|
||||||
should.Equal("d", any.Get("a", "b", "c").ToString())
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_object_lazy_any_get_all(t *testing.T) {
|
|
||||||
should := require.New(t)
|
|
||||||
any, err := UnmarshalAnyFromString(`{"a":[0],"b":[1]}`)
|
|
||||||
should.Nil(err)
|
|
||||||
should.Equal(`{"a":0,"b":1}`, any.Get('*', 0).ToString())
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_object_lazy_any_get_invalid(t *testing.T) {
|
|
||||||
should := require.New(t)
|
|
||||||
any, err := UnmarshalAnyFromString(`{}`)
|
|
||||||
should.Nil(err)
|
|
||||||
should.Equal(Invalid, any.Get("a", "b", "c").ValueType())
|
|
||||||
should.Equal(Invalid, any.Get(1).ValueType())
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_object_lazy_any_set(t *testing.T) {
|
|
||||||
should := require.New(t)
|
|
||||||
any, err := UnmarshalAnyFromString(`{"a":{"b":{"c":"d"}}}`)
|
|
||||||
should.Nil(err)
|
|
||||||
any.GetObject()["a"] = WrapInt64(1)
|
|
||||||
str, err := MarshalToString(any)
|
|
||||||
should.Nil(err)
|
|
||||||
should.Equal(`{"a":1}`, str)
|
|
||||||
}
|
|
||||||
|
|
||||||
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())
|
|
||||||
any = Wrap(TestObject{"hello", "world"})
|
|
||||||
vals := map[string]string{}
|
|
||||||
var k string
|
|
||||||
var v Any
|
|
||||||
for next, hasNext := any.IterateObject(); hasNext; {
|
|
||||||
k, v, hasNext = next()
|
|
||||||
if v.ValueType() == String {
|
|
||||||
vals[k] = v.ToString()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
should.Equal(map[string]string{"Field1":"hello"}, vals)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_object_wrapper_any_get_all(t *testing.T) {
|
func Test_object_wrapper_any_get_all(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
type TestObject struct {
|
type TestObject struct {
|
||||||
@ -201,14 +68,13 @@ func Test_object_wrapper_any_get_all(t *testing.T) {
|
|||||||
Field2 []int
|
Field2 []int
|
||||||
}
|
}
|
||||||
any := Wrap(TestObject{[]int{1, 2}, []int{3, 4}})
|
any := Wrap(TestObject{[]int{1, 2}, []int{3, 4}})
|
||||||
should.Equal(`{"Field2":3,"Field1":1}`, any.Get('*', 0).ToString())
|
should.Contains(any.Get('*', 0).ToString(), `"Field2":3`)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_write_object(t *testing.T) {
|
func Test_write_object(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(Config{IndentionStep: 2}.Froze(), buf, 4096)
|
||||||
stream.IndentionStep = 2
|
|
||||||
stream.WriteObjectStart()
|
stream.WriteObjectStart()
|
||||||
stream.WriteObjectField("hello")
|
stream.WriteObjectField("hello")
|
||||||
stream.WriteInt(1)
|
stream.WriteInt(1)
|
||||||
@ -218,37 +84,354 @@ func Test_write_object(t *testing.T) {
|
|||||||
stream.WriteObjectEnd()
|
stream.WriteObjectEnd()
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
should.Equal("{\n \"hello\":1,\n \"world\":2\n}", buf.String())
|
should.Equal("{\n \"hello\": 1,\n \"world\": 2\n}", buf.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
func Benchmark_jsoniter_object(b *testing.B) {
|
func Test_decode_one_field_struct(t *testing.T) {
|
||||||
type TestObj struct {
|
should := require.New(t)
|
||||||
|
type TestObject struct {
|
||||||
Field1 string
|
Field1 string
|
||||||
Field2 uint64
|
|
||||||
}
|
|
||||||
for n := 0; n < b.N; n++ {
|
|
||||||
iter := ParseString(`{"field1": "1", "field2": 2}`)
|
|
||||||
obj := TestObj{}
|
|
||||||
for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
|
|
||||||
switch field {
|
|
||||||
case "field1":
|
|
||||||
obj.Field1 = iter.ReadString()
|
|
||||||
case "field2":
|
|
||||||
obj.Field2 = iter.ReadUint64()
|
|
||||||
default:
|
|
||||||
iter.reportError("bind object", "unexpected field")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
obj := TestObject{}
|
||||||
|
should.Nil(UnmarshalFromString(`{}`, &obj))
|
||||||
|
should.Equal("", obj.Field1)
|
||||||
|
should.Nil(UnmarshalFromString(`{"field1": "hello"}`, &obj))
|
||||||
|
should.Equal("hello", obj.Field1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Benchmark_json_object(b *testing.B) {
|
func Test_decode_two_fields_struct(t *testing.T) {
|
||||||
type TestObj struct {
|
should := require.New(t)
|
||||||
|
type TestObject struct {
|
||||||
Field1 string
|
Field1 string
|
||||||
Field2 uint64
|
Field2 string
|
||||||
}
|
}
|
||||||
for n := 0; n < b.N; n++ {
|
obj := TestObject{}
|
||||||
result := TestObj{}
|
should.Nil(UnmarshalFromString(`{}`, &obj))
|
||||||
json.Unmarshal([]byte(`{"field1": "1", "field2": 2}`), &result)
|
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,8 +1,8 @@
|
|||||||
package jsoniter
|
package jsoniter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
|
||||||
"github.com/json-iterator/go/require"
|
"github.com/json-iterator/go/require"
|
||||||
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_encode_optional_int_pointer(t *testing.T) {
|
func Test_encode_optional_int_pointer(t *testing.T) {
|
||||||
@ -21,25 +21,26 @@ func Test_encode_optional_int_pointer(t *testing.T) {
|
|||||||
func Test_decode_struct_with_optional_field(t *testing.T) {
|
func Test_decode_struct_with_optional_field(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
type TestObject struct {
|
type TestObject struct {
|
||||||
field1 *string
|
Field1 *string
|
||||||
field2 *string
|
Field2 *string
|
||||||
}
|
}
|
||||||
obj := TestObject{}
|
obj := TestObject{}
|
||||||
UnmarshalFromString(`{"field1": null, "field2": "world"}`, &obj)
|
UnmarshalFromString(`{"field1": null, "field2": "world"}`, &obj)
|
||||||
should.Nil(obj.field1)
|
should.Nil(obj.Field1)
|
||||||
should.Equal("world", *obj.field2)
|
should.Equal("world", *obj.Field2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_encode_struct_with_optional_field(t *testing.T) {
|
func Test_encode_struct_with_optional_field(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
type TestObject struct {
|
type TestObject struct {
|
||||||
field1 *string
|
Field1 *string
|
||||||
field2 *string
|
Field2 *string
|
||||||
}
|
}
|
||||||
obj := TestObject{}
|
obj := TestObject{}
|
||||||
world := "world"
|
world := "world"
|
||||||
obj.field2 = &world
|
obj.Field2 = &world
|
||||||
str, err := MarshalToString(obj)
|
str, err := MarshalToString(obj)
|
||||||
should.Nil(err)
|
should.Nil(err)
|
||||||
should.Equal(`{"field1":null,"field2":"world"}`, str)
|
should.Contains(str, `"Field1":null`)
|
||||||
|
should.Contains(str, `"Field2":"world"`)
|
||||||
}
|
}
|
74
jsoniter_raw_message_test.go
Normal file
74
jsoniter_raw_message_test.go
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
package jsoniter
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"github.com/json-iterator/go/require"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_json_RawMessage(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
var data json.RawMessage
|
||||||
|
should.Nil(Unmarshal([]byte(`[1,2,3]`), &data))
|
||||||
|
should.Equal(`[1,2,3]`, string(data))
|
||||||
|
str, err := MarshalToString(data)
|
||||||
|
should.Nil(err)
|
||||||
|
should.Equal(`[1,2,3]`, str)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_jsoniter_RawMessage(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
var data RawMessage
|
||||||
|
should.Nil(Unmarshal([]byte(`[1,2,3]`), &data))
|
||||||
|
should.Equal(`[1,2,3]`, string(data))
|
||||||
|
str, err := MarshalToString(data)
|
||||||
|
should.Nil(err)
|
||||||
|
should.Equal(`[1,2,3]`, str)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_json_RawMessage_in_struct(t *testing.T) {
|
||||||
|
type TestObject struct {
|
||||||
|
Field1 string
|
||||||
|
Field2 json.RawMessage
|
||||||
|
}
|
||||||
|
should := require.New(t)
|
||||||
|
var data TestObject
|
||||||
|
should.Nil(Unmarshal([]byte(`{"field1": "hello", "field2": [1,2,3]}`), &data))
|
||||||
|
should.Equal(` [1,2,3]`, string(data.Field2))
|
||||||
|
should.Equal(`hello`, data.Field1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_decode_map_of_raw_message(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
type RawMap map[string]*json.RawMessage
|
||||||
|
b := []byte("{\"test\":[{\"key\":\"value\"}]}")
|
||||||
|
var rawMap RawMap
|
||||||
|
should.Nil(Unmarshal(b, &rawMap))
|
||||||
|
should.Equal(`[{"key":"value"}]`, string(*rawMap["test"]))
|
||||||
|
type Inner struct {
|
||||||
|
Key string `json:"key"`
|
||||||
|
}
|
||||||
|
var inner []Inner
|
||||||
|
Unmarshal(*rawMap["test"], &inner)
|
||||||
|
should.Equal("value", inner[0].Key)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_encode_map_of_raw_message(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
type RawMap map[string]*json.RawMessage
|
||||||
|
value := json.RawMessage("[]")
|
||||||
|
rawMap := RawMap{"hello": &value}
|
||||||
|
output, err := MarshalToString(rawMap)
|
||||||
|
should.Nil(err)
|
||||||
|
should.Equal(`{"hello":[]}`, output)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_encode_map_of_jsoniter_raw_message(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
type RawMap map[string]*RawMessage
|
||||||
|
value := RawMessage("[]")
|
||||||
|
rawMap := RawMap{"hello": &value}
|
||||||
|
output, err := MarshalToString(rawMap)
|
||||||
|
should.Nil(err)
|
||||||
|
should.Equal(`{"hello":[]}`, output)
|
||||||
|
}
|
@ -1,12 +1,12 @@
|
|||||||
package jsoniter
|
package jsoniter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_reflect_str(t *testing.T) {
|
func Test_reflect_str(t *testing.T) {
|
||||||
iter := ParseString(`"hello"`)
|
iter := ParseString(ConfigDefault, `"hello"`)
|
||||||
str := ""
|
str := ""
|
||||||
iter.ReadVal(&str)
|
iter.ReadVal(&str)
|
||||||
if str != "hello" {
|
if str != "hello" {
|
||||||
@ -16,7 +16,7 @@ func Test_reflect_str(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_reflect_ptr_str(t *testing.T) {
|
func Test_reflect_ptr_str(t *testing.T) {
|
||||||
iter := ParseString(`"hello"`)
|
iter := ParseString(ConfigDefault, `"hello"`)
|
||||||
var str *string
|
var str *string
|
||||||
iter.ReadVal(&str)
|
iter.ReadVal(&str)
|
||||||
if *str != "hello" {
|
if *str != "hello" {
|
||||||
@ -25,7 +25,7 @@ func Test_reflect_ptr_str(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_reflect_int(t *testing.T) {
|
func Test_reflect_int(t *testing.T) {
|
||||||
iter := ParseString(`123`)
|
iter := ParseString(ConfigDefault, `123`)
|
||||||
val := int(0)
|
val := int(0)
|
||||||
iter.ReadVal(&val)
|
iter.ReadVal(&val)
|
||||||
if val != 123 {
|
if val != 123 {
|
||||||
@ -34,7 +34,7 @@ func Test_reflect_int(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_reflect_int8(t *testing.T) {
|
func Test_reflect_int8(t *testing.T) {
|
||||||
iter := ParseString(`123`)
|
iter := ParseString(ConfigDefault, `123`)
|
||||||
val := int8(0)
|
val := int8(0)
|
||||||
iter.ReadVal(&val)
|
iter.ReadVal(&val)
|
||||||
if val != 123 {
|
if val != 123 {
|
||||||
@ -43,7 +43,7 @@ func Test_reflect_int8(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_reflect_int16(t *testing.T) {
|
func Test_reflect_int16(t *testing.T) {
|
||||||
iter := ParseString(`123`)
|
iter := ParseString(ConfigDefault, `123`)
|
||||||
val := int16(0)
|
val := int16(0)
|
||||||
iter.ReadVal(&val)
|
iter.ReadVal(&val)
|
||||||
if val != 123 {
|
if val != 123 {
|
||||||
@ -52,7 +52,7 @@ func Test_reflect_int16(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_reflect_int32(t *testing.T) {
|
func Test_reflect_int32(t *testing.T) {
|
||||||
iter := ParseString(`123`)
|
iter := ParseString(ConfigDefault, `123`)
|
||||||
val := int32(0)
|
val := int32(0)
|
||||||
iter.ReadVal(&val)
|
iter.ReadVal(&val)
|
||||||
if val != 123 {
|
if val != 123 {
|
||||||
@ -61,7 +61,7 @@ func Test_reflect_int32(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_reflect_int64(t *testing.T) {
|
func Test_reflect_int64(t *testing.T) {
|
||||||
iter := ParseString(`123`)
|
iter := ParseString(ConfigDefault, `123`)
|
||||||
val := int64(0)
|
val := int64(0)
|
||||||
iter.ReadVal(&val)
|
iter.ReadVal(&val)
|
||||||
if val != 123 {
|
if val != 123 {
|
||||||
@ -70,7 +70,7 @@ func Test_reflect_int64(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_reflect_uint(t *testing.T) {
|
func Test_reflect_uint(t *testing.T) {
|
||||||
iter := ParseString(`123`)
|
iter := ParseString(ConfigDefault, `123`)
|
||||||
val := uint(0)
|
val := uint(0)
|
||||||
iter.ReadVal(&val)
|
iter.ReadVal(&val)
|
||||||
if val != 123 {
|
if val != 123 {
|
||||||
@ -79,7 +79,7 @@ func Test_reflect_uint(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_reflect_uint8(t *testing.T) {
|
func Test_reflect_uint8(t *testing.T) {
|
||||||
iter := ParseString(`123`)
|
iter := ParseString(ConfigDefault, `123`)
|
||||||
val := uint8(0)
|
val := uint8(0)
|
||||||
iter.ReadVal(&val)
|
iter.ReadVal(&val)
|
||||||
if val != 123 {
|
if val != 123 {
|
||||||
@ -88,7 +88,7 @@ func Test_reflect_uint8(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_reflect_uint16(t *testing.T) {
|
func Test_reflect_uint16(t *testing.T) {
|
||||||
iter := ParseString(`123`)
|
iter := ParseString(ConfigDefault, `123`)
|
||||||
val := uint16(0)
|
val := uint16(0)
|
||||||
iter.ReadVal(&val)
|
iter.ReadVal(&val)
|
||||||
if val != 123 {
|
if val != 123 {
|
||||||
@ -97,7 +97,7 @@ func Test_reflect_uint16(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_reflect_uint32(t *testing.T) {
|
func Test_reflect_uint32(t *testing.T) {
|
||||||
iter := ParseString(`123`)
|
iter := ParseString(ConfigDefault, `123`)
|
||||||
val := uint32(0)
|
val := uint32(0)
|
||||||
iter.ReadVal(&val)
|
iter.ReadVal(&val)
|
||||||
if val != 123 {
|
if val != 123 {
|
||||||
@ -106,7 +106,7 @@ func Test_reflect_uint32(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_reflect_uint64(t *testing.T) {
|
func Test_reflect_uint64(t *testing.T) {
|
||||||
iter := ParseString(`123`)
|
iter := ParseString(ConfigDefault, `123`)
|
||||||
val := uint64(0)
|
val := uint64(0)
|
||||||
iter.ReadVal(&val)
|
iter.ReadVal(&val)
|
||||||
if val != 123 {
|
if val != 123 {
|
||||||
@ -115,7 +115,7 @@ func Test_reflect_uint64(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_reflect_byte(t *testing.T) {
|
func Test_reflect_byte(t *testing.T) {
|
||||||
iter := ParseString(`123`)
|
iter := ParseString(ConfigDefault, `123`)
|
||||||
val := byte(0)
|
val := byte(0)
|
||||||
iter.ReadVal(&val)
|
iter.ReadVal(&val)
|
||||||
if val != 123 {
|
if val != 123 {
|
||||||
@ -124,7 +124,7 @@ func Test_reflect_byte(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_reflect_float32(t *testing.T) {
|
func Test_reflect_float32(t *testing.T) {
|
||||||
iter := ParseString(`1.23`)
|
iter := ParseString(ConfigDefault, `1.23`)
|
||||||
val := float32(0)
|
val := float32(0)
|
||||||
iter.ReadVal(&val)
|
iter.ReadVal(&val)
|
||||||
if val != 1.23 {
|
if val != 1.23 {
|
||||||
@ -134,7 +134,7 @@ func Test_reflect_float32(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_reflect_float64(t *testing.T) {
|
func Test_reflect_float64(t *testing.T) {
|
||||||
iter := ParseString(`1.23`)
|
iter := ParseString(ConfigDefault, `1.23`)
|
||||||
val := float64(0)
|
val := float64(0)
|
||||||
iter.ReadVal(&val)
|
iter.ReadVal(&val)
|
||||||
if val != 1.23 {
|
if val != 1.23 {
|
||||||
@ -144,7 +144,7 @@ func Test_reflect_float64(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_reflect_bool(t *testing.T) {
|
func Test_reflect_bool(t *testing.T) {
|
||||||
iter := ParseString(`true`)
|
iter := ParseString(ConfigDefault, `true`)
|
||||||
val := false
|
val := false
|
||||||
iter.ReadVal(&val)
|
iter.ReadVal(&val)
|
||||||
if val != true {
|
if val != true {
|
||||||
|
@ -1,146 +0,0 @@
|
|||||||
package jsoniter
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
"github.com/json-iterator/go/require"
|
|
||||||
)
|
|
||||||
|
|
||||||
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_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)
|
|
||||||
}
|
|
@ -1,115 +0,0 @@
|
|||||||
package jsoniter
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"testing"
|
|
||||||
"unsafe"
|
|
||||||
"github.com/json-iterator/go/require"
|
|
||||||
)
|
|
||||||
|
|
||||||
func Test_decode_slice(t *testing.T) {
|
|
||||||
should := require.New(t)
|
|
||||||
slice := make([]string, 0, 5)
|
|
||||||
UnmarshalFromString(`["hello", "world"]`, &slice)
|
|
||||||
should.Equal([]string{"hello", "world"}, slice)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_decode_large_slice(t *testing.T) {
|
|
||||||
should := require.New(t)
|
|
||||||
slice := make([]int, 0, 1)
|
|
||||||
UnmarshalFromString(`[1,2,3,4,5,6,7,8,9]`, &slice)
|
|
||||||
should.Equal([]int{1, 2, 3, 4, 5, 6, 7, 8, 9}, slice)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_decode_nested(t *testing.T) {
|
|
||||||
type StructOfString struct {
|
|
||||||
field1 string
|
|
||||||
field2 string
|
|
||||||
}
|
|
||||||
iter := ParseString(`[{"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])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_decode_base64(t *testing.T) {
|
|
||||||
iter := ParseString(`"YWJj"`)
|
|
||||||
val := []byte{}
|
|
||||||
RegisterTypeDecoder("[]uint8", func(ptr unsafe.Pointer, iter *Iterator) {
|
|
||||||
*((*[]byte)(ptr)) = iter.ReadBase64()
|
|
||||||
})
|
|
||||||
defer CleanDecoders()
|
|
||||||
iter.ReadVal(&val)
|
|
||||||
if "abc" != string(val) {
|
|
||||||
t.Fatal(string(val))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type StructOfTagOne struct {
|
|
||||||
Field1 string `json:"field1"`
|
|
||||||
Field2 string `json:"field2"`
|
|
||||||
Field3 int `json:"field3,string"`
|
|
||||||
Field4 int `json:"field4,string"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func Benchmark_jsoniter_reflect(b *testing.B) {
|
|
||||||
b.ReportAllocs()
|
|
||||||
iter := NewIterator()
|
|
||||||
Struct := &StructOfTagOne{}
|
|
||||||
//var Struct *StructOfTagOne
|
|
||||||
input := []byte(`{"field3": "100", "field4": "100"}`)
|
|
||||||
//input := []byte(`null`)
|
|
||||||
for n := 0; n < b.N; n++ {
|
|
||||||
iter.ResetBytes(input)
|
|
||||||
iter.ReadVal(&Struct)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func Benchmark_jsoniter_direct(b *testing.B) {
|
|
||||||
b.ReportAllocs()
|
|
||||||
for n := 0; n < b.N; n++ {
|
|
||||||
//iter := ParseString(`{"field1": "hello", "field2": "world"}`)
|
|
||||||
//struct_ := StructOfString{}
|
|
||||||
//for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
|
|
||||||
// switch field {
|
|
||||||
// case "field1":
|
|
||||||
// struct_.Field1 = iter.ReadString()
|
|
||||||
// case "field2":
|
|
||||||
// struct_.Field2 = iter.ReadString()
|
|
||||||
// default:
|
|
||||||
// iter.Skip()
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
iter := ParseString(`["hello", "world"]`)
|
|
||||||
array := make([]string, 0, 2)
|
|
||||||
for iter.ReadArray() {
|
|
||||||
array = append(array, iter.ReadString())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func Benchmark_json_reflect(b *testing.B) {
|
|
||||||
b.ReportAllocs()
|
|
||||||
for n := 0; n < b.N; n++ {
|
|
||||||
Struct := StructOfTagOne{}
|
|
||||||
json.Unmarshal([]byte(`{"field3": "100"}`), &Struct)
|
|
||||||
//array := make([]string, 0, 2)
|
|
||||||
//json.Unmarshal([]byte(`["hello", "world"]`), &array)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,12 +1,14 @@
|
|||||||
package jsoniter
|
package jsoniter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"github.com/json-iterator/go/require"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_skip_number(t *testing.T) {
|
func Test_skip_number(t *testing.T) {
|
||||||
iter := ParseString(`[-0.12, "b"]`)
|
iter := ParseString(ConfigDefault, `[-0.12, "b"]`)
|
||||||
iter.ReadArray()
|
iter.ReadArray()
|
||||||
iter.Skip()
|
iter.Skip()
|
||||||
iter.ReadArray()
|
iter.ReadArray()
|
||||||
@ -16,7 +18,7 @@ func Test_skip_number(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_skip_null(t *testing.T) {
|
func Test_skip_null(t *testing.T) {
|
||||||
iter := ParseString(`[null , "b"]`)
|
iter := ParseString(ConfigDefault, `[null , "b"]`)
|
||||||
iter.ReadArray()
|
iter.ReadArray()
|
||||||
iter.Skip()
|
iter.Skip()
|
||||||
iter.ReadArray()
|
iter.ReadArray()
|
||||||
@ -26,7 +28,7 @@ func Test_skip_null(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_skip_true(t *testing.T) {
|
func Test_skip_true(t *testing.T) {
|
||||||
iter := ParseString(`[true , "b"]`)
|
iter := ParseString(ConfigDefault, `[true , "b"]`)
|
||||||
iter.ReadArray()
|
iter.ReadArray()
|
||||||
iter.Skip()
|
iter.Skip()
|
||||||
iter.ReadArray()
|
iter.ReadArray()
|
||||||
@ -36,7 +38,7 @@ func Test_skip_true(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_skip_false(t *testing.T) {
|
func Test_skip_false(t *testing.T) {
|
||||||
iter := ParseString(`[false , "b"]`)
|
iter := ParseString(ConfigDefault, `[false , "b"]`)
|
||||||
iter.ReadArray()
|
iter.ReadArray()
|
||||||
iter.Skip()
|
iter.Skip()
|
||||||
iter.ReadArray()
|
iter.ReadArray()
|
||||||
@ -46,7 +48,7 @@ func Test_skip_false(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_skip_array(t *testing.T) {
|
func Test_skip_array(t *testing.T) {
|
||||||
iter := ParseString(`[[1, [2, [3], 4]], "b"]`)
|
iter := ParseString(ConfigDefault, `[[1, [2, [3], 4]], "b"]`)
|
||||||
iter.ReadArray()
|
iter.ReadArray()
|
||||||
iter.Skip()
|
iter.Skip()
|
||||||
iter.ReadArray()
|
iter.ReadArray()
|
||||||
@ -56,7 +58,7 @@ func Test_skip_array(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_skip_empty_array(t *testing.T) {
|
func Test_skip_empty_array(t *testing.T) {
|
||||||
iter := ParseString(`[ [ ], "b"]`)
|
iter := ParseString(ConfigDefault, `[ [ ], "b"]`)
|
||||||
iter.ReadArray()
|
iter.ReadArray()
|
||||||
iter.Skip()
|
iter.Skip()
|
||||||
iter.ReadArray()
|
iter.ReadArray()
|
||||||
@ -66,7 +68,7 @@ func Test_skip_empty_array(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_skip_nested(t *testing.T) {
|
func Test_skip_nested(t *testing.T) {
|
||||||
iter := ParseString(`[ {"a" : [{"b": "c"}], "d": 102 }, "b"]`)
|
iter := ParseString(ConfigDefault, `[ {"a" : [{"b": "c"}], "d": 102 }, "b"]`)
|
||||||
iter.ReadArray()
|
iter.ReadArray()
|
||||||
iter.Skip()
|
iter.Skip()
|
||||||
iter.ReadArray()
|
iter.ReadArray()
|
||||||
@ -75,6 +77,22 @@ func Test_skip_nested(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_skip_and_return_bytes(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
iter := ParseString(ConfigDefault, `[ {"a" : [{"b": "c"}], "d": 102 }, "b"]`)
|
||||||
|
iter.ReadArray()
|
||||||
|
skipped := iter.SkipAndReturnBytes()
|
||||||
|
should.Equal(`{"a" : [{"b": "c"}], "d": 102 }`, string(skipped))
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_skip_and_return_bytes_with_reader(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
iter := Parse(ConfigDefault, bytes.NewBufferString(`[ {"a" : [{"b": "c"}], "d": 102 }, "b"]`), 4)
|
||||||
|
iter.ReadArray()
|
||||||
|
skipped := iter.SkipAndReturnBytes()
|
||||||
|
should.Equal(`{"a" : [{"b": "c"}], "d": 102 }`, string(skipped))
|
||||||
|
}
|
||||||
|
|
||||||
type TestResp struct {
|
type TestResp struct {
|
||||||
Code uint64
|
Code uint64
|
||||||
}
|
}
|
||||||
@ -106,7 +124,7 @@ func Benchmark_jsoniter_skip(b *testing.B) {
|
|||||||
}`)
|
}`)
|
||||||
for n := 0; n < b.N; n++ {
|
for n := 0; n < b.N; n++ {
|
||||||
result := TestResp{}
|
result := TestResp{}
|
||||||
iter := ParseBytes(input)
|
iter := ParseBytes(ConfigDefault, input)
|
||||||
for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
|
for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
|
||||||
switch field {
|
switch field {
|
||||||
case "code":
|
case "code":
|
||||||
|
53
jsoniter_stream_test.go
Normal file
53
jsoniter_stream_test.go
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
package jsoniter
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/json-iterator/go/require"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_writeByte_should_grow_buffer(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
stream := NewStream(ConfigDefault, nil, 1)
|
||||||
|
stream.writeByte('1')
|
||||||
|
should.Equal("1", string(stream.Buffer()))
|
||||||
|
should.Equal(1, len(stream.buf))
|
||||||
|
stream.writeByte('2')
|
||||||
|
should.Equal("12", string(stream.Buffer()))
|
||||||
|
should.Equal(2, len(stream.buf))
|
||||||
|
stream.writeThreeBytes('3', '4', '5')
|
||||||
|
should.Equal("12345", string(stream.Buffer()))
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_writeBytes_should_grow_buffer(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
stream := NewStream(ConfigDefault, nil, 1)
|
||||||
|
stream.Write([]byte{'1', '2'})
|
||||||
|
should.Equal("12", string(stream.Buffer()))
|
||||||
|
should.Equal(3, len(stream.buf))
|
||||||
|
stream.Write([]byte{'3', '4', '5', '6', '7'})
|
||||||
|
should.Equal("1234567", string(stream.Buffer()))
|
||||||
|
should.Equal(8, len(stream.buf))
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_writeIndention_should_grow_buffer(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
stream := NewStream(Config{IndentionStep: 2}.Froze(), nil, 1)
|
||||||
|
stream.WriteVal([]int{1, 2, 3})
|
||||||
|
should.Equal("[\n 1,\n 2,\n 3\n]", string(stream.Buffer()))
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_writeRaw_should_grow_buffer(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
stream := NewStream(ConfigDefault, nil, 1)
|
||||||
|
stream.WriteRaw("123")
|
||||||
|
should.Nil(stream.Error)
|
||||||
|
should.Equal("123", string(stream.Buffer()))
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_writeString_should_grow_buffer(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
stream := NewStream(ConfigDefault, nil, 0)
|
||||||
|
stream.WriteString("123")
|
||||||
|
should.Nil(stream.Error)
|
||||||
|
should.Equal(`"123"`, string(stream.Buffer()))
|
||||||
|
}
|
@ -1,38 +1,41 @@
|
|||||||
|
// +build go1.8
|
||||||
|
|
||||||
package jsoniter
|
package jsoniter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"testing"
|
|
||||||
"github.com/json-iterator/go/require"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/json-iterator/go/require"
|
||||||
|
"testing"
|
||||||
|
"unicode/utf8"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_read_normal_string(t *testing.T) {
|
func Test_read_normal_string(t *testing.T) {
|
||||||
cases := map[string]string{
|
cases := map[string]string{
|
||||||
`"0123456789012345678901234567890123456789"`: `0123456789012345678901234567890123456789`,
|
`"0123456789012345678901234567890123456789"`: `0123456789012345678901234567890123456789`,
|
||||||
`""`: ``,
|
`""`: ``,
|
||||||
`"hello"`: `hello`,
|
`"hello"`: `hello`,
|
||||||
}
|
}
|
||||||
for input, output := range cases {
|
for input, output := range cases {
|
||||||
t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(input)
|
iter := ParseString(ConfigDefault, input)
|
||||||
should.Equal(output, iter.ReadString())
|
should.Equal(output, iter.ReadString())
|
||||||
})
|
})
|
||||||
t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := Parse(bytes.NewBufferString(input), 2)
|
iter := Parse(ConfigDefault, bytes.NewBufferString(input), 2)
|
||||||
should.Equal(output, iter.ReadString())
|
should.Equal(output, iter.ReadString())
|
||||||
})
|
})
|
||||||
t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(input)
|
iter := ParseString(ConfigDefault, input)
|
||||||
should.Equal(output, string(iter.ReadStringAsSlice()))
|
should.Equal(output, string(iter.ReadStringAsSlice()))
|
||||||
})
|
})
|
||||||
t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := Parse(bytes.NewBufferString(input), 2)
|
iter := Parse(ConfigDefault, bytes.NewBufferString(input), 2)
|
||||||
should.Equal(output, string(iter.ReadStringAsSlice()))
|
should.Equal(output, string(iter.ReadStringAsSlice()))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -40,20 +43,20 @@ func Test_read_normal_string(t *testing.T) {
|
|||||||
|
|
||||||
func Test_read_exotic_string(t *testing.T) {
|
func Test_read_exotic_string(t *testing.T) {
|
||||||
cases := map[string]string{
|
cases := map[string]string{
|
||||||
`"hel\"lo"`: `hel"lo`,
|
`"hel\"lo"`: `hel"lo`,
|
||||||
`"hel\nlo"`: "hel\nlo",
|
`"hel\nlo"`: "hel\nlo",
|
||||||
`"\u4e2d\u6587"`: "中文",
|
`"\u4e2d\u6587"`: "中文",
|
||||||
`"\ud83d\udc4a"`: "\xf0\x9f\x91\x8a", // surrogate
|
`"\ud83d\udc4a"`: "\xf0\x9f\x91\x8a", // surrogate
|
||||||
}
|
}
|
||||||
for input, output := range cases {
|
for input, output := range cases {
|
||||||
t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(input)
|
iter := ParseString(ConfigDefault, input)
|
||||||
should.Equal(output, iter.ReadString())
|
should.Equal(output, iter.ReadString())
|
||||||
})
|
})
|
||||||
t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := Parse(bytes.NewBufferString(input), 2)
|
iter := Parse(ConfigDefault, bytes.NewBufferString(input), 2)
|
||||||
should.Equal(output, iter.ReadString())
|
should.Equal(output, iter.ReadString())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -61,30 +64,10 @@ func Test_read_exotic_string(t *testing.T) {
|
|||||||
|
|
||||||
func Test_read_string_as_interface(t *testing.T) {
|
func Test_read_string_as_interface(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(`"hello"`)
|
iter := ParseString(ConfigDefault, `"hello"`)
|
||||||
should.Equal("hello", iter.Read())
|
should.Equal("hello", iter.Read())
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_read_string_as_any(t *testing.T) {
|
|
||||||
should := require.New(t)
|
|
||||||
any, err := UnmarshalAnyFromString(`"hello"`)
|
|
||||||
should.Nil(err)
|
|
||||||
should.Equal("hello", any.ToString())
|
|
||||||
should.True(any.ToBool())
|
|
||||||
any, err = UnmarshalAnyFromString(`" "`)
|
|
||||||
should.False(any.ToBool())
|
|
||||||
any, err = UnmarshalAnyFromString(`"false"`)
|
|
||||||
should.False(any.ToBool())
|
|
||||||
any, err = UnmarshalAnyFromString(`"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())
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_write_string(t *testing.T) {
|
func Test_write_string(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
str, err := MarshalToString("hello")
|
str, err := MarshalToString("hello")
|
||||||
@ -98,22 +81,86 @@ func Test_write_string(t *testing.T) {
|
|||||||
func Test_write_val_string(t *testing.T) {
|
func Test_write_val_string(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(ConfigDefault, buf, 4096)
|
||||||
stream.WriteVal("hello")
|
stream.WriteVal("hello")
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
should.Equal(`"hello"`, buf.String())
|
should.Equal(`"hello"`, buf.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_decode_slash(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
var obj interface{}
|
||||||
|
should.NotNil(json.Unmarshal([]byte("\\"), &obj))
|
||||||
|
should.NotNil(UnmarshalFromString("\\", &obj))
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_html_escape(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
output, err := json.Marshal(`>`)
|
||||||
|
should.Nil(err)
|
||||||
|
should.Equal(`"\u003e"`, string(output))
|
||||||
|
output, err = ConfigCompatibleWithStandardLibrary.Marshal(`>`)
|
||||||
|
should.Nil(err)
|
||||||
|
should.Equal(`"\u003e"`, string(output))
|
||||||
|
type MyString string
|
||||||
|
output, err = ConfigCompatibleWithStandardLibrary.Marshal(MyString(`>`))
|
||||||
|
should.Nil(err)
|
||||||
|
should.Equal(`"\u003e"`, string(output))
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_string_encode_with_std(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
for i := 0; i < utf8.RuneSelf; i++ {
|
||||||
|
input := string([]byte{byte(i)})
|
||||||
|
stdOutputBytes, err := json.Marshal(input)
|
||||||
|
should.Nil(err)
|
||||||
|
stdOutput := string(stdOutputBytes)
|
||||||
|
jsoniterOutputBytes, err := ConfigCompatibleWithStandardLibrary.Marshal(input)
|
||||||
|
should.Nil(err)
|
||||||
|
jsoniterOutput := string(jsoniterOutputBytes)
|
||||||
|
should.Equal(stdOutput, jsoniterOutput)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_unicode(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
output, _ := MarshalToString(map[string]interface{}{"a": "数字山谷"})
|
||||||
|
should.Equal(`{"a":"数字山谷"}`, output)
|
||||||
|
output, _ = Config{EscapeHtml: false}.Froze().MarshalToString(map[string]interface{}{"a": "数字山谷"})
|
||||||
|
should.Equal(`{"a":"数字山谷"}`, output)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_unicode_and_escape(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
output, err := MarshalToString(`"数字山谷"`)
|
||||||
|
should.Nil(err)
|
||||||
|
should.Equal(`"\"数字山谷\""`, output)
|
||||||
|
output, err = ConfigFastest.MarshalToString(`"数字山谷"`)
|
||||||
|
should.Nil(err)
|
||||||
|
should.Equal(`"\"数字山谷\""`, output)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_unsafe_unicode(t *testing.T) {
|
||||||
|
ConfigDefault.cleanEncoders()
|
||||||
|
should := require.New(t)
|
||||||
|
output, err := ConfigDefault.MarshalToString("he\u2029\u2028he")
|
||||||
|
should.Nil(err)
|
||||||
|
should.Equal(`"he\u2029\u2028he"`, output)
|
||||||
|
output, err = ConfigFastest.MarshalToString("he\u2029\u2028he")
|
||||||
|
should.Nil(err)
|
||||||
|
should.Equal("\"he\u2029\u2028he\"", output)
|
||||||
|
}
|
||||||
|
|
||||||
func Benchmark_jsoniter_unicode(b *testing.B) {
|
func Benchmark_jsoniter_unicode(b *testing.B) {
|
||||||
for n := 0; n < b.N; n++ {
|
for n := 0; n < b.N; n++ {
|
||||||
iter := ParseString(`"\ud83d\udc4a"`)
|
iter := ParseString(ConfigDefault, `"\ud83d\udc4a"`)
|
||||||
iter.ReadString()
|
iter.ReadString()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Benchmark_jsoniter_ascii(b *testing.B) {
|
func Benchmark_jsoniter_ascii(b *testing.B) {
|
||||||
iter := NewIterator()
|
iter := NewIterator(ConfigDefault)
|
||||||
input := []byte(`"hello, world! hello, world!"`)
|
input := []byte(`"hello, world! hello, world!"`)
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
for n := 0; n < b.N; n++ {
|
for n := 0; n < b.N; n++ {
|
||||||
@ -123,7 +170,7 @@ func Benchmark_jsoniter_ascii(b *testing.B) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Benchmark_jsoniter_string_as_bytes(b *testing.B) {
|
func Benchmark_jsoniter_string_as_bytes(b *testing.B) {
|
||||||
iter := ParseString(`"hello, world!"`)
|
iter := ParseString(ConfigDefault, `"hello, world!"`)
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
for n := 0; n < b.N; n++ {
|
for n := 0; n < b.N; n++ {
|
||||||
iter.ResetBytes(iter.buf)
|
iter.ResetBytes(iter.buf)
|
||||||
|
152
output_tests/array/array/bool/json_test.go
Normal file
152
output_tests/array/array/bool/json_test.go
Normal file
@ -0,0 +1,152 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
fuzz "github.com/google/gofuzz"
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_Roundtrip(t *testing.T) {
|
||||||
|
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||||
|
for i := 0; i < 100; i++ {
|
||||||
|
var before T
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
|
||||||
|
jbStd, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||||
|
t.Fatal("stdlib marshal produced empty result and no error")
|
||||||
|
}
|
||||||
|
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||||
|
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||||
|
}
|
||||||
|
if string(jbStd) != string(jbIter) {
|
||||||
|
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||||
|
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||||
|
}
|
||||||
|
|
||||||
|
var afterStd T
|
||||||
|
err = json.Unmarshal(jbIter, &afterStd)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||||
|
err, indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
var afterIter T
|
||||||
|
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||||
|
err, indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||||
|
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||||
|
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const indentStr = "> "
|
||||||
|
|
||||||
|
func fingerprint(obj interface{}) string {
|
||||||
|
c := spew.ConfigState{
|
||||||
|
SortKeys: true,
|
||||||
|
SpewKeys: true,
|
||||||
|
}
|
||||||
|
return c.Sprintf("%v", obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func dump(obj interface{}) string {
|
||||||
|
cfg := spew.ConfigState{
|
||||||
|
Indent: indentStr,
|
||||||
|
}
|
||||||
|
return cfg.Sdump(obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func indent(src []byte, prefix string) string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
err := json.Indent(&buf, src, prefix, indentStr)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Sprintf("!!! %v", err)
|
||||||
|
}
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := fn(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = fn(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardMarshal(t *testing.B) {
|
||||||
|
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||||
|
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||||
|
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||||
|
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||||
|
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||||
|
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||||
|
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||||
|
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||||
|
}
|
3
output_tests/array/array/bool/types.go
Normal file
3
output_tests/array/array/bool/types.go
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
type T [4][4]bool
|
152
output_tests/array/array/byte/json_test.go
Normal file
152
output_tests/array/array/byte/json_test.go
Normal file
@ -0,0 +1,152 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
fuzz "github.com/google/gofuzz"
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_Roundtrip(t *testing.T) {
|
||||||
|
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||||
|
for i := 0; i < 100; i++ {
|
||||||
|
var before T
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
|
||||||
|
jbStd, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||||
|
t.Fatal("stdlib marshal produced empty result and no error")
|
||||||
|
}
|
||||||
|
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||||
|
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||||
|
}
|
||||||
|
if string(jbStd) != string(jbIter) {
|
||||||
|
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||||
|
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||||
|
}
|
||||||
|
|
||||||
|
var afterStd T
|
||||||
|
err = json.Unmarshal(jbIter, &afterStd)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||||
|
err, indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
var afterIter T
|
||||||
|
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||||
|
err, indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||||
|
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||||
|
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const indentStr = "> "
|
||||||
|
|
||||||
|
func fingerprint(obj interface{}) string {
|
||||||
|
c := spew.ConfigState{
|
||||||
|
SortKeys: true,
|
||||||
|
SpewKeys: true,
|
||||||
|
}
|
||||||
|
return c.Sprintf("%v", obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func dump(obj interface{}) string {
|
||||||
|
cfg := spew.ConfigState{
|
||||||
|
Indent: indentStr,
|
||||||
|
}
|
||||||
|
return cfg.Sdump(obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func indent(src []byte, prefix string) string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
err := json.Indent(&buf, src, prefix, indentStr)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Sprintf("!!! %v", err)
|
||||||
|
}
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := fn(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = fn(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardMarshal(t *testing.B) {
|
||||||
|
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||||
|
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||||
|
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||||
|
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||||
|
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||||
|
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||||
|
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||||
|
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||||
|
}
|
3
output_tests/array/array/byte/types.go
Normal file
3
output_tests/array/array/byte/types.go
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
type T [4][4]byte
|
152
output_tests/array/array/float64/json_test.go
Normal file
152
output_tests/array/array/float64/json_test.go
Normal file
@ -0,0 +1,152 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
fuzz "github.com/google/gofuzz"
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_Roundtrip(t *testing.T) {
|
||||||
|
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||||
|
for i := 0; i < 100; i++ {
|
||||||
|
var before T
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
|
||||||
|
jbStd, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||||
|
t.Fatal("stdlib marshal produced empty result and no error")
|
||||||
|
}
|
||||||
|
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||||
|
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||||
|
}
|
||||||
|
if string(jbStd) != string(jbIter) {
|
||||||
|
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||||
|
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||||
|
}
|
||||||
|
|
||||||
|
var afterStd T
|
||||||
|
err = json.Unmarshal(jbIter, &afterStd)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||||
|
err, indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
var afterIter T
|
||||||
|
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||||
|
err, indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||||
|
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||||
|
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const indentStr = "> "
|
||||||
|
|
||||||
|
func fingerprint(obj interface{}) string {
|
||||||
|
c := spew.ConfigState{
|
||||||
|
SortKeys: true,
|
||||||
|
SpewKeys: true,
|
||||||
|
}
|
||||||
|
return c.Sprintf("%v", obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func dump(obj interface{}) string {
|
||||||
|
cfg := spew.ConfigState{
|
||||||
|
Indent: indentStr,
|
||||||
|
}
|
||||||
|
return cfg.Sdump(obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func indent(src []byte, prefix string) string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
err := json.Indent(&buf, src, prefix, indentStr)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Sprintf("!!! %v", err)
|
||||||
|
}
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := fn(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = fn(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardMarshal(t *testing.B) {
|
||||||
|
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||||
|
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||||
|
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||||
|
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||||
|
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||||
|
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||||
|
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||||
|
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||||
|
}
|
3
output_tests/array/array/float64/types.go
Normal file
3
output_tests/array/array/float64/types.go
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
type T [4][4]float64
|
152
output_tests/array/array/int32/json_test.go
Normal file
152
output_tests/array/array/int32/json_test.go
Normal file
@ -0,0 +1,152 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
fuzz "github.com/google/gofuzz"
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_Roundtrip(t *testing.T) {
|
||||||
|
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||||
|
for i := 0; i < 100; i++ {
|
||||||
|
var before T
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
|
||||||
|
jbStd, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||||
|
t.Fatal("stdlib marshal produced empty result and no error")
|
||||||
|
}
|
||||||
|
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||||
|
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||||
|
}
|
||||||
|
if string(jbStd) != string(jbIter) {
|
||||||
|
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||||
|
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||||
|
}
|
||||||
|
|
||||||
|
var afterStd T
|
||||||
|
err = json.Unmarshal(jbIter, &afterStd)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||||
|
err, indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
var afterIter T
|
||||||
|
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||||
|
err, indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||||
|
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||||
|
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const indentStr = "> "
|
||||||
|
|
||||||
|
func fingerprint(obj interface{}) string {
|
||||||
|
c := spew.ConfigState{
|
||||||
|
SortKeys: true,
|
||||||
|
SpewKeys: true,
|
||||||
|
}
|
||||||
|
return c.Sprintf("%v", obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func dump(obj interface{}) string {
|
||||||
|
cfg := spew.ConfigState{
|
||||||
|
Indent: indentStr,
|
||||||
|
}
|
||||||
|
return cfg.Sdump(obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func indent(src []byte, prefix string) string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
err := json.Indent(&buf, src, prefix, indentStr)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Sprintf("!!! %v", err)
|
||||||
|
}
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := fn(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = fn(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardMarshal(t *testing.B) {
|
||||||
|
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||||
|
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||||
|
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||||
|
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||||
|
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||||
|
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||||
|
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||||
|
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||||
|
}
|
3
output_tests/array/array/int32/types.go
Normal file
3
output_tests/array/array/int32/types.go
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
type T [4][4]int32
|
152
output_tests/array/array/ptr_string/json_test.go
Normal file
152
output_tests/array/array/ptr_string/json_test.go
Normal file
@ -0,0 +1,152 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
fuzz "github.com/google/gofuzz"
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_Roundtrip(t *testing.T) {
|
||||||
|
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||||
|
for i := 0; i < 100; i++ {
|
||||||
|
var before T
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
|
||||||
|
jbStd, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||||
|
t.Fatal("stdlib marshal produced empty result and no error")
|
||||||
|
}
|
||||||
|
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||||
|
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||||
|
}
|
||||||
|
if string(jbStd) != string(jbIter) {
|
||||||
|
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||||
|
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||||
|
}
|
||||||
|
|
||||||
|
var afterStd T
|
||||||
|
err = json.Unmarshal(jbIter, &afterStd)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||||
|
err, indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
var afterIter T
|
||||||
|
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||||
|
err, indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||||
|
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||||
|
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const indentStr = "> "
|
||||||
|
|
||||||
|
func fingerprint(obj interface{}) string {
|
||||||
|
c := spew.ConfigState{
|
||||||
|
SortKeys: true,
|
||||||
|
SpewKeys: true,
|
||||||
|
}
|
||||||
|
return c.Sprintf("%v", obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func dump(obj interface{}) string {
|
||||||
|
cfg := spew.ConfigState{
|
||||||
|
Indent: indentStr,
|
||||||
|
}
|
||||||
|
return cfg.Sdump(obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func indent(src []byte, prefix string) string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
err := json.Indent(&buf, src, prefix, indentStr)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Sprintf("!!! %v", err)
|
||||||
|
}
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := fn(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = fn(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardMarshal(t *testing.B) {
|
||||||
|
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||||
|
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||||
|
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||||
|
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||||
|
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||||
|
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||||
|
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||||
|
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||||
|
}
|
3
output_tests/array/array/ptr_string/types.go
Normal file
3
output_tests/array/array/ptr_string/types.go
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
type T [4][4]*string
|
152
output_tests/array/array/string/json_test.go
Normal file
152
output_tests/array/array/string/json_test.go
Normal file
@ -0,0 +1,152 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
fuzz "github.com/google/gofuzz"
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_Roundtrip(t *testing.T) {
|
||||||
|
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||||
|
for i := 0; i < 100; i++ {
|
||||||
|
var before T
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
|
||||||
|
jbStd, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||||
|
t.Fatal("stdlib marshal produced empty result and no error")
|
||||||
|
}
|
||||||
|
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||||
|
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||||
|
}
|
||||||
|
if string(jbStd) != string(jbIter) {
|
||||||
|
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||||
|
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||||
|
}
|
||||||
|
|
||||||
|
var afterStd T
|
||||||
|
err = json.Unmarshal(jbIter, &afterStd)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||||
|
err, indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
var afterIter T
|
||||||
|
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||||
|
err, indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||||
|
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||||
|
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const indentStr = "> "
|
||||||
|
|
||||||
|
func fingerprint(obj interface{}) string {
|
||||||
|
c := spew.ConfigState{
|
||||||
|
SortKeys: true,
|
||||||
|
SpewKeys: true,
|
||||||
|
}
|
||||||
|
return c.Sprintf("%v", obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func dump(obj interface{}) string {
|
||||||
|
cfg := spew.ConfigState{
|
||||||
|
Indent: indentStr,
|
||||||
|
}
|
||||||
|
return cfg.Sdump(obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func indent(src []byte, prefix string) string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
err := json.Indent(&buf, src, prefix, indentStr)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Sprintf("!!! %v", err)
|
||||||
|
}
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := fn(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = fn(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardMarshal(t *testing.B) {
|
||||||
|
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||||
|
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||||
|
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||||
|
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||||
|
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||||
|
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||||
|
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||||
|
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||||
|
}
|
3
output_tests/array/array/string/types.go
Normal file
3
output_tests/array/array/string/types.go
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
type T [4][4]string
|
152
output_tests/array/array/uint8/json_test.go
Normal file
152
output_tests/array/array/uint8/json_test.go
Normal file
@ -0,0 +1,152 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
fuzz "github.com/google/gofuzz"
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_Roundtrip(t *testing.T) {
|
||||||
|
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||||
|
for i := 0; i < 100; i++ {
|
||||||
|
var before T
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
|
||||||
|
jbStd, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||||
|
t.Fatal("stdlib marshal produced empty result and no error")
|
||||||
|
}
|
||||||
|
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||||
|
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||||
|
}
|
||||||
|
if string(jbStd) != string(jbIter) {
|
||||||
|
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||||
|
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||||
|
}
|
||||||
|
|
||||||
|
var afterStd T
|
||||||
|
err = json.Unmarshal(jbIter, &afterStd)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||||
|
err, indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
var afterIter T
|
||||||
|
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||||
|
err, indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||||
|
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||||
|
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const indentStr = "> "
|
||||||
|
|
||||||
|
func fingerprint(obj interface{}) string {
|
||||||
|
c := spew.ConfigState{
|
||||||
|
SortKeys: true,
|
||||||
|
SpewKeys: true,
|
||||||
|
}
|
||||||
|
return c.Sprintf("%v", obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func dump(obj interface{}) string {
|
||||||
|
cfg := spew.ConfigState{
|
||||||
|
Indent: indentStr,
|
||||||
|
}
|
||||||
|
return cfg.Sdump(obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func indent(src []byte, prefix string) string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
err := json.Indent(&buf, src, prefix, indentStr)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Sprintf("!!! %v", err)
|
||||||
|
}
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := fn(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = fn(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardMarshal(t *testing.B) {
|
||||||
|
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||||
|
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||||
|
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||||
|
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||||
|
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||||
|
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||||
|
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||||
|
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||||
|
}
|
3
output_tests/array/array/uint8/types.go
Normal file
3
output_tests/array/array/uint8/types.go
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
type T [4][4]uint8
|
152
output_tests/array/bool/json_test.go
Normal file
152
output_tests/array/bool/json_test.go
Normal file
@ -0,0 +1,152 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
fuzz "github.com/google/gofuzz"
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_Roundtrip(t *testing.T) {
|
||||||
|
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||||
|
for i := 0; i < 100; i++ {
|
||||||
|
var before T
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
|
||||||
|
jbStd, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||||
|
t.Fatal("stdlib marshal produced empty result and no error")
|
||||||
|
}
|
||||||
|
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||||
|
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||||
|
}
|
||||||
|
if string(jbStd) != string(jbIter) {
|
||||||
|
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||||
|
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||||
|
}
|
||||||
|
|
||||||
|
var afterStd T
|
||||||
|
err = json.Unmarshal(jbIter, &afterStd)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||||
|
err, indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
var afterIter T
|
||||||
|
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||||
|
err, indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||||
|
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||||
|
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const indentStr = "> "
|
||||||
|
|
||||||
|
func fingerprint(obj interface{}) string {
|
||||||
|
c := spew.ConfigState{
|
||||||
|
SortKeys: true,
|
||||||
|
SpewKeys: true,
|
||||||
|
}
|
||||||
|
return c.Sprintf("%v", obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func dump(obj interface{}) string {
|
||||||
|
cfg := spew.ConfigState{
|
||||||
|
Indent: indentStr,
|
||||||
|
}
|
||||||
|
return cfg.Sdump(obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func indent(src []byte, prefix string) string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
err := json.Indent(&buf, src, prefix, indentStr)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Sprintf("!!! %v", err)
|
||||||
|
}
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := fn(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = fn(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardMarshal(t *testing.B) {
|
||||||
|
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||||
|
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||||
|
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||||
|
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||||
|
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||||
|
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||||
|
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||||
|
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||||
|
}
|
3
output_tests/array/bool/types.go
Normal file
3
output_tests/array/bool/types.go
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
type T [4]bool
|
152
output_tests/array/byte/json_test.go
Normal file
152
output_tests/array/byte/json_test.go
Normal file
@ -0,0 +1,152 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
fuzz "github.com/google/gofuzz"
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_Roundtrip(t *testing.T) {
|
||||||
|
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||||
|
for i := 0; i < 100; i++ {
|
||||||
|
var before T
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
|
||||||
|
jbStd, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||||
|
t.Fatal("stdlib marshal produced empty result and no error")
|
||||||
|
}
|
||||||
|
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||||
|
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||||
|
}
|
||||||
|
if string(jbStd) != string(jbIter) {
|
||||||
|
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||||
|
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||||
|
}
|
||||||
|
|
||||||
|
var afterStd T
|
||||||
|
err = json.Unmarshal(jbIter, &afterStd)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||||
|
err, indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
var afterIter T
|
||||||
|
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||||
|
err, indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||||
|
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||||
|
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const indentStr = "> "
|
||||||
|
|
||||||
|
func fingerprint(obj interface{}) string {
|
||||||
|
c := spew.ConfigState{
|
||||||
|
SortKeys: true,
|
||||||
|
SpewKeys: true,
|
||||||
|
}
|
||||||
|
return c.Sprintf("%v", obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func dump(obj interface{}) string {
|
||||||
|
cfg := spew.ConfigState{
|
||||||
|
Indent: indentStr,
|
||||||
|
}
|
||||||
|
return cfg.Sdump(obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func indent(src []byte, prefix string) string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
err := json.Indent(&buf, src, prefix, indentStr)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Sprintf("!!! %v", err)
|
||||||
|
}
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := fn(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = fn(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardMarshal(t *testing.B) {
|
||||||
|
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||||
|
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||||
|
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||||
|
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||||
|
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||||
|
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||||
|
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||||
|
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||||
|
}
|
3
output_tests/array/byte/types.go
Normal file
3
output_tests/array/byte/types.go
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
type T [4]byte
|
152
output_tests/array/float64/json_test.go
Normal file
152
output_tests/array/float64/json_test.go
Normal file
@ -0,0 +1,152 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
fuzz "github.com/google/gofuzz"
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_Roundtrip(t *testing.T) {
|
||||||
|
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||||
|
for i := 0; i < 100; i++ {
|
||||||
|
var before T
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
|
||||||
|
jbStd, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||||
|
t.Fatal("stdlib marshal produced empty result and no error")
|
||||||
|
}
|
||||||
|
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||||
|
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||||
|
}
|
||||||
|
if string(jbStd) != string(jbIter) {
|
||||||
|
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||||
|
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||||
|
}
|
||||||
|
|
||||||
|
var afterStd T
|
||||||
|
err = json.Unmarshal(jbIter, &afterStd)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||||
|
err, indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
var afterIter T
|
||||||
|
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||||
|
err, indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||||
|
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||||
|
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const indentStr = "> "
|
||||||
|
|
||||||
|
func fingerprint(obj interface{}) string {
|
||||||
|
c := spew.ConfigState{
|
||||||
|
SortKeys: true,
|
||||||
|
SpewKeys: true,
|
||||||
|
}
|
||||||
|
return c.Sprintf("%v", obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func dump(obj interface{}) string {
|
||||||
|
cfg := spew.ConfigState{
|
||||||
|
Indent: indentStr,
|
||||||
|
}
|
||||||
|
return cfg.Sdump(obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func indent(src []byte, prefix string) string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
err := json.Indent(&buf, src, prefix, indentStr)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Sprintf("!!! %v", err)
|
||||||
|
}
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := fn(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = fn(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardMarshal(t *testing.B) {
|
||||||
|
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||||
|
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||||
|
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||||
|
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||||
|
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||||
|
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||||
|
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||||
|
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||||
|
}
|
3
output_tests/array/float64/types.go
Normal file
3
output_tests/array/float64/types.go
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
type T [4]float64
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user