From 1c450fd3aa19a1c2417b33aadd3210867b5baa4b Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Sat, 8 Nov 2014 15:56:40 +0100 Subject: [PATCH] Fixed floating point value parsing in the form "4e2" or "4E2". --- src/Internals/JsonParser.cpp | 5 ++++- test/JsonParser_Array_Tests.cpp | 4 ++-- test/JsonParser_Object_Tests.cpp | 4 ++-- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Internals/JsonParser.cpp b/src/Internals/JsonParser.cpp index 06b00025..6e42bc45 100644 --- a/src/Internals/JsonParser.cpp +++ b/src/Internals/JsonParser.cpp @@ -165,9 +165,12 @@ void JsonParser::parseBooleanTo(JsonVariant &destination) { void JsonParser::parseNumberTo(JsonVariant &destination) { char *endOfLong; long longValue = strtol(_ptr, &endOfLong, 10); + char stopChar = *endOfLong; // Could it be a floating point value? - if (*endOfLong == '.') { + bool couldBeFloat = stopChar == '.' || stopChar == 'e' || stopChar == 'E'; + + if (couldBeFloat) { // Yes => parse it as a double double doubleValue = strtod(_ptr, &_ptr); // Count the decimal digits diff --git a/test/JsonParser_Array_Tests.cpp b/test/JsonParser_Array_Tests.cpp index 0c7e6f2b..01d2e767 100644 --- a/test/JsonParser_Array_Tests.cpp +++ b/test/JsonParser_Array_Tests.cpp @@ -114,12 +114,12 @@ TEST_F(JsonParser_Array_Tests, TwoIntegers) { } TEST_F(JsonParser_Array_Tests, TwoDoubles) { - whenInputIs("[4.2,8.4]"); + whenInputIs("[4.2,1e2]"); parseMustSucceed(); sizeMustBe(2); firstElementMustBe(4.2); - secondElementMustBe(8.4); + secondElementMustBe(1e2); } TEST_F(JsonParser_Array_Tests, TwoBooleans) { diff --git a/test/JsonParser_Object_Tests.cpp b/test/JsonParser_Object_Tests.cpp index 33a18c34..11a32243 100644 --- a/test/JsonParser_Object_Tests.cpp +++ b/test/JsonParser_Object_Tests.cpp @@ -145,11 +145,11 @@ TEST_F(JsonParser_Object_Test, TwoIntergers) { } TEST_F(JsonParser_Object_Test, TwoDoubles) { - whenInputIs("{\"key1\":12.345,\"key2\":-7.89}"); + whenInputIs("{\"key1\":12.345,\"key2\":-7E89}"); parseMustSucceed(); sizeMustBe(2); keyMustHaveValue("key1", 12.345); - keyMustHaveValue("key2", -7.89); + keyMustHaveValue("key2", -7E89); } TEST_F(JsonParser_Object_Test, TwoBooleans) {