Refactor unsupported value for float64

This commit is contained in:
Masaaki Goshima 2020-11-17 15:06:05 +09:00
parent e88d0248b5
commit 69ea157270
1 changed files with 8 additions and 8 deletions

View File

@ -35,6 +35,12 @@ func errUnsupportedValue(code *opcode, ptr uintptr) *UnsupportedValueError {
} }
} }
func errUnsupportedFloat(v float64) *UnsupportedValueError {
return &UnsupportedValueError{
Value: reflect.ValueOf(v),
Str: strconv.FormatFloat(v, 'g', -1, 64),
}
}
func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error {
recursiveLevel := 0 recursiveLevel := 0
seenPtr := map[uintptr]struct{}{} seenPtr := map[uintptr]struct{}{}
@ -140,10 +146,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error {
case opFloat64: case opFloat64:
v := e.ptrToFloat64(load(ctxptr, code.idx)) v := e.ptrToFloat64(load(ctxptr, code.idx))
if math.IsInf(v, 0) || math.IsNaN(v) { if math.IsInf(v, 0) || math.IsNaN(v) {
return &UnsupportedValueError{ return errUnsupportedFloat(v)
Value: reflect.ValueOf(v),
Str: strconv.FormatFloat(v, 'g', -1, 64),
}
} }
e.encodeFloat64(v) e.encodeFloat64(v)
e.encodeByte(',') e.encodeByte(',')
@ -151,10 +154,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error {
case opFloat64Indent: case opFloat64Indent:
v := e.ptrToFloat64(load(ctxptr, code.idx)) v := e.ptrToFloat64(load(ctxptr, code.idx))
if math.IsInf(v, 0) || math.IsNaN(v) { if math.IsInf(v, 0) || math.IsNaN(v) {
return &UnsupportedValueError{ return errUnsupportedFloat(v)
Value: reflect.ValueOf(v),
Str: strconv.FormatFloat(v, 'g', -1, 64),
}
} }
e.encodeFloat64(v) e.encodeFloat64(v)
e.encodeBytes([]byte{',', '\n'}) e.encodeBytes([]byte{',', '\n'})