Commit b46f658b authored by LEFEBVREJP email's avatar LEFEBVREJP email
Browse files

Merge branch 'parse_update' into 'master'

JSON integer v.s double

See merge request !95
parents 5aa49012 bc2a4ed2
Pipeline #101748 passed with stages
in 21 minutes and 23 seconds
...@@ -221,7 +221,8 @@ class RADIX_PUBLIC JSONParserImpl ...@@ -221,7 +221,8 @@ class RADIX_PUBLIC JSONParserImpl
// ([Ee][+-]?[0-9]+)? // ([Ee][+-]?[0-9]+)?
bool parse_number(value_type& value) bool parse_number(value_type& value)
{ {
value = value_type(); bool is_float = false;
value = value_type();
if (m_po >= m_text.size()) return false; if (m_po >= m_text.size()) return false;
size_t len = 0; size_t len = 0;
...@@ -263,7 +264,7 @@ class RADIX_PUBLIC JSONParserImpl ...@@ -263,7 +264,7 @@ class RADIX_PUBLIC JSONParserImpl
{ {
try try
{ {
value = value_type(std::stod(std::string(&m_text[m_po - len], len))); value = value_type(std::stoi(std::string(&m_text[m_po - len], len)));
return true; return true;
} }
catch (...) catch (...)
...@@ -276,6 +277,7 @@ class RADIX_PUBLIC JSONParserImpl ...@@ -276,6 +277,7 @@ class RADIX_PUBLIC JSONParserImpl
// (\.[0-9]+)? // (\.[0-9]+)?
if (ch == '.') if (ch == '.')
{ {
is_float = true;
m_po++; m_po++;
m_col++; m_col++;
len++; len++;
...@@ -302,6 +304,7 @@ class RADIX_PUBLIC JSONParserImpl ...@@ -302,6 +304,7 @@ class RADIX_PUBLIC JSONParserImpl
// ([Ee][+-]?[0-9]+)? // ([Ee][+-]?[0-9]+)?
if (ch == 'E' || ch == 'e') if (ch == 'E' || ch == 'e')
{ {
is_float = true;
m_po++; m_po++;
m_col++; m_col++;
len++; len++;
...@@ -340,7 +343,12 @@ class RADIX_PUBLIC JSONParserImpl ...@@ -340,7 +343,12 @@ class RADIX_PUBLIC JSONParserImpl
try try
{ {
value = value_type(std::stod(std::string(&m_text[m_po - len], len))); double v = std::stod(std::string(&m_text[m_po - len], len));
if (is_float)
value = value_type(v);
else
value = value_type(int(v >= 0 ? v + .5 : v - .5));
return true; return true;
} }
catch (...) catch (...)
......
...@@ -12,6 +12,8 @@ using namespace radix; ...@@ -12,6 +12,8 @@ using namespace radix;
// bool parse_from_stream(std::istream& in_stream) // bool parse_from_stream(std::istream& in_stream)
// bool parse_from_file(std::string fn) // bool parse_from_file(std::string fn)
double allowedDelta = 1e-4;
TEST(JSONParser, parse_from_file) TEST(JSONParser, parse_from_file)
{ {
{ {
...@@ -66,13 +68,14 @@ TEST(JSONParser, parse_from_file) ...@@ -66,13 +68,14 @@ TEST(JSONParser, parse_from_file)
TEST(JSONParser, parse_from_stream) TEST(JSONParser, parse_from_stream)
{ {
JSONParser jp;
std::string errorMsg;
std::stringstream ss("");
// check code paths in JSONParser class // check code paths in JSONParser class
{ {
JSONParser jp;
std::string errorMsg;
// trailing junk // trailing junk
std::stringstream ss("{}\r\nfoo"); ss.str("{}\r\nfoo");
EXPECT_FALSE(jp.parse_from_stream(ss)); EXPECT_FALSE(jp.parse_from_stream(ss));
errorMsg = jp.last_error(); errorMsg = jp.last_error();
EXPECT_EQ("unexpected trailing character(s) at line 2 column 1", errorMsg); EXPECT_EQ("unexpected trailing character(s) at line 2 column 1", errorMsg);
...@@ -233,6 +236,83 @@ TEST(JSONParser, parse_from_stream) ...@@ -233,6 +236,83 @@ TEST(JSONParser, parse_from_stream)
errorMsg = jp.last_error(); errorMsg = jp.last_error();
EXPECT_EQ("string missing closing quote at line 1 column 5", errorMsg); EXPECT_EQ("string missing closing quote at line 1 column 5", errorMsg);
} }
// test numbers
{
int iVal;
double dVal;
// integer
ss.str("123");
EXPECT_TRUE(jp.parse_from_stream(ss));
iVal = jp.root().to_int();
EXPECT_EQ(123, iVal);
EXPECT_EQ(Value::TYPE_INTEGER, jp.root().type());
// float
ss.str("123.4");
EXPECT_TRUE(jp.parse_from_stream(ss));
dVal = jp.root().to_double();
EXPECT_NEAR(123.4, dVal, allowedDelta);
EXPECT_EQ(Value::TYPE_DOUBLE, jp.root().type());
// scientific notation
ss.str("123e45");
EXPECT_TRUE(jp.parse_from_stream(ss));
dVal = jp.root().to_double();
EXPECT_NEAR(123e45, dVal, allowedDelta);
EXPECT_EQ(Value::TYPE_DOUBLE, jp.root().type());
// Zeros
ss.str("0");
EXPECT_TRUE(jp.parse_from_stream(ss));
iVal = jp.root().to_int();
EXPECT_EQ(0, iVal);
EXPECT_EQ(Value::TYPE_INTEGER, jp.root().type());
ss.str("-0");
EXPECT_TRUE(jp.parse_from_stream(ss));
iVal = jp.root().to_int();
EXPECT_EQ(0, iVal);
EXPECT_EQ(Value::TYPE_INTEGER, jp.root().type());
ss.str("0.0");
EXPECT_TRUE(jp.parse_from_stream(ss));
dVal = jp.root().to_double();
EXPECT_NEAR(0, dVal, allowedDelta);
EXPECT_EQ(Value::TYPE_DOUBLE, jp.root().type());
ss.str("-0.0e-1");
EXPECT_TRUE(jp.parse_from_stream(ss));
dVal = jp.root().to_double();
EXPECT_NEAR(0, dVal, allowedDelta);
EXPECT_EQ(Value::TYPE_DOUBLE, jp.root().type());
// Negative
ss.str("-123");
EXPECT_TRUE(jp.parse_from_stream(ss));
iVal = jp.root().to_int();
EXPECT_EQ(-123, iVal);
EXPECT_EQ(Value::TYPE_INTEGER, jp.root().type());
ss.str("-123.4");
EXPECT_TRUE(jp.parse_from_stream(ss));
dVal = jp.root().to_double();
EXPECT_NEAR(-123.4, dVal, allowedDelta);
EXPECT_EQ(Value::TYPE_DOUBLE, jp.root().type());
ss.str("-123e45");
EXPECT_TRUE(jp.parse_from_stream(ss));
dVal = jp.root().to_double();
EXPECT_NEAR(-123e45, dVal, allowedDelta);
EXPECT_EQ(Value::TYPE_DOUBLE, jp.root().type());
ss.str("-1234e-5");
EXPECT_TRUE(jp.parse_from_stream(ss));
dVal = jp.root().to_double();
EXPECT_NEAR(-0.01234, dVal, allowedDelta);
EXPECT_EQ(Value::TYPE_DOUBLE, jp.root().type());
}
} }
bool parse_and_test(const std::string& file) bool parse_and_test(const std::string& file)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment