1
0
mirror of https://github.com/json-iterator/go.git synced 2025-04-14 11:18:49 +02:00
This commit is contained in:
Tao Wen 2017-06-06 23:27:00 +08:00
parent 925df245d3
commit cfffa29c8a
43 changed files with 283 additions and 290 deletions

View File

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

View File

@ -2,11 +2,11 @@ package jsoniter
import ( import (
"bytes" "bytes"
"encoding/json"
"errors"
"io" "io"
"reflect" "reflect"
"errors"
"unsafe" "unsafe"
"encoding/json"
) )
// Unmarshal adapts to json/encoding Unmarshal API // Unmarshal adapts to json/encoding Unmarshal API

View File

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

View File

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

View File

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

View File

@ -2,8 +2,8 @@ package jsoniter
import ( import (
"io" "io"
"unsafe"
"strconv" "strconv"
"unsafe"
) )
type int64LazyAny struct { type int64LazyAny struct {
@ -163,4 +163,4 @@ func (any *int64Any) Parse() *Iterator {
func (any *int64Any) GetInterface() interface{} { func (any *int64Any) GetInterface() interface{} {
return any.val return any.val
} }

View File

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

View File

@ -1,9 +1,9 @@
package jsoniter package jsoniter
import ( import (
"unsafe"
"fmt" "fmt"
"reflect" "reflect"
"unsafe"
) )
type objectLazyAny struct { type objectLazyAny struct {

View File

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

View File

@ -1,12 +1,11 @@
package jsoniter package jsoniter
import ( import (
"io"
"strconv" "strconv"
"unsafe" "unsafe"
"io"
) )
type uint64LazyAny struct { type uint64LazyAny struct {
baseAny baseAny
buf []byte buf []byte
@ -164,4 +163,4 @@ func (any *uint64Any) Parse() *Iterator {
func (any *uint64Any) GetInterface() interface{} { func (any *uint64Any) GetInterface() interface{} {
return any.val return any.val
} }

View File

@ -276,4 +276,3 @@ func (iter *Iterator) ReadBase64() (ret []byte) {
} }
return ret[:n] return ret[:n]
} }

View File

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

View File

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

View File

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

View File

@ -44,10 +44,10 @@ func (iter *Iterator) readFieldHash() int32 {
b += 'a' - 'A' b += 'a' - 'A'
} }
if b == '"' { if b == '"' {
iter.head = i+1 iter.head = i + 1
c = iter.nextToken() c = iter.nextToken()
if c != ':' { if c != ':' {
iter.reportError("readFieldHash", `expect :, but found ` + string([]byte{c})) iter.reportError("readFieldHash", `expect :, but found `+string([]byte{c}))
} }
return int32(hash) return int32(hash)
} }
@ -60,7 +60,7 @@ func (iter *Iterator) readFieldHash() int32 {
} }
} }
} }
iter.reportError("readFieldHash", `expect ", but found ` + string([]byte{c})) iter.reportError("readFieldHash", `expect ", but found `+string([]byte{c}))
return 0 return 0
} }

View File

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

View File

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

View File

