commit 2aae61b7da29229de37bd7deecbaaf29b48d9236
parent 71c0966e1a7260957ced1f424f1bc2219207eda1
Author: Lorenzo Stoakes <lstoakes@gmail.com>
Date: Sat, 28 Jul 2018 18:22:11 +0100
Add support for negative floats with 0 significand
Previously, floating point numbers of the form -0.1234..., i.e. those
with a 0 significand would result in a 'wrong char' error.
These are valid numbers in JSON under RFC 4627 so we should support
them. This patch adds this support and a test for both float64 and
float32.
Diffstat:
2 files changed, 12 insertions(+), 2 deletions(-)
diff --git a/decode_number_float.go b/decode_number_float.go
@@ -53,7 +53,7 @@ func (dec *Decoder) getFloatNegative() (float64, error) {
// look for following numbers
for ; dec.cursor < dec.length || dec.read(); dec.cursor++ {
switch dec.data[dec.cursor] {
- case '1', '2', '3', '4', '5', '6', '7', '8', '9':
+ case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
return dec.getFloat()
default:
return 0, dec.raiseInvalidJSONErr(dec.cursor)
@@ -203,7 +203,7 @@ func (dec *Decoder) getFloat32Negative() (float32, error) {
// look for following numbers
for ; dec.cursor < dec.length || dec.read(); dec.cursor++ {
switch dec.data[dec.cursor] {
- case '1', '2', '3', '4', '5', '6', '7', '8', '9':
+ case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
return dec.getFloat32()
default:
return 0, dec.raiseInvalidJSONErr(dec.cursor)
diff --git a/decode_number_float_test.go b/decode_number_float_test.go
@@ -195,6 +195,11 @@ func TestDecoderFloat64(t *testing.T) {
expectedResult: -788.76,
},
{
+ name: "basic-float3",
+ json: "-0.1234",
+ expectedResult: -0.1234,
+ },
+ {
name: "basic-exp-too-big",
json: "1e10000000000 ",
expectedResult: 0,
@@ -539,6 +544,11 @@ func TestDecoderFloat32(t *testing.T) {
expectedResult: -788.76,
},
{
+ name: "basic-float3",
+ json: "-0.1234",
+ expectedResult: -0.1234,
+ },
+ {
name: "error",
json: "83zez4",
expectedResult: 0,