mirror of
https://github.com/json-iterator/go.git
synced 2025-04-23 11:37:32 +02:00
fix lossy float marshal and omit empty
This commit is contained in:
parent
945fe53724
commit
39c9bb226a
@ -99,18 +99,42 @@ func (cfg *frozenConfig) supportUnexportedStructFields() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type lossyFloat32Encoder struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *lossyFloat32Encoder) encode(ptr unsafe.Pointer, stream *Stream) {
|
||||||
|
stream.WriteFloat32Lossy(*((*float32)(ptr)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *lossyFloat32Encoder) encodeInterface(val interface{}, stream *Stream) {
|
||||||
|
writeToStream(val, stream, encoder)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *lossyFloat32Encoder) isEmpty(ptr unsafe.Pointer) bool {
|
||||||
|
return *((*float32)(ptr)) == 0
|
||||||
|
}
|
||||||
|
|
||||||
|
type lossyFloat64Encoder struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *lossyFloat64Encoder) encode(ptr unsafe.Pointer, stream *Stream) {
|
||||||
|
stream.WriteFloat64Lossy(*((*float64)(ptr)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *lossyFloat64Encoder) encodeInterface(val interface{}, stream *Stream) {
|
||||||
|
writeToStream(val, stream, encoder)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *lossyFloat64Encoder) isEmpty(ptr unsafe.Pointer) bool {
|
||||||
|
return *((*float64)(ptr)) == 0
|
||||||
|
}
|
||||||
|
|
||||||
// EnableLossyFloatMarshalling keeps 10**(-6) precision
|
// EnableLossyFloatMarshalling keeps 10**(-6) precision
|
||||||
// for float variables for better performance.
|
// for float variables for better performance.
|
||||||
func (cfg *frozenConfig) marshalFloatWith6Digits() {
|
func (cfg *frozenConfig) marshalFloatWith6Digits() {
|
||||||
// for better performance
|
// for better performance
|
||||||
cfg.addEncoderToCache(reflect.TypeOf((*float32)(nil)).Elem(), &funcEncoder{func(ptr unsafe.Pointer, stream *Stream) {
|
cfg.addEncoderToCache(reflect.TypeOf((*float32)(nil)).Elem(), &lossyFloat32Encoder{})
|
||||||
val := *((*float32)(ptr))
|
cfg.addEncoderToCache(reflect.TypeOf((*float64)(nil)).Elem(), &lossyFloat64Encoder{})
|
||||||
stream.WriteFloat32Lossy(val)
|
|
||||||
}})
|
|
||||||
cfg.addEncoderToCache(reflect.TypeOf((*float64)(nil)).Elem(), &funcEncoder{func(ptr unsafe.Pointer, stream *Stream) {
|
|
||||||
val := *((*float64)(ptr))
|
|
||||||
stream.WriteFloat64Lossy(val)
|
|
||||||
}})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type htmlEscapedStringEncoder struct {
|
type htmlEscapedStringEncoder struct {
|
||||||
|
@ -162,6 +162,17 @@ func Test_read_float_scientific(t *testing.T) {
|
|||||||
should.Equal(float64(10), obj)
|
should.Equal(float64(10), obj)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_lossy_float_marshal(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
api := Config{MarshalFloatWith6Digits: true}.Froze()
|
||||||
|
output, err := api.MarshalToString(float64(0.1234567))
|
||||||
|
should.Nil(err)
|
||||||
|
should.Equal("0.123457", output)
|
||||||
|
output, err = api.MarshalToString(float32(0.1234567))
|
||||||
|
should.Nil(err)
|
||||||
|
should.Equal("0.123457", output)
|
||||||
|
}
|
||||||
|
|
||||||
func Benchmark_jsoniter_float(b *testing.B) {
|
func Benchmark_jsoniter_float(b *testing.B) {
|
||||||
b.ReportAllocs()
|
b.ReportAllocs()
|
||||||
input := []byte(`1.1123,`)
|
input := []byte(`1.1123,`)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user