@ -1,12 +1,12 @@
package jsoniter package jsoniter
import ( import (
"encoding"
"encoding/json"
"fmt" "fmt"
"reflect" "reflect"
"sync/atomic" "sync/atomic"
"unsafe" "unsafe"
"encoding/json"
"encoding"
) )
/* /*

View File

@ -1,10 +1,10 @@
package jsoniter package jsoniter
import ( import (
"unsafe"
"reflect"
"io"
"fmt" "fmt"
"io"
"reflect"
"unsafe"
) )
func decoderOfSlice(typ reflect.Type) (Decoder, error) { func decoderOfSlice(typ reflect.Type) (Decoder, error) {
@ -21,7 +21,7 @@ func encoderOfSlice(typ reflect.Type) (Encoder, error) {
return nil, err return nil, err
} }
if typ.Elem().Kind() == reflect.Map { if typ.Elem().Kind() == reflect.Map {
encoder = &optionalEncoder{ encoder} encoder = &optionalEncoder{encoder}
} }
return &sliceEncoder{typ, typ.Elem(), encoder}, nil return &sliceEncoder{typ, typ.Elem(), encoder}, nil
} }
@ -88,30 +88,30 @@ func (decoder *sliceDecoder) doDecode(ptr unsafe.Pointer, iter *Iterator) {
return return
} }
offset := uintptr(0) 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() { if !iter.ReadArray() {
slice.Len = 1 slice.Len = 1
return return
} }
offset += decoder.elemType.Size() 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() { if !iter.ReadArray() {
slice.Len = 2 slice.Len = 2
return return
} }
offset += decoder.elemType.Size() 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() { if !iter.ReadArray() {
slice.Len = 3 slice.Len = 3
return return
} }
offset += decoder.elemType.Size() 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 slice.Len = 4
for iter.ReadArray() { for iter.ReadArray() {
growOne(slice, decoder.sliceType, decoder.elemType) growOne(slice, decoder.sliceType, decoder.elemType)
offset += decoder.elemType.Size() offset += decoder.elemType.Size()
decoder.elemDecoder.decode(unsafe.Pointer(uintptr(slice.Data) + offset), iter) decoder.elemDecoder.decode(unsafe.Pointer(uintptr(slice.Data)+offset), iter)
} }
} }
@ -155,4 +155,4 @@ func reuseSlice(slice *sliceHeader, sliceType reflect.Type, expectedCap int) {
dst := unsafe.Pointer(reflect.MakeSlice(sliceType, 0, expectedCap).Pointer()) dst := unsafe.Pointer(reflect.MakeSlice(sliceType, 0, expectedCap).Pointer())
slice.Cap = expectedCap slice.Cap = expectedCap
slice.Data = dst slice.Data = dst
} }

View File

@ -1,11 +1,11 @@
package jsoniter package jsoniter
import ( import (
"unsafe"
"reflect"
"encoding/json"
"encoding" "encoding"
"encoding/json"
"reflect"
"strconv" "strconv"
"unsafe"
) )
type mapDecoder struct { type mapDecoder struct {

View File

@ -1,9 +1,9 @@
package jsoniter package jsoniter
import ( import (
"unsafe"
"encoding/json"
"encoding/base64" "encoding/base64"
"encoding/json"
"unsafe"
) )
type stringCodec struct { type stringCodec struct {
@ -386,7 +386,7 @@ type base64Codec struct {
func (codec *base64Codec) decode(ptr unsafe.Pointer, iter *Iterator) { func (codec *base64Codec) decode(ptr unsafe.Pointer, iter *Iterator) {
encoding := base64.StdEncoding encoding := base64.StdEncoding
src := iter.SkipAndReturnBytes() src := iter.SkipAndReturnBytes()
src = src[1:len(src)-1] src = src[1 : len(src)-1]
decodedLen := encoding.DecodedLen(len(src)) decodedLen := encoding.DecodedLen(len(src))
dst := make([]byte, decodedLen) dst := make([]byte, decodedLen)
_, err := encoding.Decode(dst, src) _, err := encoding.Decode(dst, src)

View File

@ -1,12 +1,12 @@
package jsoniter package jsoniter
import ( import (
"io"
"fmt" "fmt"
"io"
"reflect" "reflect"
"unsafe"
"strings" "strings"
"unicode" "unicode"
"unsafe"
) )
func encoderOfStruct(typ reflect.Type) (Encoder, error) { func encoderOfStruct(typ reflect.Type) (Encoder, error) {
@ -47,7 +47,7 @@ func encoderOfStruct(typ reflect.Type) (Encoder, error) {
} }
for _, fieldName := range fieldNames { for _, fieldName := range fieldNames {
fields[fieldName] = &structFieldEncoder{field, fieldName, encoder, omitempty} fields[fieldName] = &structFieldEncoder{field, fieldName, encoder, omitempty}
} }
} }
if len(fields) == 0 { if len(fields) == 0 {
return &emptyStructEncoder{}, nil return &emptyStructEncoder{}, nil
@ -138,7 +138,7 @@ func EnableUnexportedStructFieldsSupport() {
func createStructDecoder(typ reflect.Type, fields map[string]*structFieldDecoder) (Decoder, error) { func createStructDecoder(typ reflect.Type, fields map[string]*structFieldDecoder) (Decoder, error) {
knownHash := map[int32]struct{}{ knownHash := map[int32]struct{}{
0: struct{}{}, 0: {},
} }
switch len(fields) { switch len(fields) {
case 0: case 0:
@ -203,7 +203,7 @@ func createStructDecoder(typ reflect.Type, fields map[string]*structFieldDecoder
} }
} }
return &threeFieldsStructDecoder{typ, return &threeFieldsStructDecoder{typ,
fieldName1, fieldDecoder1, fieldName2, fieldDecoder2, fieldName3, fieldDecoder3}, nil fieldName1, fieldDecoder1, fieldName2, fieldDecoder2, fieldName3, fieldDecoder3}, nil
case 4: case 4:
var fieldName1 int32 var fieldName1 int32
var fieldName2 int32 var fieldName2 int32
@ -236,8 +236,8 @@ func createStructDecoder(typ reflect.Type, fields map[string]*structFieldDecoder
} }
} }
return &fourFieldsStructDecoder{typ, return &fourFieldsStructDecoder{typ,
fieldName1, fieldDecoder1, fieldName2, fieldDecoder2, fieldName3, fieldDecoder3, fieldName1, fieldDecoder1, fieldName2, fieldDecoder2, fieldName3, fieldDecoder3,
fieldName4, fieldDecoder4}, nil fieldName4, fieldDecoder4}, nil
case 5: case 5:
var fieldName1 int32 var fieldName1 int32
var fieldName2 int32 var fieldName2 int32
@ -275,8 +275,8 @@ func createStructDecoder(typ reflect.Type, fields map[string]*structFieldDecoder
} }
} }
return &fiveFieldsStructDecoder{typ, return &fiveFieldsStructDecoder{typ,
fieldName1, fieldDecoder1, fieldName2, fieldDecoder2, fieldName3, fieldDecoder3, fieldName1, fieldDecoder1, fieldName2, fieldDecoder2, fieldName3, fieldDecoder3,
fieldName4, fieldDecoder4, fieldName5, fieldDecoder5}, nil fieldName4, fieldDecoder4, fieldName5, fieldDecoder5}, nil
case 6: case 6:
var fieldName1 int32 var fieldName1 int32
var fieldName2 int32 var fieldName2 int32
@ -319,8 +319,8 @@ func createStructDecoder(typ reflect.Type, fields map[string]*structFieldDecoder
} }
} }
return &sixFieldsStructDecoder{typ, return &sixFieldsStructDecoder{typ,
fieldName1, fieldDecoder1, fieldName2, fieldDecoder2, fieldName3, fieldDecoder3, fieldName1, fieldDecoder1, fieldName2, fieldDecoder2, fieldName3, fieldDecoder3,
fieldName4, fieldDecoder4, fieldName5, fieldDecoder5, fieldName6, fieldDecoder6}, nil fieldName4, fieldDecoder4, fieldName5, fieldDecoder5, fieldName6, fieldDecoder6}, nil
case 7: case 7:
var fieldName1 int32 var fieldName1 int32
var fieldName2 int32 var fieldName2 int32
@ -368,9 +368,9 @@ func createStructDecoder(typ reflect.Type, fields map[string]*structFieldDecoder
} }
} }
return &sevenFieldsStructDecoder{typ, return &sevenFieldsStructDecoder{typ,
fieldName1, fieldDecoder1, fieldName2, fieldDecoder2, fieldName3, fieldDecoder3, fieldName1, fieldDecoder1, fieldName2, fieldDecoder2, fieldName3, fieldDecoder3,
fieldName4, fieldDecoder4, fieldName5, fieldDecoder5, fieldName6, fieldDecoder6, fieldName4, fieldDecoder4, fieldName5, fieldDecoder5, fieldName6, fieldDecoder6,
fieldName7, fieldDecoder7}, nil fieldName7, fieldDecoder7}, nil
case 8: case 8:
var fieldName1 int32 var fieldName1 int32
var fieldName2 int32 var fieldName2 int32
@ -423,9 +423,9 @@ func createStructDecoder(typ reflect.Type, fields map[string]*structFieldDecoder
} }
} }
return &eightFieldsStructDecoder{typ, return &eightFieldsStructDecoder{typ,
fieldName1, fieldDecoder1, fieldName2, fieldDecoder2, fieldName3, fieldDecoder3, fieldName1, fieldDecoder1, fieldName2, fieldDecoder2, fieldName3, fieldDecoder3,
fieldName4, fieldDecoder4, fieldName5, fieldDecoder5, fieldName6, fieldDecoder6, fieldName4, fieldDecoder4, fieldName5, fieldDecoder5, fieldName6, fieldDecoder6,
fieldName7, fieldDecoder7, fieldName8, fieldDecoder8}, nil fieldName7, fieldDecoder7, fieldName8, fieldDecoder8}, nil
case 9: case 9:
var fieldName1 int32 var fieldName1 int32
var fieldName2 int32 var fieldName2 int32
@ -483,9 +483,9 @@ func createStructDecoder(typ reflect.Type, fields map[string]*structFieldDecoder
} }
} }
return &nineFieldsStructDecoder{typ, return &nineFieldsStructDecoder{typ,
fieldName1, fieldDecoder1, fieldName2, fieldDecoder2, fieldName3, fieldDecoder3, fieldName1, fieldDecoder1, fieldName2, fieldDecoder2, fieldName3, fieldDecoder3,
fieldName4, fieldDecoder4, fieldName5, fieldDecoder5, fieldName6, fieldDecoder6, fieldName4, fieldDecoder4, fieldName5, fieldDecoder5, fieldName6, fieldDecoder6,
fieldName7, fieldDecoder7, fieldName8, fieldDecoder8, fieldName9, fieldDecoder9}, nil fieldName7, fieldDecoder7, fieldName8, fieldDecoder8, fieldName9, fieldDecoder9}, nil
case 10: case 10:
var fieldName1 int32 var fieldName1 int32
var fieldName2 int32 var fieldName2 int32
@ -548,10 +548,10 @@ func createStructDecoder(typ reflect.Type, fields map[string]*structFieldDecoder
} }
} }
return &tenFieldsStructDecoder{typ, return &tenFieldsStructDecoder{typ,
fieldName1, fieldDecoder1, fieldName2, fieldDecoder2, fieldName3, fieldDecoder3, fieldName1, fieldDecoder1, fieldName2, fieldDecoder2, fieldName3, fieldDecoder3,
fieldName4, fieldDecoder4, fieldName5, fieldDecoder5, fieldName6, fieldDecoder6, fieldName4, fieldDecoder4, fieldName5, fieldDecoder5, fieldName6, fieldDecoder6,
fieldName7, fieldDecoder7, fieldName8, fieldDecoder8, fieldName9, fieldDecoder9, fieldName7, fieldDecoder7, fieldName8, fieldDecoder8, fieldName9, fieldDecoder9,
fieldName10, fieldDecoder10}, nil fieldName10, fieldDecoder10}, nil
} }
return &generalStructDecoder{typ, fields}, nil return &generalStructDecoder{typ, fields}, nil
} }

View File

@ -319,8 +319,8 @@ func (stream *Stream) writeIndention(delta int) {
stream.writeByte('\n') stream.writeByte('\n')
toWrite := stream.indention - delta toWrite := stream.indention - delta
stream.ensure(toWrite) stream.ensure(toWrite)
for i := 0 ; i < toWrite && stream.n < len(stream.buf); i++ { for i := 0; i < toWrite && stream.n < len(stream.buf); i++ {
stream.buf[stream.n] = ' ' stream.buf[stream.n] = ' '
stream.n ++ stream.n++
} }
} }

View File

@ -21,12 +21,12 @@ func (stream *Stream) WriteFloat32Lossy(val float32) {
val = -val val = -val
} }
if val > 0x4ffffff { if val > 0x4ffffff {
stream.WriteRaw(strconv.FormatFloat(float64(val), 'f', -1, 32)); stream.WriteRaw(strconv.FormatFloat(float64(val), 'f', -1, 32))
return return
} }
precision := 6 precision := 6
exp := uint64(1000000) // 6 exp := uint64(1000000) // 6
lval := uint64(float64(val) * float64(exp) + 0.5) lval := uint64(float64(val)*float64(exp) + 0.5)
stream.WriteUint64(lval / exp) stream.WriteUint64(lval / exp)
fval := lval % exp fval := lval % exp
if fval == 0 { if fval == 0 {
@ -38,7 +38,7 @@ func (stream *Stream) WriteFloat32Lossy(val float32) {
stream.writeByte('0') stream.writeByte('0')
} }
stream.WriteUint64(fval) stream.WriteUint64(fval)
for stream.buf[stream.n - 1] == '0' { for stream.buf[stream.n-1] == '0' {
stream.n-- stream.n--
} }
} }
@ -53,12 +53,12 @@ func (stream *Stream) WriteFloat64Lossy(val float64) {
val = -val val = -val
} }
if val > 0x4ffffff { if val > 0x4ffffff {
stream.WriteRaw(strconv.FormatFloat(val, 'f', -1, 64)); stream.WriteRaw(strconv.FormatFloat(val, 'f', -1, 64))
return return
} }
precision := 6 precision := 6
exp := uint64(1000000) // 6 exp := uint64(1000000) // 6
lval := uint64(val * float64(exp) + 0.5) lval := uint64(val*float64(exp) + 0.5)
stream.WriteUint64(lval / exp) stream.WriteUint64(lval / exp)
fval := lval % exp fval := lval % exp
if fval == 0 { if fval == 0 {
@ -70,7 +70,7 @@ func (stream *Stream) WriteFloat64Lossy(val float64) {
stream.writeByte('0') stream.writeByte('0')
} }
stream.WriteUint64(fval) stream.WriteUint64(fval)
for stream.buf[stream.n - 1] == '0' { for stream.buf[stream.n-1] == '0' {
stream.n-- stream.n--
} }
} }
@ -85,4 +85,4 @@ func EnableLossyFloatMarshalling() {
val := *((*float64)(ptr)) val := *((*float64)(ptr))
stream.WriteFloat64Lossy(val) stream.WriteFloat64Lossy(val)
}) })
} }

View File

@ -5,7 +5,7 @@ var DIGITS []uint32
func init() { func init() {
DIGITS = make([]uint32, 1000) DIGITS = make([]uint32, 1000)
for i := uint32(0); i < 1000; i++ { for i := uint32(0); i < 1000; i++ {
DIGITS[i] = (((i / 100) + '0') << 16) + ((((i / 10) % 10) + '0') << 8) + i % 10 + '0'; DIGITS[i] = (((i / 100) + '0') << 16) + ((((i / 10) % 10) + '0') << 8) + i%10 + '0'
if i < 10 { if i < 10 {
DIGITS[i] += 2 << 24 DIGITS[i] += 2 << 24
} else if i < 100 { } else if i < 100 {
@ -32,8 +32,8 @@ func writeFirstBuf(buf []byte, v uint32, n int) int {
func writeBuf(buf []byte, v uint32, n int) { func writeBuf(buf []byte, v uint32, n int) {
buf[n] = byte(v >> 16) buf[n] = byte(v >> 16)
buf[n + 1] = byte(v >> 8) buf[n+1] = byte(v >> 8)
buf[n + 2] = byte(v) buf[n+2] = byte(v)
} }
func (stream *Stream) WriteUint8(val uint8) { func (stream *Stream) WriteUint8(val uint8) {
@ -62,7 +62,7 @@ func (stream *Stream) WriteUint16(val uint16) {
stream.n = writeFirstBuf(stream.buf, DIGITS[val], stream.n) stream.n = writeFirstBuf(stream.buf, DIGITS[val], stream.n)
return return
} }
r1 := val - q1 * 1000 r1 := val - q1*1000
n := writeFirstBuf(stream.buf, DIGITS[q1], stream.n) n := writeFirstBuf(stream.buf, DIGITS[q1], stream.n)
writeBuf(stream.buf, DIGITS[r1], n) writeBuf(stream.buf, DIGITS[r1], n)
stream.n = n + 3 stream.n = n + 3
@ -85,7 +85,7 @@ func (stream *Stream) WriteInt16(nval int16) {
stream.n = writeFirstBuf(stream.buf, DIGITS[val], n) stream.n = writeFirstBuf(stream.buf, DIGITS[val], n)
return return
} }
r1 := val - q1 * 1000 r1 := val - q1*1000
n = writeFirstBuf(stream.buf, DIGITS[q1], n) n = writeFirstBuf(stream.buf, DIGITS[q1], n)
writeBuf(stream.buf, DIGITS[r1], n) writeBuf(stream.buf, DIGITS[r1], n)
stream.n = n + 3 stream.n = n + 3
@ -100,7 +100,7 @@ func (stream *Stream) WriteUint32(val uint32) {
stream.n = writeFirstBuf(stream.buf, DIGITS[val], n) stream.n = writeFirstBuf(stream.buf, DIGITS[val], n)
return return
} }
r1 := val - q1 * 1000 r1 := val - q1*1000
q2 := q1 / 1000 q2 := q1 / 1000
if q2 == 0 { if q2 == 0 {
n := writeFirstBuf(stream.buf, DIGITS[q1], n) n := writeFirstBuf(stream.buf, DIGITS[q1], n)
@ -108,19 +108,19 @@ func (stream *Stream) WriteUint32(val uint32) {
stream.n = n + 3 stream.n = n + 3
return return
} }
r2 := q1 - q2 * 1000 r2 := q1 - q2*1000
q3 := q2 / 1000 q3 := q2 / 1000
if q3 == 0 { if q3 == 0 {
n = writeFirstBuf(stream.buf, DIGITS[q2], n) n = writeFirstBuf(stream.buf, DIGITS[q2], n)
} else { } else {
r3 := q2 - q3 * 1000 r3 := q2 - q3*1000
stream.buf[n] = byte(q3 + '0') stream.buf[n] = byte(q3 + '0')
n++ n++
writeBuf(stream.buf, DIGITS[r3], n) writeBuf(stream.buf, DIGITS[r3], n)
n += 3 n += 3
} }
writeBuf(stream.buf, DIGITS[r2], n) writeBuf(stream.buf, DIGITS[r2], n)
writeBuf(stream.buf, DIGITS[r1], n + 3) writeBuf(stream.buf, DIGITS[r1], n+3)
stream.n = n + 6 stream.n = n + 6
} }
@ -128,7 +128,7 @@ func (stream *Stream) WriteInt32(nval int32) {
stream.ensure(11) stream.ensure(11)
n := stream.n n := stream.n
var val uint32 var val uint32
if (nval < 0) { if nval < 0 {
val = uint32(-nval) val = uint32(-nval)
stream.buf[n] = '-' stream.buf[n] = '-'
n++ n++
@ -140,7 +140,7 @@ func (stream *Stream) WriteInt32(nval int32) {
stream.n = writeFirstBuf(stream.buf, DIGITS[val], n) stream.n = writeFirstBuf(stream.buf, DIGITS[val], n)
return return
} }
r1 := val - q1 * 1000 r1 := val - q1*1000
q2 := q1 / 1000 q2 := q1 / 1000
if q2 == 0 { if q2 == 0 {
n := writeFirstBuf(stream.buf, DIGITS[q1], n) n := writeFirstBuf(stream.buf, DIGITS[q1], n)
@ -148,19 +148,19 @@ func (stream *Stream) WriteInt32(nval int32) {
stream.n = n + 3 stream.n = n + 3
return return
} }
r2 := q1 - q2 * 1000 r2 := q1 - q2*1000
q3 := q2 / 1000 q3 := q2 / 1000
if q3 == 0 { if q3 == 0 {
n = writeFirstBuf(stream.buf, DIGITS[q2], n) n = writeFirstBuf(stream.buf, DIGITS[q2], n)
} else { } else {
r3 := q2 - q3 * 1000 r3 := q2 - q3*1000
stream.buf[n] = byte(q3 + '0') stream.buf[n] = byte(q3 + '0')
n++ n++
writeBuf(stream.buf, DIGITS[r3], n) writeBuf(stream.buf, DIGITS[r3], n)
n += 3 n += 3
} }
writeBuf(stream.buf, DIGITS[r2], n) writeBuf(stream.buf, DIGITS[r2], n)
writeBuf(stream.buf, DIGITS[r1], n + 3) writeBuf(stream.buf, DIGITS[r1], n+3)
stream.n = n + 6 stream.n = n + 6
} }
@ -172,7 +172,7 @@ func (stream *Stream) WriteUint64(val uint64) {
stream.n = writeFirstBuf(stream.buf, DIGITS[val], n) stream.n = writeFirstBuf(stream.buf, DIGITS[val], n)
return return
} }
r1 := val - q1 * 1000 r1 := val - q1*1000
q2 := q1 / 1000 q2 := q1 / 1000
if q2 == 0 { if q2 == 0 {
n := writeFirstBuf(stream.buf, DIGITS[q1], n) n := writeFirstBuf(stream.buf, DIGITS[q1], n)
@ -180,51 +180,51 @@ func (stream *Stream) WriteUint64(val uint64) {
stream.n = n + 3 stream.n = n + 3
return return
} }
r2 := q1 - q2 * 1000 r2 := q1 - q2*1000
q3 := q2 / 1000 q3 := q2 / 1000
if q3 == 0 { if q3 == 0 {
n = writeFirstBuf(stream.buf, DIGITS[q2], n) n = writeFirstBuf(stream.buf, DIGITS[q2], n)
writeBuf(stream.buf, DIGITS[r2], n) writeBuf(stream.buf, DIGITS[r2], n)
writeBuf(stream.buf, DIGITS[r1], n + 3) writeBuf(stream.buf, DIGITS[r1], n+3)
stream.n = n + 6 stream.n = n + 6
return return
} }
r3 := q2 - q3 * 1000 r3 := q2 - q3*1000
q4 := q3 / 1000 q4 := q3 / 1000
if q4 == 0 { if q4 == 0 {
n = writeFirstBuf(stream.buf, DIGITS[q3], n) n = writeFirstBuf(stream.buf, DIGITS[q3], n)
writeBuf(stream.buf, DIGITS[r3], n) writeBuf(stream.buf, DIGITS[r3], n)
writeBuf(stream.buf, DIGITS[r2], n + 3) writeBuf(stream.buf, DIGITS[r2], n+3)
writeBuf(stream.buf, DIGITS[r1], n + 6) writeBuf(stream.buf, DIGITS[r1], n+6)
stream.n = n + 9 stream.n = n + 9
return return
} }
r4 := q3 - q4 * 1000 r4 := q3 - q4*1000
q5 := q4 / 1000 q5 := q4 / 1000
if q5 == 0 { if q5 == 0 {
n = writeFirstBuf(stream.buf, DIGITS[q4], n) n = writeFirstBuf(stream.buf, DIGITS[q4], n)
writeBuf(stream.buf, DIGITS[r4], n) writeBuf(stream.buf, DIGITS[r4], n)
writeBuf(stream.buf, DIGITS[r3], n + 3) writeBuf(stream.buf, DIGITS[r3], n+3)
writeBuf(stream.buf, DIGITS[r2], n + 6) writeBuf(stream.buf, DIGITS[r2], n+6)
writeBuf(stream.buf, DIGITS[r1], n + 9) writeBuf(stream.buf, DIGITS[r1], n+9)
stream.n = n + 12 stream.n = n + 12
return return
} }
r5 := q4 - q5 * 1000 r5 := q4 - q5*1000
q6 := q5 / 1000 q6 := q5 / 1000
if q6 == 0 { if q6 == 0 {
n = writeFirstBuf(stream.buf, DIGITS[q5], n) n = writeFirstBuf(stream.buf, DIGITS[q5], n)
} else { } else {
n = writeFirstBuf(stream.buf, DIGITS[q6], n) n = writeFirstBuf(stream.buf, DIGITS[q6], n)
r6 := q5 - q6 * 1000 r6 := q5 - q6*1000
writeBuf(stream.buf, DIGITS[r6], n) writeBuf(stream.buf, DIGITS[r6], n)
n += 3 n += 3
} }
writeBuf(stream.buf, DIGITS[r5], n) writeBuf(stream.buf, DIGITS[r5], n)
writeBuf(stream.buf, DIGITS[r4], n + 3) writeBuf(stream.buf, DIGITS[r4], n+3)
writeBuf(stream.buf, DIGITS[r3], n + 6) writeBuf(stream.buf, DIGITS[r3], n+6)
writeBuf(stream.buf, DIGITS[r2], n + 9) writeBuf(stream.buf, DIGITS[r2], n+9)
writeBuf(stream.buf, DIGITS[r1], n + 12) writeBuf(stream.buf, DIGITS[r1], n+12)
stream.n = n + 15 stream.n = n + 15
} }
@ -232,7 +232,7 @@ func (stream *Stream) WriteInt64(nval int64) {
stream.ensure(20) stream.ensure(20)
n := stream.n n := stream.n
var val uint64 var val uint64
if (nval < 0) { if nval < 0 {
val = uint64(-nval) val = uint64(-nval)
stream.buf[n] = '-' stream.buf[n] = '-'
n++ n++
@ -244,7 +244,7 @@ func (stream *Stream) WriteInt64(nval int64) {
stream.n = writeFirstBuf(stream.buf, DIGITS[val], n) stream.n = writeFirstBuf(stream.buf, DIGITS[val], n)
return return
} }
r1 := val - q1 * 1000; r1 := val - q1*1000
q2 := q1 / 1000 q2 := q1 / 1000
if q2 == 0 { if q2 == 0 {
n := writeFirstBuf(stream.buf, DIGITS[q1], n) n := writeFirstBuf(stream.buf, DIGITS[q1], n)
@ -252,52 +252,52 @@ func (stream *Stream) WriteInt64(nval int64) {
stream.n = n + 3 stream.n = n + 3
return return
} }
r2 := q1 - q2 * 1000 r2 := q1 - q2*1000
q3 := q2 / 1000 q3 := q2 / 1000
if q3 == 0 { if q3 == 0 {
n = writeFirstBuf(stream.buf, DIGITS[q2], n) n = writeFirstBuf(stream.buf, DIGITS[q2], n)
writeBuf(stream.buf, DIGITS[r2], n) writeBuf(stream.buf, DIGITS[r2], n)
writeBuf(stream.buf, DIGITS[r1], n + 3) writeBuf(stream.buf, DIGITS[r1], n+3)
stream.n = n + 6 stream.n = n + 6
return return
} }
r3 := q2 - q3 * 1000 r3 := q2 - q3*1000
q4 := q3 / 1000 q4 := q3 / 1000
if q4 == 0 { if q4 == 0 {
n = writeFirstBuf(stream.buf, DIGITS[q3], n) n = writeFirstBuf(stream.buf, DIGITS[q3], n)
writeBuf(stream.buf, DIGITS[r3], n) writeBuf(stream.buf, DIGITS[r3], n)
writeBuf(stream.buf, DIGITS[r2], n + 3) writeBuf(stream.buf, DIGITS[r2], n+3)
writeBuf(stream.buf, DIGITS[r1], n + 6) writeBuf(stream.buf, DIGITS[r1], n+6)
stream.n = n + 9 stream.n = n + 9
return return
} }
r4 := q3 - q4 * 1000 r4 := q3 - q4*1000
q5 := q4 / 1000 q5 := q4 / 1000
if q5 == 0 { if q5 == 0 {
n = writeFirstBuf(stream.buf, DIGITS[q4], n) n = writeFirstBuf(stream.buf, DIGITS[q4], n)
writeBuf(stream.buf, DIGITS[r4], n) writeBuf(stream.buf, DIGITS[r4], n)
writeBuf(stream.buf, DIGITS[r3], n + 3) writeBuf(stream.buf, DIGITS[r3], n+3)
writeBuf(stream.buf, DIGITS[r2], n + 6) writeBuf(stream.buf, DIGITS[r2], n+6)
writeBuf(stream.buf, DIGITS[r1], n + 9) writeBuf(stream.buf, DIGITS[r1], n+9)
stream.n = n + 12 stream.n = n + 12
return return
} }
r5 := q4 - q5 * 1000 r5 := q4 - q5*1000
q6 := q5 / 1000 q6 := q5 / 1000
if q6 == 0 { if q6 == 0 {
n = writeFirstBuf(stream.buf, DIGITS[q5], n) n = writeFirstBuf(stream.buf, DIGITS[q5], n)
} else { } else {
stream.buf[n] = byte(q6 + '0') stream.buf[n] = byte(q6 + '0')
n++ n++
r6 := q5 - q6 * 1000 r6 := q5 - q6*1000
writeBuf(stream.buf, DIGITS[r6], n) writeBuf(stream.buf, DIGITS[r6], n)
n += 3 n += 3
} }
writeBuf(stream.buf, DIGITS[r5], n) writeBuf(stream.buf, DIGITS[r5], n)
writeBuf(stream.buf, DIGITS[r4], n + 3) writeBuf(stream.buf, DIGITS[r4], n+3)
writeBuf(stream.buf, DIGITS[r3], n + 6) writeBuf(stream.buf, DIGITS[r3], n+6)
writeBuf(stream.buf, DIGITS[r2], n + 9) writeBuf(stream.buf, DIGITS[r2], n+9)
writeBuf(stream.buf, DIGITS[r1], n + 12) writeBuf(stream.buf, DIGITS[r1], n+12)
stream.n = n + 15 stream.n = n + 15
} }
@ -307,4 +307,4 @@ func (stream *Stream) WriteInt(val int) {
func (stream *Stream) WriteUint(val uint) { func (stream *Stream) WriteUint(val uint) {
stream.WriteUint64(uint64(val)) stream.WriteUint64(uint64(val))
} }

View File

@ -1,11 +1,11 @@
package jsoniter package jsoniter
import ( import (
"testing"
"github.com/json-iterator/go/require"
"encoding/json"
"bytes" "bytes"
"encoding/json"
"github.com/json-iterator/go/require"
"io/ioutil" "io/ioutil"
"testing"
) )
func Test_new_decoder(t *testing.T) { func Test_new_decoder(t *testing.T) {
@ -57,4 +57,4 @@ func Test_use_number(t *testing.T) {
var obj2 interface{} var obj2 interface{}
should.Nil(decoder2.Decode(&obj2)) should.Nil(decoder2.Decode(&obj2))
should.Equal(json.Number("123"), obj2) should.Equal(json.Number("123"), obj2)
} }

View File

@ -1,11 +1,11 @@
package jsoniter package jsoniter
import ( import (
"encoding/json"
"testing"
"github.com/json-iterator/go/require"
"bytes" "bytes"
"encoding/json"
"github.com/json-iterator/go/require"
"io" "io"
"testing"
) )
func Test_empty_array(t *testing.T) { 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) { func Test_wrap_array(t *testing.T) {
should := require.New(t) should := require.New(t)
any := Wrap([]int{1,2,3}) any := Wrap([]int{1, 2, 3})
should.Equal("[1,2,3]", any.ToString()) should.Equal("[1,2,3]", any.ToString())
var element Any var element Any
var elements []int var elements []int
@ -93,9 +93,9 @@ func Test_wrap_array(t *testing.T) {
elements = append(elements, element.ToInt()) elements = append(elements, element.ToInt())
} }
should.Equal([]int{1, 2, 3}, elements) should.Equal([]int{1, 2, 3}, elements)
any = Wrap([]int{1,2,3}) any = Wrap([]int{1, 2, 3})
should.Equal(3, any.Size()) should.Equal(3, any.Size())
any = Wrap([]int{1,2,3}) any = Wrap([]int{1, 2, 3})
should.Equal(2, any.Get(1).ToInt()) should.Equal(2, any.Get(1).ToInt())
} }
@ -103,7 +103,7 @@ func Test_array_lazy_any_get(t *testing.T) {
should := require.New(t) should := require.New(t)
any, err := UnmarshalAnyFromString("[1,[2,3],4]") any, err := UnmarshalAnyFromString("[1,[2,3],4]")
should.Nil(err) 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()) 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) should := require.New(t)
any, err := UnmarshalAnyFromString("[[1],[2],[3,4]]") any, err := UnmarshalAnyFromString("[[1],[2],[3,4]]")
should.Nil(err) 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) { func Test_array_wrapper_any_get_all(t *testing.T) {
should := require.New(t) should := require.New(t)
any := wrapArray([][]int{ any := wrapArray([][]int{
[]int{1, 2}, {1, 2},
[]int{3, 4}, {3, 4},
[]int{5, 6}, {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) { func Test_array_lazy_any_get_invalid(t *testing.T) {
should := require.New(t) should := require.New(t)
any, err := UnmarshalAnyFromString("[]") any, err := UnmarshalAnyFromString("[]")
should.Nil(err) should.Nil(err)
should.Equal(Invalid, any.Get(1,1).ValueType()) should.Equal(Invalid, any.Get(1, 1).ValueType())
should.NotNil(any.Get(1,1).LastError()) should.NotNil(any.Get(1, 1).LastError())
should.Equal(Invalid, any.Get("1").ValueType()) should.Equal(Invalid, any.Get("1").ValueType())
should.NotNil(any.Get("1").LastError()) should.NotNil(any.Get("1").LastError())
} }
@ -244,7 +244,7 @@ func Test_write_val_empty_array(t *testing.T) {
func Test_write_array_of_interface_in_struct(t *testing.T) { func Test_write_array_of_interface_in_struct(t *testing.T) {
should := require.New(t) should := require.New(t)
type TestObject struct { type TestObject struct {
Field []interface{} Field []interface{}
Field2 string Field2 string
} }
val := TestObject{[]interface{}{1, 2}, ""} val := TestObject{[]interface{}{1, 2}, ""}
@ -266,10 +266,10 @@ func Test_json_RawMessage(t *testing.T) {
func Test_encode_byte_array(t *testing.T) { func Test_encode_byte_array(t *testing.T) {
should := require.New(t) should := require.New(t)
bytes, err := json.Marshal([]byte{1,2,3}) bytes, err := json.Marshal([]byte{1, 2, 3})
should.Nil(err) should.Nil(err)
should.Equal(`"AQID"`, string(bytes)) should.Equal(`"AQID"`, string(bytes))
bytes, err = Marshal([]byte{1,2,3}) bytes, err = Marshal([]byte{1, 2, 3})
should.Nil(err) should.Nil(err)
should.Equal(`"AQID"`, string(bytes)) should.Equal(`"AQID"`, string(bytes))
} }
@ -279,10 +279,10 @@ func Test_decode_byte_array(t *testing.T) {
data := []byte{} data := []byte{}
err := json.Unmarshal([]byte(`"AQID"`), &data) err := json.Unmarshal([]byte(`"AQID"`), &data)
should.Nil(err) should.Nil(err)
should.Equal([]byte{1,2,3}, data) should.Equal([]byte{1, 2, 3}, data)
err = Unmarshal([]byte(`"AQID"`), &data) err = Unmarshal([]byte(`"AQID"`), &data)
should.Nil(err) should.Nil(err)
should.Equal([]byte{1,2,3}, data) should.Equal([]byte{1, 2, 3}, data)
} }
func Benchmark_jsoniter_array(b *testing.B) { func Benchmark_jsoniter_array(b *testing.B) {

View File

@ -1,9 +1,9 @@
package jsoniter package jsoniter
import ( import (
"testing"
"bytes" "bytes"
"github.com/json-iterator/go/require" "github.com/json-iterator/go/require"
"testing"
) )
func Test_true(t *testing.T) { func Test_true(t *testing.T) {
@ -38,7 +38,6 @@ func Test_write_true_false(t *testing.T) {
should.Equal("truefalse", buf.String()) should.Equal("truefalse", buf.String())
} }
func Test_write_val_bool(t *testing.T) { func Test_write_val_bool(t *testing.T) {
should := require.New(t) should := require.New(t)
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
@ -47,4 +46,4 @@ func Test_write_val_bool(t *testing.T) {
stream.Flush() stream.Flush()
should.Nil(stream.Error) should.Nil(stream.Error)
should.Equal("true", buf.String()) should.Equal("true", buf.String())
} }

View File

@ -1,13 +1,13 @@
package jsoniter package jsoniter
import ( import (
"encoding/json"
"github.com/json-iterator/go/require"
"reflect" "reflect"
"strconv" "strconv"
"testing" "testing"
"time" "time"
"unsafe" "unsafe"
"github.com/json-iterator/go/require"
"encoding/json"
) )
func Test_customize_type_decoder(t *testing.T) { func Test_customize_type_decoder(t *testing.T) {
@ -179,7 +179,7 @@ func (obj *ObjectImplementedUnmarshaler) UnmarshalJSON([]byte) error {
func Test_unmarshaler(t *testing.T) { func Test_unmarshaler(t *testing.T) {
type TestObject struct { type TestObject struct {
Field *ObjectImplementedUnmarshaler Field *ObjectImplementedUnmarshaler
Field2 string Field2 string
} }
should := require.New(t) should := require.New(t)
@ -196,7 +196,7 @@ func Test_unmarshaler(t *testing.T) {
func Test_unmarshaler_and_decoder(t *testing.T) { func Test_unmarshaler_and_decoder(t *testing.T) {
type TestObject struct { type TestObject struct {
Field *ObjectImplementedUnmarshaler Field *ObjectImplementedUnmarshaler
Field2 string Field2 string
} }
should := require.New(t) should := require.New(t)
@ -213,4 +213,4 @@ func Test_unmarshaler_and_decoder(t *testing.T) {
err = Unmarshal([]byte(`{"Field":"hello"}`), &obj) err = Unmarshal([]byte(`{"Field":"hello"}`), &obj)
should.Nil(err) should.Nil(err)
should.Equal(10, int(*obj.Field)) should.Equal(10, int(*obj.Field))
} }

View File

@ -1,10 +1,10 @@
package jsoniter package jsoniter
import ( import (
"fmt"
"testing"
"github.com/json-iterator/go/require"
"encoding/json" "encoding/json"
"fmt"
"github.com/json-iterator/go/require"
"testing"
) )
func Test_bind_api_demo(t *testing.T) { func Test_bind_api_demo(t *testing.T) {
@ -25,13 +25,13 @@ func Test_iterator_api_demo(t *testing.T) {
} }
type People struct { type People struct {
Name string Name string
Gender string Gender string
Age int Age int
Address string Address string
Mobile string Mobile string
Country string Country string
Height int Height int
} }
func jsoniterMarshal(p *People) error { func jsoniterMarshal(p *People) error {
@ -84,4 +84,3 @@ func BenchmarkStdMarshal(b *testing.B) {
} }
} }
} }

View File

@ -1,9 +1,9 @@
package jsoniter package jsoniter
import ( import (
"github.com/json-iterator/go/require"
"io" "io"
"testing" "testing"
"github.com/json-iterator/go/require"
) )
func Test_string_end(t *testing.T) { func Test_string_end(t *testing.T) {

View File

@ -1,12 +1,12 @@
package jsoniter package jsoniter
import ( import (
"bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"testing"
"github.com/json-iterator/go/require" "github.com/json-iterator/go/require"
"bytes"
"strconv" "strconv"
"testing"
) )
func Test_read_big_float(t *testing.T) { func Test_read_big_float(t *testing.T) {
@ -46,14 +46,14 @@ func Test_read_float(t *testing.T) {
// streaming // streaming
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) { t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
should := require.New(t) should := require.New(t)
iter := Parse(bytes.NewBufferString(input + ","), 2) iter := Parse(bytes.NewBufferString(input+","), 2)
expected, err := strconv.ParseFloat(input, 32) expected, err := strconv.ParseFloat(input, 32)
should.Nil(err) should.Nil(err)
should.Equal(float32(expected), iter.ReadFloat32()) should.Equal(float32(expected), iter.ReadFloat32())
}) })
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) { t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
should := require.New(t) should := require.New(t)
iter := Parse(bytes.NewBufferString(input + ","), 2) iter := Parse(bytes.NewBufferString(input+","), 2)
expected, err := strconv.ParseFloat(input, 64) expected, err := strconv.ParseFloat(input, 64)
should.Nil(err) should.Nil(err)
should.Equal(expected, iter.ReadFloat64()) should.Equal(expected, iter.ReadFloat64())
@ -85,7 +85,7 @@ func Test_wrap_float(t *testing.T) {
func Test_write_float32(t *testing.T) { func Test_write_float32(t *testing.T) {
vals := []float32{0, 1, -1, 99, 0xff, 0xfff, 0xffff, 0xfffff, 0xffffff, 0x4ffffff, 0xfffffff, vals := []float32{0, 1, -1, 99, 0xff, 0xfff, 0xffff, 0xfffff, 0xffffff, 0x4ffffff, 0xfffffff,
-0x4ffffff, -0xfffffff, 1.2345, 1.23456, 1.234567, 1.001} -0x4ffffff, -0xfffffff, 1.2345, 1.23456, 1.234567, 1.001}
for _, val := range vals { for _, val := range vals {
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) { t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
should := require.New(t) should := require.New(t)
@ -118,7 +118,7 @@ func Test_write_float32(t *testing.T) {
func Test_write_float64(t *testing.T) { func Test_write_float64(t *testing.T) {
vals := []float64{0, 1, -1, 99, 0xff, 0xfff, 0xffff, 0xfffff, 0xffffff, 0x4ffffff, 0xfffffff, vals := []float64{0, 1, -1, 99, 0xff, 0xfff, 0xffff, 0xfffff, 0xffffff, 0x4ffffff, 0xfffffff,
-0x4ffffff, -0xfffffff, 1.2345, 1.23456, 1.234567, 1.001} -0x4ffffff, -0xfffffff, 1.2345, 1.23456, 1.234567, 1.001}
for _, val := range vals { for _, val := range vals {
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) { t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
should := require.New(t) should := require.New(t)

View File

@ -3,12 +3,12 @@ package jsoniter
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"testing"
"github.com/json-iterator/go/require"
"fmt" "fmt"
"strconv" "github.com/json-iterator/go/require"
"io/ioutil"
"io" "io"
"io/ioutil"
"strconv"
"testing"
) )
func Test_read_uint64_invalid(t *testing.T) { func Test_read_uint64_invalid(t *testing.T) {

View File

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

View File

@ -1,9 +1,9 @@
package jsoniter package jsoniter
import ( import (
"testing"
"github.com/json-iterator/go/require" "github.com/json-iterator/go/require"
"math/big" "math/big"
"testing"
) )
func Test_read_map(t *testing.T) { func Test_read_map(t *testing.T) {
@ -41,12 +41,12 @@ func Test_wrap_map(t *testing.T) {
vals[k] = v.ToString() 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) { func Test_map_wrapper_any_get_all(t *testing.T) {
should := require.New(t) should := require.New(t)
any := Wrap(map[string][]int{"Field1": []int{1, 2}}) any := Wrap(map[string][]int{"Field1": {1, 2}})
should.Equal(`{"Field1":1}`, any.Get('*', 0).ToString()) should.Equal(`{"Field1":1}`, any.Get('*', 0).ToString())
} }
@ -86,7 +86,7 @@ func Test_decode_int_key_map(t *testing.T) {
func Test_encode_TextMarshaler_key_map(t *testing.T) { func Test_encode_TextMarshaler_key_map(t *testing.T) {
should := require.New(t) should := require.New(t)
f, _, _ := big.ParseFloat("1", 10, 64, big.ToZero) f, _, _ := big.ParseFloat("1", 10, 64, big.ToZero)
val := map[*big.Float]string{f: "2"} val := map[*big.Float]string{f: "2"}
str, err := MarshalToString(val) str, err := MarshalToString(val)
should.Nil(err) should.Nil(err)
@ -100,4 +100,4 @@ func Test_decode_TextMarshaler_key_map(t *testing.T) {
str, err := MarshalToString(val) str, err := MarshalToString(val)
should.Nil(err) should.Nil(err)
should.Equal(`{"1":"2"}`, str) should.Equal(`{"1":"2"}`, str)
} }

View File

@ -1,9 +1,9 @@
package jsoniter package jsoniter
import ( import (
"testing"
"github.com/json-iterator/go/require"
"bytes" "bytes"
"github.com/json-iterator/go/require"
"testing"
) )
func Test_read_null(t *testing.T) { func Test_read_null(t *testing.T) {

View File

@ -1,10 +1,10 @@
package jsoniter package jsoniter
import ( import (
"encoding/json"
"testing"
"github.com/json-iterator/go/require"
"bytes" "bytes"
"encoding/json"
"github.com/json-iterator/go/require"
"testing"
) )
func Test_empty_object(t *testing.T) { func Test_empty_object(t *testing.T) {
@ -100,7 +100,7 @@ func Test_object_any_lazy_iterator(t *testing.T) {
should.False(hasNext) should.False(hasNext)
vals[k] = v.ToString() 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{} vals = map[string]string{}
for next, hasNext := any.IterateObject(); hasNext; { for next, hasNext := any.IterateObject(); hasNext; {
k, v, hasNext = next() k, v, hasNext = next()
@ -108,7 +108,7 @@ func Test_object_any_lazy_iterator(t *testing.T) {
vals[k] = v.ToString() 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) { func Test_object_any_with_two_lazy_iterators(t *testing.T) {
@ -194,7 +194,7 @@ func Test_wrap_object(t *testing.T) {
vals[k] = v.ToString() 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) { func Test_object_wrapper_any_get_all(t *testing.T) {

View File

@ -1,8 +1,8 @@
package jsoniter package jsoniter
import ( import (
"testing"
"github.com/json-iterator/go/require" "github.com/json-iterator/go/require"
"testing"
) )
func Test_encode_optional_int_pointer(t *testing.T) { func Test_encode_optional_int_pointer(t *testing.T) {
@ -43,4 +43,4 @@ func Test_encode_struct_with_optional_field(t *testing.T) {
should.Nil(err) should.Nil(err)
should.Contains(str, `"field1":null`) should.Contains(str, `"field1":null`)
should.Contains(str, `"field2":"world"`) should.Contains(str, `"field2":"world"`)
} }

View File

@ -1,8 +1,8 @@
package jsoniter package jsoniter
import ( import (
"testing"
"fmt" "fmt"
"testing"
) )
func Test_reflect_str(t *testing.T) { func Test_reflect_str(t *testing.T) {
@ -151,4 +151,4 @@ func Test_reflect_bool(t *testing.T) {
fmt.Println(iter.Error) fmt.Println(iter.Error)
t.Fatal(val) t.Fatal(val)
} }
} }

View File

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

View File

@ -3,9 +3,9 @@ package jsoniter
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/json-iterator/go/require"
"testing" "testing"
"unsafe" "unsafe"
"github.com/json-iterator/go/require"
) )
func Test_decode_slice(t *testing.T) { func Test_decode_slice(t *testing.T) {

View File

@ -1,8 +1,8 @@
package jsoniter package jsoniter
import ( import (
"testing"
"github.com/json-iterator/go/require" "github.com/json-iterator/go/require"
"testing"
) )
func Test_writeByte_should_grow_buffer(t *testing.T) { func Test_writeByte_should_grow_buffer(t *testing.T) {
@ -33,7 +33,7 @@ func Test_writeIndention_should_grow_buffer(t *testing.T) {
should := require.New(t) should := require.New(t)
stream := NewStream(nil, 1) stream := NewStream(nil, 1)
stream.IndentionStep = 2 stream.IndentionStep = 2
stream.WriteVal([]int{1,2,3}) stream.WriteVal([]int{1, 2, 3})
should.Equal("[\n 1,\n 2,\n 3\n]", string(stream.Buffer())) should.Equal("[\n 1,\n 2,\n 3\n]", string(stream.Buffer()))
} }
@ -51,4 +51,4 @@ func Test_writeString_should_grow_buffer(t *testing.T) {
stream.WriteString("123") stream.WriteString("123")
should.Nil(stream.Error) should.Nil(stream.Error)
should.Equal(`"123"`, string(stream.Buffer())) should.Equal(`"123"`, string(stream.Buffer()))
} }

View File

@ -3,15 +3,15 @@ package jsoniter
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"testing"
"github.com/json-iterator/go/require"
"fmt" "fmt"
"github.com/json-iterator/go/require"
"testing"
) )
func Test_read_normal_string(t *testing.T) { func Test_read_normal_string(t *testing.T) {
cases := map[string]string{ cases := map[string]string{
`"0123456789012345678901234567890123456789"`: `0123456789012345678901234567890123456789`, `"0123456789012345678901234567890123456789"`: `0123456789012345678901234567890123456789`,
`""`: ``, `""`: ``,
`"hello"`: `hello`, `"hello"`: `hello`,
} }
for input, output := range cases { for input, output := range cases {
@ -40,8 +40,8 @@ func Test_read_normal_string(t *testing.T) {
func Test_read_exotic_string(t *testing.T) { func Test_read_exotic_string(t *testing.T) {
cases := map[string]string{ cases := map[string]string{
`"hel\"lo"`: `hel"lo`, `"hel\"lo"`: `hel"lo`,
`"hel\nlo"`: "hel\nlo", `"hel\nlo"`: "hel\nlo",
`"\u4e2d\u6587"`: "中文", `"\u4e2d\u6587"`: "中文",
`"\ud83d\udc4a"`: "\xf0\x9f\x91\x8a", // surrogate `"\ud83d\udc4a"`: "\xf0\x9f\x91\x8a", // surrogate
} }