vendor: github.com/json-iterator/go v1.1.10

full diff: https://github.com/json-iterator/go/compare/v1.1.6...v1.1.10

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2021-06-21 12:43:28 +02:00
parent c451d82af9
commit f114f6c871
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
21 changed files with 277 additions and 117 deletions

View File

@ -41,7 +41,7 @@ github.com/hashicorp/golang-lru 7f827b33c0f158ec5dfbba01bb0b
github.com/imdario/mergo 1afb36080aec31e0d1528973ebe6721b191b0369 # v0.3.8 github.com/imdario/mergo 1afb36080aec31e0d1528973ebe6721b191b0369 # v0.3.8
github.com/inconshreveable/mousetrap 76626ae9c91c4f2a10f34cad8ce83ea42c93bb75 # v1.0.0 github.com/inconshreveable/mousetrap 76626ae9c91c4f2a10f34cad8ce83ea42c93bb75 # v1.0.0
github.com/jaguilar/vt100 ad4c4a5743050fb7f88ce968dca9422f72a0e3f2 git://github.com/tonistiigi/vt100.git github.com/jaguilar/vt100 ad4c4a5743050fb7f88ce968dca9422f72a0e3f2 git://github.com/tonistiigi/vt100.git
github.com/json-iterator/go 0ff49de124c6f76f8494e194af75bde0f1a49a29 # 1.1.6 github.com/json-iterator/go a1ca0830781e007c66b225121d2cdb3a649421f6 # v1.1.10
github.com/matttproud/golang_protobuf_extensions c12348ce28de40eed0136aa2b644d0ee0650e56c # v1.0.1 github.com/matttproud/golang_protobuf_extensions c12348ce28de40eed0136aa2b644d0ee0650e56c # v1.0.1
github.com/Microsoft/go-winio 5b44b70ab3ab4d291a7c1d28afe7b4afeced0ed4 # v0.4.15 github.com/Microsoft/go-winio 5b44b70ab3ab4d291a7c1d28afe7b4afeced0ed4 # v0.4.15
github.com/Microsoft/hcsshim 5bc557dd210ff2caf615e6e22d398123de77fc11 # v0.8.9 github.com/Microsoft/hcsshim 5bc557dd210ff2caf615e6e22d398123de77fc11 # v0.8.9

View File

