diff --git a/include/chaiscript/utility/json.hpp b/include/chaiscript/utility/json.hpp index 6fe2e58..676b469 100644 --- a/include/chaiscript/utility/json.hpp +++ b/include/chaiscript/utility/json.hpp @@ -569,13 +569,15 @@ struct JSONParser { bool isDouble = false; bool isNegative = false; long exp = 0; + if( offset < str.size() && str[offset] == '-' ) { + isNegative = true; + ++offset; + } for (; offset < str.size() ;) { c = str[offset++]; - if( c == '-' ) { - isNegative = true; - } else if( c >= '0' && c <= '9' ) { + if( c >= '0' && c <= '9' ) { val += c; - } else if( c == '.' ) { + } else if( c == '.' && !isDouble ) { val += c; isDouble = true; } else { diff --git a/unittests/json_4.chai b/unittests/json_4.chai index 34ecfa9..ae07024 100644 --- a/unittests/json_4.chai +++ b/unittests/json_4.chai @@ -1,2 +1,22 @@ assert_equal(from_json("1.234"), 1.234) assert_equal(from_json("-1.234"), -1.234) + +auto caught = false; +try { + from_json("-1-5.3"); +} +catch(e) { + assert_equal("JSON ERROR: Number: unexpected character '-'", e.what()); + caught = true; +} +assert_equal(caught, true); + +caught = false; +try { + from_json("-15.3.2"); +} +catch(e) { + assert_equal("JSON ERROR: Number: unexpected character '.'", e.what()); + caught = true; +} +assert_equal(caught, true);