| 329 | } |
| 330 | |
| 331 | func (bj BinaryJSON) marshalFloat64To(buf []byte) ([]byte, error) { |
| 332 | // NOTE: copied from Go standard library. |
| 333 | f := bj.GetFloat64() |
| 334 | if math.IsInf(f, 0) || math.IsNaN(f) { |
| 335 | return buf, &json.UnsupportedValueError{Str: strconv.FormatFloat(f, 'g', -1, 64)} |
| 336 | } |
| 337 | |
| 338 | // Convert as if by ES6 number to string conversion. |
| 339 | // This matches most other JSON generators. |
| 340 | // See golang.org/issue/6384 and golang.org/issue/14135. |
| 341 | // Like fmt %g, but the exponent cutoffs are different |
| 342 | // and exponents themselves are not padded to two digits. |
| 343 | abs := math.Abs(f) |
| 344 | ffmt := byte('f') |
| 345 | // Note: Must use float32 comparisons for underlying float32 value to get precise cutoffs right. |
| 346 | if abs != 0 { |
| 347 | if abs < 1e-6 || abs >= 1e21 { |
| 348 | ffmt = 'e' |
| 349 | } |
| 350 | } |
| 351 | buf = strconv.AppendFloat(buf, f, ffmt, -1, 64) |
| 352 | if ffmt == 'e' { |
| 353 | // clean up e-09 to e-9 |
| 354 | n := len(buf) |
| 355 | if n >= 4 && buf[n-4] == 'e' && buf[n-3] == '-' && buf[n-2] == '0' { |
| 356 | buf[n-2] = buf[n-1] |
| 357 | buf = buf[:n-1] |
| 358 | } |
| 359 | } |
| 360 | return buf, nil |
| 361 | } |
| 362 | |
| 363 | func (bj BinaryJSON) marshalArrayTo(buf []byte) ([]byte, error) { |
| 364 | elemCount := int(jsonEndian.Uint32(bj.Value)) |