in json/visitor.go [429:476]
func (vs *Visitor) onFloat(f float64, bits int) error {
if err := vs.tryElemNext(); err != nil {
return err
}
if math.IsInf(f, 0) || math.IsNaN(f) {
if !vs.ignoreInvalidFloat {
return fmt.Errorf("unsupported float value: %v", f)
}
return vs.w.write(nullSymbol)
}
b := strconv.AppendFloat(vs.scratch[:0], f, 'g', -1, bits)
if vs.explicitRadixPoint {
// b can be in either decimal form or scientific notation.
// For decimal form, append ".0" if radix point '.' is not present in the encoded number.
// e.g. 1 becomes 1.0.
// For scientific notation, append ".0" to mantissa if radix point '.' is not present in the encoded mantissa.
// e.g. 1e+2 becomes 1.0e+2.
needDp := true
expIdx := len(b)
loop:
for i, c := range b {
switch c {
case 'e': // exponent separator
expIdx = i
break loop
case '.': // decimal point
needDp = false
break loop
}
}
if err := vs.w.write(b[:expIdx]); err != nil {
return err
}
if needDp {
if err := vs.w.write([]byte(".0")); err != nil {
return err
}
}
return vs.w.write(b[expIdx:])
}
return vs.w.write(b)
}