1
0
mirror of https://github.com/json-iterator/go.git synced 2025-06-24 23:16:47 +02:00

#53 move current config EnableXXX

This commit is contained in:
Tao Wen
2017-06-13 17:47:40 +08:00
parent 48e9f6ec84
commit d0418857ce
14 changed files with 218 additions and 152 deletions

View File

@ -2,13 +2,12 @@ package jsoniter
import (
"strconv"
"unsafe"
)
var POW10 []uint64
var _POW10 []uint64
func init() {
POW10 = []uint64{1, 10, 100, 1000, 10000, 100000, 1000000}
_POW10 = []uint64{1, 10, 100, 1000, 10000, 100000, 1000000}
}
func (stream *Stream) WriteFloat32(val float32) {
@ -34,7 +33,7 @@ func (stream *Stream) WriteFloat32Lossy(val float32) {
}
stream.writeByte('.')
stream.ensure(10)
for p := precision - 1; p > 0 && fval < POW10[p]; p-- {
for p := precision - 1; p > 0 && fval < _POW10[p]; p-- {
stream.writeByte('0')
}
stream.WriteUint64(fval)
@ -66,7 +65,7 @@ func (stream *Stream) WriteFloat64Lossy(val float64) {
}
stream.writeByte('.')
stream.ensure(10)
for p := precision - 1; p > 0 && fval < POW10[p]; p-- {
for p := precision - 1; p > 0 && fval < _POW10[p]; p-- {
stream.writeByte('0')
}
stream.WriteUint64(fval)
@ -74,17 +73,3 @@ func (stream *Stream) WriteFloat64Lossy(val float64) {
stream.n--
}
}
// EnableLossyFloatMarshalling keeps 10**(-6) precision
// for float variables for better performance.
func EnableLossyFloatMarshalling() {
// for better performance
RegisterTypeEncoder("float32", func(ptr unsafe.Pointer, stream *Stream) {
val := *((*float32)(ptr))
stream.WriteFloat32Lossy(val)
})
RegisterTypeEncoder("float64", func(ptr unsafe.Pointer, stream *Stream) {
val := *((*float64)(ptr))
stream.WriteFloat64Lossy(val)
})
}