@ -1,5 +1,5 @@
[![Sourcegraph](https://sourcegraph.com/github.com/json-iterator/go/-/badge.svg)](https://sourcegraph.com/github.com/json-iterator/go?badge) [![Sourcegraph](https://sourcegraph.com/github.com/json-iterator/go/-/badge.svg)](https://sourcegraph.com/github.com/json-iterator/go?badge)
[![GoDoc](http://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)](http://godoc.org/github.com/json-iterator/go) [![GoDoc](http://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)](https://pkg.go.dev/github.com/json-iterator/go)
[![Build Status](https://travis-ci.org/json-iterator/go.svg?branch=master)](https://travis-ci.org/json-iterator/go) [![Build Status](https://travis-ci.org/json-iterator/go.svg?branch=master)](https://travis-ci.org/json-iterator/go)
[![codecov](https://codecov.io/gh/json-iterator/go/branch/master/graph/badge.svg)](https://codecov.io/gh/json-iterator/go) [![codecov](https://codecov.io/gh/json-iterator/go/branch/master/graph/badge.svg)](https://codecov.io/gh/json-iterator/go)
[![rcard](https://goreportcard.com/badge/github.com/json-iterator/go)](https://goreportcard.com/report/github.com/json-iterator/go) [![rcard](https://goreportcard.com/badge/github.com/json-iterator/go)](https://goreportcard.com/report/github.com/json-iterator/go)
@ -19,7 +19,7 @@ Source code: https://github.com/json-iterator/go-benchmark/blob/master/src/githu
Raw Result (easyjson requires static code generation) Raw Result (easyjson requires static code generation)
| | ns/op | allocation bytes | allocation times | | | ns/op | allocation bytes | allocation times |
| --- | --- | --- | --- | | --------------- | ----------- | ---------------- | ---------------- |
| std decode | 35510 ns/op | 1960 B/op | 99 allocs/op | | std decode | 35510 ns/op | 1960 B/op | 99 allocs/op |
| easyjson decode | 8499 ns/op | 160 B/op | 4 allocs/op | | easyjson decode | 8499 ns/op | 160 B/op | 4 allocs/op |
| jsoniter decode | 5623 ns/op | 160 B/op | 3 allocs/op | | jsoniter decode | 5623 ns/op | 160 B/op | 3 allocs/op |
@ -44,7 +44,7 @@ json.Marshal(&data)
with with
```go ```go
import "github.com/json-iterator/go" import jsoniter "github.com/json-iterator/go"
var json = jsoniter.ConfigCompatibleWithStandardLibrary var json = jsoniter.ConfigCompatibleWithStandardLibrary
json.Marshal(&data) json.Marshal(&data)
@ -60,7 +60,7 @@ json.Unmarshal(input, &data)
with with
```go ```go
import "github.com/json-iterator/go" import jsoniter "github.com/json-iterator/go"
var json = jsoniter.ConfigCompatibleWithStandardLibrary var json = jsoniter.ConfigCompatibleWithStandardLibrary
json.Unmarshal(input, &data) json.Unmarshal(input, &data)
@ -78,10 +78,10 @@ go get github.com/json-iterator/go
Contributors Contributors
* [thockin](https://github.com/thockin) - [thockin](https://github.com/thockin)
* [mattn](https://github.com/mattn) - [mattn](https://github.com/mattn)
* [cch123](https://github.com/cch123) - [cch123](https://github.com/cch123)
* [Oleg Shaldybin](https://github.com/olegshaldybin) - [Oleg Shaldybin](https://github.com/olegshaldybin)
* [Jason Toffaletti](https://github.com/toffaletti) - [Jason Toffaletti](https://github.com/toffaletti)
Report issue or pull request, or email taowen@gmail.com, or [![Gitter chat](https://badges.gitter.im/gitterHQ/gitter.png)](https://gitter.im/json-iterator/Lobby) Report issue or pull request, or email taowen@gmail.com, or [![Gitter chat](https://badges.gitter.im/gitterHQ/gitter.png)](https://gitter.im/json-iterator/Lobby)

View File

@ -16,7 +16,7 @@ func Unmarshal(data []byte, v interface{}) error {
return ConfigDefault.Unmarshal(data, v) return ConfigDefault.Unmarshal(data, v)
} }
// UnmarshalFromString convenient method to read from string instead of []byte // UnmarshalFromString is a convenient method to read from string instead of []byte
func UnmarshalFromString(str string, v interface{}) error { func UnmarshalFromString(str string, v interface{}) error {
return ConfigDefault.UnmarshalFromString(str, v) return ConfigDefault.UnmarshalFromString(str, v)
} }

View File

@ -64,7 +64,6 @@ func (any *stringAny) ToInt64() int64 {
flag := 1 flag := 1
startPos := 0 startPos := 0
endPos := 0
if any.val[0] == '+' || any.val[0] == '-' { if any.val[0] == '+' || any.val[0] == '-' {
startPos = 1 startPos = 1
} }
@ -73,6 +72,7 @@ func (any *stringAny) ToInt64() int64 {
flag = -1 flag = -1
} }
endPos := startPos
for i := startPos; i < len(any.val); i++ { for i := startPos; i < len(any.val); i++ {
if any.val[i] >= '0' && any.val[i] <= '9' { if any.val[i] >= '0' && any.val[i] <= '9' {
endPos = i + 1 endPos = i + 1
@ -98,7 +98,6 @@ func (any *stringAny) ToUint64() uint64 {
} }
startPos := 0 startPos := 0
endPos := 0
if any.val[0] == '-' { if any.val[0] == '-' {
return 0 return 0
@ -107,6 +106,7 @@ func (any *stringAny) ToUint64() uint64 {
startPos = 1 startPos = 1
} }
endPos := startPos
for i := startPos; i < len(any.val); i++ { for i := startPos; i < len(any.val); i++ {
if any.val[i] >= '0' && any.val[i] <= '9' { if any.val[i] >= '0' && any.val[i] <= '9' {
endPos = i + 1 endPos = i + 1

View File

@ -183,11 +183,11 @@ func (cfg *frozenConfig) validateJsonRawMessage(extension EncoderExtension) {
encoder := &funcEncoder{func(ptr unsafe.Pointer, stream *Stream) { encoder := &funcEncoder{func(ptr unsafe.Pointer, stream *Stream) {
rawMessage := *(*json.RawMessage)(ptr) rawMessage := *(*json.RawMessage)(ptr)
iter := cfg.BorrowIterator([]byte(rawMessage)) iter := cfg.BorrowIterator([]byte(rawMessage))
defer cfg.ReturnIterator(iter)
iter.Read() iter.Read()
if iter.Error != nil { if iter.Error != nil && iter.Error != io.EOF {
stream.WriteRaw("null") stream.WriteRaw("null")
} else { } else {
cfg.ReturnIterator(iter)
stream.WriteRaw(string(rawMessage)) stream.WriteRaw(string(rawMessage))
} }
}, func(ptr unsafe.Pointer) bool { }, func(ptr unsafe.Pointer) bool {

11
vendor/github.com/json-iterator/go/go.mod generated vendored Normal file
View File

@ -0,0 +1,11 @@
module github.com/json-iterator/go
go 1.12
require (
github.com/davecgh/go-spew v1.1.1
github.com/google/gofuzz v1.0.0
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742
github.com/stretchr/testify v1.3.0
)

View File

@ -74,6 +74,7 @@ type Iterator struct {
buf []byte buf []byte
head int head int
tail int tail int
depth int
captureStartedAt int captureStartedAt int
captured []byte captured []byte
Error error Error error
@ -88,6 +89,7 @@ func NewIterator(cfg API) *Iterator {
buf: nil, buf: nil,
head: 0, head: 0,
tail: 0, tail: 0,
depth: 0,
} }
} }
@ -99,6 +101,7 @@ func Parse(cfg API, reader io.Reader, bufSize int) *Iterator {
buf: make([]byte, bufSize), buf: make([]byte, bufSize),
head: 0, head: 0,
tail: 0, tail: 0,
depth: 0,
} }
} }
@ -110,6 +113,7 @@ func ParseBytes(cfg API, input []byte) *Iterator {
buf: input, buf: input,
head: 0, head: 0,
tail: len(input), tail: len(input),
depth: 0,
} }
} }
@ -128,6 +132,7 @@ func (iter *Iterator) Reset(reader io.Reader) *Iterator {
iter.reader = reader iter.reader = reader
iter.head = 0 iter.head = 0
iter.tail = 0 iter.tail = 0
iter.depth = 0
return iter return iter
} }
@ -137,6 +142,7 @@ func (iter *Iterator) ResetBytes(input []byte) *Iterator {
iter.buf = input iter.buf = input
iter.head = 0 iter.head = 0
iter.tail = len(input) iter.tail = len(input)
iter.depth = 0
return iter return iter
} }
@ -320,3 +326,24 @@ func (iter *Iterator) Read() interface{} {
return nil return nil
} }
} }
// limit maximum depth of nesting, as allowed by https://tools.ietf.org/html/rfc7159#section-9
const maxDepth = 10000
func (iter *Iterator) incrementDepth() (success bool) {
iter.depth++
if iter.depth <= maxDepth {
return true
}
iter.ReportError("incrementDepth", "exceeded max depth")
return false
}
func (iter *Iterator) decrementDepth() (success bool) {
iter.depth--
if iter.depth >= 0 {
return true
}
iter.ReportError("decrementDepth", "unexpected negative nesting")
return false
}

View File

@ -28,26 +28,32 @@ func (iter *Iterator) ReadArray() (ret bool) {
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 == '[' {
if !iter.incrementDepth() {
return false
}
c = iter.nextToken() c = iter.nextToken()
if c != ']' { if c != ']' {
iter.unreadByte() iter.unreadByte()
if !callback(iter) { if !callback(iter) {
iter.decrementDepth()
return false return false
} }
c = iter.nextToken() c = iter.nextToken()
for c == ',' { for c == ',' {
if !callback(iter) { if !callback(iter) {
iter.decrementDepth()
return false return false
} }
c = iter.nextToken() c = iter.nextToken()
} }
if c != ']' { if c != ']' {
iter.ReportError("ReadArrayCB", "expect ] in the end, but found "+string([]byte{c})) iter.ReportError("ReadArrayCB", "expect ] in the end, but found "+string([]byte{c}))
iter.decrementDepth()
return false return false
} }
return true return iter.decrementDepth()
} }
return true return iter.decrementDepth()
} }
if c == 'n' { if c == 'n' {
iter.skipThreeBytes('u', 'l', 'l') iter.skipThreeBytes('u', 'l', 'l')

View File

@ -112,6 +112,9 @@ func (iter *Iterator) ReadObjectCB(callback func(*Iterator, string) bool) bool {
c := iter.nextToken() c := iter.nextToken()
var field string var field string
if c == '{' { if c == '{' {
if !iter.incrementDepth() {
return false
}
c = iter.nextToken() c = iter.nextToken()
if c == '"' { if c == '"' {
iter.unreadByte() iter.unreadByte()
@ -121,6 +124,7 @@ func (iter *Iterator) ReadObjectCB(callback func(*Iterator, string) bool) bool {
iter.ReportError("ReadObject", "expect : after object field, but found "+string([]byte{c})) iter.ReportError("ReadObject", "expect : after object field, but found "+string([]byte{c}))
} }
if !callback(iter, field) { if !callback(iter, field) {
iter.decrementDepth()
return false return false
} }
c = iter.nextToken() c = iter.nextToken()
@ -131,20 +135,23 @@ func (iter *Iterator) ReadObjectCB(callback func(*Iterator, string) bool) bool {
iter.ReportError("ReadObject", "expect : after object field, but found "+string([]byte{c})) iter.ReportError("ReadObject", "expect : after object field, but found "+string([]byte{c}))
} }
if !callback(iter, field) { if !callback(iter, field) {
iter.decrementDepth()
return false return false
} }
c = iter.nextToken() c = iter.nextToken()
} }
if c != '}' { if c != '}' {
iter.ReportError("ReadObjectCB", `object not ended with }`) iter.ReportError("ReadObjectCB", `object not ended with }`)
iter.decrementDepth()
return false return false
} }
return true return iter.decrementDepth()
} }
if c == '}' { if c == '}' {
return true return iter.decrementDepth()
} }
iter.ReportError("ReadObjectCB", `expect " after }, but found `+string([]byte{c})) iter.ReportError("ReadObjectCB", `expect " after {, but found `+string([]byte{c}))
iter.decrementDepth()
return false return false
} }
if c == 'n' { if c == 'n' {
@ -159,15 +166,20 @@ func (iter *Iterator) ReadObjectCB(callback func(*Iterator, string) bool) bool {
func (iter *Iterator) ReadMapCB(callback func(*Iterator, string) bool) bool { func (iter *Iterator) ReadMapCB(callback func(*Iterator, string) bool) bool {
c := iter.nextToken() c := iter.nextToken()
if c == '{' { if c == '{' {
if !iter.incrementDepth() {
return false
}
c = iter.nextToken() c = iter.nextToken()
if c == '"' { if c == '"' {
iter.unreadByte() iter.unreadByte()
field := iter.ReadString() field := iter.ReadString()
if iter.nextToken() != ':' { if iter.nextToken() != ':' {
iter.ReportError("ReadMapCB", "expect : after object field, but found "+string([]byte{c})) iter.ReportError("ReadMapCB", "expect : after object field, but found "+string([]byte{c}))
iter.decrementDepth()
return false return false
} }
if !callback(iter, field) { if !callback(iter, field) {
iter.decrementDepth()
return false return false
} }
c = iter.nextToken() c = iter.nextToken()
@ -175,23 +187,27 @@ func (iter *Iterator) ReadMapCB(callback func(*Iterator, string) bool) bool {
field = iter.ReadString() field = iter.ReadString()
if iter.nextToken() != ':' { if iter.nextToken() != ':' {
iter.ReportError("ReadMapCB", "expect : after object field, but found "+string([]byte{c})) iter.ReportError("ReadMapCB", "expect : after object field, but found "+string([]byte{c}))
iter.decrementDepth()
return false return false
} }
if !callback(iter, field) { if !callback(iter, field) {
iter.decrementDepth()
return false return false
} }
c = iter.nextToken() c = iter.nextToken()
} }
if c != '}' { if c != '}' {
iter.ReportError("ReadMapCB", `object not ended with }`) iter.ReportError("ReadMapCB", `object not ended with }`)
iter.decrementDepth()
return false return false
} }
return true return iter.decrementDepth()
} }
if c == '}' { if c == '}' {
return true return iter.decrementDepth()
} }
iter.ReportError("ReadMapCB", `expect " after }, but found `+string([]byte{c})) iter.ReportError("ReadMapCB", `expect " after {, but found `+string([]byte{c}))
iter.decrementDepth()
return false return false
} }
if c == 'n' { if c == 'n' {

View File

@ -37,17 +37,24 @@ func (iter *Iterator) SkipAndReturnBytes() []byte {
return iter.stopCapture() return iter.stopCapture()
} }
type captureBuffer struct { // SkipAndAppendBytes skips next JSON element and appends its content to
startedAt int // buffer, returning the result.
captured []byte func (iter *Iterator) SkipAndAppendBytes(buf []byte) []byte {
iter.startCaptureTo(buf, iter.head)
iter.Skip()
return iter.stopCapture()
} }
func (iter *Iterator) startCapture(captureStartedAt int) { func (iter *Iterator) startCaptureTo(buf []byte, captureStartedAt int) {
if iter.captured != nil { if iter.captured != nil {
panic("already in capture mode") panic("already in capture mode")
} }
iter.captureStartedAt = captureStartedAt iter.captureStartedAt = captureStartedAt
iter.captured = make([]byte, 0, 32) iter.captured = buf
}
func (iter *Iterator) startCapture(captureStartedAt int) {
iter.startCaptureTo(make([]byte, 0, 32), captureStartedAt)
} }
func (iter *Iterator) stopCapture() []byte { func (iter *Iterator) stopCapture() []byte {
@ -58,13 +65,7 @@ func (iter *Iterator) stopCapture() []byte {
remaining := iter.buf[iter.captureStartedAt:iter.head] remaining := iter.buf[iter.captureStartedAt:iter.head]
iter.captureStartedAt = -1 iter.captureStartedAt = -1
iter.captured = nil iter.captured = nil
if len(captured) == 0 { return append(captured, remaining...)
copied := make([]byte, len(remaining))
copy(copied, remaining)
return copied
}
captured = append(captured, remaining...)
return captured
} }
// Skip skips a json object and positions to relatively the next json object // Skip skips a json object and positions to relatively the next json object

View File

@ -22,6 +22,9 @@ func (iter *Iterator) skipNumber() {
func (iter *Iterator) skipArray() { func (iter *Iterator) skipArray() {
level := 1 level := 1
if !iter.incrementDepth() {
return
}
for { for {
for i := iter.head; i < iter.tail; i++ { for i := iter.head; i < iter.tail; i++ {
switch iter.buf[i] { switch iter.buf[i] {
@ -31,8 +34,14 @@ func (iter *Iterator) skipArray() {
i = iter.head - 1 // it will be i++ soon i = iter.head - 1 // it will be i++ soon
case '[': // If open symbol, increase level case '[': // If open symbol, increase level
level++ level++
if !iter.incrementDepth() {
return
}
case ']': // If close symbol, increase level case ']': // If close symbol, increase level
level-- level--
if !iter.decrementDepth() {
return
}
// If we have returned to the original level, we're done // If we have returned to the original level, we're done
if level == 0 { if level == 0 {
@ -50,6 +59,10 @@ func (iter *Iterator) skipArray() {
func (iter *Iterator) skipObject() { func (iter *Iterator) skipObject() {
level := 1 level := 1
if !iter.incrementDepth() {
return
}
for { for {
for i := iter.head; i < iter.tail; i++ { for i := iter.head; i < iter.tail; i++ {
switch iter.buf[i] { switch iter.buf[i] {
@ -59,8 +72,14 @@ func (iter *Iterator) skipObject() {
i = iter.head - 1 // it will be i++ soon i = iter.head - 1 // it will be i++ soon
case '{': // If open symbol, increase level case '{': // If open symbol, increase level
level++ level++
if !iter.incrementDepth() {
return
}
case '}': // If close symbol, increase level case '}': // If close symbol, increase level
level-- level--
if !iter.decrementDepth() {
return
}
// If we have returned to the original level, we're done // If we have returned to the original level, we're done
if level == 0 { if level == 0 {

View File

@ -60,6 +60,7 @@ func (b *ctx) append(prefix string) *ctx {
// ReadVal copy the underlying JSON into go interface, same as json.Unmarshal // ReadVal copy the underlying JSON into go interface, same as json.Unmarshal
func (iter *Iterator) ReadVal(obj interface{}) { func (iter *Iterator) ReadVal(obj interface{}) {
depth := iter.depth
cacheKey := reflect2.RTypeOf(obj) cacheKey := reflect2.RTypeOf(obj)
decoder := iter.cfg.getDecoderFromCache(cacheKey) decoder := iter.cfg.getDecoderFromCache(cacheKey)
if decoder == nil { if decoder == nil {
@ -76,6 +77,10 @@ func (iter *Iterator) ReadVal(obj interface{}) {
return return
} }
decoder.Decode(ptr, iter) decoder.Decode(ptr, iter)
if iter.depth != depth {
iter.ReportError("ReadVal", "unexpected mismatched nesting")
return
}
} }
// WriteVal copy the go interface into underlying JSON, same as json.Marshal // WriteVal copy the go interface into underlying JSON, same as json.Marshal

View File

@ -341,10 +341,10 @@ func describeStruct(ctx *ctx, typ reflect2.Type) *StructDescriptor {
if ctx.onlyTaggedField && !hastag && !field.Anonymous() { if ctx.onlyTaggedField && !hastag && !field.Anonymous() {
continue continue
} }
tagParts := strings.Split(tag, ",") if tag == "-" || field.Name() == "_" {
if tag == "-" {
continue continue
} }
tagParts := strings.Split(tag, ",")
if field.Anonymous() && (tag == "" || tagParts[0] == "") { if field.Anonymous() && (tag == "" || tagParts[0] == "") {
if field.Type().Kind() == reflect.Struct { if field.Type().Kind() == reflect.Struct {
structDescriptor := describeStruct(ctx, field.Type()) structDescriptor := describeStruct(ctx, field.Type())
@ -475,7 +475,7 @@ func calcFieldNames(originalFieldName string, tagProvidedFieldName string, whole
fieldNames = []string{tagProvidedFieldName} fieldNames = []string{tagProvidedFieldName}
} }
// private? // private?
isNotExported := unicode.IsLower(rune(originalFieldName[0])) isNotExported := unicode.IsLower(rune(originalFieldName[0])) || originalFieldName[0] == '_'
if isNotExported { if isNotExported {
fieldNames = []string{} fieldNames = []string{}
} }

View File

@ -49,20 +49,7 @@ func decoderOfMapKey(ctx *ctx, typ reflect2.Type) ValDecoder {
return decoder return decoder
} }
} }
switch typ.Kind() {
case reflect.String:
return decoderOfType(ctx, reflect2.DefaultTypeOfKind(reflect.String))
case reflect.Bool,
reflect.Uint8, reflect.Int8,
reflect.Uint16, reflect.Int16,
reflect.Uint32, reflect.Int32,
reflect.Uint64, reflect.Int64,
reflect.Uint, reflect.Int,
reflect.Float32, reflect.Float64,
reflect.Uintptr:
typ = reflect2.DefaultTypeOfKind(typ.Kind())
return &numericMapKeyDecoder{decoderOfType(ctx, typ)}
default:
ptrType := reflect2.PtrTo(typ) ptrType := reflect2.PtrTo(typ)
if ptrType.Implements(unmarshalerType) { if ptrType.Implements(unmarshalerType) {
return &referenceDecoder{ return &referenceDecoder{
@ -88,6 +75,21 @@ func decoderOfMapKey(ctx *ctx, typ reflect2.Type) ValDecoder {
valType: typ, valType: typ,
} }
} }
switch typ.Kind() {
case reflect.String:
return decoderOfType(ctx, reflect2.DefaultTypeOfKind(reflect.String))
case reflect.Bool,
reflect.Uint8, reflect.Int8,
reflect.Uint16, reflect.Int16,
reflect.Uint32, reflect.Int32,
reflect.Uint64, reflect.Int64,
reflect.Uint, reflect.Int,
reflect.Float32, reflect.Float64,
reflect.Uintptr:
typ = reflect2.DefaultTypeOfKind(typ.Kind())
return &numericMapKeyDecoder{decoderOfType(ctx, typ)}
default:
return &lazyErrorDecoder{err: fmt.Errorf("unsupported map key type: %v", typ)} return &lazyErrorDecoder{err: fmt.Errorf("unsupported map key type: %v", typ)}
} }
} }
@ -103,6 +105,19 @@ func encoderOfMapKey(ctx *ctx, typ reflect2.Type) ValEncoder {
return encoder return encoder
} }
} }
if typ == textMarshalerType {
return &directTextMarshalerEncoder{
stringEncoder: ctx.EncoderOf(reflect2.TypeOf("")),
}
}
if typ.Implements(textMarshalerType) {
return &textMarshalerEncoder{
valType: typ,
stringEncoder: ctx.EncoderOf(reflect2.TypeOf("")),
}
}
switch typ.Kind() { switch typ.Kind() {
case reflect.String: case reflect.String:
return encoderOfType(ctx, reflect2.DefaultTypeOfKind(reflect.String)) return encoderOfType(ctx, reflect2.DefaultTypeOfKind(reflect.String))
@ -117,17 +132,6 @@ func encoderOfMapKey(ctx *ctx, typ reflect2.Type) ValEncoder {
typ = reflect2.DefaultTypeOfKind(typ.Kind()) typ = reflect2.DefaultTypeOfKind(typ.Kind())
return &numericMapKeyEncoder{encoderOfType(ctx, typ)} return &numericMapKeyEncoder{encoderOfType(ctx, typ)}
default: default:
if typ == textMarshalerType {
return &directTextMarshalerEncoder{
stringEncoder: ctx.EncoderOf(reflect2.TypeOf("")),
}
}
if typ.Implements(textMarshalerType) {
return &textMarshalerEncoder{
valType: typ,
stringEncoder: ctx.EncoderOf(reflect2.TypeOf("")),
}
}
if typ.Kind() == reflect.Interface { if typ.Kind() == reflect.Interface {
return &dynamicMapKeyEncoder{ctx, typ} return &dynamicMapKeyEncoder{ctx, typ}
} }
@ -163,10 +167,6 @@ func (decoder *mapDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
if c == '}' { if c == '}' {
return return
} }
if c != '"' {
iter.ReportError("ReadMapCB", `expect " after }, but found `+string([]byte{c}))
return
}
iter.unreadByte() iter.unreadByte()
key := decoder.keyType.UnsafeNew() key := decoder.keyType.UnsafeNew()
decoder.keyDecoder.Decode(key, iter) decoder.keyDecoder.Decode(key, iter)
@ -249,6 +249,10 @@ type mapEncoder struct {
} }
func (encoder *mapEncoder) Encode(ptr unsafe.Pointer, stream *Stream) { func (encoder *mapEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
if *(*unsafe.Pointer)(ptr) == nil {
stream.WriteNil()
return
}
stream.WriteObjectStart() stream.WriteObjectStart()
iter := encoder.mapType.UnsafeIterate(ptr) iter := encoder.mapType.UnsafeIterate(ptr)
for i := 0; iter.HasNext(); i++ { for i := 0; iter.HasNext(); i++ {
@ -286,16 +290,17 @@ func (encoder *sortKeysMapEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
stream.WriteObjectStart() stream.WriteObjectStart()
mapIter := encoder.mapType.UnsafeIterate(ptr) mapIter := encoder.mapType.UnsafeIterate(ptr)
subStream := stream.cfg.BorrowStream(nil) subStream := stream.cfg.BorrowStream(nil)
subStream.Attachment = stream.Attachment
subIter := stream.cfg.BorrowIterator(nil) subIter := stream.cfg.BorrowIterator(nil)
keyValues := encodedKeyValues{} keyValues := encodedKeyValues{}
for mapIter.HasNext() { for mapIter.HasNext() {
subStream.buf = make([]byte, 0, 64)
key, elem := mapIter.UnsafeNext() key, elem := mapIter.UnsafeNext()
subStreamIndex := subStream.Buffered()
encoder.keyEncoder.Encode(key, subStream) encoder.keyEncoder.Encode(key, subStream)
if subStream.Error != nil && subStream.Error != io.EOF && stream.Error == nil { if subStream.Error != nil && subStream.Error != io.EOF && stream.Error == nil {
stream.Error = subStream.Error stream.Error = subStream.Error
} }
encodedKey := subStream.Buffer() encodedKey := subStream.Buffer()[subStreamIndex:]
subIter.ResetBytes(encodedKey) subIter.ResetBytes(encodedKey)
decodedKey := subIter.ReadString() decodedKey := subIter.ReadString()
if stream.indention > 0 { if stream.indention > 0 {
@ -306,7 +311,7 @@ func (encoder *sortKeysMapEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
encoder.elemEncoder.Encode(elem, subStream) encoder.elemEncoder.Encode(elem, subStream)
keyValues = append(keyValues, encodedKV{ keyValues = append(keyValues, encodedKV{
key: decodedKey, key: decodedKey,
keyValue: subStream.Buffer(), keyValue: subStream.Buffer()[subStreamIndex:],
}) })
} }
sort.Sort(keyValues) sort.Sort(keyValues)
@ -316,6 +321,9 @@ func (encoder *sortKeysMapEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
} }
stream.Write(keyValue.keyValue) stream.Write(keyValue.keyValue)
} }
if subStream.Error != nil && stream.Error == nil {
stream.Error = subStream.Error
}
stream.WriteObjectEnd() stream.WriteObjectEnd()
stream.cfg.ReturnStream(subStream) stream.cfg.ReturnStream(subStream)
stream.cfg.ReturnIterator(subIter) stream.cfg.ReturnIterator(subIter)

View File

@ -3,8 +3,9 @@ package jsoniter
import ( import (
"encoding" "encoding"
"encoding/json" "encoding/json"
"github.com/modern-go/reflect2"
"unsafe" "unsafe"
"github.com/modern-go/reflect2"
) )
var marshalerType = reflect2.TypeOfPtr((*json.Marshaler)(nil)).Elem() var marshalerType = reflect2.TypeOfPtr((*json.Marshaler)(nil)).Elem()
@ -93,10 +94,17 @@ func (encoder *marshalerEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
stream.WriteNil() stream.WriteNil()
return return
} }
bytes, err := json.Marshal(obj) marshaler := obj.(json.Marshaler)
bytes, err := marshaler.MarshalJSON()
if err != nil { if err != nil {
stream.Error = err stream.Error = err
} else { } else {
// html escape was already done by jsoniter
// but the extra '\n' should be trimed
l := len(bytes)
if l > 0 && bytes[l-1] == '\n' {
bytes = bytes[:l-1]
}
stream.Write(bytes) stream.Write(bytes)
} }
} }

View File

@ -432,17 +432,19 @@ func (codec *base64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
} }
func (codec *base64Codec) Encode(ptr unsafe.Pointer, stream *Stream) { func (codec *base64Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
src := *((*[]byte)(ptr)) if codec.sliceType.UnsafeIsNil(ptr) {
if len(src) == 0 {
stream.WriteNil() stream.WriteNil()
return return
} }
src := *((*[]byte)(ptr))
encoding := base64.StdEncoding encoding := base64.StdEncoding
stream.writeByte('"') stream.writeByte('"')
if len(src) != 0 {
size := encoding.EncodedLen(len(src)) size := encoding.EncodedLen(len(src))
buf := make([]byte, size) buf := make([]byte, size)
encoding.Encode(buf, src) encoding.Encode(buf, src)
stream.buf = append(stream.buf, buf...) stream.buf = append(stream.buf, buf...)
}
stream.writeByte('"') stream.writeByte('"')
} }

View File

@ -2,7 +2,6 @@ package jsoniter
import ( import (
"github.com/modern-go/reflect2" "github.com/modern-go/reflect2"
"reflect"
"unsafe" "unsafe"
) )
@ -10,9 +9,6 @@ func decoderOfOptional(ctx *ctx, typ reflect2.Type) ValDecoder {
ptrType := typ.(*reflect2.UnsafePtrType) ptrType := typ.(*reflect2.UnsafePtrType)
elemType := ptrType.Elem() elemType := ptrType.Elem()
decoder := decoderOfType(ctx, elemType) decoder := decoderOfType(ctx, elemType)
if ctx.prefix == "" && elemType.Kind() == reflect.Ptr {
return &dereferenceDecoder{elemType, decoder}
}
return &OptionalDecoder{elemType, decoder} return &OptionalDecoder{elemType, decoder}
} }

View File

@ -500,16 +500,20 @@ func (decoder *generalStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator)
if !iter.readObjectStart() { if !iter.readObjectStart() {
return return
} }
if !iter.incrementDepth() {
return
}
var c byte var c byte
for c = ','; c == ','; c = iter.nextToken() { for c = ','; c == ','; c = iter.nextToken() {
decoder.decodeOneField(ptr, iter) decoder.decodeOneField(ptr, iter)
} }
if iter.Error != nil && iter.Error != io.EOF { if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error()) iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
} }
if c != '}' { if c != '}' {
iter.ReportError("struct Decode", `expect }, but found `+string([]byte{c})) iter.ReportError("struct Decode", `expect }, but found `+string([]byte{c}))
} }
iter.decrementDepth()
} }
func (decoder *generalStructDecoder) decodeOneField(ptr unsafe.Pointer, iter *Iterator) { func (decoder *generalStructDecoder) decodeOneField(ptr unsafe.Pointer, iter *Iterator) {
@ -530,8 +534,8 @@ func (decoder *generalStructDecoder) decodeOneField(ptr unsafe.Pointer, iter *It
} }
} }
if fieldDecoder == nil { if fieldDecoder == nil {
msg := "found unknown field: " + field
if decoder.disallowUnknownFields { if decoder.disallowUnknownFields {
msg := "found unknown field: " + field
iter.ReportError("ReadObject", msg) iter.ReportError("ReadObject", msg)
} }
c := iter.nextToken() c := iter.nextToken()
@ -571,6 +575,9 @@ func (decoder *oneFieldStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator)
if !iter.readObjectStart() { if !iter.readObjectStart() {
return return
} }
if !iter.incrementDepth() {
return
}
for { for {
if iter.readFieldHash() == decoder.fieldHash { if iter.readFieldHash() == decoder.fieldHash {
decoder.fieldDecoder.Decode(ptr, iter) decoder.fieldDecoder.Decode(ptr, iter)
@ -581,9 +588,10 @@ func (decoder *oneFieldStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator)
break break
} }
} }
if iter.Error != nil && iter.Error != io.EOF { if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error()) iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
} }
iter.decrementDepth()
} }
type twoFieldsStructDecoder struct { type twoFieldsStructDecoder struct {
@ -598,6 +606,9 @@ func (decoder *twoFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator
if !iter.readObjectStart() { if !iter.readObjectStart() {
return return
} }
if !iter.incrementDepth() {
return
}
for { for {
switch iter.readFieldHash() { switch iter.readFieldHash() {
case decoder.fieldHash1: case decoder.fieldHash1:
@ -611,9 +622,10 @@ func (decoder *twoFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator
break break
} }
} }
if iter.Error != nil && iter.Error != io.EOF { if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error()) iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
} }
iter.decrementDepth()
} }
type threeFieldsStructDecoder struct { type threeFieldsStructDecoder struct {
@ -630,6 +642,9 @@ func (decoder *threeFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterat
if !iter.readObjectStart() { if !iter.readObjectStart() {
return return
} }
if !iter.incrementDepth() {
return
}
for { for {
switch iter.readFieldHash() { switch iter.readFieldHash() {
case decoder.fieldHash1: case decoder.fieldHash1:
@ -645,9 +660,10 @@ func (decoder *threeFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterat
break break
} }
} }
if iter.Error != nil && iter.Error != io.EOF { if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error()) iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
} }
iter.decrementDepth()
} }
type fourFieldsStructDecoder struct { type fourFieldsStructDecoder struct {
@ -666,6 +682,9 @@ func (decoder *fourFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterato
if !iter.readObjectStart() { if !iter.readObjectStart() {
return return
} }
if !iter.incrementDepth() {
return
}
for { for {
switch iter.readFieldHash() { switch iter.readFieldHash() {
case decoder.fieldHash1: case decoder.fieldHash1:
@ -683,9 +702,10 @@ func (decoder *fourFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterato
break break
} }
} }
if iter.Error != nil && iter.Error != io.EOF { if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error()) iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
} }
iter.decrementDepth()
} }
type fiveFieldsStructDecoder struct { type fiveFieldsStructDecoder struct {
@ -706,6 +726,9 @@ func (decoder *fiveFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterato
if !iter.readObjectStart() { if !iter.readObjectStart() {
return return
} }
if !iter.incrementDepth() {
return
}
for { for {
switch iter.readFieldHash() { switch iter.readFieldHash() {
case decoder.fieldHash1: case decoder.fieldHash1:
@ -725,9 +748,10 @@ func (decoder *fiveFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterato
break break
} }
} }
if iter.Error != nil && iter.Error != io.EOF { if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error()) iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
} }
iter.decrementDepth()
} }
type sixFieldsStructDecoder struct { type sixFieldsStructDecoder struct {
@ -750,6 +774,9 @@ func (decoder *sixFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator
if !iter.readObjectStart() { if !iter.readObjectStart() {
return return
} }
if !iter.incrementDepth() {
return
}
for { for {
switch iter.readFieldHash() { switch iter.readFieldHash() {
case decoder.fieldHash1: case decoder.fieldHash1:
@ -771,9 +798,10 @@ func (decoder *sixFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator
break break
} }
} }
if iter.Error != nil && iter.Error != io.EOF { if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error()) iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
} }
iter.decrementDepth()
} }
type sevenFieldsStructDecoder struct { type sevenFieldsStructDecoder struct {
@ -798,6 +826,9 @@ func (decoder *sevenFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterat
if !iter.readObjectStart() { if !iter.readObjectStart() {
return return
} }
if !iter.incrementDepth() {
return
}
for { for {
switch iter.readFieldHash() { switch iter.readFieldHash() {
case decoder.fieldHash1: case decoder.fieldHash1:
@ -821,9 +852,10 @@ func (decoder *sevenFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterat
break break
} }
} }
if iter.Error != nil && iter.Error != io.EOF { if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error()) iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
} }
iter.decrementDepth()
} }
type eightFieldsStructDecoder struct { type eightFieldsStructDecoder struct {
@ -850,6 +882,9 @@ func (decoder *eightFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterat
if !iter.readObjectStart() { if !iter.readObjectStart() {
return return
} }
if !iter.incrementDepth() {
return
}
for { for {
switch iter.readFieldHash() { switch iter.readFieldHash() {
case decoder.fieldHash1: case decoder.fieldHash1:
@ -875,9 +910,10 @@ func (decoder *eightFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterat
break break
} }
} }
if iter.Error != nil && iter.Error != io.EOF { if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error()) iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
} }
iter.decrementDepth()
} }
type nineFieldsStructDecoder struct { type nineFieldsStructDecoder struct {
@ -906,6 +942,9 @@ func (decoder *nineFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterato
if !iter.readObjectStart() { if !iter.readObjectStart() {
return return
} }
if !iter.incrementDepth() {
return
}
for { for {
switch iter.readFieldHash() { switch iter.readFieldHash() {
case decoder.fieldHash1: case decoder.fieldHash1:
@ -933,9 +972,10 @@ func (decoder *nineFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterato
break break
} }
} }
if iter.Error != nil && iter.Error != io.EOF { if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error()) iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
} }
iter.decrementDepth()
} }
type tenFieldsStructDecoder struct { type tenFieldsStructDecoder struct {
@ -966,6 +1006,9 @@ func (decoder *tenFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator
if !iter.readObjectStart() { if !iter.readObjectStart() {
return return
} }
if !iter.incrementDepth() {
return
}
for { for {
switch iter.readFieldHash() { switch iter.readFieldHash() {
case decoder.fieldHash1: case decoder.fieldHash1:
@ -995,9 +1038,10 @@ func (decoder *tenFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator
break break
} }
} }
if iter.Error != nil && iter.Error != io.EOF { if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error()) iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
} }
iter.decrementDepth()
} }
type structFieldDecoder struct { type structFieldDecoder struct {

View File

@ -200,6 +200,7 @@ type stringModeStringEncoder struct {
func (encoder *stringModeStringEncoder) Encode(ptr unsafe.Pointer, stream *Stream) { func (encoder *stringModeStringEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
tempStream := encoder.cfg.BorrowStream(nil) tempStream := encoder.cfg.BorrowStream(nil)
tempStream.Attachment = stream.Attachment
defer encoder.cfg.ReturnStream(tempStream) defer encoder.cfg.ReturnStream(tempStream)
encoder.elemEncoder.Encode(ptr, tempStream) encoder.elemEncoder.Encode(ptr, tempStream)
stream.WriteString(string(tempStream.Buffer())) stream.WriteString(string(tempStream.Buffer()))

View File

@ -103,14 +103,14 @@ func (stream *Stream) Flush() error {
if stream.Error != nil { if stream.Error != nil {
return stream.Error return stream.Error
} }
n, err := stream.out.Write(stream.buf) _, err := stream.out.Write(stream.buf)
if err != nil { if err != nil {
if stream.Error == nil { if stream.Error == nil {
stream.Error = err stream.Error = err
} }
return err return err
} }
stream.buf = stream.buf[n:] stream.buf = stream.buf[:0]
return nil return nil
} }
@ -177,7 +177,6 @@ func (stream *Stream) WriteEmptyObject() {
func (stream *Stream) WriteMore() { func (stream *Stream) WriteMore() {
stream.writeByte(',') stream.writeByte(',')
stream.writeIndention(0) stream.writeIndention(0)
stream.Flush()
} }
// WriteArrayStart write [ with possible indention // WriteArrayStart write [ with possible indention

View File

@ -1,6 +1,7 @@
package jsoniter package jsoniter
import ( import (
"fmt"
"math" "math"
"strconv" "strconv"
) )
@ -13,6 +14,10 @@ func init() {
// WriteFloat32 write float32 to stream // WriteFloat32 write float32 to stream
func (stream *Stream) WriteFloat32(val float32) { func (stream *Stream) WriteFloat32(val float32) {
if math.IsInf(float64(val), 0) || math.IsNaN(float64(val)) {
stream.Error = fmt.Errorf("unsupported value: %f", val)
return
}
abs := math.Abs(float64(val)) abs := math.Abs(float64(val))
fmt := byte('f') fmt := byte('f')
// Note: Must use float32 comparisons for underlying float32 value to get precise cutoffs right. // Note: Must use float32 comparisons for underlying float32 value to get precise cutoffs right.
@ -26,6 +31,10 @@ func (stream *Stream) WriteFloat32(val float32) {
// WriteFloat32Lossy write float32 to stream with ONLY 6 digits precision although much much faster // WriteFloat32Lossy write float32 to stream with ONLY 6 digits precision although much much faster
func (stream *Stream) WriteFloat32Lossy(val float32) { func (stream *Stream) WriteFloat32Lossy(val float32) {
if math.IsInf(float64(val), 0) || math.IsNaN(float64(val)) {
stream.Error = fmt.Errorf("unsupported value: %f", val)
return
}
if val < 0 { if val < 0 {
stream.writeByte('-') stream.writeByte('-')
val = -val val = -val
@ -54,6 +63,10 @@ func (stream *Stream) WriteFloat32Lossy(val float32) {
// WriteFloat64 write float64 to stream // WriteFloat64 write float64 to stream
func (stream *Stream) WriteFloat64(val float64) { func (stream *Stream) WriteFloat64(val float64) {
if math.IsInf(val, 0) || math.IsNaN(val) {
stream.Error = fmt.Errorf("unsupported value: %f", val)
return
}
abs := math.Abs(val) abs := math.Abs(val)
fmt := byte('f') fmt := byte('f')
// Note: Must use float32 comparisons for underlying float32 value to get precise cutoffs right. // Note: Must use float32 comparisons for underlying float32 value to get precise cutoffs right.
@ -67,6 +80,10 @@ func (stream *Stream) WriteFloat64(val float64) {
// WriteFloat64Lossy write float64 to stream with ONLY 6 digits precision although much much faster // WriteFloat64Lossy write float64 to stream with ONLY 6 digits precision although much much faster
func (stream *Stream) WriteFloat64Lossy(val float64) { func (stream *Stream) WriteFloat64Lossy(val float64) {
if math.IsInf(val, 0) || math.IsNaN(val) {
stream.Error = fmt.Errorf("unsupported value: %f", val)
return
}
if val < 0 { if val < 0 {
stream.writeByte('-') stream.writeByte('-')
val = -val val = -val