mirror of
https://github.com/ChaiScript/ChaiScript.git
synced 2025-12-06 16:57:04 +08:00
Merge pull request #377 from totalgee/negative_exponent_json
Fix JSON parsing for floats with negative exponents
This commit is contained in:
commit
7f822be5db
@ -568,6 +568,7 @@ struct JSONParser {
|
|||||||
char c = '\0';
|
char c = '\0';
|
||||||
bool isDouble = false;
|
bool isDouble = false;
|
||||||
bool isNegative = false;
|
bool isNegative = false;
|
||||||
|
bool isExpNegative = false;
|
||||||
long exp = 0;
|
long exp = 0;
|
||||||
if( offset < str.size() && str.at(offset) == '-' ) {
|
if( offset < str.size() && str.at(offset) == '-' ) {
|
||||||
isNegative = true;
|
isNegative = true;
|
||||||
@ -587,7 +588,7 @@ struct JSONParser {
|
|||||||
if( offset < str.size() && (c == 'E' || c == 'e' )) {
|
if( offset < str.size() && (c == 'E' || c == 'e' )) {
|
||||||
c = str.at(offset++);
|
c = str.at(offset++);
|
||||||
if( c == '-' ) {
|
if( c == '-' ) {
|
||||||
exp_str += '-';
|
isExpNegative = true;
|
||||||
} else if( c == '+' ) {
|
} else if( c == '+' ) {
|
||||||
// do nothing
|
// do nothing
|
||||||
} else {
|
} else {
|
||||||
@ -603,9 +604,9 @@ struct JSONParser {
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
exp = chaiscript::parse_num<long>( exp_str );
|
exp = chaiscript::parse_num<long>( exp_str ) * (isExpNegative?-1:1);
|
||||||
}
|
}
|
||||||
else if( offset < str.size() && (!isspace( c ) && c != ',' && c != ']' && c != '}' )) {
|
else if( offset < str.size() && (!isspace( c ) && c != ',' && c != ']' && c != '}' )) {
|
||||||
throw std::runtime_error(std::string("JSON ERROR: Number: unexpected character '") + c + "'");
|
throw std::runtime_error(std::string("JSON ERROR: Number: unexpected character '") + c + "'");
|
||||||
|
|||||||
5
unittests/json_14.chai
Normal file
5
unittests/json_14.chai
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
assert_equal(from_json("9.9e-02"), 9.9e-02)
|
||||||
|
assert_equal(from_json("-13.57e+3"), -13570.0)
|
||||||
|
assert_equal(from_json("1E-01"), 0.1)
|
||||||
|
assert_equal(from_json("-314159e-5"), -3.14159)
|
||||||
|
assert_equal(from_json("5e+04"), 50000)
|
||||||
Loading…
x
Reference in New Issue
Block a user