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
91 Commits
jsoniter-g
...
revert-55-
Author | SHA1 | Date | |
---|---|---|---|
787edc95b0 | |||
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 |
10
.idea/libraries/Go_SDK.xml
generated
Normal file
10
.idea/libraries/Go_SDK.xml
generated
Normal file
@ -0,0 +1,10 @@
|
||||
<component name="libraryTable">
|
||||
<library name="Go SDK">
|
||||
<CLASSES>
|
||||
<root url="file:///usr/local/go/src" />
|
||||
</CLASSES>
|
||||
<SOURCES>
|
||||
<root url="file:///usr/local/go/src" />
|
||||
</SOURCES>
|
||||
</library>
|
||||
</component>
|
66
README.md
66
README.md
@ -2,61 +2,55 @@
|
||||
|
||||
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)
|
||||
|
||||
# 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
|
||||
import "github.com/json-iterator/go"
|
||||
iter := jsoniter.ParseString(`[0,1,2,3]`)
|
||||
var := iter.Read()
|
||||
fmt.Println(val)
|
||||
jsoniter.Marshal(&data)
|
||||
```
|
||||
|
||||
# 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
|
||||
import "github.com/json-iterator/go"
|
||||
iter := ParseString(`[0, [1, 2], [3, 4], 5]`)
|
||||
count := 0
|
||||
for iter.ReadArray() {
|
||||
iter.Skip()
|
||||
count++
|
||||
}
|
||||
fmt.Println(count) // 4
|
||||
jsoniter.Unmarshal(input, &data)
|
||||
```
|
||||
|
||||
# Any-API for maximum flexibility
|
||||
|
||||
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
|
||||
|
||||
```
|
||||
|
@ -1014,4 +1014,4 @@ func typeAndKind(v interface{}) (reflect.Type, reflect.Kind) {
|
||||
k = t.Kind()
|
||||
}
|
||||
return t, k
|
||||
}
|
||||
}
|
||||
|
47
example_test.go
Normal file
47
example_test.go
Normal file
@ -0,0 +1,47 @@
|
||||
package jsoniter_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func ExampleMarshal() {
|
||||
type ColorGroup struct {
|
||||
ID int
|
||||
Name string
|
||||
Colors []string
|
||||
}
|
||||
group := ColorGroup{
|
||||
ID: 1,
|
||||
Name: "Reds",
|
||||
Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
|
||||
}
|
||||
b, err := jsoniter.Marshal(group)
|
||||
if err != nil {
|
||||
fmt.Println("error:", err)
|
||||
}
|
||||
os.Stdout.Write(b)
|
||||
// Output:
|
||||
// {"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]}
|
||||
}
|
||||
|
||||
func ExampleUnmarshal() {
|
||||
var jsonBlob = []byte(`[
|
||||
{"Name": "Platypus", "Order": "Monotremata"},
|
||||
{"Name": "Quoll", "Order": "Dasyuromorphia"}
|
||||
]`)
|
||||
type Animal struct {
|
||||
Name string
|
||||
Order string
|
||||
}
|
||||
var animals []Animal
|
||||
err := jsoniter.Unmarshal(jsonBlob, &animals)
|
||||
if err != nil {
|
||||
fmt.Println("error:", err)
|
||||
}
|
||||
fmt.Printf("%+v", animals)
|
||||
// Output:
|
||||
// [{Name:Platypus Order:Monotremata} {Name:Quoll Order:Dasyuromorphia}]
|
||||
}
|
@ -1,13 +1,37 @@
|
||||
// Package jsoniter implements encoding and decoding of JSON as defined in
|
||||
// RFC 4627 and provides interfaces with identical syntax of standard lib encoding/json.
|
||||
// Converting from encoding/json to jsoniter is no more than replacing the package with jsoniter
|
||||
// and variable type declarations (if any).
|
||||
// jsoniter interfaces gives 100% compatibility with code using standard lib.
|
||||
//
|
||||
// "JSON and Go"
|
||||
// (https://golang.org/doc/articles/json_and_go.html)
|
||||
// gives a description of how Marshal/Unmarshal operate
|
||||
// between arbitrary or predefined json objects and bytes,
|
||||
// and it applies to jsoniter.Marshal/Unmarshal as well.
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"io"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"reflect"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// Unmarshal adapts to json/encoding APIs
|
||||
// Unmarshal adapts to json/encoding Unmarshal API
|
||||
//
|
||||
// Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v.
|
||||
// Refer to https://godoc.org/encoding/json#Unmarshal for more information
|
||||
func Unmarshal(data []byte, v interface{}) error {
|
||||
data = data[:lastNotSpacePos(data)]
|
||||
iter := ParseBytes(data)
|
||||
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()
|
||||
@ -21,7 +45,9 @@ func Unmarshal(data []byte, v interface{}) error {
|
||||
return iter.Error
|
||||
}
|
||||
|
||||
// UnmarshalAny adapts to
|
||||
func UnmarshalAny(data []byte) (Any, error) {
|
||||
data = data[:lastNotSpacePos(data)]
|
||||
iter := ParseBytes(data)
|
||||
any := iter.ReadAny()
|
||||
if iter.head == iter.tail {
|
||||
@ -36,8 +62,18 @@ func UnmarshalAny(data []byte) (Any, error) {
|
||||
return any, iter.Error
|
||||
}
|
||||
|
||||
func lastNotSpacePos(data []byte) int {
|
||||
for i := len(data) - 1; i >= 0; i-- {
|
||||
if data[i] != ' ' && data[i] != '\t' && data[i] != '\r' && data[i] != '\n' {
|
||||
return i + 1
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func UnmarshalFromString(str string, v interface{}) error {
|
||||
data := []byte(str)
|
||||
data = data[:lastNotSpacePos(data)]
|
||||
iter := ParseBytes(data)
|
||||
iter.ReadVal(v)
|
||||
if iter.head == iter.tail {
|
||||
@ -54,6 +90,7 @@ func UnmarshalFromString(str string, v interface{}) error {
|
||||
|
||||
func UnmarshalAnyFromString(str string) (Any, error) {
|
||||
data := []byte(str)
|
||||
data = data[:lastNotSpacePos(data)]
|
||||
iter := ParseBytes(data)
|
||||
any := iter.ReadAny()
|
||||
if iter.head == iter.tail {
|
||||
@ -68,15 +105,17 @@ func UnmarshalAnyFromString(str string) (Any, error) {
|
||||
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) {
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(buf, 4096)
|
||||
stream := NewStream(nil, 256)
|
||||
stream.WriteVal(v)
|
||||
stream.Flush()
|
||||
if stream.Error != nil {
|
||||
return nil, stream.Error
|
||||
}
|
||||
return buf.Bytes(), nil
|
||||
return stream.Buffer(), nil
|
||||
}
|
||||
|
||||
func MarshalToString(v interface{}) (string, error) {
|
||||
@ -85,4 +124,68 @@ func MarshalToString(v interface{}) (string, error) {
|
||||
return "", err
|
||||
}
|
||||
return string(buf), nil
|
||||
}
|
||||
}
|
||||
|
||||
// NewDecoder adapts to json/stream NewDecoder API.
|
||||
//
|
||||
// NewDecoder returns a new decoder that reads from r.
|
||||
//
|
||||
// Instead of a json/encoding Decoder, an AdaptedDecoder is returned
|
||||
// Refer to https://godoc.org/encoding/json#NewDecoder for more information
|
||||
func NewDecoder(reader io.Reader) *AdaptedDecoder {
|
||||
iter := Parse(reader, 512)
|
||||
return &AdaptedDecoder{iter}
|
||||
}
|
||||
|
||||
// AdaptedDecoder reads and decodes JSON values from an input stream.
|
||||
// AdaptedDecoder provides identical APIs with json/stream Decoder (Token() and UseNumber() are in progress)
|
||||
type AdaptedDecoder struct {
|
||||
iter *Iterator
|
||||
}
|
||||
|
||||
func (adapter *AdaptedDecoder) Decode(obj interface{}) error {
|
||||
adapter.iter.ReadVal(obj)
|
||||
err := adapter.iter.Error
|
||||
if err == io.EOF {
|
||||
return nil
|
||||
}
|
||||
return adapter.iter.Error
|
||||
}
|
||||
|
||||
func (adapter *AdaptedDecoder) More() bool {
|
||||
return adapter.iter.head != adapter.iter.tail
|
||||
}
|
||||
|
||||
func (adapter *AdaptedDecoder) Buffered() io.Reader {
|
||||
remaining := adapter.iter.buf[adapter.iter.head:adapter.iter.tail]
|
||||
return bytes.NewReader(remaining)
|
||||
}
|
||||
|
||||
func (decoder *AdaptedDecoder) UseNumber() {
|
||||
RegisterTypeDecoder("interface {}", func(ptr unsafe.Pointer, iter *Iterator) {
|
||||
if iter.WhatIsNext() == Number {
|
||||
*((*interface{})(ptr)) = json.Number(iter.readNumberAsString())
|
||||
} else {
|
||||
*((*interface{})(ptr)) = iter.Read()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func NewEncoder(writer io.Writer) *AdaptedEncoder {
|
||||
stream := NewStream(writer, 512)
|
||||
return &AdaptedEncoder{stream}
|
||||
}
|
||||
|
||||
type AdaptedEncoder struct {
|
||||
stream *Stream
|
||||
}
|
||||
|
||||
func (adapter *AdaptedEncoder) Encode(val interface{}) error {
|
||||
adapter.stream.WriteVal(val)
|
||||
adapter.stream.Flush()
|
||||
return adapter.stream.Error
|
||||
}
|
||||
|
||||
func (adapter *AdaptedEncoder) SetIndent(prefix, indent string) {
|
||||
adapter.stream.IndentionStep = len(indent)
|
||||
}
|
||||
|
@ -98,6 +98,10 @@ func Wrap(val interface{}) Any {
|
||||
if val == nil {
|
||||
return &nilAny{}
|
||||
}
|
||||
asAny, isAny := val.(Any)
|
||||
if isAny {
|
||||
return asAny
|
||||
}
|
||||
type_ := reflect.TypeOf(val)
|
||||
switch type_.Kind() {
|
||||
case reflect.Slice:
|
||||
|
@ -1,9 +1,9 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"unsafe"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
type arrayLazyAny struct {
|
||||
@ -44,7 +44,7 @@ func (any *arrayLazyAny) fillCacheUntil(target int) Any {
|
||||
return any.cache[target]
|
||||
}
|
||||
iter := any.Parse()
|
||||
if (len(any.remaining) == len(any.buf)) {
|
||||
if len(any.remaining) == len(any.buf) {
|
||||
iter.head++
|
||||
c := iter.nextToken()
|
||||
if c != ']' {
|
||||
@ -337,9 +337,9 @@ func (any *arrayLazyAny) GetInterface() interface{} {
|
||||
|
||||
type arrayAny struct {
|
||||
baseAny
|
||||
err error
|
||||
cache []Any
|
||||
val reflect.Value
|
||||
err error
|
||||
cache []Any
|
||||
val reflect.Value
|
||||
}
|
||||
|
||||
func wrapArray(val interface{}) *arrayAny {
|
||||
@ -536,4 +536,4 @@ func (any *arrayAny) WriteTo(stream *Stream) {
|
||||
func (any *arrayAny) GetInterface() interface{} {
|
||||
any.fillCache()
|
||||
return any.cache
|
||||
}
|
||||
}
|
||||
|
@ -2,15 +2,15 @@ package jsoniter
|
||||
|
||||
import (
|
||||
"io"
|
||||
"unsafe"
|
||||
"strconv"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
type float64LazyAny struct {
|
||||
baseAny
|
||||
buf []byte
|
||||
iter *Iterator
|
||||
err error
|
||||
buf []byte
|
||||
iter *Iterator
|
||||
err error
|
||||
cache float64
|
||||
}
|
||||
|
||||
@ -163,4 +163,4 @@ func (any *floatAny) WriteTo(stream *Stream) {
|
||||
|
||||
func (any *floatAny) GetInterface() interface{} {
|
||||
return any.val
|
||||
}
|
||||
}
|
||||
|
@ -67,4 +67,4 @@ func (any *int32Any) Parse() *Iterator {
|
||||
|
||||
func (any *int32Any) GetInterface() interface{} {
|
||||
return any.val
|
||||
}
|
||||
}
|
||||
|
@ -2,8 +2,8 @@ package jsoniter
|
||||
|
||||
import (
|
||||
"io"
|
||||
"unsafe"
|
||||
"strconv"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
type int64LazyAny struct {
|
||||
@ -163,4 +163,4 @@ func (any *int64Any) Parse() *Iterator {
|
||||
|
||||
func (any *int64Any) GetInterface() interface{} {
|
||||
return any.val
|
||||
}
|
||||
}
|
||||
|
@ -62,4 +62,4 @@ func (any *nilAny) Parse() *Iterator {
|
||||
|
||||
func (any *nilAny) GetInterface() interface{} {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,9 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"unsafe"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
type objectLazyAny struct {
|
||||
@ -322,6 +322,7 @@ func (any *objectLazyAny) IterateObject() (func() (string, Any, bool), bool) {
|
||||
any.err = iter.Error
|
||||
return key, value, true
|
||||
} else {
|
||||
nextKey = ""
|
||||
remaining = nil
|
||||
any.remaining = nil
|
||||
any.err = iter.Error
|
||||
|
@ -5,7 +5,7 @@ import (
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type stringLazyAny struct{
|
||||
type stringLazyAny struct {
|
||||
baseAny
|
||||
buf []byte
|
||||
iter *Iterator
|
||||
@ -136,9 +136,9 @@ func (any *stringLazyAny) GetInterface() interface{} {
|
||||
return any.cache
|
||||
}
|
||||
|
||||
type stringAny struct{
|
||||
type stringAny struct {
|
||||
baseAny
|
||||
err error
|
||||
err error
|
||||
val string
|
||||
}
|
||||
|
||||
@ -146,7 +146,6 @@ func (any *stringAny) Parse() *Iterator {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
func (any *stringAny) ValueType() ValueType {
|
||||
return String
|
||||
}
|
||||
@ -228,4 +227,4 @@ func (any *stringAny) WriteTo(stream *Stream) {
|
||||
|
||||
func (any *stringAny) GetInterface() interface{} {
|
||||
return any.val
|
||||
}
|
||||
}
|
||||
|
@ -1,12 +1,11 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"io"
|
||||
"strconv"
|
||||
"unsafe"
|
||||
"io"
|
||||
)
|
||||
|
||||
|
||||
type uint64LazyAny struct {
|
||||
baseAny
|
||||
buf []byte
|
||||
@ -164,4 +163,4 @@ func (any *uint64Any) Parse() *Iterator {
|
||||
|
||||
func (any *uint64Any) GetInterface() interface{} {
|
||||
return any.val
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +1,9 @@
|
||||
//
|
||||
// Besides, jsoniter.Iterator provides a different set of interfaces
|
||||
// iterating given bytes/string/reader
|
||||
// and yielding parsed elements one by one.
|
||||
// This set of interfaces reads input as required and gives
|
||||
// better performance.
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
@ -276,4 +282,3 @@ func (iter *Iterator) ReadBase64() (ret []byte) {
|
||||
}
|
||||
return ret[:n]
|
||||
}
|
||||
|
||||
|
@ -18,12 +18,11 @@ func (iter *Iterator) ReadArray() (ret bool) {
|
||||
case ',':
|
||||
return true
|
||||
default:
|
||||
iter.reportError("ReadArray", "expect [ or , or ] or n, but found: " + string([]byte{c}))
|
||||
iter.reportError("ReadArray", "expect [ or , or ] or n, but found: "+string([]byte{c}))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func (iter *Iterator) ReadArrayCB(callback func(*Iterator) bool) (ret bool) {
|
||||
c := iter.nextToken()
|
||||
if c == '[' {
|
||||
@ -46,6 +45,6 @@ func (iter *Iterator) ReadArrayCB(callback func(*Iterator) bool) (ret bool) {
|
||||
iter.skipFixedBytes(3)
|
||||
return true // null
|
||||
}
|
||||
iter.reportError("ReadArrayCB", "expect [ or n, but found: " + string([]byte{c}))
|
||||
iter.reportError("ReadArrayCB", "expect [ or n, but found: "+string([]byte{c}))
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
@ -2,11 +2,13 @@ package jsoniter
|
||||
|
||||
import (
|
||||
"io"
|
||||
"math/big"
|
||||
"strconv"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
var floatDigits []int8
|
||||
|
||||
const invalidCharForNumber = int8(-1)
|
||||
const endOfNumber = int8(-2)
|
||||
const dotInNumber = int8(-3)
|
||||
@ -19,11 +21,45 @@ func init() {
|
||||
for i := int8('0'); i <= int8('9'); i++ {
|
||||
floatDigits[i] = i - int8('0')
|
||||
}
|
||||
floatDigits[','] = endOfNumber;
|
||||
floatDigits[']'] = endOfNumber;
|
||||
floatDigits['}'] = endOfNumber;
|
||||
floatDigits[' '] = endOfNumber;
|
||||
floatDigits['.'] = dotInNumber;
|
||||
floatDigits[','] = endOfNumber
|
||||
floatDigits[']'] = endOfNumber
|
||||
floatDigits['}'] = endOfNumber
|
||||
floatDigits[' '] = endOfNumber
|
||||
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) {
|
||||
@ -40,7 +76,7 @@ func (iter *Iterator) readPositiveFloat32() (ret float32) {
|
||||
value := uint64(0)
|
||||
c := byte(' ')
|
||||
i := iter.head
|
||||
non_decimal_loop:
|
||||
non_decimal_loop:
|
||||
for ; i < iter.tail; i++ {
|
||||
c = iter.buf[i]
|
||||
ind := floatDigits[c]
|
||||
@ -56,14 +92,14 @@ func (iter *Iterator) readPositiveFloat32() (ret float32) {
|
||||
if value > uint64SafeToMultiple10 {
|
||||
return iter.readFloat32SlowPath()
|
||||
}
|
||||
value = (value << 3) + (value << 1) + uint64(ind); // value = value * 10 + ind;
|
||||
value = (value << 3) + (value << 1) + uint64(ind) // value = value * 10 + ind;
|
||||
}
|
||||
if c == '.' {
|
||||
i++
|
||||
decimalPlaces := 0;
|
||||
decimalPlaces := 0
|
||||
for ; i < iter.tail; i++ {
|
||||
c = iter.buf[i]
|
||||
ind := floatDigits[c];
|
||||
ind := floatDigits[c]
|
||||
switch ind {
|
||||
case endOfNumber:
|
||||
if decimalPlaces > 0 && decimalPlaces < len(POW10) {
|
||||
@ -71,7 +107,7 @@ func (iter *Iterator) readPositiveFloat32() (ret float32) {
|
||||
return float32(float64(value) / float64(POW10[decimalPlaces]))
|
||||
}
|
||||
// too many decimal places
|
||||
return iter.readFloat32SlowPath()
|
||||
return iter.readFloat32SlowPath()
|
||||
case invalidCharForNumber:
|
||||
fallthrough
|
||||
case dotInNumber:
|
||||
@ -87,10 +123,10 @@ func (iter *Iterator) readPositiveFloat32() (ret float32) {
|
||||
return iter.readFloat32SlowPath()
|
||||
}
|
||||
|
||||
func (iter *Iterator) readFloat32SlowPath() (ret float32) {
|
||||
func (iter *Iterator) readNumberAsString() (ret string) {
|
||||
strBuf := [16]byte{}
|
||||
str := strBuf[0:0]
|
||||
load_loop:
|
||||
load_loop:
|
||||
for {
|
||||
for i := iter.head; i < iter.tail; i++ {
|
||||
c := iter.buf[i]
|
||||
@ -99,6 +135,7 @@ func (iter *Iterator) readFloat32SlowPath() (ret float32) {
|
||||
str = append(str, c)
|
||||
continue
|
||||
default:
|
||||
iter.head = i
|
||||
break load_loop
|
||||
}
|
||||
}
|
||||
@ -109,7 +146,18 @@ func (iter *Iterator) readFloat32SlowPath() (ret float32) {
|
||||
if iter.Error != nil && iter.Error != io.EOF {
|
||||
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 {
|
||||
iter.Error = err
|
||||
return
|
||||
@ -131,7 +179,7 @@ func (iter *Iterator) readPositiveFloat64() (ret float64) {
|
||||
value := uint64(0)
|
||||
c := byte(' ')
|
||||
i := iter.head
|
||||
non_decimal_loop:
|
||||
non_decimal_loop:
|
||||
for ; i < iter.tail; i++ {
|
||||
c = iter.buf[i]
|
||||
ind := floatDigits[c]
|
||||
@ -147,14 +195,14 @@ func (iter *Iterator) readPositiveFloat64() (ret float64) {
|
||||
if value > uint64SafeToMultiple10 {
|
||||
return iter.readFloat64SlowPath()
|
||||
}
|
||||
value = (value << 3) + (value << 1) + uint64(ind); // value = value * 10 + ind;
|
||||
value = (value << 3) + (value << 1) + uint64(ind) // value = value * 10 + ind;
|
||||
}
|
||||
if c == '.' {
|
||||
i++
|
||||
decimalPlaces := 0;
|
||||
decimalPlaces := 0
|
||||
for ; i < iter.tail; i++ {
|
||||
c = iter.buf[i]
|
||||
ind := floatDigits[c];
|
||||
ind := floatDigits[c]
|
||||
switch ind {
|
||||
case endOfNumber:
|
||||
if decimalPlaces > 0 && decimalPlaces < len(POW10) {
|
||||
@ -179,28 +227,11 @@ func (iter *Iterator) readPositiveFloat64() (ret float64) {
|
||||
}
|
||||
|
||||
func (iter *Iterator) readFloat64SlowPath() (ret float64) {
|
||||
strBuf := [16]byte{}
|
||||
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
|
||||
}
|
||||
}
|
||||
str := iter.readNumberAsString()
|
||||
if iter.Error != nil && iter.Error != io.EOF {
|
||||
return
|
||||
}
|
||||
val, err := strconv.ParseFloat(*(*string)(unsafe.Pointer(&str)), 64)
|
||||
val, err := strconv.ParseFloat(str, 64)
|
||||
if err != nil {
|
||||
iter.Error = err
|
||||
return
|
||||
|
@ -6,8 +6,8 @@ import (
|
||||
|
||||
var intDigits []int8
|
||||
|
||||
const uint32SafeToMultiply10 = uint32(0xffffffff) / 10 - 1
|
||||
const uint64SafeToMultiple10 = uint64(0xffffffffffffffff) / 10 - 1
|
||||
const uint32SafeToMultiply10 = uint32(0xffffffff)/10 - 1
|
||||
const uint64SafeToMultiple10 = uint64(0xffffffffffffffff)/10 - 1
|
||||
const int64Max = uint64(0x7fffffffffffffff)
|
||||
const int32Max = uint32(0x7fffffff)
|
||||
const int16Max = uint32(0x7fff)
|
||||
@ -17,7 +17,7 @@ const uint8Max = uint32(0xffff)
|
||||
|
||||
func init() {
|
||||
intDigits = make([]int8, 256)
|
||||
for i := 0; i < len(floatDigits); i++ {
|
||||
for i := 0; i < len(intDigits); i++ {
|
||||
intDigits[i] = invalidCharForNumber
|
||||
}
|
||||
for i := int8('0'); i <= int8('9'); i++ {
|
||||
@ -37,15 +37,15 @@ func (iter *Iterator) ReadInt8() (ret int8) {
|
||||
c := iter.nextToken()
|
||||
if c == '-' {
|
||||
val := iter.readUint32(iter.readByte())
|
||||
if val > int8Max + 1 {
|
||||
iter.reportError("ReadInt8", "overflow: " + strconv.FormatInt(int64(val), 10))
|
||||
if val > int8Max+1 {
|
||||
iter.reportError("ReadInt8", "overflow: "+strconv.FormatInt(int64(val), 10))
|
||||
return
|
||||
}
|
||||
return -int8(val)
|
||||
} else {
|
||||
val := iter.readUint32(c)
|
||||
if val > int8Max {
|
||||
iter.reportError("ReadInt8", "overflow: " + strconv.FormatInt(int64(val), 10))
|
||||
iter.reportError("ReadInt8", "overflow: "+strconv.FormatInt(int64(val), 10))
|
||||
return
|
||||
}
|
||||
return int8(val)
|
||||
@ -55,7 +55,7 @@ func (iter *Iterator) ReadInt8() (ret int8) {
|
||||
func (iter *Iterator) ReadUint8() (ret uint8) {
|
||||
val := iter.readUint32(iter.nextToken())
|
||||
if val > uint8Max {
|
||||
iter.reportError("ReadUint8", "overflow: " + strconv.FormatInt(int64(val), 10))
|
||||
iter.reportError("ReadUint8", "overflow: "+strconv.FormatInt(int64(val), 10))
|
||||
return
|
||||
}
|
||||
return uint8(val)
|
||||
@ -65,15 +65,15 @@ func (iter *Iterator) ReadInt16() (ret int16) {
|
||||
c := iter.nextToken()
|
||||
if c == '-' {
|
||||
val := iter.readUint32(iter.readByte())
|
||||
if val > int16Max + 1 {
|
||||
iter.reportError("ReadInt16", "overflow: " + strconv.FormatInt(int64(val), 10))
|
||||
if val > int16Max+1 {
|
||||
iter.reportError("ReadInt16", "overflow: "+strconv.FormatInt(int64(val), 10))
|
||||
return
|
||||
}
|
||||
return -int16(val)
|
||||
} else {
|
||||
val := iter.readUint32(c)
|
||||
if val > int16Max {
|
||||
iter.reportError("ReadInt16", "overflow: " + strconv.FormatInt(int64(val), 10))
|
||||
iter.reportError("ReadInt16", "overflow: "+strconv.FormatInt(int64(val), 10))
|
||||
return
|
||||
}
|
||||
return int16(val)
|
||||
@ -83,7 +83,7 @@ func (iter *Iterator) ReadInt16() (ret int16) {
|
||||
func (iter *Iterator) ReadUint16() (ret uint16) {
|
||||
val := iter.readUint32(iter.nextToken())
|
||||
if val > uint16Max {
|
||||
iter.reportError("ReadUint16", "overflow: " + strconv.FormatInt(int64(val), 10))
|
||||
iter.reportError("ReadUint16", "overflow: "+strconv.FormatInt(int64(val), 10))
|
||||
return
|
||||
}
|
||||
return uint16(val)
|
||||
@ -93,15 +93,15 @@ func (iter *Iterator) ReadInt32() (ret int32) {
|
||||
c := iter.nextToken()
|
||||
if c == '-' {
|
||||
val := iter.readUint32(iter.readByte())
|
||||
if val > int32Max + 1 {
|
||||
iter.reportError("ReadInt32", "overflow: " + strconv.FormatInt(int64(val), 10))
|
||||
if val > int32Max+1 {
|
||||
iter.reportError("ReadInt32", "overflow: "+strconv.FormatInt(int64(val), 10))
|
||||
return
|
||||
}
|
||||
return -int32(val)
|
||||
} else {
|
||||
val := iter.readUint32(c)
|
||||
if val > int32Max {
|
||||
iter.reportError("ReadInt32", "overflow: " + strconv.FormatInt(int64(val), 10))
|
||||
iter.reportError("ReadInt32", "overflow: "+strconv.FormatInt(int64(val), 10))
|
||||
return
|
||||
}
|
||||
return int32(val)
|
||||
@ -118,11 +118,11 @@ func (iter *Iterator) readUint32(c byte) (ret uint32) {
|
||||
return 0 // single zero
|
||||
}
|
||||
if ind == invalidCharForNumber {
|
||||
iter.reportError("readUint32", "unexpected character: " + string([]byte{byte(ind)}))
|
||||
iter.reportError("readUint32", "unexpected character: "+string([]byte{byte(ind)}))
|
||||
return
|
||||
}
|
||||
value := uint32(ind)
|
||||
if iter.tail - iter.head > 10 {
|
||||
if iter.tail-iter.head > 10 {
|
||||
i := iter.head
|
||||
ind2 := intDigits[iter.buf[i]]
|
||||
if ind2 == invalidCharForNumber {
|
||||
@ -133,7 +133,7 @@ func (iter *Iterator) readUint32(c byte) (ret uint32) {
|
||||
ind3 := intDigits[iter.buf[i]]
|
||||
if ind3 == invalidCharForNumber {
|
||||
iter.head = i
|
||||
return value * 10 + uint32(ind2)
|
||||
return value*10 + uint32(ind2)
|
||||
}
|
||||
//iter.head = i + 1
|
||||
//value = value * 100 + uint32(ind2) * 10 + uint32(ind3)
|
||||
@ -141,35 +141,35 @@ func (iter *Iterator) readUint32(c byte) (ret uint32) {
|
||||
ind4 := intDigits[iter.buf[i]]
|
||||
if ind4 == invalidCharForNumber {
|
||||
iter.head = i
|
||||
return value * 100 + uint32(ind2) * 10 + uint32(ind3)
|
||||
return value*100 + uint32(ind2)*10 + uint32(ind3)
|
||||
}
|
||||
i++
|
||||
ind5 := intDigits[iter.buf[i]]
|
||||
if ind5 == invalidCharForNumber {
|
||||
iter.head = i
|
||||
return value * 1000 + uint32(ind2) * 100 + uint32(ind3) * 10 + uint32(ind4)
|
||||
return value*1000 + uint32(ind2)*100 + uint32(ind3)*10 + uint32(ind4)
|
||||
}
|
||||
i++
|
||||
ind6 := intDigits[iter.buf[i]]
|
||||
if ind6 == invalidCharForNumber {
|
||||
iter.head = i
|
||||
return value * 10000 + uint32(ind2) * 1000 + uint32(ind3) * 100 + uint32(ind4) * 10 + uint32(ind5)
|
||||
return value*10000 + uint32(ind2)*1000 + uint32(ind3)*100 + uint32(ind4)*10 + uint32(ind5)
|
||||
}
|
||||
i++
|
||||
ind7 := intDigits[iter.buf[i]]
|
||||
if ind7 == invalidCharForNumber {
|
||||
iter.head = i
|
||||
return value * 100000 + uint32(ind2) * 10000 + uint32(ind3) * 1000 + uint32(ind4) * 100 + uint32(ind5) * 10 + uint32(ind6)
|
||||
return value*100000 + uint32(ind2)*10000 + uint32(ind3)*1000 + uint32(ind4)*100 + uint32(ind5)*10 + uint32(ind6)
|
||||
}
|
||||
i++
|
||||
ind8 := intDigits[iter.buf[i]]
|
||||
if ind8 == invalidCharForNumber {
|
||||
iter.head = i
|
||||
return value * 1000000 + uint32(ind2) * 100000 + uint32(ind3) * 10000 + uint32(ind4) * 1000 + uint32(ind5) * 100 + uint32(ind6) * 10 + uint32(ind7)
|
||||
return value*1000000 + uint32(ind2)*100000 + uint32(ind3)*10000 + uint32(ind4)*1000 + uint32(ind5)*100 + uint32(ind6)*10 + uint32(ind7)
|
||||
}
|
||||
i++
|
||||
ind9 := intDigits[iter.buf[i]]
|
||||
value = value * 10000000 + uint32(ind2) * 1000000 + uint32(ind3) * 100000 + uint32(ind4) * 10000 + uint32(ind5) * 1000 + uint32(ind6) * 100 + uint32(ind7) * 10 + uint32(ind8)
|
||||
value = value*10000000 + uint32(ind2)*1000000 + uint32(ind3)*100000 + uint32(ind4)*10000 + uint32(ind5)*1000 + uint32(ind6)*100 + uint32(ind7)*10 + uint32(ind8)
|
||||
iter.head = i
|
||||
if ind9 == invalidCharForNumber {
|
||||
return value
|
||||
@ -194,7 +194,7 @@ func (iter *Iterator) readUint32(c byte) (ret uint32) {
|
||||
}
|
||||
value = (value << 3) + (value << 1) + uint32(ind)
|
||||
}
|
||||
if (!iter.loadMore()) {
|
||||
if !iter.loadMore() {
|
||||
return value
|
||||
}
|
||||
}
|
||||
@ -204,15 +204,15 @@ func (iter *Iterator) ReadInt64() (ret int64) {
|
||||
c := iter.nextToken()
|
||||
if c == '-' {
|
||||
val := iter.readUint64(iter.readByte())
|
||||
if val > int64Max + 1 {
|
||||
iter.reportError("ReadInt64", "overflow: " + strconv.FormatUint(uint64(val), 10))
|
||||
if val > int64Max+1 {
|
||||
iter.reportError("ReadInt64", "overflow: "+strconv.FormatUint(uint64(val), 10))
|
||||
return
|
||||
}
|
||||
return -int64(val)
|
||||
} else {
|
||||
val := iter.readUint64(c)
|
||||
if val > int64Max {
|
||||
iter.reportError("ReadInt64", "overflow: " + strconv.FormatUint(uint64(val), 10))
|
||||
iter.reportError("ReadInt64", "overflow: "+strconv.FormatUint(uint64(val), 10))
|
||||
return
|
||||
}
|
||||
return int64(val)
|
||||
@ -229,7 +229,7 @@ func (iter *Iterator) readUint64(c byte) (ret uint64) {
|
||||
return 0 // single zero
|
||||
}
|
||||
if ind == invalidCharForNumber {
|
||||
iter.reportError("readUint64", "unexpected character: " + string([]byte{byte(ind)}))
|
||||
iter.reportError("readUint64", "unexpected character: "+string([]byte{byte(ind)}))
|
||||
return
|
||||
}
|
||||
value := uint64(ind)
|
||||
@ -252,7 +252,7 @@ func (iter *Iterator) readUint64(c byte) (ret uint64) {
|
||||
}
|
||||
value = (value << 3) + (value << 1) + uint64(ind)
|
||||
}
|
||||
if (!iter.loadMore()) {
|
||||
if !iter.loadMore() {
|
||||
return value
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,10 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
func (iter *Iterator) ReadObject() (ret string) {
|
||||
c := iter.nextToken()
|
||||
switch c {
|
||||
@ -22,7 +27,7 @@ func (iter *Iterator) ReadObject() (ret string) {
|
||||
case '}':
|
||||
return "" // end of object
|
||||
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
|
||||
}
|
||||
}
|
||||
@ -35,11 +40,14 @@ func (iter *Iterator) readFieldHash() int32 {
|
||||
for i := iter.head; i < iter.tail; i++ {
|
||||
// require ascii string and no escape
|
||||
b := iter.buf[i]
|
||||
if 'A' <= b && b <= 'Z' {
|
||||
b += 'a' - 'A'
|
||||
}
|
||||
if b == '"' {
|
||||
iter.head = i+1
|
||||
iter.head = i + 1
|
||||
c = iter.nextToken()
|
||||
if c != ':' {
|
||||
iter.reportError("readFieldHash", `expect :, but found ` + string([]byte{c}))
|
||||
iter.reportError("readFieldHash", `expect :, but found `+string([]byte{c}))
|
||||
}
|
||||
return int32(hash)
|
||||
}
|
||||
@ -52,14 +60,14 @@ func (iter *Iterator) readFieldHash() int32 {
|
||||
}
|
||||
}
|
||||
}
|
||||
iter.reportError("readFieldHash", `expect ", but found ` + string([]byte{c}))
|
||||
iter.reportError("readFieldHash", `expect ", but found `+string([]byte{c}))
|
||||
return 0
|
||||
}
|
||||
|
||||
func calcHash(str string) int32 {
|
||||
hash := int64(0x811c9dc5)
|
||||
for _, b := range str {
|
||||
hash ^= int64(b)
|
||||
hash ^= int64(unicode.ToLower(b))
|
||||
hash *= 0x1000193
|
||||
}
|
||||
return int32(hash)
|
||||
@ -76,7 +84,7 @@ func (iter *Iterator) ReadObjectCB(callback func(*Iterator, string) bool) bool {
|
||||
return false
|
||||
}
|
||||
for iter.nextToken() == ',' {
|
||||
field := string(iter.readObjectFieldAsBytes())
|
||||
field = string(iter.readObjectFieldAsBytes())
|
||||
if !callback(iter, field) {
|
||||
return false
|
||||
}
|
||||
@ -97,6 +105,46 @@ func (iter *Iterator) ReadObjectCB(callback func(*Iterator, string) bool) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (iter *Iterator) ReadMapCB(callback func(*Iterator, string) bool) bool {
|
||||
c := iter.nextToken()
|
||||
if c == '{' {
|
||||
c = iter.nextToken()
|
||||
if c == '"' {
|
||||
iter.unreadByte()
|
||||
field := iter.ReadString()
|
||||
if iter.nextToken() != ':' {
|
||||
iter.reportError("ReadMapCB", "expect : after object field")
|
||||
return false
|
||||
}
|
||||
if !callback(iter, field) {
|
||||
return false
|
||||
}
|
||||
for iter.nextToken() == ',' {
|
||||
field = iter.ReadString()
|
||||
if iter.nextToken() != ':' {
|
||||
iter.reportError("ReadMapCB", "expect : after object field")
|
||||
return false
|
||||
}
|
||||
if !callback(iter, field) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
if c == '}' {
|
||||
return true
|
||||
}
|
||||
iter.reportError("ReadMapCB", `expect " after }`)
|
||||
return false
|
||||
}
|
||||
if c == 'n' {
|
||||
iter.skipFixedBytes(3)
|
||||
return true // null
|
||||
}
|
||||
iter.reportError("ReadMapCB", `expect { or n`)
|
||||
return false
|
||||
}
|
||||
|
||||
func (iter *Iterator) readObjectStart() bool {
|
||||
c := iter.nextToken()
|
||||
if c == '{' {
|
||||
@ -106,8 +154,11 @@ func (iter *Iterator) readObjectStart() bool {
|
||||
}
|
||||
iter.unreadByte()
|
||||
return true
|
||||
} else if c == 'n' {
|
||||
iter.skipFixedBytes(3)
|
||||
return false
|
||||
}
|
||||
iter.reportError("readObjectStart", "expect { ")
|
||||
iter.reportError("readObjectStart", "expect { or n")
|
||||
return false
|
||||
}
|
||||
|
||||
|
@ -29,6 +29,15 @@ func (iter *Iterator) ReadBool() (ret bool) {
|
||||
return
|
||||
}
|
||||
|
||||
func (iter *Iterator) SkipAndReturnBytes() []byte {
|
||||
if iter.reader != nil {
|
||||
panic("reader input does not support this api")
|
||||
}
|
||||
before := iter.head
|
||||
iter.Skip()
|
||||
after := iter.head
|
||||
return iter.buf[before:after]
|
||||
}
|
||||
|
||||
// Skip skips a json object and positions to relatively the next json object
|
||||
func (iter *Iterator) Skip() {
|
||||
@ -193,15 +202,15 @@ func (iter *Iterator) skipUntilBreak() {
|
||||
}
|
||||
|
||||
func (iter *Iterator) skipFixedBytes(n int) {
|
||||
iter.head += n;
|
||||
if (iter.head >= iter.tail) {
|
||||
more := iter.head - iter.tail;
|
||||
iter.head += n
|
||||
if iter.head >= iter.tail {
|
||||
more := iter.head - iter.tail
|
||||
if !iter.loadMore() {
|
||||
if more > 0 {
|
||||
iter.reportError("skipFixedBytes", "unexpected end");
|
||||
iter.reportError("skipFixedBytes", "unexpected end")
|
||||
}
|
||||
return
|
||||
}
|
||||
iter.head += more;
|
||||
iter.head += more
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,64 +2,42 @@ package jsoniter
|
||||
|
||||
import (
|
||||
"unicode/utf16"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// TODO: avoid append
|
||||
func (iter *Iterator) ReadString() (ret string) {
|
||||
c := iter.nextToken()
|
||||
if c == '"' {
|
||||
copied := make([]byte, 32)
|
||||
j := 0
|
||||
fast_loop:
|
||||
for {
|
||||
i := iter.head
|
||||
for ; i < iter.tail && j < len(copied); i++ {
|
||||
c := iter.buf[i]
|
||||
if c == '"' {
|
||||
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
|
||||
for i := iter.head; i < iter.tail; i++ {
|
||||
c := iter.buf[i]
|
||||
if c == '"' {
|
||||
ret = string(iter.buf[iter.head:i])
|
||||
iter.head = i + 1
|
||||
return ret
|
||||
} else if c == '\\' {
|
||||
break
|
||||
}
|
||||
}
|
||||
return iter.readStringSlowPath(copied[:j])
|
||||
return iter.readStringSlowPath()
|
||||
} else if c == 'n' {
|
||||
iter.skipFixedBytes(3)
|
||||
return ""
|
||||
}
|
||||
iter.reportError("ReadString", `expects " or n`)
|
||||
return
|
||||
}
|
||||
|
||||
func (iter *Iterator) readStringSlowPath(str []byte) (ret string) {
|
||||
func (iter *Iterator) readStringSlowPath() (ret string) {
|
||||
var str []byte
|
||||
var c byte
|
||||
for iter.Error == nil {
|
||||
c = iter.readByte()
|
||||
if c == '"' {
|
||||
return *(*string)(unsafe.Pointer(&str))
|
||||
return string(str)
|
||||
}
|
||||
if c == '\\' {
|
||||
c = iter.readByte()
|
||||
switch c {
|
||||
case 'u':
|
||||
case 'u', 'U':
|
||||
r := iter.readU4()
|
||||
if utf16.IsSurrogate(r) {
|
||||
c = iter.readByte()
|
||||
@ -75,7 +53,7 @@ func (iter *Iterator) readStringSlowPath(str []byte) (ret string) {
|
||||
if iter.Error != nil {
|
||||
return
|
||||
}
|
||||
if c != 'u' {
|
||||
if c != 'u' && c != 'U' {
|
||||
iter.reportError("ReadString",
|
||||
`expects \u after utf16 surrogate, but \u not found`)
|
||||
return
|
||||
@ -114,6 +92,7 @@ func (iter *Iterator) readStringSlowPath(str []byte) (ret string) {
|
||||
str = append(str, c)
|
||||
}
|
||||
}
|
||||
iter.reportError("ReadString", "unexpected end of input")
|
||||
return
|
||||
}
|
||||
|
||||
@ -125,13 +104,13 @@ func (iter *Iterator) ReadStringAsSlice() (ret []byte) {
|
||||
// for: field name, base64, number
|
||||
if iter.buf[i] == '"' {
|
||||
// fast path: reuse the underlying buffer
|
||||
ret = iter.buf[iter.head : i]
|
||||
ret = iter.buf[iter.head:i]
|
||||
iter.head = i + 1
|
||||
return ret
|
||||
}
|
||||
}
|
||||
readLen := iter.tail - iter.head
|
||||
copied := make([]byte, readLen, readLen * 2)
|
||||
copied := make([]byte, readLen, readLen*2)
|
||||
copy(copied, iter.buf[iter.head:iter.tail])
|
||||
iter.head = iter.tail
|
||||
for iter.Error == nil {
|
||||
@ -154,9 +133,11 @@ func (iter *Iterator) readU4() (ret rune) {
|
||||
return
|
||||
}
|
||||
if c >= '0' && c <= '9' {
|
||||
ret = ret * 16 + rune(c - '0')
|
||||
ret = ret*16 + rune(c-'0')
|
||||
} else if c >= 'a' && c <= 'f' {
|
||||
ret = ret * 16 + rune(c - 'a' + 10)
|
||||
ret = ret*16 + rune(c-'a'+10)
|
||||
} else if c >= 'A' && c <= 'F' {
|
||||
ret = ret*16 + rune(c-'A'+10)
|
||||
} else {
|
||||
iter.reportError("readU4", "expects 0~9 or a~f")
|
||||
return
|
||||
@ -178,14 +159,14 @@ const (
|
||||
mask3 = 0x0F // 0000 1111
|
||||
mask4 = 0x07 // 0000 0111
|
||||
|
||||
rune1Max = 1 << 7 - 1
|
||||
rune2Max = 1 << 11 - 1
|
||||
rune3Max = 1 << 16 - 1
|
||||
rune1Max = 1<<7 - 1
|
||||
rune2Max = 1<<11 - 1
|
||||
rune3Max = 1<<16 - 1
|
||||
|
||||
surrogateMin = 0xD800
|
||||
surrogateMax = 0xDFFF
|
||||
|
||||
maxRune = '\U0010FFFF' // Maximum valid Unicode code point.
|
||||
maxRune = '\U0010FFFF' // Maximum valid Unicode code point.
|
||||
runeError = '\uFFFD' // the "error" Rune or "Unicode replacement character"
|
||||
)
|
||||
|
||||
@ -196,22 +177,22 @@ func appendRune(p []byte, r rune) []byte {
|
||||
p = append(p, byte(r))
|
||||
return p
|
||||
case i <= rune2Max:
|
||||
p = append(p, t2 | byte(r >> 6))
|
||||
p = append(p, tx | byte(r) & maskx)
|
||||
p = append(p, t2|byte(r>>6))
|
||||
p = append(p, tx|byte(r)&maskx)
|
||||
return p
|
||||
case i > maxRune, surrogateMin <= i && i <= surrogateMax:
|
||||
r = runeError
|
||||
fallthrough
|
||||
case i <= rune3Max:
|
||||
p = append(p, t3 | byte(r >> 12))
|
||||
p = append(p, tx | byte(r >> 6) & maskx)
|
||||
p = append(p, tx | byte(r) & maskx)
|
||||
p = append(p, t3|byte(r>>12))
|
||||
p = append(p, tx|byte(r>>6)&maskx)
|
||||
p = append(p, tx|byte(r)&maskx)
|
||||
return p
|
||||
default:
|
||||
p = append(p, t4 | byte(r >> 18))
|
||||
p = append(p, tx | byte(r >> 12) & maskx)
|
||||
p = append(p, tx | byte(r >> 6) & maskx)
|
||||
p = append(p, tx | byte(r) & maskx)
|
||||
p = append(p, t4|byte(r>>18))
|
||||
p = append(p, tx|byte(r>>12)&maskx)
|
||||
p = append(p, tx|byte(r>>6)&maskx)
|
||||
p = append(p, tx|byte(r)&maskx)
|
||||
return p
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,32 +1,38 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"encoding"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"sync/atomic"
|
||||
"unsafe"
|
||||
"errors"
|
||||
)
|
||||
|
||||
/*
|
||||
Reflection on type to create decoders, which is then cached
|
||||
Reflection on value is avoided as we can, as the reflect.Value itself will allocate, with following exceptions
|
||||
1. create instance of new value, for example *int will need a int to be allocated
|
||||
2. append to slice, if the existing cap is not enough, allocate will be done using Reflect.New
|
||||
3. assignment to map, both key and value will be reflect.Value
|
||||
For a simple struct binding, it will be reflect.Value free and allocation free
|
||||
*/
|
||||
|
||||
// Decoder is an internal type registered to cache as needed.
|
||||
// Don't confuse jsoniter.Decoder with json.Decoder.
|
||||
// For json.Decoder's adapter, refer to jsoniter.AdapterDecoder(todo link).
|
||||
//
|
||||
// Reflection on type to create decoders, which is then cached
|
||||
// Reflection on value is avoided as we can, as the reflect.Value itself will allocate, with following exceptions
|
||||
// 1. create instance of new value, for example *int will need a int to be allocated
|
||||
// 2. append to slice, if the existing cap is not enough, allocate will be done using Reflect.New
|
||||
// 3. assignment to map, both key and value will be reflect.Value
|
||||
// For a simple struct binding, it will be reflect.Value free and allocation free
|
||||
type Decoder interface {
|
||||
decode(ptr unsafe.Pointer, iter *Iterator)
|
||||
}
|
||||
|
||||
// Encoder is an internal type registered to cache as needed.
|
||||
// Don't confuse jsoniter.Encoder with json.Encoder.
|
||||
// For json.Encoder's adapter, refer to jsoniter.AdapterEncoder(todo godoc link).
|
||||
type Encoder interface {
|
||||
isEmpty(ptr unsafe.Pointer) bool
|
||||
encode(ptr unsafe.Pointer, stream *Stream)
|
||||
encodeInterface(val interface{}, stream *Stream)
|
||||
}
|
||||
|
||||
func WriteToStream(val interface{}, stream *Stream, encoder Encoder) {
|
||||
func writeToStream(val interface{}, stream *Stream, encoder Encoder) {
|
||||
e := (*emptyInterface)(unsafe.Pointer(&val))
|
||||
if reflect.TypeOf(val).Kind() == reflect.Ptr {
|
||||
encoder.encode(unsafe.Pointer(&e.word), stream)
|
||||
@ -37,7 +43,7 @@ func WriteToStream(val interface{}, stream *Stream, encoder Encoder) {
|
||||
|
||||
type DecoderFunc func(ptr unsafe.Pointer, iter *Iterator)
|
||||
type EncoderFunc func(ptr unsafe.Pointer, stream *Stream)
|
||||
type ExtensionFunc func(typ reflect.Type, field *reflect.StructField) ([]string, DecoderFunc)
|
||||
type ExtensionFunc func(typ reflect.Type, field *reflect.StructField) ([]string, EncoderFunc, DecoderFunc)
|
||||
|
||||
type funcDecoder struct {
|
||||
fun DecoderFunc
|
||||
@ -56,7 +62,11 @@ func (encoder *funcEncoder) encode(ptr unsafe.Pointer, stream *Stream) {
|
||||
}
|
||||
|
||||
func (encoder *funcEncoder) encodeInterface(val interface{}, stream *Stream) {
|
||||
WriteToStream(val, stream, encoder)
|
||||
writeToStream(val, stream, encoder)
|
||||
}
|
||||
|
||||
func (encoder *funcEncoder) isEmpty(ptr unsafe.Pointer) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
var DECODERS unsafe.Pointer
|
||||
@ -67,7 +77,12 @@ var fieldDecoders map[string]Decoder
|
||||
var typeEncoders map[string]Encoder
|
||||
var fieldEncoders map[string]Encoder
|
||||
var extensions []ExtensionFunc
|
||||
var jsonNumberType reflect.Type
|
||||
var jsonRawMessageType reflect.Type
|
||||
var anyType reflect.Type
|
||||
var marshalerType reflect.Type
|
||||
var unmarshalerType reflect.Type
|
||||
var textUnmarshalerType reflect.Type
|
||||
|
||||
func init() {
|
||||
typeDecoders = map[string]Decoder{}
|
||||
@ -77,7 +92,12 @@ func init() {
|
||||
extensions = []ExtensionFunc{}
|
||||
atomic.StorePointer(&DECODERS, unsafe.Pointer(&map[string]Decoder{}))
|
||||
atomic.StorePointer(&ENCODERS, unsafe.Pointer(&map[string]Encoder{}))
|
||||
jsonNumberType = reflect.TypeOf((*json.Number)(nil)).Elem()
|
||||
jsonRawMessageType = reflect.TypeOf((*json.RawMessage)(nil)).Elem()
|
||||
anyType = reflect.TypeOf((*Any)(nil)).Elem()
|
||||
marshalerType = reflect.TypeOf((*json.Marshaler)(nil)).Elem()
|
||||
unmarshalerType = reflect.TypeOf((*json.Unmarshaler)(nil)).Elem()
|
||||
textUnmarshalerType = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem()
|
||||
}
|
||||
|
||||
func addDecoderToCache(cacheKey reflect.Type, decoder Decoder) {
|
||||
@ -143,10 +163,18 @@ func RegisterExtension(extension ExtensionFunc) {
|
||||
extensions = append(extensions, extension)
|
||||
}
|
||||
|
||||
// CleanDecoders cleans decoders registered
|
||||
// CleanDecoders cleans decoders registered or cached
|
||||
func CleanDecoders() {
|
||||
typeDecoders = map[string]Decoder{}
|
||||
fieldDecoders = map[string]Decoder{}
|
||||
atomic.StorePointer(&DECODERS, unsafe.Pointer(&map[string]Decoder{}))
|
||||
}
|
||||
|
||||
// CleanEncoders cleans encoders registered or cached
|
||||
func CleanEncoders() {
|
||||
typeEncoders = map[string]Encoder{}
|
||||
fieldEncoders = map[string]Encoder{}
|
||||
atomic.StorePointer(&ENCODERS, unsafe.Pointer(&map[string]Encoder{}))
|
||||
}
|
||||
|
||||
type optionalDecoder struct {
|
||||
@ -171,7 +199,6 @@ func (decoder *optionalDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
}
|
||||
|
||||
type optionalEncoder struct {
|
||||
valueType reflect.Type
|
||||
valueEncoder Encoder
|
||||
}
|
||||
|
||||
@ -184,92 +211,58 @@ func (encoder *optionalEncoder) encode(ptr unsafe.Pointer, stream *Stream) {
|
||||
}
|
||||
|
||||
func (encoder *optionalEncoder) encodeInterface(val interface{}, stream *Stream) {
|
||||
WriteToStream(val, stream, encoder)
|
||||
writeToStream(val, stream, encoder)
|
||||
}
|
||||
|
||||
type mapDecoder struct {
|
||||
mapType reflect.Type
|
||||
elemType reflect.Type
|
||||
elemDecoder Decoder
|
||||
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()
|
||||
|
||||
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())
|
||||
func (encoder *optionalEncoder) isEmpty(ptr unsafe.Pointer) bool {
|
||||
if *((*unsafe.Pointer)(ptr)) == nil {
|
||||
return true
|
||||
} else {
|
||||
return encoder.valueEncoder.isEmpty(*((*unsafe.Pointer)(ptr)))
|
||||
}
|
||||
}
|
||||
|
||||
type mapEncoder struct {
|
||||
mapType reflect.Type
|
||||
elemType reflect.Type
|
||||
elemEncoder Encoder
|
||||
mapInterface emptyInterface
|
||||
type placeholderEncoder struct {
|
||||
valueEncoder Encoder
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
stream.WriteObjectField(key.String())
|
||||
val := realVal.MapIndex(key).Interface()
|
||||
encoder.elemEncoder.encodeInterface(val, stream)
|
||||
}
|
||||
stream.WriteObjectEnd()
|
||||
func (encoder *placeholderEncoder) encode(ptr unsafe.Pointer, stream *Stream) {
|
||||
encoder.valueEncoder.encode(ptr, stream)
|
||||
}
|
||||
|
||||
func (encoder *mapEncoder) encodeInterface(val interface{}, stream *Stream) {
|
||||
WriteToStream(val, stream, encoder)
|
||||
func (encoder *placeholderEncoder) encodeInterface(val interface{}, stream *Stream) {
|
||||
writeToStream(val, stream, encoder)
|
||||
}
|
||||
|
||||
type mapInterfaceEncoder struct {
|
||||
mapType reflect.Type
|
||||
elemType reflect.Type
|
||||
elemEncoder Encoder
|
||||
mapInterface emptyInterface
|
||||
func (encoder *placeholderEncoder) isEmpty(ptr unsafe.Pointer) bool {
|
||||
return encoder.valueEncoder.isEmpty(ptr)
|
||||
}
|
||||
|
||||
func (encoder *mapInterfaceEncoder) 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()
|
||||
}
|
||||
stream.WriteObjectField(key.String())
|
||||
val := realVal.MapIndex(key).Interface()
|
||||
encoder.elemEncoder.encode(unsafe.Pointer(&val), stream)
|
||||
}
|
||||
stream.WriteObjectEnd()
|
||||
type placeholderDecoder struct {
|
||||
valueDecoder Decoder
|
||||
}
|
||||
|
||||
func (encoder *mapInterfaceEncoder) encodeInterface(val interface{}, stream *Stream) {
|
||||
WriteToStream(val, stream, encoder)
|
||||
func (decoder *placeholderDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
decoder.valueDecoder.decode(ptr, iter)
|
||||
}
|
||||
|
||||
// emptyInterface is the header for an interface{} value.
|
||||
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
|
||||
}
|
||||
|
||||
@ -291,7 +284,6 @@ func (iter *Iterator) ReadVal(obj interface{}) {
|
||||
cachedDecoder.decode(e.word, iter)
|
||||
}
|
||||
|
||||
|
||||
func (stream *Stream) WriteVal(val interface{}) {
|
||||
if nil == val {
|
||||
stream.WriteNil()
|
||||
@ -334,6 +326,42 @@ func decoderOfType(typ reflect.Type) (Decoder, error) {
|
||||
if typeDecoder != nil {
|
||||
return typeDecoder, nil
|
||||
}
|
||||
if typ.Kind() == reflect.Ptr {
|
||||
typeDecoder := typeDecoders[typ.Elem().String()]
|
||||
if typeDecoder != nil {
|
||||
return &optionalDecoder{typ.Elem(), typeDecoder}, nil
|
||||
}
|
||||
}
|
||||
cacheKey := typ
|
||||
cachedDecoder := getDecoderFromCache(cacheKey)
|
||||
if cachedDecoder != nil {
|
||||
return cachedDecoder, nil
|
||||
}
|
||||
placeholder := &placeholderDecoder{}
|
||||
addDecoderToCache(cacheKey, placeholder)
|
||||
newDecoder, err := createDecoderOfType(typ)
|
||||
placeholder.valueDecoder = newDecoder
|
||||
addDecoderToCache(cacheKey, newDecoder)
|
||||
return newDecoder, err
|
||||
}
|
||||
|
||||
func createDecoderOfType(typ reflect.Type) (Decoder, error) {
|
||||
if typ.String() == "[]uint8" {
|
||||
return &base64Codec{}, nil
|
||||
}
|
||||
if typ.AssignableTo(jsonRawMessageType) {
|
||||
return &jsonRawMessageCodec{}, nil
|
||||
}
|
||||
if typ.AssignableTo(jsonNumberType) {
|
||||
return &jsonNumberCodec{}, nil
|
||||
}
|
||||
if typ.ConvertibleTo(unmarshalerType) {
|
||||
templateInterface := reflect.New(typ).Elem().Interface()
|
||||
return &optionalDecoder{typ, &unmarshalerDecoder{extractInterface(templateInterface)}}, nil
|
||||
}
|
||||
if typ.ConvertibleTo(anyType) {
|
||||
return &anyCodec{}, nil
|
||||
}
|
||||
switch typ.Kind() {
|
||||
case reflect.String:
|
||||
return &stringCodec{}, nil
|
||||
@ -365,12 +393,12 @@ func decoderOfType(typ reflect.Type) (Decoder, error) {
|
||||
return &boolCodec{}, nil
|
||||
case reflect.Interface:
|
||||
if typ.NumMethod() == 0 {
|
||||
return &interfaceCodec{}, nil
|
||||
return &emptyInterfaceCodec{}, nil
|
||||
} else {
|
||||
return nil, errors.New("unsupportd type: " + typ.String())
|
||||
return &nonEmptyInterfaceCodec{}, nil
|
||||
}
|
||||
case reflect.Struct:
|
||||
return prefix(fmt.Sprintf("[%s]", typeName)).addToDecoder(decoderOfStruct(typ))
|
||||
return prefix(fmt.Sprintf("[%s]", typ.String())).addToDecoder(decoderOfStruct(typ))
|
||||
case reflect.Slice:
|
||||
return prefix("[slice]").addToDecoder(decoderOfSlice(typ))
|
||||
case reflect.Map:
|
||||
@ -383,15 +411,49 @@ func decoderOfType(typ reflect.Type) (Decoder, error) {
|
||||
}
|
||||
|
||||
func encoderOfType(typ reflect.Type) (Encoder, error) {
|
||||
if typ.ConvertibleTo(anyType) {
|
||||
return &anyCodec{}, nil
|
||||
}
|
||||
typeName := typ.String()
|
||||
typeEncoder := typeEncoders[typeName]
|
||||
if typeEncoder != nil {
|
||||
return typeEncoder, nil
|
||||
}
|
||||
switch typ.Kind() {
|
||||
if typ.Kind() == reflect.Ptr {
|
||||
typeEncoder := typeEncoders[typ.Elem().String()]
|
||||
if typeEncoder != nil {
|
||||
return &optionalEncoder{typeEncoder}, nil
|
||||
}
|
||||
}
|
||||
cacheKey := typ
|
||||
cachedEncoder := getEncoderFromCache(cacheKey)
|
||||
if cachedEncoder != nil {
|
||||
return cachedEncoder, nil
|
||||
}
|
||||
placeholder := &placeholderEncoder{}
|
||||
addEncoderToCache(cacheKey, placeholder)
|
||||
newEncoder, err := createEncoderOfType(typ)
|
||||
placeholder.valueEncoder = newEncoder
|
||||
addEncoderToCache(cacheKey, newEncoder)
|
||||
return newEncoder, err
|
||||
}
|
||||
|
||||
func createEncoderOfType(typ reflect.Type) (Encoder, error) {
|
||||
if typ.String() == "[]uint8" {
|
||||
return &base64Codec{}, nil
|
||||
}
|
||||
if typ.AssignableTo(jsonRawMessageType) {
|
||||
return &jsonRawMessageCodec{}, nil
|
||||
}
|
||||
if typ.AssignableTo(jsonNumberType) {
|
||||
return &jsonNumberCodec{}, nil
|
||||
}
|
||||
if typ.ConvertibleTo(marshalerType) {
|
||||
templateInterface := reflect.New(typ).Elem().Interface()
|
||||
return &marshalerEncoder{extractInterface(templateInterface)}, nil
|
||||
}
|
||||
if typ.ConvertibleTo(anyType) {
|
||||
return &anyCodec{}, nil
|
||||
}
|
||||
kind := typ.Kind()
|
||||
switch kind {
|
||||
case reflect.String:
|
||||
return &stringCodec{}, nil
|
||||
case reflect.Int:
|
||||
@ -421,9 +483,13 @@ func encoderOfType(typ reflect.Type) (Encoder, error) {
|
||||
case reflect.Bool:
|
||||
return &boolCodec{}, nil
|
||||
case reflect.Interface:
|
||||
return &interfaceCodec{}, nil
|
||||
if typ.NumMethod() == 0 {
|
||||
return &emptyInterfaceCodec{}, nil
|
||||
} else {
|
||||
return &nonEmptyInterfaceCodec{}, nil
|
||||
}
|
||||
case reflect.Struct:
|
||||
return prefix(fmt.Sprintf("[%s]", typeName)).addToEncoder(encoderOfStruct(typ))
|
||||
return prefix(fmt.Sprintf("[%s]", typ.String())).addToEncoder(encoderOfStruct(typ))
|
||||
case reflect.Slice:
|
||||
return prefix("[slice]").addToEncoder(encoderOfSlice(typ))
|
||||
case reflect.Map:
|
||||
@ -450,7 +516,7 @@ func encoderOfOptional(typ reflect.Type) (Encoder, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &optionalEncoder{elemType, decoder}, nil
|
||||
return &optionalEncoder{decoder}, nil
|
||||
}
|
||||
|
||||
func decoderOfMap(typ reflect.Type) (Decoder, error) {
|
||||
@ -459,7 +525,11 @@ func decoderOfMap(typ reflect.Type) (Decoder, error) {
|
||||
return nil, err
|
||||
}
|
||||
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 extractInterface(val interface{}) emptyInterface {
|
||||
return *((*emptyInterface)(unsafe.Pointer(&val)))
|
||||
}
|
||||
|
||||
func encoderOfMap(typ reflect.Type) (Encoder, error) {
|
||||
@ -469,9 +539,5 @@ func encoderOfMap(typ reflect.Type) (Encoder, error) {
|
||||
return nil, err
|
||||
}
|
||||
mapInterface := reflect.New(typ).Elem().Interface()
|
||||
if elemType.Kind() == reflect.Interface && elemType.NumMethod() == 0 {
|
||||
return &mapInterfaceEncoder{typ, elemType, encoder, *((*emptyInterface)(unsafe.Pointer(&mapInterface)))}, nil
|
||||
} else {
|
||||
return &mapEncoder{typ, elemType, encoder, *((*emptyInterface)(unsafe.Pointer(&mapInterface)))}, nil
|
||||
}
|
||||
return &mapEncoder{typ, elemType, encoder, *((*emptyInterface)(unsafe.Pointer(&mapInterface)))}, nil
|
||||
}
|
||||
|
@ -1,10 +1,10 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"unsafe"
|
||||
"reflect"
|
||||
"io"
|
||||
"fmt"
|
||||
"io"
|
||||
"reflect"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
func decoderOfSlice(typ reflect.Type) (Decoder, error) {
|
||||
@ -20,6 +20,9 @@ func encoderOfSlice(typ reflect.Type) (Encoder, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if typ.Elem().Kind() == reflect.Map {
|
||||
encoder = &optionalEncoder{encoder}
|
||||
}
|
||||
return &sliceEncoder{typ, typ.Elem(), encoder}, nil
|
||||
}
|
||||
|
||||
@ -50,7 +53,12 @@ func (encoder *sliceEncoder) encode(ptr unsafe.Pointer, stream *Stream) {
|
||||
}
|
||||
|
||||
func (encoder *sliceEncoder) encodeInterface(val interface{}, stream *Stream) {
|
||||
WriteToStream(val, stream, encoder)
|
||||
writeToStream(val, stream, encoder)
|
||||
}
|
||||
|
||||
func (encoder *sliceEncoder) isEmpty(ptr unsafe.Pointer) bool {
|
||||
slice := (*sliceHeader)(ptr)
|
||||
return slice.Len == 0
|
||||
}
|
||||
|
||||
type sliceDecoder struct {
|
||||
@ -80,30 +88,30 @@ func (decoder *sliceDecoder) doDecode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
return
|
||||
}
|
||||
offset := uintptr(0)
|
||||
decoder.elemDecoder.decode(unsafe.Pointer(uintptr(slice.Data) + offset), iter)
|
||||
decoder.elemDecoder.decode(unsafe.Pointer(uintptr(slice.Data)+offset), iter)
|
||||
if !iter.ReadArray() {
|
||||
slice.Len = 1
|
||||
return
|
||||
}
|
||||
offset += decoder.elemType.Size()
|
||||
decoder.elemDecoder.decode(unsafe.Pointer(uintptr(slice.Data) + offset), iter)
|
||||
decoder.elemDecoder.decode(unsafe.Pointer(uintptr(slice.Data)+offset), iter)
|
||||
if !iter.ReadArray() {
|
||||
slice.Len = 2
|
||||
return
|
||||
}
|
||||
offset += decoder.elemType.Size()
|
||||
decoder.elemDecoder.decode(unsafe.Pointer(uintptr(slice.Data) + offset), iter)
|
||||
decoder.elemDecoder.decode(unsafe.Pointer(uintptr(slice.Data)+offset), iter)
|
||||
if !iter.ReadArray() {
|
||||
slice.Len = 3
|
||||
return
|
||||
}
|
||||
offset += decoder.elemType.Size()
|
||||
decoder.elemDecoder.decode(unsafe.Pointer(uintptr(slice.Data) + offset), iter)
|
||||
decoder.elemDecoder.decode(unsafe.Pointer(uintptr(slice.Data)+offset), iter)
|
||||
slice.Len = 4
|
||||
for iter.ReadArray() {
|
||||
growOne(slice, decoder.sliceType, decoder.elemType)
|
||||
offset += decoder.elemType.Size()
|
||||
decoder.elemDecoder.decode(unsafe.Pointer(uintptr(slice.Data) + offset), iter)
|
||||
decoder.elemDecoder.decode(unsafe.Pointer(uintptr(slice.Data)+offset), iter)
|
||||
}
|
||||
}
|
||||
|
||||
@ -147,4 +155,4 @@ func reuseSlice(slice *sliceHeader, sliceType reflect.Type, expectedCap int) {
|
||||
dst := unsafe.Pointer(reflect.MakeSlice(sliceType, 0, expectedCap).Pointer())
|
||||
slice.Cap = expectedCap
|
||||
slice.Data = dst
|
||||
}
|
||||
}
|
||||
|
138
feature_reflect_map.go
Normal file
138
feature_reflect_map.go
Normal file
@ -0,0 +1,138 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"encoding"
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
type mapDecoder struct {
|
||||
mapType reflect.Type
|
||||
keyType reflect.Type
|
||||
elemType reflect.Type
|
||||
elemDecoder Decoder
|
||||
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 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
|
||||
switch {
|
||||
case keyType.Kind() == reflect.String:
|
||||
realVal.SetMapIndex(reflect.ValueOf(keyStr), elem.Elem())
|
||||
return true
|
||||
case keyType.Implements(textUnmarshalerType):
|
||||
textUnmarshaler := reflect.New(keyType.Elem()).Interface().(encoding.TextUnmarshaler)
|
||||
err := textUnmarshaler.UnmarshalText([]byte(keyStr))
|
||||
if err != nil {
|
||||
iter.reportError("read map key as TextUnmarshaler", err.Error())
|
||||
return false
|
||||
}
|
||||
realVal.SetMapIndex(reflect.ValueOf(textUnmarshaler), elem.Elem())
|
||||
return true
|
||||
default:
|
||||
switch keyType.Kind() {
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
n, err := strconv.ParseInt(keyStr, 10, 64)
|
||||
if err != nil || reflect.Zero(keyType).OverflowInt(n) {
|
||||
iter.reportError("read map key as int64", "read int64 failed")
|
||||
return false
|
||||
}
|
||||
realVal.SetMapIndex(reflect.ValueOf(n).Convert(keyType), elem.Elem())
|
||||
return true
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
||||
n, err := strconv.ParseUint(keyStr, 10, 64)
|
||||
if err != nil || reflect.Zero(keyType).OverflowUint(n) {
|
||||
iter.reportError("read map key as uint64", "read uint64 failed")
|
||||
return false
|
||||
}
|
||||
realVal.SetMapIndex(reflect.ValueOf(n).Convert(keyType), elem.Elem())
|
||||
return true
|
||||
}
|
||||
}
|
||||
iter.reportError("read map key", "unexpected map key type "+keyType.String())
|
||||
return true
|
||||
})
|
||||
}
|
||||
|
||||
type mapEncoder struct {
|
||||
mapType reflect.Type
|
||||
elemType reflect.Type
|
||||
elemEncoder Encoder
|
||||
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
|
||||
}
|
@ -1,6 +1,8 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
@ -12,11 +14,16 @@ func (codec *stringCodec) decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
}
|
||||
|
||||
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) {
|
||||
WriteToStream(val, stream, encoder)
|
||||
writeToStream(val, stream, encoder)
|
||||
}
|
||||
|
||||
func (codec *stringCodec) isEmpty(ptr unsafe.Pointer) bool {
|
||||
return *((*string)(ptr)) == ""
|
||||
}
|
||||
|
||||
type intCodec struct {
|
||||
@ -31,7 +38,11 @@ func (codec *intCodec) encode(ptr unsafe.Pointer, 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 int8Codec struct {
|
||||
@ -46,7 +57,11 @@ func (codec *int8Codec) encode(ptr unsafe.Pointer, 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 {
|
||||
@ -61,7 +76,11 @@ func (codec *int16Codec) encode(ptr unsafe.Pointer, 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 {
|
||||
@ -76,7 +95,11 @@ func (codec *int32Codec) encode(ptr unsafe.Pointer, 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 {
|
||||
@ -91,7 +114,11 @@ func (codec *int64Codec) encode(ptr unsafe.Pointer, 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 {
|
||||
@ -106,7 +133,11 @@ func (codec *uintCodec) encode(ptr unsafe.Pointer, 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 {
|
||||
@ -121,7 +152,11 @@ func (codec *uint8Codec) encode(ptr unsafe.Pointer, 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 {
|
||||
@ -136,7 +171,11 @@ func (codec *uint16Codec) encode(ptr unsafe.Pointer, 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 {
|
||||
@ -151,7 +190,11 @@ func (codec *uint32Codec) encode(ptr unsafe.Pointer, 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 {
|
||||
@ -166,7 +209,11 @@ func (codec *uint64Codec) encode(ptr unsafe.Pointer, 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 {
|
||||
@ -181,7 +228,11 @@ func (codec *float32Codec) encode(ptr unsafe.Pointer, 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 {
|
||||
@ -196,7 +247,11 @@ func (codec *float64Codec) encode(ptr unsafe.Pointer, 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 {
|
||||
@ -211,24 +266,67 @@ func (codec *boolCodec) encode(ptr unsafe.Pointer, 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()
|
||||
}
|
||||
|
||||
func (codec *interfaceCodec) encode(ptr unsafe.Pointer, stream *Stream) {
|
||||
func (codec *emptyInterfaceCodec) encode(ptr unsafe.Pointer, stream *Stream) {
|
||||
stream.WriteVal(*((*interface{})(ptr)))
|
||||
}
|
||||
|
||||
func (encoder *interfaceCodec) encodeInterface(val interface{}, stream *Stream) {
|
||||
func (encoder *emptyInterfaceCodec) encodeInterface(val interface{}, stream *Stream) {
|
||||
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 {
|
||||
}
|
||||
|
||||
@ -244,6 +342,91 @@ func (encoder *anyCodec) encodeInterface(val interface{}, stream *Stream) {
|
||||
(val.(Any)).WriteTo(stream)
|
||||
}
|
||||
|
||||
func (encoder *anyCodec) isEmpty(ptr unsafe.Pointer) bool {
|
||||
return (*((*Any)(ptr))).Size() == 0
|
||||
}
|
||||
|
||||
type jsonNumberCodec struct {
|
||||
}
|
||||
|
||||
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 base64Codec struct {
|
||||
}
|
||||
|
||||
func (codec *base64Codec) decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
encoding := base64.StdEncoding
|
||||
src := iter.SkipAndReturnBytes()
|
||||
src = src[1 : len(src)-1]
|
||||
decodedLen := encoding.DecodedLen(len(src))
|
||||
dst := make([]byte, decodedLen)
|
||||
_, err := encoding.Decode(dst, src)
|
||||
if err != nil {
|
||||
iter.reportError("decode base64", err.Error())
|
||||
} else {
|
||||
*((*[]byte)(ptr)) = dst
|
||||
}
|
||||
}
|
||||
|
||||
func (codec *base64Codec) encode(ptr unsafe.Pointer, stream *Stream) {
|
||||
encoding := base64.StdEncoding
|
||||
stream.writeByte('"')
|
||||
src := *((*[]byte)(ptr))
|
||||
toGrow := encoding.EncodedLen(len(src))
|
||||
stream.ensure(toGrow)
|
||||
encoding.Encode(stream.buf[stream.n:], src)
|
||||
stream.n += toGrow
|
||||
stream.writeByte('"')
|
||||
}
|
||||
|
||||
func (encoder *base64Codec) encodeInterface(val interface{}, stream *Stream) {
|
||||
encoding := base64.StdEncoding
|
||||
stream.writeByte('"')
|
||||
src := val.([]byte)
|
||||
toGrow := encoding.EncodedLen(len(src))
|
||||
stream.ensure(toGrow)
|
||||
encoding.Encode(stream.buf[stream.n:], src)
|
||||
stream.n += toGrow
|
||||
stream.writeByte('"')
|
||||
}
|
||||
|
||||
func (encoder *base64Codec) isEmpty(ptr unsafe.Pointer) bool {
|
||||
return len(*((*[]byte)(ptr))) == 0
|
||||
}
|
||||
|
||||
type stringNumberDecoder struct {
|
||||
elemDecoder Decoder
|
||||
}
|
||||
@ -263,4 +446,53 @@ func (decoder *stringNumberDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
iter.reportError("stringNumberDecoder", `expect "`)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type marshalerEncoder struct {
|
||||
templateInterface emptyInterface
|
||||
}
|
||||
|
||||
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 {
|
||||
templateInterface := encoder.templateInterface
|
||||
templateInterface.word = ptr
|
||||
realInterface := (*interface{})(unsafe.Pointer(&templateInterface))
|
||||
marshaler := (*realInterface).(json.Marshaler)
|
||||
bytes, err := marshaler.MarshalJSON()
|
||||
if err != nil {
|
||||
return true
|
||||
} else {
|
||||
return len(bytes) > 0
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
bytes := iter.SkipAndReturnBytes()
|
||||
err := unmarshaler.UnmarshalJSON(bytes)
|
||||
if err != nil {
|
||||
iter.reportError("unmarshaler", err.Error())
|
||||
}
|
||||
}
|
||||
|
@ -1,71 +1,86 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"io"
|
||||
"fmt"
|
||||
"io"
|
||||
"reflect"
|
||||
"unsafe"
|
||||
"strings"
|
||||
"unicode"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
|
||||
func encoderOfStruct(typ reflect.Type) (Encoder, error) {
|
||||
structEncoder_ := &structEncoder{}
|
||||
for i := 0; i < typ.NumField(); i++ {
|
||||
field := typ.Field(i)
|
||||
var fieldNames []string
|
||||
fields := map[string]*structFieldEncoder{}
|
||||
for _, field := range listStructFields(typ) {
|
||||
fieldEncoderKey := fmt.Sprintf("%s/%s", typ.String(), field.Name)
|
||||
var extensionProvidedFieldNames []string
|
||||
for _, extension := range extensions {
|
||||
alternativeFieldNames, _ := extension(typ, &field)
|
||||
alternativeFieldNames, fun, _ := extension(typ, field)
|
||||
if alternativeFieldNames != nil {
|
||||
fieldNames = alternativeFieldNames
|
||||
extensionProvidedFieldNames = alternativeFieldNames
|
||||
}
|
||||
if fun != nil {
|
||||
fieldEncoders[fieldEncoderKey] = &funcEncoder{fun}
|
||||
}
|
||||
}
|
||||
tagParts := strings.Split(field.Tag.Get("json"), ",")
|
||||
// if fieldNames set by extension, use theirs, otherwise try tags
|
||||
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]}
|
||||
fieldNames := calcFieldNames(field.Name, tagParts[0], extensionProvidedFieldNames)
|
||||
omitempty := false
|
||||
for _, tagPart := range tagParts {
|
||||
if tagPart == "omitempty" {
|
||||
omitempty = true
|
||||
}
|
||||
}
|
||||
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}
|
||||
encoder := fieldEncoders[fieldEncoderKey]
|
||||
var err error
|
||||
if encoder == nil && len(fieldNames) > 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
|
||||
if field.Type.Kind() == reflect.Map {
|
||||
encoder = &optionalEncoder{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})
|
||||
}
|
||||
fields[fieldName] = &structFieldEncoder{field, fieldName, encoder, omitempty}
|
||||
}
|
||||
}
|
||||
if structEncoder_.firstField == nil {
|
||||
if len(fields) == 0 {
|
||||
return &emptyStructEncoder{}, nil
|
||||
}
|
||||
for _, field := range fields {
|
||||
structEncoder_.fields = append(structEncoder_.fields, field)
|
||||
}
|
||||
return structEncoder_, nil
|
||||
}
|
||||
|
||||
func listStructFields(typ reflect.Type) []*reflect.StructField {
|
||||
fields := []*reflect.StructField{}
|
||||
for i := 0; i < typ.NumField(); i++ {
|
||||
field := typ.Field(i)
|
||||
if field.Anonymous {
|
||||
fields = append(fields, listStructFields(field.Type)...)
|
||||
} else {
|
||||
fields = append(fields, &field)
|
||||
}
|
||||
}
|
||||
return fields
|
||||
}
|
||||
|
||||
func decoderOfStruct(typ reflect.Type) (Decoder, error) {
|
||||
fields := map[string]*structFieldDecoder{}
|
||||
for i := 0; i < typ.NumField(); i++ {
|
||||
field := typ.Field(i)
|
||||
fieldDecoderKey := fmt.Sprintf("%s/%s", typ.String(), field.Name)
|
||||
var fieldNames []string
|
||||
var extensionProviedFieldNames []string
|
||||
for _, extension := range extensions {
|
||||
alternativeFieldNames, fun := extension(typ, &field)
|
||||
alternativeFieldNames, _, fun := extension(typ, &field)
|
||||
if alternativeFieldNames != nil {
|
||||
fieldNames = alternativeFieldNames
|
||||
extensionProviedFieldNames = alternativeFieldNames
|
||||
}
|
||||
if fun != nil {
|
||||
fieldDecoders[fieldDecoderKey] = &funcDecoder{fun}
|
||||
@ -73,19 +88,8 @@ func decoderOfStruct(typ reflect.Type) (Decoder, error) {
|
||||
}
|
||||
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 {
|
||||
fieldNames := calcFieldNames(field.Name, tagParts[0], extensionProviedFieldNames)
|
||||
if decoder == nil && len(fieldNames) > 0 {
|
||||
var err error
|
||||
decoder, err = decoderOfType(field.Type)
|
||||
if err != nil {
|
||||
@ -102,9 +106,39 @@ func decoderOfStruct(typ reflect.Type) (Decoder, error) {
|
||||
return createStructDecoder(typ, fields)
|
||||
}
|
||||
|
||||
func calcFieldNames(originalFieldName string, tagProvidedFieldName string, extensionProvidedFieldNames []string) []string {
|
||||
// tag => extension => exported? => original
|
||||
isNotExported := unicode.IsLower(rune(originalFieldName[0]))
|
||||
var fieldNames []string
|
||||
/// tagParts[0] always present, even if no tags
|
||||
switch tagProvidedFieldName {
|
||||
case "":
|
||||
if extensionProvidedFieldNames != nil {
|
||||
fieldNames = extensionProvidedFieldNames
|
||||
} else {
|
||||
if isNotExported {
|
||||
fieldNames = []string{}
|
||||
} else {
|
||||
fieldNames = []string{originalFieldName}
|
||||
}
|
||||
}
|
||||
case "-":
|
||||
fieldNames = []string{}
|
||||
default:
|
||||
fieldNames = []string{tagProvidedFieldName}
|
||||
}
|
||||
return fieldNames
|
||||
}
|
||||
|
||||
func EnableUnexportedStructFieldsSupport() {
|
||||
RegisterExtension(func(type_ reflect.Type, field *reflect.StructField) ([]string, EncoderFunc, DecoderFunc) {
|
||||
return []string{field.Name}, nil, nil
|
||||
})
|
||||
}
|
||||
|
||||
func createStructDecoder(typ reflect.Type, fields map[string]*structFieldDecoder) (Decoder, error) {
|
||||
knownHash := map[int32]struct{}{
|
||||
0: struct{}{},
|
||||
0: {},
|
||||
}
|
||||
switch len(fields) {
|
||||
case 0:
|
||||
@ -591,11 +625,11 @@ func (decoder *oneFieldStructDecoder) decode(ptr unsafe.Pointer, iter *Iterator)
|
||||
}
|
||||
|
||||
type twoFieldsStructDecoder struct {
|
||||
typ reflect.Type
|
||||
fieldHash1 int32
|
||||
fieldDecoder1 *structFieldDecoder
|
||||
fieldHash2 int32
|
||||
fieldDecoder2 *structFieldDecoder
|
||||
typ reflect.Type
|
||||
fieldHash1 int32
|
||||
fieldDecoder1 *structFieldDecoder
|
||||
fieldHash2 int32
|
||||
fieldDecoder2 *structFieldDecoder
|
||||
}
|
||||
|
||||
func (decoder *twoFieldsStructDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
@ -621,13 +655,13 @@ func (decoder *twoFieldsStructDecoder) decode(ptr unsafe.Pointer, iter *Iterator
|
||||
}
|
||||
|
||||
type threeFieldsStructDecoder struct {
|
||||
typ reflect.Type
|
||||
fieldHash1 int32
|
||||
fieldDecoder1 *structFieldDecoder
|
||||
fieldHash2 int32
|
||||
fieldDecoder2 *structFieldDecoder
|
||||
fieldHash3 int32
|
||||
fieldDecoder3 *structFieldDecoder
|
||||
typ reflect.Type
|
||||
fieldHash1 int32
|
||||
fieldDecoder1 *structFieldDecoder
|
||||
fieldHash2 int32
|
||||
fieldDecoder2 *structFieldDecoder
|
||||
fieldHash3 int32
|
||||
fieldDecoder3 *structFieldDecoder
|
||||
}
|
||||
|
||||
func (decoder *threeFieldsStructDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
@ -655,15 +689,15 @@ func (decoder *threeFieldsStructDecoder) decode(ptr unsafe.Pointer, iter *Iterat
|
||||
}
|
||||
|
||||
type fourFieldsStructDecoder struct {
|
||||
typ reflect.Type
|
||||
fieldHash1 int32
|
||||
fieldDecoder1 *structFieldDecoder
|
||||
fieldHash2 int32
|
||||
fieldDecoder2 *structFieldDecoder
|
||||
fieldHash3 int32
|
||||
fieldDecoder3 *structFieldDecoder
|
||||
fieldHash4 int32
|
||||
fieldDecoder4 *structFieldDecoder
|
||||
typ reflect.Type
|
||||
fieldHash1 int32
|
||||
fieldDecoder1 *structFieldDecoder
|
||||
fieldHash2 int32
|
||||
fieldDecoder2 *structFieldDecoder
|
||||
fieldHash3 int32
|
||||
fieldDecoder3 *structFieldDecoder
|
||||
fieldHash4 int32
|
||||
fieldDecoder4 *structFieldDecoder
|
||||
}
|
||||
|
||||
func (decoder *fourFieldsStructDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
@ -693,17 +727,17 @@ func (decoder *fourFieldsStructDecoder) decode(ptr unsafe.Pointer, iter *Iterato
|
||||
}
|
||||
|
||||
type fiveFieldsStructDecoder struct {
|
||||
typ reflect.Type
|
||||
fieldHash1 int32
|
||||
fieldDecoder1 *structFieldDecoder
|
||||
fieldHash2 int32
|
||||
fieldDecoder2 *structFieldDecoder
|
||||
fieldHash3 int32
|
||||
fieldDecoder3 *structFieldDecoder
|
||||
fieldHash4 int32
|
||||
fieldDecoder4 *structFieldDecoder
|
||||
fieldHash5 int32
|
||||
fieldDecoder5 *structFieldDecoder
|
||||
typ reflect.Type
|
||||
fieldHash1 int32
|
||||
fieldDecoder1 *structFieldDecoder
|
||||
fieldHash2 int32
|
||||
fieldDecoder2 *structFieldDecoder
|
||||
fieldHash3 int32
|
||||
fieldDecoder3 *structFieldDecoder
|
||||
fieldHash4 int32
|
||||
fieldDecoder4 *structFieldDecoder
|
||||
fieldHash5 int32
|
||||
fieldDecoder5 *structFieldDecoder
|
||||
}
|
||||
|
||||
func (decoder *fiveFieldsStructDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
@ -735,19 +769,19 @@ func (decoder *fiveFieldsStructDecoder) decode(ptr unsafe.Pointer, iter *Iterato
|
||||
}
|
||||
|
||||
type sixFieldsStructDecoder struct {
|
||||
typ reflect.Type
|
||||
fieldHash1 int32
|
||||
fieldDecoder1 *structFieldDecoder
|
||||
fieldHash2 int32
|
||||
fieldDecoder2 *structFieldDecoder
|
||||
fieldHash3 int32
|
||||
fieldDecoder3 *structFieldDecoder
|
||||
fieldHash4 int32
|
||||
fieldDecoder4 *structFieldDecoder
|
||||
fieldHash5 int32
|
||||
fieldDecoder5 *structFieldDecoder
|
||||
fieldHash6 int32
|
||||
fieldDecoder6 *structFieldDecoder
|
||||
typ reflect.Type
|
||||
fieldHash1 int32
|
||||
fieldDecoder1 *structFieldDecoder
|
||||
fieldHash2 int32
|
||||
fieldDecoder2 *structFieldDecoder
|
||||
fieldHash3 int32
|
||||
fieldDecoder3 *structFieldDecoder
|
||||
fieldHash4 int32
|
||||
fieldDecoder4 *structFieldDecoder
|
||||
fieldHash5 int32
|
||||
fieldDecoder5 *structFieldDecoder
|
||||
fieldHash6 int32
|
||||
fieldDecoder6 *structFieldDecoder
|
||||
}
|
||||
|
||||
func (decoder *sixFieldsStructDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
@ -781,21 +815,21 @@ func (decoder *sixFieldsStructDecoder) decode(ptr unsafe.Pointer, iter *Iterator
|
||||
}
|
||||
|
||||
type sevenFieldsStructDecoder struct {
|
||||
typ reflect.Type
|
||||
fieldHash1 int32
|
||||
fieldDecoder1 *structFieldDecoder
|
||||
fieldHash2 int32
|
||||
fieldDecoder2 *structFieldDecoder
|
||||
fieldHash3 int32
|
||||
fieldDecoder3 *structFieldDecoder
|
||||
fieldHash4 int32
|
||||
fieldDecoder4 *structFieldDecoder
|
||||
fieldHash5 int32
|
||||
fieldDecoder5 *structFieldDecoder
|
||||
fieldHash6 int32
|
||||
fieldDecoder6 *structFieldDecoder
|
||||
fieldHash7 int32
|
||||
fieldDecoder7 *structFieldDecoder
|
||||
typ reflect.Type
|
||||
fieldHash1 int32
|
||||
fieldDecoder1 *structFieldDecoder
|
||||
fieldHash2 int32
|
||||
fieldDecoder2 *structFieldDecoder
|
||||
fieldHash3 int32
|
||||
fieldDecoder3 *structFieldDecoder
|
||||
fieldHash4 int32
|
||||
fieldDecoder4 *structFieldDecoder
|
||||
fieldHash5 int32
|
||||
fieldDecoder5 *structFieldDecoder
|
||||
fieldHash6 int32
|
||||
fieldDecoder6 *structFieldDecoder
|
||||
fieldHash7 int32
|
||||
fieldDecoder7 *structFieldDecoder
|
||||
}
|
||||
|
||||
func (decoder *sevenFieldsStructDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
@ -831,23 +865,23 @@ func (decoder *sevenFieldsStructDecoder) decode(ptr unsafe.Pointer, iter *Iterat
|
||||
}
|
||||
|
||||
type eightFieldsStructDecoder struct {
|
||||
typ reflect.Type
|
||||
fieldHash1 int32
|
||||
fieldDecoder1 *structFieldDecoder
|
||||
fieldHash2 int32
|
||||
fieldDecoder2 *structFieldDecoder
|
||||
fieldHash3 int32
|
||||
fieldDecoder3 *structFieldDecoder
|
||||
fieldHash4 int32
|
||||
fieldDecoder4 *structFieldDecoder
|
||||
fieldHash5 int32
|
||||
fieldDecoder5 *structFieldDecoder
|
||||
fieldHash6 int32
|
||||
fieldDecoder6 *structFieldDecoder
|
||||
fieldHash7 int32
|
||||
fieldDecoder7 *structFieldDecoder
|
||||
fieldHash8 int32
|
||||
fieldDecoder8 *structFieldDecoder
|
||||
typ reflect.Type
|
||||
fieldHash1 int32
|
||||
fieldDecoder1 *structFieldDecoder
|
||||
fieldHash2 int32
|
||||
fieldDecoder2 *structFieldDecoder
|
||||
fieldHash3 int32
|
||||
fieldDecoder3 *structFieldDecoder
|
||||
fieldHash4 int32
|
||||
fieldDecoder4 *structFieldDecoder
|
||||
fieldHash5 int32
|
||||
fieldDecoder5 *structFieldDecoder
|
||||
fieldHash6 int32
|
||||
fieldDecoder6 *structFieldDecoder
|
||||
fieldHash7 int32
|
||||
fieldDecoder7 *structFieldDecoder
|
||||
fieldHash8 int32
|
||||
fieldDecoder8 *structFieldDecoder
|
||||
}
|
||||
|
||||
func (decoder *eightFieldsStructDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
@ -885,25 +919,25 @@ func (decoder *eightFieldsStructDecoder) decode(ptr unsafe.Pointer, iter *Iterat
|
||||
}
|
||||
|
||||
type nineFieldsStructDecoder struct {
|
||||
typ reflect.Type
|
||||
fieldHash1 int32
|
||||
fieldDecoder1 *structFieldDecoder
|
||||
fieldHash2 int32
|
||||
fieldDecoder2 *structFieldDecoder
|
||||
fieldHash3 int32
|
||||
fieldDecoder3 *structFieldDecoder
|
||||
fieldHash4 int32
|
||||
fieldDecoder4 *structFieldDecoder
|
||||
fieldHash5 int32
|
||||
fieldDecoder5 *structFieldDecoder
|
||||
fieldHash6 int32
|
||||
fieldDecoder6 *structFieldDecoder
|
||||
fieldHash7 int32
|
||||
fieldDecoder7 *structFieldDecoder
|
||||
fieldHash8 int32
|
||||
fieldDecoder8 *structFieldDecoder
|
||||
fieldHash9 int32
|
||||
fieldDecoder9 *structFieldDecoder
|
||||
typ reflect.Type
|
||||
fieldHash1 int32
|
||||
fieldDecoder1 *structFieldDecoder
|
||||
fieldHash2 int32
|
||||
fieldDecoder2 *structFieldDecoder
|
||||
fieldHash3 int32
|
||||
fieldDecoder3 *structFieldDecoder
|
||||
fieldHash4 int32
|
||||
fieldDecoder4 *structFieldDecoder
|
||||
fieldHash5 int32
|
||||
fieldDecoder5 *structFieldDecoder
|
||||
fieldHash6 int32
|
||||
fieldDecoder6 *structFieldDecoder
|
||||
fieldHash7 int32
|
||||
fieldDecoder7 *structFieldDecoder
|
||||
fieldHash8 int32
|
||||
fieldDecoder8 *structFieldDecoder
|
||||
fieldHash9 int32
|
||||
fieldDecoder9 *structFieldDecoder
|
||||
}
|
||||
|
||||
func (decoder *nineFieldsStructDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
@ -1021,6 +1055,7 @@ type structFieldEncoder struct {
|
||||
field *reflect.StructField
|
||||
fieldName string
|
||||
fieldEncoder Encoder
|
||||
omitempty bool
|
||||
}
|
||||
|
||||
func (encoder *structFieldEncoder) encode(ptr unsafe.Pointer, stream *Stream) {
|
||||
@ -1033,29 +1068,63 @@ func (encoder *structFieldEncoder) encode(ptr unsafe.Pointer, 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 {
|
||||
fieldPtr := uintptr(ptr) + encoder.field.Offset
|
||||
return encoder.fieldEncoder.isEmpty(unsafe.Pointer(fieldPtr))
|
||||
}
|
||||
|
||||
type structEncoder struct {
|
||||
firstField *structFieldEncoder
|
||||
fields []*structFieldEncoder
|
||||
}
|
||||
|
||||
func (encoder *structEncoder) encode(ptr unsafe.Pointer, stream *Stream) {
|
||||
stream.WriteObjectStart()
|
||||
encoder.firstField.encode(ptr, stream)
|
||||
isNotFirst := false
|
||||
for _, field := range encoder.fields {
|
||||
stream.WriteMore()
|
||||
if field.omitempty && field.isEmpty(ptr) {
|
||||
continue
|
||||
}
|
||||
if isNotFirst {
|
||||
stream.WriteMore()
|
||||
}
|
||||
field.encode(ptr, stream)
|
||||
isNotFirst = true
|
||||
}
|
||||
stream.WriteObjectEnd()
|
||||
}
|
||||
|
||||
func (encoder *structEncoder) encodeInterface(val interface{}, stream *Stream) {
|
||||
WriteToStream(val, stream, encoder)
|
||||
var encoderToUse Encoder
|
||||
encoderToUse = encoder
|
||||
if len(encoder.fields) == 1 {
|
||||
firstEncoder := encoder.fields[0].fieldEncoder
|
||||
firstEncoderName := reflect.TypeOf(firstEncoder).String()
|
||||
// interface{} has inline optimization for this case
|
||||
if firstEncoderName == "*jsoniter.optionalEncoder" {
|
||||
encoderToUse = &structEncoder{
|
||||
fields: []*structFieldEncoder{{
|
||||
field: encoder.fields[0].field,
|
||||
fieldName: encoder.fields[0].fieldName,
|
||||
fieldEncoder: firstEncoder.(*optionalEncoder).valueEncoder,
|
||||
omitempty: encoder.fields[0].omitempty,
|
||||
}},
|
||||
}
|
||||
}
|
||||
}
|
||||
writeToStream(val, stream, encoderToUse)
|
||||
}
|
||||
|
||||
func (encoder *structEncoder) isEmpty(ptr unsafe.Pointer) bool {
|
||||
for _, field := range encoder.fields {
|
||||
if !field.isEmpty(ptr) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
type emptyStructEncoder struct {
|
||||
}
|
||||
@ -1065,5 +1134,9 @@ func (encoder *emptyStructEncoder) encode(ptr unsafe.Pointer, 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 true
|
||||
}
|
||||
|
@ -32,24 +32,32 @@ func (b *Stream) Buffered() int {
|
||||
return b.n
|
||||
}
|
||||
|
||||
func (b *Stream) Buffer() []byte {
|
||||
return b.buf[:b.n]
|
||||
}
|
||||
|
||||
// Write writes the contents of p into the buffer.
|
||||
// It returns the number of bytes written.
|
||||
// If nn < len(p), it also returns an error explaining
|
||||
// why the write is short.
|
||||
func (b *Stream) Write(p []byte) (nn int, err error) {
|
||||
for len(p) > b.Available() && b.Error == nil {
|
||||
var n int
|
||||
if b.Buffered() == 0 {
|
||||
// Large write, empty buffer.
|
||||
// Write directly from p to avoid copy.
|
||||
n, b.Error = b.out.Write(p)
|
||||
if b.out == nil {
|
||||
b.growAtLeast(len(p))
|
||||
} else {
|
||||
n = copy(b.buf[b.n:], p)
|
||||
b.n += n
|
||||
b.Flush()
|
||||
var n int
|
||||
if b.Buffered() == 0 {
|
||||
// Large write, empty buffer.
|
||||
// Write directly from p to avoid copy.
|
||||
n, b.Error = b.out.Write(p)
|
||||
} else {
|
||||
n = copy(b.buf[b.n:], p)
|
||||
b.n += n
|
||||
b.Flush()
|
||||
}
|
||||
nn += n
|
||||
p = p[n:]
|
||||
}
|
||||
nn += n
|
||||
p = p[n:]
|
||||
}
|
||||
if b.Error != nil {
|
||||
return nn, b.Error
|
||||
@ -60,14 +68,13 @@ func (b *Stream) Write(p []byte) (nn int, err error) {
|
||||
return nn, nil
|
||||
}
|
||||
|
||||
|
||||
// WriteByte writes a single byte.
|
||||
func (b *Stream) writeByte(c byte) {
|
||||
if b.Error != nil {
|
||||
return
|
||||
}
|
||||
if b.Available() <= 0 && b.Flush() != nil {
|
||||
return
|
||||
if b.Available() < 1 {
|
||||
b.growAtLeast(1)
|
||||
}
|
||||
b.buf[b.n] = c
|
||||
b.n++
|
||||
@ -77,11 +84,11 @@ func (b *Stream) writeTwoBytes(c1 byte, c2 byte) {
|
||||
if b.Error != nil {
|
||||
return
|
||||
}
|
||||
if b.Available() <= 1 && b.Flush() != nil {
|
||||
return
|
||||
if b.Available() < 2 {
|
||||
b.growAtLeast(2)
|
||||
}
|
||||
b.buf[b.n] = c1
|
||||
b.buf[b.n + 1] = c2
|
||||
b.buf[b.n+1] = c2
|
||||
b.n += 2
|
||||
}
|
||||
|
||||
@ -89,12 +96,12 @@ func (b *Stream) writeThreeBytes(c1 byte, c2 byte, c3 byte) {
|
||||
if b.Error != nil {
|
||||
return
|
||||
}
|
||||
if b.Available() <= 2 && b.Flush() != nil {
|
||||
return
|
||||
if b.Available() < 3 {
|
||||
b.growAtLeast(3)
|
||||
}
|
||||
b.buf[b.n] = c1
|
||||
b.buf[b.n + 1] = c2
|
||||
b.buf[b.n + 2] = c3
|
||||
b.buf[b.n+1] = c2
|
||||
b.buf[b.n+2] = c3
|
||||
b.n += 3
|
||||
}
|
||||
|
||||
@ -102,13 +109,13 @@ func (b *Stream) writeFourBytes(c1 byte, c2 byte, c3 byte, c4 byte) {
|
||||
if b.Error != nil {
|
||||
return
|
||||
}
|
||||
if b.Available() <= 3 && b.Flush() != nil {
|
||||
return
|
||||
if b.Available() < 4 {
|
||||
b.growAtLeast(4)
|
||||
}
|
||||
b.buf[b.n] = c1
|
||||
b.buf[b.n + 1] = c2
|
||||
b.buf[b.n + 2] = c3
|
||||
b.buf[b.n + 3] = c4
|
||||
b.buf[b.n+1] = c2
|
||||
b.buf[b.n+2] = c3
|
||||
b.buf[b.n+3] = c4
|
||||
b.n += 4
|
||||
}
|
||||
|
||||
@ -116,19 +123,22 @@ func (b *Stream) writeFiveBytes(c1 byte, c2 byte, c3 byte, c4 byte, c5 byte) {
|
||||
if b.Error != nil {
|
||||
return
|
||||
}
|
||||
if b.Available() <= 3 && b.Flush() != nil {
|
||||
return
|
||||
if b.Available() < 5 {
|
||||
b.growAtLeast(5)
|
||||
}
|
||||
b.buf[b.n] = c1
|
||||
b.buf[b.n + 1] = c2
|
||||
b.buf[b.n + 2] = c3
|
||||
b.buf[b.n + 3] = c4
|
||||
b.buf[b.n + 4] = c5
|
||||
b.buf[b.n+1] = c2
|
||||
b.buf[b.n+2] = c3
|
||||
b.buf[b.n+3] = c4
|
||||
b.buf[b.n+4] = c5
|
||||
b.n += 5
|
||||
}
|
||||
|
||||
// Flush writes any buffered data to the underlying io.Writer.
|
||||
func (b *Stream) Flush() error {
|
||||
if b.out == nil {
|
||||
return nil
|
||||
}
|
||||
if b.Error != nil {
|
||||
return b.Error
|
||||
}
|
||||
@ -141,7 +151,7 @@ func (b *Stream) Flush() error {
|
||||
}
|
||||
if err != nil {
|
||||
if n > 0 && n < b.n {
|
||||
copy(b.buf[0:b.n - n], b.buf[n:b.n])
|
||||
copy(b.buf[0:b.n-n], b.buf[n:b.n])
|
||||
}
|
||||
b.n -= n
|
||||
b.Error = err
|
||||
@ -151,13 +161,28 @@ func (b *Stream) Flush() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Stream) WriteRaw(s string) {
|
||||
for len(s) > b.Available() && b.Error == nil {
|
||||
n := copy(b.buf[b.n:], s)
|
||||
b.n += n
|
||||
s = s[n:]
|
||||
b.Flush()
|
||||
func (b *Stream) ensure(minimal int) {
|
||||
available := b.Available()
|
||||
if available < minimal {
|
||||
if b.n > 1024 {
|
||||
b.Flush()
|
||||
}
|
||||
b.growAtLeast(minimal)
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Stream) growAtLeast(minimal int) {
|
||||
toGrow := len(b.buf)
|
||||
if toGrow < minimal {
|
||||
toGrow = minimal
|
||||
}
|
||||
newBuf := make([]byte, len(b.buf)+toGrow)
|
||||
copy(newBuf, b.Buffer())
|
||||
b.buf = newBuf
|
||||
}
|
||||
|
||||
func (b *Stream) WriteRaw(s string) {
|
||||
b.ensure(len(s))
|
||||
if b.Error != nil {
|
||||
return
|
||||
}
|
||||
@ -166,18 +191,13 @@ func (b *Stream) WriteRaw(s string) {
|
||||
}
|
||||
|
||||
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 {
|
||||
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++
|
||||
@ -189,7 +209,7 @@ func (stream *Stream) WriteString(s string) {
|
||||
stream.buf[n] = c
|
||||
n++
|
||||
} else {
|
||||
break;
|
||||
break
|
||||
}
|
||||
}
|
||||
if i == valLen {
|
||||
@ -200,14 +220,14 @@ func (stream *Stream) WriteString(s string) {
|
||||
}
|
||||
stream.n = n
|
||||
// for the remaining parts, we process them char by char
|
||||
stream.writeStringSlowPath(s, i, valLen);
|
||||
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) {
|
||||
switch c {
|
||||
case '"':
|
||||
stream.writeTwoBytes('\\', '"')
|
||||
case '\\':
|
||||
@ -223,7 +243,7 @@ func (stream *Stream) writeStringSlowPath(s string, i int, valLen int) {
|
||||
case '\t':
|
||||
stream.writeTwoBytes('\\', 't')
|
||||
default:
|
||||
stream.writeByte(c);
|
||||
stream.writeByte(c)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -293,21 +313,14 @@ func (stream *Stream) WriteArrayEnd() {
|
||||
}
|
||||
|
||||
func (stream *Stream) writeIndention(delta int) {
|
||||
if (stream.indention == 0) {
|
||||
if stream.indention == 0 {
|
||||
return
|
||||
}
|
||||
stream.writeByte('\n')
|
||||
toWrite := stream.indention - delta
|
||||
i := 0
|
||||
for {
|
||||
for ; i < toWrite && stream.n < len(stream.buf); i++ {
|
||||
stream.buf[stream.n] = ' '
|
||||
stream.n ++
|
||||
}
|
||||
if i == toWrite {
|
||||
break;
|
||||
} else {
|
||||
stream.Flush()
|
||||
}
|
||||
stream.ensure(toWrite)
|
||||
for i := 0; i < toWrite && stream.n < len(stream.buf); i++ {
|
||||
stream.buf[stream.n] = ' '
|
||||
stream.n++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package jsoniter
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
var POW10 []uint64
|
||||
@ -11,61 +12,79 @@ func init() {
|
||||
}
|
||||
|
||||
func (stream *Stream) WriteFloat32(val float32) {
|
||||
stream.WriteRaw(strconv.FormatFloat(float64(val), 'f', -1, 32))
|
||||
}
|
||||
|
||||
func (stream *Stream) WriteFloat32Lossy(val float32) {
|
||||
if val < 0 {
|
||||
stream.writeByte('-')
|
||||
val = -val
|
||||
}
|
||||
if val > 0x4ffffff {
|
||||
stream.WriteRaw(strconv.FormatFloat(float64(val), 'f', -1, 32));
|
||||
stream.WriteRaw(strconv.FormatFloat(float64(val), 'f', -1, 32))
|
||||
return
|
||||
}
|
||||
precision := 6
|
||||
exp := uint64(1000000) // 6
|
||||
lval := uint64(float64(val) * float64(exp) + 0.5)
|
||||
lval := uint64(float64(val)*float64(exp) + 0.5)
|
||||
stream.WriteUint64(lval / exp)
|
||||
fval := lval % exp
|
||||
if fval == 0 {
|
||||
return
|
||||
}
|
||||
stream.writeByte('.')
|
||||
if stream.Available() < 10 {
|
||||
stream.Flush()
|
||||
}
|
||||
stream.ensure(10)
|
||||
for p := precision - 1; p > 0 && fval < POW10[p]; p-- {
|
||||
stream.writeByte('0')
|
||||
}
|
||||
stream.WriteUint64(fval);
|
||||
for stream.buf[stream.n - 1] == '0' {
|
||||
stream.n--;
|
||||
stream.WriteUint64(fval)
|
||||
for stream.buf[stream.n-1] == '0' {
|
||||
stream.n--
|
||||
}
|
||||
}
|
||||
|
||||
func (stream *Stream) WriteFloat64(val float64) {
|
||||
stream.WriteRaw(strconv.FormatFloat(float64(val), 'f', -1, 64))
|
||||
}
|
||||
|
||||
func (stream *Stream) WriteFloat64Lossy(val float64) {
|
||||
if val < 0 {
|
||||
stream.writeByte('-')
|
||||
val = -val
|
||||
}
|
||||
if val > 0x4ffffff {
|
||||
stream.WriteRaw(strconv.FormatFloat(val, 'f', -1, 64));
|
||||
stream.WriteRaw(strconv.FormatFloat(val, 'f', -1, 64))
|
||||
return
|
||||
}
|
||||
precision := 6
|
||||
exp := uint64(1000000) // 6
|
||||
lval := uint64(val * float64(exp) + 0.5)
|
||||
lval := uint64(val*float64(exp) + 0.5)
|
||||
stream.WriteUint64(lval / exp)
|
||||
fval := lval % exp
|
||||
if fval == 0 {
|
||||
return
|
||||
}
|
||||
stream.writeByte('.')
|
||||
if stream.Available() < 10 {
|
||||
stream.Flush()
|
||||
}
|
||||
stream.ensure(10)
|
||||
for p := precision - 1; p > 0 && fval < POW10[p]; p-- {
|
||||
stream.writeByte('0')
|
||||
}
|
||||
stream.WriteUint64(fval);
|
||||
for stream.buf[stream.n - 1] == '0' {
|
||||
stream.n--;
|
||||
stream.WriteUint64(fval)
|
||||
for stream.buf[stream.n-1] == '0' {
|
||||
stream.n--
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// EnableLossyFloatMarshalling keeps 10**(-6) precision
|
||||
// for float variables for better performance.
|
||||
func EnableLossyFloatMarshalling() {
|
||||
// for better performance
|
||||
RegisterTypeEncoder("float32", func(ptr unsafe.Pointer, stream *Stream) {
|
||||
val := *((*float32)(ptr))
|
||||
stream.WriteFloat32Lossy(val)
|
||||
})
|
||||
RegisterTypeEncoder("float64", func(ptr unsafe.Pointer, stream *Stream) {
|
||||
val := *((*float64)(ptr))
|
||||
stream.WriteFloat64Lossy(val)
|
||||
})
|
||||
}
|
||||
|
@ -1,46 +1,11 @@
|
||||
package jsoniter
|
||||
|
||||
var digits []uint8
|
||||
var digitTens []uint8
|
||||
var digitOnes []uint8
|
||||
var DIGITS []uint32
|
||||
|
||||
func init() {
|
||||
digits = []uint8{
|
||||
'0', '1', '2', '3', '4', '5',
|
||||
'6', '7', '8', '9', 'a', 'b',
|
||||
'c', 'd', 'e', 'f', 'g', 'h',
|
||||
'i', 'j', 'k', 'l', 'm', 'n',
|
||||
'o', 'p', 'q', 'r', 's', 't',
|
||||
'u', 'v', 'w', 'x', 'y', 'z',
|
||||
}
|
||||
digitTens = []uint8{
|
||||
'0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
|
||||
'1', '1', '1', '1', '1', '1', '1', '1', '1', '1',
|
||||
'2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
|
||||
'3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
|
||||
'4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
|
||||
'5', '5', '5', '5', '5', '5', '5', '5', '5', '5',
|
||||
'6', '6', '6', '6', '6', '6', '6', '6', '6', '6',
|
||||
'7', '7', '7', '7', '7', '7', '7', '7', '7', '7',
|
||||
'8', '8', '8', '8', '8', '8', '8', '8', '8', '8',
|
||||
'9', '9', '9', '9', '9', '9', '9', '9', '9', '9',
|
||||
}
|
||||
digitOnes = []uint8{
|
||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
||||
}
|
||||
DIGITS = make([]uint32, 1000)
|
||||
for i := uint32(0); i < 1000; i++ {
|
||||
DIGITS[i] = (((i / 100) + '0') << 16) + ((((i / 10) % 10) + '0') << 8) + i % 10 + '0';
|
||||
DIGITS[i] = (((i / 100) + '0') << 16) + ((((i / 10) % 10) + '0') << 8) + i%10 + '0'
|
||||
if i < 10 {
|
||||
DIGITS[i] += 2 << 24
|
||||
} else if i < 100 {
|
||||
@ -67,24 +32,20 @@ func writeFirstBuf(buf []byte, v uint32, n int) int {
|
||||
|
||||
func writeBuf(buf []byte, v uint32, n int) {
|
||||
buf[n] = byte(v >> 16)
|
||||
buf[n + 1] = byte(v >> 8)
|
||||
buf[n + 2] = byte(v)
|
||||
buf[n+1] = byte(v >> 8)
|
||||
buf[n+2] = byte(v)
|
||||
}
|
||||
|
||||
func (stream *Stream) WriteUint8(val uint8) {
|
||||
if stream.Available() < 3 {
|
||||
stream.Flush()
|
||||
}
|
||||
stream.ensure(3)
|
||||
stream.n = writeFirstBuf(stream.buf, DIGITS[val], stream.n)
|
||||
}
|
||||
|
||||
func (stream *Stream) WriteInt8(nval int8) {
|
||||
if stream.Available() < 4 {
|
||||
stream.Flush()
|
||||
}
|
||||
stream.ensure(4)
|
||||
n := stream.n
|
||||
var val uint8
|
||||
if (nval < 0) {
|
||||
if nval < 0 {
|
||||
val = uint8(-nval)
|
||||
stream.buf[n] = '-'
|
||||
n++
|
||||
@ -95,15 +56,13 @@ func (stream *Stream) WriteInt8(nval int8) {
|
||||
}
|
||||
|
||||
func (stream *Stream) WriteUint16(val uint16) {
|
||||
if stream.Available() < 5 {
|
||||
stream.Flush()
|
||||
}
|
||||
stream.ensure(5)
|
||||
q1 := val / 1000
|
||||
if q1 == 0 {
|
||||
stream.n = writeFirstBuf(stream.buf, DIGITS[val], stream.n)
|
||||
return
|
||||
}
|
||||
r1 := val - q1 * 1000;
|
||||
r1 := val - q1*1000
|
||||
n := writeFirstBuf(stream.buf, DIGITS[q1], stream.n)
|
||||
writeBuf(stream.buf, DIGITS[r1], n)
|
||||
stream.n = n + 3
|
||||
@ -111,12 +70,10 @@ func (stream *Stream) WriteUint16(val uint16) {
|
||||
}
|
||||
|
||||
func (stream *Stream) WriteInt16(nval int16) {
|
||||
if stream.Available() < 6 {
|
||||
stream.Flush()
|
||||
}
|
||||
stream.ensure(6)
|
||||
n := stream.n
|
||||
var val uint16
|
||||
if (nval < 0) {
|
||||
if nval < 0 {
|
||||
val = uint16(-nval)
|
||||
stream.buf[n] = '-'
|
||||
n++
|
||||
@ -128,7 +85,7 @@ func (stream *Stream) WriteInt16(nval int16) {
|
||||
stream.n = writeFirstBuf(stream.buf, DIGITS[val], n)
|
||||
return
|
||||
}
|
||||
r1 := val - q1 * 1000;
|
||||
r1 := val - q1*1000
|
||||
n = writeFirstBuf(stream.buf, DIGITS[q1], n)
|
||||
writeBuf(stream.buf, DIGITS[r1], n)
|
||||
stream.n = n + 3
|
||||
@ -136,16 +93,14 @@ func (stream *Stream) WriteInt16(nval int16) {
|
||||
}
|
||||
|
||||
func (stream *Stream) WriteUint32(val uint32) {
|
||||
if stream.Available() < 10 {
|
||||
stream.Flush()
|
||||
}
|
||||
stream.ensure(10)
|
||||
n := stream.n
|
||||
q1 := val / 1000
|
||||
if q1 == 0 {
|
||||
stream.n = writeFirstBuf(stream.buf, DIGITS[val], n)
|
||||
return
|
||||
}
|
||||
r1 := val - q1 * 1000;
|
||||
r1 := val - q1*1000
|
||||
q2 := q1 / 1000
|
||||
if q2 == 0 {
|
||||
n := writeFirstBuf(stream.buf, DIGITS[q1], n)
|
||||
@ -153,29 +108,27 @@ func (stream *Stream) WriteUint32(val uint32) {
|
||||
stream.n = n + 3
|
||||
return
|
||||
}
|
||||
r2 := q1 - q2 * 1000
|
||||
r2 := q1 - q2*1000
|
||||
q3 := q2 / 1000
|
||||
if q3 == 0 {
|
||||
n = writeFirstBuf(stream.buf, DIGITS[q2], n)
|
||||
} else {
|
||||
r3 := q2 - q3 * 1000
|
||||
r3 := q2 - q3*1000
|
||||
stream.buf[n] = byte(q3 + '0')
|
||||
n++
|
||||
writeBuf(stream.buf, DIGITS[r3], n)
|
||||
n += 3
|
||||
}
|
||||
writeBuf(stream.buf, DIGITS[r2], n)
|
||||
writeBuf(stream.buf, DIGITS[r1], n + 3)
|
||||
writeBuf(stream.buf, DIGITS[r1], n+3)
|
||||
stream.n = n + 6
|
||||
}
|
||||
|
||||
func (stream *Stream) WriteInt32(nval int32) {
|
||||
if stream.Available() < 11 {
|
||||
stream.Flush()
|
||||
}
|
||||
stream.ensure(11)
|
||||
n := stream.n
|
||||
var val uint32
|
||||
if (nval < 0) {
|
||||
if nval < 0 {
|
||||
val = uint32(-nval)
|
||||
stream.buf[n] = '-'
|
||||
n++
|
||||
@ -187,7 +140,7 @@ func (stream *Stream) WriteInt32(nval int32) {
|
||||
stream.n = writeFirstBuf(stream.buf, DIGITS[val], n)
|
||||
return
|
||||
}
|
||||
r1 := val - q1 * 1000;
|
||||
r1 := val - q1*1000
|
||||
q2 := q1 / 1000
|
||||
if q2 == 0 {
|
||||
n := writeFirstBuf(stream.buf, DIGITS[q1], n)
|
||||
@ -195,33 +148,31 @@ func (stream *Stream) WriteInt32(nval int32) {
|
||||
stream.n = n + 3
|
||||
return
|
||||
}
|
||||
r2 := q1 - q2 * 1000
|
||||
r2 := q1 - q2*1000
|
||||
q3 := q2 / 1000
|
||||
if q3 == 0 {
|
||||
n = writeFirstBuf(stream.buf, DIGITS[q2], n)
|
||||
} else {
|
||||
r3 := q2 - q3 * 1000
|
||||
r3 := q2 - q3*1000
|
||||
stream.buf[n] = byte(q3 + '0')
|
||||
n++
|
||||
writeBuf(stream.buf, DIGITS[r3], n)
|
||||
n += 3
|
||||
}
|
||||
writeBuf(stream.buf, DIGITS[r2], n)
|
||||
writeBuf(stream.buf, DIGITS[r1], n + 3)
|
||||
writeBuf(stream.buf, DIGITS[r1], n+3)
|
||||
stream.n = n + 6
|
||||
}
|
||||
|
||||
func (stream *Stream) WriteUint64(val uint64) {
|
||||
if stream.Available() < 20 {
|
||||
stream.Flush()
|
||||
}
|
||||
stream.ensure(20)
|
||||
n := stream.n
|
||||
q1 := val / 1000
|
||||
if q1 == 0 {
|
||||
stream.n = writeFirstBuf(stream.buf, DIGITS[val], n)
|
||||
return
|
||||
}
|
||||
r1 := val - q1 * 1000;
|
||||
r1 := val - q1*1000
|
||||
q2 := q1 / 1000
|
||||
if q2 == 0 {
|
||||
n := writeFirstBuf(stream.buf, DIGITS[q1], n)
|
||||
@ -229,61 +180,59 @@ func (stream *Stream) WriteUint64(val uint64) {
|
||||
stream.n = n + 3
|
||||
return
|
||||
}
|
||||
r2 := q1 - q2 * 1000
|
||||
r2 := q1 - q2*1000
|
||||
q3 := q2 / 1000
|
||||
if q3 == 0 {
|
||||
n = writeFirstBuf(stream.buf, DIGITS[q2], n)
|
||||
writeBuf(stream.buf, DIGITS[r2], n)
|
||||
writeBuf(stream.buf, DIGITS[r1], n + 3)
|
||||
writeBuf(stream.buf, DIGITS[r1], n+3)
|
||||
stream.n = n + 6
|
||||
return
|
||||
}
|
||||
r3 := q2 - q3 * 1000
|
||||
r3 := q2 - q3*1000
|
||||
q4 := q3 / 1000
|
||||
if q4 == 0 {
|
||||
n = writeFirstBuf(stream.buf, DIGITS[q3], n)
|
||||
writeBuf(stream.buf, DIGITS[r3], n)
|
||||
writeBuf(stream.buf, DIGITS[r2], n + 3)
|
||||
writeBuf(stream.buf, DIGITS[r1], n + 6)
|
||||
writeBuf(stream.buf, DIGITS[r2], n+3)
|
||||
writeBuf(stream.buf, DIGITS[r1], n+6)
|
||||
stream.n = n + 9
|
||||
return
|
||||
}
|
||||
r4 := q3 - q4 * 1000
|
||||
r4 := q3 - q4*1000
|
||||
q5 := q4 / 1000
|
||||
if q5 == 0 {
|
||||
n = writeFirstBuf(stream.buf, DIGITS[q4], n)
|
||||
writeBuf(stream.buf, DIGITS[r4], n)
|
||||
writeBuf(stream.buf, DIGITS[r3], n + 3)
|
||||
writeBuf(stream.buf, DIGITS[r2], n + 6)
|
||||
writeBuf(stream.buf, DIGITS[r1], n + 9)
|
||||
writeBuf(stream.buf, DIGITS[r3], n+3)
|
||||
writeBuf(stream.buf, DIGITS[r2], n+6)
|
||||
writeBuf(stream.buf, DIGITS[r1], n+9)
|
||||
stream.n = n + 12
|
||||
return
|
||||
}
|
||||
r5 := q4 - q5 * 1000
|
||||
r5 := q4 - q5*1000
|
||||
q6 := q5 / 1000
|
||||
if q6 == 0 {
|
||||
n = writeFirstBuf(stream.buf, DIGITS[q5], n)
|
||||
} else {
|
||||
n = writeFirstBuf(stream.buf, DIGITS[q6], n)
|
||||
r6 := q5 - q6 * 1000
|
||||
r6 := q5 - q6*1000
|
||||
writeBuf(stream.buf, DIGITS[r6], n)
|
||||
n += 3
|
||||
}
|
||||
writeBuf(stream.buf, DIGITS[r5], n)
|
||||
writeBuf(stream.buf, DIGITS[r4], n + 3)
|
||||
writeBuf(stream.buf, DIGITS[r3], n + 6)
|
||||
writeBuf(stream.buf, DIGITS[r2], n + 9)
|
||||
writeBuf(stream.buf, DIGITS[r1], n + 12)
|
||||
writeBuf(stream.buf, DIGITS[r4], n+3)
|
||||
writeBuf(stream.buf, DIGITS[r3], n+6)
|
||||
writeBuf(stream.buf, DIGITS[r2], n+9)
|
||||
writeBuf(stream.buf, DIGITS[r1], n+12)
|
||||
stream.n = n + 15
|
||||
}
|
||||
|
||||
func (stream *Stream) WriteInt64(nval int64) {
|
||||
if stream.Available() < 20 {
|
||||
stream.Flush()
|
||||
}
|
||||
stream.ensure(20)
|
||||
n := stream.n
|
||||
var val uint64
|
||||
if (nval < 0) {
|
||||
if nval < 0 {
|
||||
val = uint64(-nval)
|
||||
stream.buf[n] = '-'
|
||||
n++
|
||||
@ -295,7 +244,7 @@ func (stream *Stream) WriteInt64(nval int64) {
|
||||
stream.n = writeFirstBuf(stream.buf, DIGITS[val], n)
|
||||
return
|
||||
}
|
||||
r1 := val - q1 * 1000;
|
||||
r1 := val - q1*1000
|
||||
q2 := q1 / 1000
|
||||
if q2 == 0 {
|
||||
n := writeFirstBuf(stream.buf, DIGITS[q1], n)
|
||||
@ -303,52 +252,52 @@ func (stream *Stream) WriteInt64(nval int64) {
|
||||
stream.n = n + 3
|
||||
return
|
||||
}
|
||||
r2 := q1 - q2 * 1000
|
||||
r2 := q1 - q2*1000
|
||||
q3 := q2 / 1000
|
||||
if q3 == 0 {
|
||||
n = writeFirstBuf(stream.buf, DIGITS[q2], n)
|
||||
writeBuf(stream.buf, DIGITS[r2], n)
|
||||
writeBuf(stream.buf, DIGITS[r1], n + 3)
|
||||
writeBuf(stream.buf, DIGITS[r1], n+3)
|
||||
stream.n = n + 6
|
||||
return
|
||||
}
|
||||
r3 := q2 - q3 * 1000
|
||||
r3 := q2 - q3*1000
|
||||
q4 := q3 / 1000
|
||||
if q4 == 0 {
|
||||
n = writeFirstBuf(stream.buf, DIGITS[q3], n)
|
||||
writeBuf(stream.buf, DIGITS[r3], n)
|
||||
writeBuf(stream.buf, DIGITS[r2], n + 3)
|
||||
writeBuf(stream.buf, DIGITS[r1], n + 6)
|
||||
writeBuf(stream.buf, DIGITS[r2], n+3)
|
||||
writeBuf(stream.buf, DIGITS[r1], n+6)
|
||||
stream.n = n + 9
|
||||
return
|
||||
}
|
||||
r4 := q3 - q4 * 1000
|
||||
r4 := q3 - q4*1000
|
||||
q5 := q4 / 1000
|
||||
if q5 == 0 {
|
||||
n = writeFirstBuf(stream.buf, DIGITS[q4], n)
|
||||
writeBuf(stream.buf, DIGITS[r4], n)
|
||||
writeBuf(stream.buf, DIGITS[r3], n + 3)
|
||||
writeBuf(stream.buf, DIGITS[r2], n + 6)
|
||||
writeBuf(stream.buf, DIGITS[r1], n + 9)
|
||||
writeBuf(stream.buf, DIGITS[r3], n+3)
|
||||
writeBuf(stream.buf, DIGITS[r2], n+6)
|
||||
writeBuf(stream.buf, DIGITS[r1], n+9)
|
||||
stream.n = n + 12
|
||||
return
|
||||
}
|
||||
r5 := q4 - q5 * 1000
|
||||
r5 := q4 - q5*1000
|
||||
q6 := q5 / 1000
|
||||
if q6 == 0 {
|
||||
n = writeFirstBuf(stream.buf, DIGITS[q5], n)
|
||||
} else {
|
||||
stream.buf[n] = byte(q6 + '0')
|
||||
n++
|
||||
r6 := q5 - q6 * 1000
|
||||
r6 := q5 - q6*1000
|
||||
writeBuf(stream.buf, DIGITS[r6], n)
|
||||
n += 3
|
||||
}
|
||||
writeBuf(stream.buf, DIGITS[r5], n)
|
||||
writeBuf(stream.buf, DIGITS[r4], n + 3)
|
||||
writeBuf(stream.buf, DIGITS[r3], n + 6)
|
||||
writeBuf(stream.buf, DIGITS[r2], n + 9)
|
||||
writeBuf(stream.buf, DIGITS[r1], n + 12)
|
||||
writeBuf(stream.buf, DIGITS[r4], n+3)
|
||||
writeBuf(stream.buf, DIGITS[r3], n+6)
|
||||
writeBuf(stream.buf, DIGITS[r2], n+9)
|
||||
writeBuf(stream.buf, DIGITS[r1], n+12)
|
||||
stream.n = n + 15
|
||||
}
|
||||
|
||||
@ -358,4 +307,4 @@ func (stream *Stream) WriteInt(val int) {
|
||||
|
||||
func (stream *Stream) WriteUint(val uint) {
|
||||
stream.WriteUint64(uint64(val))
|
||||
}
|
||||
}
|
||||
|
60
jsoniter_adapter_test.go
Normal file
60
jsoniter_adapter_test.go
Normal file
@ -0,0 +1,60 @@
|
||||
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_new_encoder(t *testing.T) {
|
||||
should := require.New(t)
|
||||
buf1 := &bytes.Buffer{}
|
||||
encoder1 := json.NewEncoder(buf1)
|
||||
encoder1.Encode([]int{1})
|
||||
should.Equal("[1]\n", buf1.String())
|
||||
buf2 := &bytes.Buffer{}
|
||||
encoder2 := NewEncoder(buf2)
|
||||
encoder2.Encode([]int{1})
|
||||
should.Equal("[1]", buf2.String())
|
||||
}
|
||||
|
||||
func Test_use_number(t *testing.T) {
|
||||
should := require.New(t)
|
||||
decoder1 := json.NewDecoder(bytes.NewBufferString(`123`))
|
||||
decoder1.UseNumber()
|
||||
decoder2 := NewDecoder(bytes.NewBufferString(`123`))
|
||||
decoder2.UseNumber()
|
||||
var obj1 interface{}
|
||||
should.Nil(decoder1.Decode(&obj1))
|
||||
should.Equal(json.Number("123"), obj1)
|
||||
var obj2 interface{}
|
||||
should.Nil(decoder2.Decode(&obj2))
|
||||
should.Equal(json.Number("123"), obj2)
|
||||
}
|
@ -1,11 +1,11 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"github.com/json-iterator/go/require"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"github.com/json-iterator/go/require"
|
||||
"io"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_empty_array(t *testing.T) {
|
||||
@ -84,7 +84,7 @@ func Test_read_array_with_any_iterator(t *testing.T) {
|
||||
|
||||
func Test_wrap_array(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := Wrap([]int{1,2,3})
|
||||
any := Wrap([]int{1, 2, 3})
|
||||
should.Equal("[1,2,3]", any.ToString())
|
||||
var element Any
|
||||
var elements []int
|
||||
@ -93,9 +93,9 @@ func Test_wrap_array(t *testing.T) {
|
||||
elements = append(elements, element.ToInt())
|
||||
}
|
||||
should.Equal([]int{1, 2, 3}, elements)
|
||||
any = Wrap([]int{1,2,3})
|
||||
any = Wrap([]int{1, 2, 3})
|
||||
should.Equal(3, any.Size())
|
||||
any = Wrap([]int{1,2,3})
|
||||
any = Wrap([]int{1, 2, 3})
|
||||
should.Equal(2, any.Get(1).ToInt())
|
||||
}
|
||||
|
||||
@ -103,7 +103,7 @@ func Test_array_lazy_any_get(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any, err := UnmarshalAnyFromString("[1,[2,3],4]")
|
||||
should.Nil(err)
|
||||
should.Equal(3, any.Get(1,1).ToInt())
|
||||
should.Equal(3, any.Get(1, 1).ToInt())
|
||||
should.Equal("[1,[2,3],4]", any.ToString())
|
||||
}
|
||||
|
||||
@ -111,25 +111,25 @@ func Test_array_lazy_any_get_all(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any, err := UnmarshalAnyFromString("[[1],[2],[3,4]]")
|
||||
should.Nil(err)
|
||||
should.Equal("[1,2,3]", any.Get('*',0).ToString())
|
||||
should.Equal("[1,2,3]", any.Get('*', 0).ToString())
|
||||
}
|
||||
|
||||
func Test_array_wrapper_any_get_all(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := wrapArray([][]int{
|
||||
[]int{1, 2},
|
||||
[]int{3, 4},
|
||||
[]int{5, 6},
|
||||
{1, 2},
|
||||
{3, 4},
|
||||
{5, 6},
|
||||
})
|
||||
should.Equal("[1,3,5]", any.Get('*',0).ToString())
|
||||
should.Equal("[1,3,5]", any.Get('*', 0).ToString())
|
||||
}
|
||||
|
||||
func Test_array_lazy_any_get_invalid(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any, err := UnmarshalAnyFromString("[]")
|
||||
should.Nil(err)
|
||||
should.Equal(Invalid, any.Get(1,1).ValueType())
|
||||
should.NotNil(any.Get(1,1).LastError())
|
||||
should.Equal(Invalid, any.Get(1, 1).ValueType())
|
||||
should.NotNil(any.Get(1, 1).LastError())
|
||||
should.Equal(Invalid, any.Get("1").ValueType())
|
||||
should.NotNil(any.Get("1").LastError())
|
||||
}
|
||||
@ -244,13 +244,45 @@ func Test_write_val_empty_array(t *testing.T) {
|
||||
func Test_write_array_of_interface_in_struct(t *testing.T) {
|
||||
should := require.New(t)
|
||||
type TestObject struct {
|
||||
Field []interface{}
|
||||
Field []interface{}
|
||||
Field2 string
|
||||
}
|
||||
val := TestObject{[]interface{}{1, 2}, ""}
|
||||
str, err := MarshalToString(val)
|
||||
should.Nil(err)
|
||||
should.Equal(`{"Field":[1,2],"Field2":""}`, str)
|
||||
should.Contains(str, `"Field":[1,2]`)
|
||||
should.Contains(str, `"Field2":""`)
|
||||
}
|
||||
|
||||
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_encode_byte_array(t *testing.T) {
|
||||
should := require.New(t)
|
||||
bytes, err := json.Marshal([]byte{1, 2, 3})
|
||||
should.Nil(err)
|
||||
should.Equal(`"AQID"`, string(bytes))
|
||||
bytes, err = Marshal([]byte{1, 2, 3})
|
||||
should.Nil(err)
|
||||
should.Equal(`"AQID"`, string(bytes))
|
||||
}
|
||||
|
||||
func Test_decode_byte_array(t *testing.T) {
|
||||
should := require.New(t)
|
||||
data := []byte{}
|
||||
err := json.Unmarshal([]byte(`"AQID"`), &data)
|
||||
should.Nil(err)
|
||||
should.Equal([]byte{1, 2, 3}, data)
|
||||
err = Unmarshal([]byte(`"AQID"`), &data)
|
||||
should.Nil(err)
|
||||
should.Equal([]byte{1, 2, 3}, data)
|
||||
}
|
||||
|
||||
func Benchmark_jsoniter_array(b *testing.B) {
|
||||
|
@ -1,9 +1,9 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"bytes"
|
||||
"github.com/json-iterator/go/require"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_true(t *testing.T) {
|
||||
@ -38,7 +38,6 @@ func Test_write_true_false(t *testing.T) {
|
||||
should.Equal("truefalse", buf.String())
|
||||
}
|
||||
|
||||
|
||||
func Test_write_val_bool(t *testing.T) {
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
@ -47,4 +46,4 @@ func Test_write_val_bool(t *testing.T) {
|
||||
stream.Flush()
|
||||
should.Nil(stream.Error)
|
||||
should.Equal("true", buf.String())
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,8 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/json-iterator/go/require"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"testing"
|
||||
@ -29,6 +31,42 @@ func Test_customize_type_decoder(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func Test_customize_type_encoder(t *testing.T) {
|
||||
should := require.New(t)
|
||||
RegisterTypeEncoder("time.Time", func(ptr unsafe.Pointer, stream *Stream) {
|
||||
t := *((*time.Time)(ptr))
|
||||
stream.WriteString(t.UTC().Format("2006-01-02 15:04:05"))
|
||||
})
|
||||
defer 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) {
|
||||
CleanEncoders()
|
||||
should := require.New(t)
|
||||
RegisterTypeEncoder("[]uint8", func(ptr unsafe.Pointer, stream *Stream) {
|
||||
t := *((*[]byte)(ptr))
|
||||
stream.WriteString(string(t))
|
||||
})
|
||||
defer 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)
|
||||
EnableLossyFloatMarshalling()
|
||||
defer CleanEncoders()
|
||||
str, err := MarshalToString(float32(1.23456789))
|
||||
should.Nil(err)
|
||||
should.Equal("1.234568", str)
|
||||
}
|
||||
|
||||
type Tom struct {
|
||||
field1 string
|
||||
}
|
||||
@ -50,20 +88,129 @@ type TestObject1 struct {
|
||||
}
|
||||
|
||||
func Test_customize_field_by_extension(t *testing.T) {
|
||||
RegisterExtension(func(type_ reflect.Type, field *reflect.StructField) ([]string, DecoderFunc) {
|
||||
should := require.New(t)
|
||||
RegisterExtension(func(type_ reflect.Type, field *reflect.StructField) ([]string, EncoderFunc, DecoderFunc) {
|
||||
if type_.String() == "jsoniter.TestObject1" && field.Name == "field1" {
|
||||
return []string{"field-1"}, func(ptr unsafe.Pointer, iter *Iterator) {
|
||||
encode := func(ptr unsafe.Pointer, stream *Stream) {
|
||||
str := *((*string)(ptr))
|
||||
val, _ := strconv.Atoi(str)
|
||||
stream.WriteInt(val)
|
||||
}
|
||||
decode := func(ptr unsafe.Pointer, iter *Iterator) {
|
||||
*((*string)(ptr)) = strconv.Itoa(iter.ReadInt())
|
||||
}
|
||||
return []string{"field-1"}, encode, decode
|
||||
}
|
||||
return nil, nil
|
||||
return nil, nil, nil
|
||||
})
|
||||
obj := TestObject1{}
|
||||
err := Unmarshal([]byte(`{"field-1": 100}`), &obj)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if obj.field1 != "100" {
|
||||
t.Fatal(obj.field1)
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
func Test_unexported_fields(t *testing.T) {
|
||||
EnableUnexportedStructFieldsSupport()
|
||||
should := require.New(t)
|
||||
type TestObject struct {
|
||||
field1 string
|
||||
field2 string `json:"field-2"`
|
||||
}
|
||||
obj := TestObject{}
|
||||
obj.field1 = "hello"
|
||||
should.Nil(UnmarshalFromString(`{}`, &obj))
|
||||
should.Equal("hello", obj.field1)
|
||||
should.Nil(UnmarshalFromString(`{"field1": "world", "field-2": "abc"}`, &obj))
|
||||
should.Equal("world", obj.field1)
|
||||
should.Equal("abc", obj.field2)
|
||||
str, err := MarshalToString(obj)
|
||||
should.Nil(err)
|
||||
should.Contains(str, `"field-2":"abc"`)
|
||||
}
|
||||
|
||||
type ObjectImplementedMarshaler int
|
||||
|
||||
func (obj *ObjectImplementedMarshaler) MarshalJSON() ([]byte, error) {
|
||||
return []byte(`"hello"`), nil
|
||||
}
|
||||
|
||||
func Test_marshaler(t *testing.T) {
|
||||
type TestObject struct {
|
||||
Field *ObjectImplementedMarshaler
|
||||
}
|
||||
should := require.New(t)
|
||||
val := ObjectImplementedMarshaler(100)
|
||||
obj := TestObject{&val}
|
||||
bytes, err := json.Marshal(obj)
|
||||
should.Nil(err)
|
||||
should.Equal(`{"Field":"hello"}`, string(bytes))
|
||||
str, err := MarshalToString(obj)
|
||||
should.Nil(err)
|
||||
should.Equal(`{"Field":"hello"}`, str)
|
||||
}
|
||||
|
||||
func Test_marshaler_and_encoder(t *testing.T) {
|
||||
type TestObject struct {
|
||||
Field *ObjectImplementedMarshaler
|
||||
}
|
||||
should := require.New(t)
|
||||
RegisterTypeEncoder("jsoniter.ObjectImplementedMarshaler", func(ptr unsafe.Pointer, stream *Stream) {
|
||||
stream.WriteString("hello from encoder")
|
||||
})
|
||||
val := ObjectImplementedMarshaler(100)
|
||||
obj := TestObject{&val}
|
||||
bytes, err := json.Marshal(obj)
|
||||
should.Nil(err)
|
||||
should.Equal(`{"Field":"hello"}`, string(bytes))
|
||||
str, err := MarshalToString(obj)
|
||||
should.Nil(err)
|
||||
should.Equal(`{"Field":"hello from encoder"}`, str)
|
||||
}
|
||||
|
||||
type ObjectImplementedUnmarshaler int
|
||||
|
||||
func (obj *ObjectImplementedUnmarshaler) UnmarshalJSON([]byte) error {
|
||||
*obj = 100
|
||||
return nil
|
||||
}
|
||||
|
||||
func Test_unmarshaler(t *testing.T) {
|
||||
type TestObject struct {
|
||||
Field *ObjectImplementedUnmarshaler
|
||||
Field2 string
|
||||
}
|
||||
should := require.New(t)
|
||||
obj := TestObject{}
|
||||
val := ObjectImplementedUnmarshaler(0)
|
||||
obj.Field = &val
|
||||
err := json.Unmarshal([]byte(`{"Field":"hello"}`), &obj)
|
||||
should.Nil(err)
|
||||
should.Equal(100, int(*obj.Field))
|
||||
err = Unmarshal([]byte(`{"Field":"hello"}`), &obj)
|
||||
should.Nil(err)
|
||||
should.Equal(100, int(*obj.Field))
|
||||
}
|
||||
|
||||
func Test_unmarshaler_and_decoder(t *testing.T) {
|
||||
type TestObject struct {
|
||||
Field *ObjectImplementedUnmarshaler
|
||||
Field2 string
|
||||
}
|
||||
should := require.New(t)
|
||||
RegisterTypeDecoder("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":"hello"}`), &obj)
|
||||
should.Nil(err)
|
||||
should.Equal(100, int(*obj.Field))
|
||||
err = Unmarshal([]byte(`{"Field":"hello"}`), &obj)
|
||||
should.Nil(err)
|
||||
should.Equal(10, int(*obj.Field))
|
||||
}
|
||||
|
@ -1,15 +1,18 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/json-iterator/go/require"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_bind_api_demo(t *testing.T) {
|
||||
iter := ParseString(`[0,1,2,3]`)
|
||||
should := require.New(t)
|
||||
val := []int{}
|
||||
iter.ReadVal(&val)
|
||||
fmt.Println(val[3])
|
||||
err := UnmarshalFromString(`[0,1,2,3] `, &val)
|
||||
should.Nil(err)
|
||||
should.Equal([]int{0, 1, 2, 3}, val)
|
||||
}
|
||||
|
||||
func Test_iterator_api_demo(t *testing.T) {
|
||||
@ -21,19 +24,63 @@ func Test_iterator_api_demo(t *testing.T) {
|
||||
fmt.Println(total)
|
||||
}
|
||||
|
||||
type User struct {
|
||||
userID int
|
||||
name string
|
||||
tags []string
|
||||
type People struct {
|
||||
Name string
|
||||
Gender string
|
||||
Age int
|
||||
Address string
|
||||
Mobile string
|
||||
Country string
|
||||
Height int
|
||||
}
|
||||
|
||||
func Test_iterator_and_bind_api(t *testing.T) {
|
||||
iter := ParseString(`[123, {"name": "taowen", "tags": ["crazy", "hacker"]}]`)
|
||||
user := User{}
|
||||
iter.ReadArray()
|
||||
user.userID = iter.ReadInt()
|
||||
iter.ReadArray()
|
||||
iter.ReadVal(&user)
|
||||
iter.ReadArray() // array end
|
||||
fmt.Println(user)
|
||||
}
|
||||
func jsoniterMarshal(p *People) error {
|
||||
_, err := Marshal(p)
|
||||
if nil != err {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func stdMarshal(p *People) error {
|
||||
_, err := json.Marshal(p)
|
||||
if nil != err {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func BenchmarkJosniterMarshal(b *testing.B) {
|
||||
var p People
|
||||
p.Address = "上海市徐汇区漕宝路"
|
||||
p.Age = 30
|
||||
p.Country = "中国"
|
||||
p.Gender = "male"
|
||||
p.Height = 170
|
||||
p.Mobile = "18502120533"
|
||||
p.Name = "Elvin"
|
||||
b.ReportAllocs()
|
||||
for i := 0; i < b.N; i++ {
|
||||
err := jsoniterMarshal(&p)
|
||||
if nil != err {
|
||||
b.Error(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStdMarshal(b *testing.B) {
|
||||
var p People
|
||||
p.Address = "上海市徐汇区漕宝路"
|
||||
p.Age = 30
|
||||
p.Country = "中国"
|
||||
p.Gender = "male"
|
||||
p.Height = 170
|
||||
p.Mobile = "18502120533"
|
||||
p.Name = "Elvin"
|
||||
b.ReportAllocs()
|
||||
for i := 0; i < b.N; i++ {
|
||||
err := stdMarshal(&p)
|
||||
if nil != err {
|
||||
b.Error(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,9 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"github.com/json-iterator/go/require"
|
||||
"io"
|
||||
"testing"
|
||||
"github.com/json-iterator/go/require"
|
||||
)
|
||||
|
||||
func Test_string_end(t *testing.T) {
|
||||
|
@ -1,14 +1,30 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"testing"
|
||||
"github.com/json-iterator/go/require"
|
||||
"bytes"
|
||||
"strconv"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_read_big_float(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := ParseString(`12.3`)
|
||||
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(`92233720368547758079223372036854775807`)
|
||||
val := iter.ReadBigInt()
|
||||
should.NotNil(val)
|
||||
should.Equal(`92233720368547758079223372036854775807`, val.String())
|
||||
}
|
||||
|
||||
func Test_read_float(t *testing.T) {
|
||||
inputs := []string{`1.1`, `1000`, `9223372036854775807`, `12.3`, `-12.3`, `720368.54775807`, `720368.547758075`}
|
||||
for _, input := range inputs {
|
||||
@ -30,14 +46,14 @@ func Test_read_float(t *testing.T) {
|
||||
// streaming
|
||||
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := Parse(bytes.NewBufferString(input + ","), 2)
|
||||
iter := Parse(bytes.NewBufferString(input+","), 2)
|
||||
expected, err := strconv.ParseFloat(input, 32)
|
||||
should.Nil(err)
|
||||
should.Equal(float32(expected), iter.ReadFloat32())
|
||||
})
|
||||
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := Parse(bytes.NewBufferString(input + ","), 2)
|
||||
iter := Parse(bytes.NewBufferString(input+","), 2)
|
||||
expected, err := strconv.ParseFloat(input, 64)
|
||||
should.Nil(err)
|
||||
should.Equal(expected, iter.ReadFloat64())
|
||||
@ -69,13 +85,13 @@ func Test_wrap_float(t *testing.T) {
|
||||
|
||||
func Test_write_float32(t *testing.T) {
|
||||
vals := []float32{0, 1, -1, 99, 0xff, 0xfff, 0xffff, 0xfffff, 0xffffff, 0x4ffffff, 0xfffffff,
|
||||
-0x4ffffff, -0xfffffff, 1.2345, 1.23456, 1.234567, 1.001}
|
||||
-0x4ffffff, -0xfffffff, 1.2345, 1.23456, 1.234567, 1.001}
|
||||
for _, val := range vals {
|
||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(buf, 4096)
|
||||
stream.WriteFloat32(val)
|
||||
stream.WriteFloat32Lossy(val)
|
||||
stream.Flush()
|
||||
should.Nil(stream.Error)
|
||||
should.Equal(strconv.FormatFloat(float64(val), 'f', -1, 32), buf.String())
|
||||
@ -94,7 +110,7 @@ func Test_write_float32(t *testing.T) {
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(buf, 10)
|
||||
stream.WriteRaw("abcdefg")
|
||||
stream.WriteFloat32(1.123456)
|
||||
stream.WriteFloat32Lossy(1.123456)
|
||||
stream.Flush()
|
||||
should.Nil(stream.Error)
|
||||
should.Equal("abcdefg1.123456", buf.String())
|
||||
@ -102,13 +118,13 @@ func Test_write_float32(t *testing.T) {
|
||||
|
||||
func Test_write_float64(t *testing.T) {
|
||||
vals := []float64{0, 1, -1, 99, 0xff, 0xfff, 0xffff, 0xfffff, 0xffffff, 0x4ffffff, 0xfffffff,
|
||||
-0x4ffffff, -0xfffffff, 1.2345, 1.23456, 1.234567, 1.001}
|
||||
-0x4ffffff, -0xfffffff, 1.2345, 1.23456, 1.234567, 1.001}
|
||||
for _, val := range vals {
|
||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(buf, 4096)
|
||||
stream.WriteFloat64(val)
|
||||
stream.WriteFloat64Lossy(val)
|
||||
stream.Flush()
|
||||
should.Nil(stream.Error)
|
||||
should.Equal(strconv.FormatFloat(val, 'f', -1, 64), buf.String())
|
||||
@ -127,12 +143,34 @@ func Test_write_float64(t *testing.T) {
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(buf, 10)
|
||||
stream.WriteRaw("abcdefg")
|
||||
stream.WriteFloat64(1.123456)
|
||||
stream.WriteFloat64Lossy(1.123456)
|
||||
stream.Flush()
|
||||
should.Nil(stream.Error)
|
||||
should.Equal("abcdefg1.123456", buf.String())
|
||||
}
|
||||
|
||||
func Test_read_float64_cursor(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := ParseString("[1.23456789\n,2,3]")
|
||||
should.True(iter.ReadArray())
|
||||
should.Equal(1.23456789, iter.Read())
|
||||
should.True(iter.ReadArray())
|
||||
should.Equal(float64(2), iter.Read())
|
||||
}
|
||||
|
||||
func Test_read_float_scientific(t *testing.T) {
|
||||
should := require.New(t)
|
||||
var obj interface{}
|
||||
should.Nil(UnmarshalFromString(`1e1`, &obj))
|
||||
should.Equal(float64(10), obj)
|
||||
should.Nil(json.Unmarshal([]byte(`1e1`), &obj))
|
||||
should.Equal(float64(10), obj)
|
||||
should.Nil(UnmarshalFromString(`1.0e1`, &obj))
|
||||
should.Equal(float64(10), obj)
|
||||
should.Nil(json.Unmarshal([]byte(`1.0e1`), &obj))
|
||||
should.Equal(float64(10), obj)
|
||||
}
|
||||
|
||||
func Benchmark_jsoniter_float(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
input := []byte(`1.1123,`)
|
||||
|
@ -3,12 +3,12 @@ package jsoniter
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"github.com/json-iterator/go/require"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"io/ioutil"
|
||||
"github.com/json-iterator/go/require"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"strconv"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_read_uint64_invalid(t *testing.T) {
|
||||
@ -421,6 +421,17 @@ func Test_write_val_int_ptr(t *testing.T) {
|
||||
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) {
|
||||
stream := NewStream(ioutil.Discard, 64)
|
||||
for n := 0; n < b.N; n++ {
|
||||
|
@ -1,9 +1,10 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"github.com/json-iterator/go/require"
|
||||
"testing"
|
||||
"unsafe"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
func Test_write_array_of_interface(t *testing.T) {
|
||||
@ -16,7 +17,7 @@ func Test_write_array_of_interface(t *testing.T) {
|
||||
|
||||
func Test_write_map_of_interface(t *testing.T) {
|
||||
should := require.New(t)
|
||||
val := map[string]interface{}{"hello":"world"}
|
||||
val := map[string]interface{}{"hello": "world"}
|
||||
str, err := MarshalToString(val)
|
||||
should.Nil(err)
|
||||
should.Equal(`{"hello":"world"}`, str)
|
||||
@ -27,7 +28,7 @@ func Test_write_map_of_interface_in_struct(t *testing.T) {
|
||||
Field map[string]interface{}
|
||||
}
|
||||
should := require.New(t)
|
||||
val := TestObject{map[string]interface{}{"hello":"world"}}
|
||||
val := TestObject{map[string]interface{}{"hello": "world"}}
|
||||
str, err := MarshalToString(val)
|
||||
should.Nil(err)
|
||||
should.Equal(`{"Field":{"hello":"world"}}`, str)
|
||||
@ -35,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) {
|
||||
type TestObject struct {
|
||||
Field map[string]interface{}
|
||||
Field map[string]interface{}
|
||||
Field2 string
|
||||
}
|
||||
should := require.New(t)
|
||||
val := TestObject{map[string]interface{}{"hello":"world"}, ""}
|
||||
val := TestObject{map[string]interface{}{"hello": "world"}, ""}
|
||||
str, err := MarshalToString(val)
|
||||
should.Nil(err)
|
||||
should.Equal(`{"Field":{"hello":"world"},"Field2":""}`, str)
|
||||
should.Contains(str, `"Field":{"hello":"world"}`)
|
||||
}
|
||||
|
||||
type MyInterface interface {
|
||||
@ -59,7 +60,7 @@ func Test_write_map_of_custom_interface(t *testing.T) {
|
||||
should := require.New(t)
|
||||
myStr := MyString("world")
|
||||
should.Equal("world", myStr.Hello())
|
||||
val := map[string]MyInterface{"hello":myStr}
|
||||
val := map[string]MyInterface{"hello": myStr}
|
||||
str, err := MarshalToString(val)
|
||||
should.Nil(err)
|
||||
should.Equal(`{"hello":"world"}`, str)
|
||||
@ -91,4 +92,64 @@ func Test_read_custom_interface(t *testing.T) {
|
||||
err := UnmarshalFromString(`"hello"`, &val)
|
||||
should.Nil(err)
|
||||
should.Equal("hello", val.Hello())
|
||||
}
|
||||
|
||||
func Test_decode_object_contain_empty_interface(t *testing.T) {
|
||||
type TestObject struct {
|
||||
Field interface{}
|
||||
}
|
||||
should := require.New(t)
|
||||
obj := TestObject{}
|
||||
obj.Field = 1024
|
||||
should.Nil(UnmarshalFromString(`{"Field": "hello"}`, &obj))
|
||||
should.Equal("hello", obj.Field)
|
||||
}
|
||||
|
||||
func Test_decode_object_contain_non_empty_interface(t *testing.T) {
|
||||
type TestObject struct {
|
||||
Field MyInterface
|
||||
}
|
||||
should := require.New(t)
|
||||
obj := TestObject{}
|
||||
obj.Field = MyString("abc")
|
||||
should.Nil(UnmarshalFromString(`{"Field": "hello"}`, &obj))
|
||||
should.Equal(MyString("hello"), obj.Field)
|
||||
}
|
||||
|
||||
func Test_encode_object_contain_empty_interface(t *testing.T) {
|
||||
type TestObject struct {
|
||||
Field interface{}
|
||||
}
|
||||
should := require.New(t)
|
||||
obj := TestObject{}
|
||||
obj.Field = 1024
|
||||
str, err := MarshalToString(obj)
|
||||
should.Nil(err)
|
||||
should.Equal(`{"Field":1024}`, str)
|
||||
}
|
||||
|
||||
func Test_encode_object_contain_non_empty_interface(t *testing.T) {
|
||||
type TestObject struct {
|
||||
Field MyInterface
|
||||
}
|
||||
should := require.New(t)
|
||||
obj := TestObject{}
|
||||
obj.Field = MyString("hello")
|
||||
str, err := MarshalToString(obj)
|
||||
should.Nil(err)
|
||||
should.Equal(`{"Field":"hello"}`, str)
|
||||
}
|
||||
|
||||
|
||||
func Test_nil_non_empty_interface(t *testing.T) {
|
||||
CleanEncoders()
|
||||
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))
|
||||
}
|
@ -1,8 +1,10 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"github.com/json-iterator/go/require"
|
||||
"math/big"
|
||||
"testing"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
func Test_read_map(t *testing.T) {
|
||||
@ -40,12 +42,12 @@ func Test_wrap_map(t *testing.T) {
|
||||
vals[k] = v.ToString()
|
||||
}
|
||||
}
|
||||
should.Equal(map[string]string{"Field1":"hello"}, vals)
|
||||
should.Equal(map[string]string{"Field1": "hello"}, vals)
|
||||
}
|
||||
|
||||
func Test_map_wrapper_any_get_all(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := Wrap(map[string][]int{"Field1": []int{1, 2}})
|
||||
any := Wrap(map[string][]int{"Field1": {1, 2}})
|
||||
should.Equal(`{"Field1":1}`, any.Get('*', 0).ToString())
|
||||
}
|
||||
|
||||
@ -56,3 +58,72 @@ func Test_write_val_map(t *testing.T) {
|
||||
should.Nil(err)
|
||||
should.Equal(`{"1":"2"}`, str)
|
||||
}
|
||||
|
||||
func Test_slice_of_map(t *testing.T) {
|
||||
should := require.New(t)
|
||||
val := []map[string]string{{"1": "2"}}
|
||||
str, err := MarshalToString(val)
|
||||
should.Nil(err)
|
||||
should.Equal(`[{"1":"2"}]`, str)
|
||||
val = []map[string]string{}
|
||||
should.Nil(UnmarshalFromString(str, &val))
|
||||
should.Equal("2", val[0]["1"])
|
||||
}
|
||||
|
||||
func Test_encode_int_key_map(t *testing.T) {
|
||||
should := require.New(t)
|
||||
val := map[int]string{1: "2"}
|
||||
str, err := MarshalToString(val)
|
||||
should.Nil(err)
|
||||
should.Equal(`{"1":"2"}`, str)
|
||||
}
|
||||
|
||||
func Test_decode_int_key_map(t *testing.T) {
|
||||
should := require.New(t)
|
||||
var val map[int]string
|
||||
should.Nil(UnmarshalFromString(`{"1":"2"}`, &val))
|
||||
should.Equal(map[int]string{1: "2"}, val)
|
||||
}
|
||||
|
||||
func Test_encode_TextMarshaler_key_map(t *testing.T) {
|
||||
should := require.New(t)
|
||||
f, _, _ := big.ParseFloat("1", 10, 64, big.ToZero)
|
||||
val := map[*big.Float]string{f: "2"}
|
||||
str, err := MarshalToString(val)
|
||||
should.Nil(err)
|
||||
should.Equal(`{"1":"2"}`, str)
|
||||
}
|
||||
|
||||
func Test_decode_TextMarshaler_key_map(t *testing.T) {
|
||||
should := require.New(t)
|
||||
var val map[*big.Float]string
|
||||
should.Nil(UnmarshalFromString(`{"1":"2"}`, &val))
|
||||
str, err := MarshalToString(val)
|
||||
should.Nil(err)
|
||||
should.Equal(`{"1":"2"}`, str)
|
||||
}
|
||||
|
||||
|
||||
|
||||
func Test_map_key_with_escaped_char(t *testing.T) {
|
||||
type Ttest struct {
|
||||
Map map[string]string
|
||||
}
|
||||
var jsonBytes = []byte(`
|
||||
{
|
||||
"Map":{
|
||||
"k\"ey": "val"
|
||||
}
|
||||
}`)
|
||||
should := require.New(t)
|
||||
{
|
||||
var obj Ttest
|
||||
should.Nil(json.Unmarshal(jsonBytes, &obj))
|
||||
should.Equal(map[string]string{"k\"ey":"val"}, obj.Map)
|
||||
}
|
||||
{
|
||||
var obj Ttest
|
||||
should.Nil(Unmarshal(jsonBytes, &obj))
|
||||
should.Equal(map[string]string{"k\"ey":"val"}, obj.Map)
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,9 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"github.com/json-iterator/go/require"
|
||||
"bytes"
|
||||
"github.com/json-iterator/go/require"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_read_null(t *testing.T) {
|
||||
@ -39,6 +39,7 @@ func Test_encode_null(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_decode_null_object(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := ParseString(`[null,"a"]`)
|
||||
iter.ReadArray()
|
||||
if iter.ReadObject() != "" {
|
||||
@ -48,6 +49,12 @@ func Test_decode_null_object(t *testing.T) {
|
||||
if iter.ReadString() != "a" {
|
||||
t.FailNow()
|
||||
}
|
||||
type TestObject struct {
|
||||
Field string
|
||||
}
|
||||
objs := []TestObject{}
|
||||
should.Nil(UnmarshalFromString("[null]", &objs))
|
||||
should.Len(objs, 1)
|
||||
}
|
||||
|
||||
func Test_decode_null_array(t *testing.T) {
|
||||
@ -66,7 +73,7 @@ func Test_decode_null_string(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := ParseString(`[null,"a"]`)
|
||||
should.True(iter.ReadArray())
|
||||
should.True(iter.ReadNil())
|
||||
should.Equal("", iter.ReadString())
|
||||
should.True(iter.ReadArray())
|
||||
should.Equal("a", iter.ReadString())
|
||||
}
|
||||
|
@ -1,10 +1,10 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"github.com/json-iterator/go/require"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"github.com/json-iterator/go/require"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_empty_object(t *testing.T) {
|
||||
@ -100,12 +100,15 @@ func Test_object_any_lazy_iterator(t *testing.T) {
|
||||
should.False(hasNext)
|
||||
vals[k] = v.ToString()
|
||||
|
||||
should.Equal(map[string]string{"a":"b", "c":"d"}, vals)
|
||||
should.Equal(map[string]string{"a": "b", "c": "d"}, vals)
|
||||
vals = map[string]string{}
|
||||
for next, hasNext := any.IterateObject(); hasNext; k, v, hasNext = next() {
|
||||
vals[k] = v.ToString()
|
||||
for next, hasNext := any.IterateObject(); hasNext; {
|
||||
k, v, hasNext = next()
|
||||
if v.ValueType() == String {
|
||||
vals[k] = v.ToString()
|
||||
}
|
||||
}
|
||||
should.Equal(map[string]string{"a":"b", "c":"d"}, vals)
|
||||
should.Equal(map[string]string{"a": "b", "c": "d"}, vals)
|
||||
}
|
||||
|
||||
func Test_object_any_with_two_lazy_iterators(t *testing.T) {
|
||||
@ -150,7 +153,7 @@ 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())
|
||||
should.Contains(any.Get('*', 0).ToString(), `"a":0`)
|
||||
}
|
||||
|
||||
func Test_object_lazy_any_get_invalid(t *testing.T) {
|
||||
@ -191,7 +194,7 @@ func Test_wrap_object(t *testing.T) {
|
||||
vals[k] = v.ToString()
|
||||
}
|
||||
}
|
||||
should.Equal(map[string]string{"Field1":"hello"}, vals)
|
||||
should.Equal(map[string]string{"Field1": "hello"}, vals)
|
||||
}
|
||||
|
||||
func Test_object_wrapper_any_get_all(t *testing.T) {
|
||||
@ -201,7 +204,7 @@ func Test_object_wrapper_any_get_all(t *testing.T) {
|
||||
Field2 []int
|
||||
}
|
||||
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) {
|
||||
|
@ -1,8 +1,8 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"github.com/json-iterator/go/require"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_encode_optional_int_pointer(t *testing.T) {
|
||||
@ -41,5 +41,6 @@ func Test_encode_struct_with_optional_field(t *testing.T) {
|
||||
obj.field2 = &world
|
||||
str, err := MarshalToString(obj)
|
||||
should.Nil(err)
|
||||
should.Equal(`{"field1":null,"field2":"world"}`, str)
|
||||
}
|
||||
should.Contains(str, `"field1":null`)
|
||||
should.Contains(str, `"field2":"world"`)
|
||||
}
|
||||
|
@ -1,8 +1,8 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_reflect_str(t *testing.T) {
|
||||
@ -151,4 +151,4 @@ func Test_reflect_bool(t *testing.T) {
|
||||
fmt.Println(iter.Error)
|
||||
t.Fatal(val)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,113 +1,114 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"bytes"
|
||||
"github.com/json-iterator/go/require"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_decode_one_field_struct(t *testing.T) {
|
||||
should := require.New(t)
|
||||
type TestObject struct {
|
||||
field1 string
|
||||
Field1 string
|
||||
}
|
||||
obj := TestObject{}
|
||||
should.Nil(UnmarshalFromString(`{}`, &obj))
|
||||
should.Equal("", obj.field1)
|
||||
should.Equal("", obj.Field1)
|
||||
should.Nil(UnmarshalFromString(`{"field1": "hello"}`, &obj))
|
||||
should.Equal("hello", obj.field1)
|
||||
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
|
||||
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)
|
||||
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
|
||||
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)
|
||||
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
|
||||
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)
|
||||
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
|
||||
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)
|
||||
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
|
||||
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)
|
||||
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) {
|
||||
@ -144,3 +145,97 @@ func Test_write_val_one_field_struct(t *testing.T) {
|
||||
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_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())
|
||||
}
|
||||
|
||||
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_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_anonymous_struct_marshal(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)
|
||||
}
|
||||
|
@ -3,9 +3,9 @@ package jsoniter
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/json-iterator/go/require"
|
||||
"testing"
|
||||
"unsafe"
|
||||
"github.com/json-iterator/go/require"
|
||||
)
|
||||
|
||||
func Test_decode_slice(t *testing.T) {
|
||||
|
54
jsoniter_stream_test.go
Normal file
54
jsoniter_stream_test.go
Normal file
@ -0,0 +1,54 @@
|
||||
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(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(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(nil, 1)
|
||||
stream.IndentionStep = 2
|
||||
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(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(nil, 0)
|
||||
stream.WriteString("123")
|
||||
should.Nil(stream.Error)
|
||||
should.Equal(`"123"`, string(stream.Buffer()))
|
||||
}
|
@ -3,15 +3,15 @@ package jsoniter
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"github.com/json-iterator/go/require"
|
||||
"fmt"
|
||||
"github.com/json-iterator/go/require"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_read_normal_string(t *testing.T) {
|
||||
cases := map[string]string{
|
||||
`"0123456789012345678901234567890123456789"`: `0123456789012345678901234567890123456789`,
|
||||
`""`: ``,
|
||||
`""`: ``,
|
||||
`"hello"`: `hello`,
|
||||
}
|
||||
for input, output := range cases {
|
||||
@ -40,8 +40,8 @@ func Test_read_normal_string(t *testing.T) {
|
||||
|
||||
func Test_read_exotic_string(t *testing.T) {
|
||||
cases := map[string]string{
|
||||
`"hel\"lo"`: `hel"lo`,
|
||||
`"hel\nlo"`: "hel\nlo",
|
||||
`"hel\"lo"`: `hel"lo`,
|
||||
`"hel\nlo"`: "hel\nlo",
|
||||
`"\u4e2d\u6587"`: "中文",
|
||||
`"\ud83d\udc4a"`: "\xf0\x9f\x91\x8a", // surrogate
|
||||
}
|
||||
@ -105,6 +105,13 @@ func Test_write_val_string(t *testing.T) {
|
||||
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 Benchmark_jsoniter_unicode(b *testing.B) {
|
||||
for n := 0; n < b.N; n++ {
|
||||
iter := ParseString(`"\ud83d\udc4a"`)
|
||||
|
Reference in New Issue
Block a user