mirror of
https://github.com/pocoproject/poco.git
synced 2025-04-17 15:14:48 +02:00
Merge pull request #33 from syvex/JSONParse64
Fix JSON Parser to handle 64-bit integers (Issue #32)
This commit is contained in:
commit
249135b05c
@ -105,8 +105,13 @@ public:
|
||||
virtual std::string asString() const;
|
||||
/// Returns a string representation of the token.
|
||||
|
||||
#if defined(POCO_HAVE_INT64)
|
||||
virtual Int64 asInteger() const;
|
||||
/// Returns a 64-bit integer representation of the token.
|
||||
#else
|
||||
virtual int asInteger() const;
|
||||
/// Returns an integer representation of the token.
|
||||
#endif
|
||||
|
||||
virtual double asFloat() const;
|
||||
/// Returns a floating-point representation of the token.
|
||||
|
@ -76,12 +76,27 @@ std::string Token::asString() const
|
||||
}
|
||||
|
||||
|
||||
#if defined(POCO_HAVE_INT64)
|
||||
|
||||
|
||||
Int64 Token::asInteger() const
|
||||
{
|
||||
return NumberParser::parse64(_value);
|
||||
}
|
||||
|
||||
|
||||
#else
|
||||
|
||||
|
||||
int Token::asInteger() const
|
||||
{
|
||||
return NumberParser::parse(_value);
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
double Token::asFloat() const
|
||||
{
|
||||
return NumberParser::parseFloat(_value);
|
||||
|
@ -575,18 +575,20 @@ void Parser::readValue(const Token* token)
|
||||
case Token::INTEGER_LITERAL_TOKEN:
|
||||
if ( _handler != NULL )
|
||||
{
|
||||
int value = token->asInteger();
|
||||
#if defined(POCO_HAVE_INT64)
|
||||
if ( value == std::numeric_limits<int>::max()
|
||||
|| value == std::numeric_limits<int>::min() )
|
||||
{
|
||||
_handler->value(NumberParser::parse64(token->asString()));
|
||||
}
|
||||
else
|
||||
{
|
||||
_handler->value(token->asInteger());
|
||||
}
|
||||
Int64 value = token->asInteger();
|
||||
// if number is 32-bit, then handle as such
|
||||
if ( value > std::numeric_limits<int>::max()
|
||||
|| value < std::numeric_limits<int>::min() )
|
||||
{
|
||||
_handler->value(value);
|
||||
}
|
||||
else
|
||||
{
|
||||
_handler->value(static_cast<int>(value));
|
||||
}
|
||||
#else
|
||||
int value = token->asInteger();
|
||||
_handle->value(value);
|
||||
#endif
|
||||
}
|
||||
@ -697,5 +699,4 @@ bool Parser::readElements(bool firstCall)
|
||||
throw JSONException(format("Invalid token '%s' found.", token->asString()));
|
||||
}
|
||||
|
||||
|
||||
} } // namespace Poco::JSON
|
||||
|
@ -203,6 +203,40 @@ void JSONTest::testNumberProperty()
|
||||
assert(value == 1969);
|
||||
}
|
||||
|
||||
#if defined(POCO_HAVE_INT64)
|
||||
|
||||
|
||||
void JSONTest::testNumber64Property()
|
||||
{
|
||||
std::string json = "{ \"test\" : 5000000000000000 }";
|
||||
Parser parser;
|
||||
Var result;
|
||||
|
||||
try
|
||||
{
|
||||
DefaultHandler handler;
|
||||
parser.setHandler(&handler);
|
||||
parser.parse(json);
|
||||
result = handler.result();
|
||||
}
|
||||
catch(JSONException& jsone)
|
||||
{
|
||||
std::cout << jsone.message() << std::endl;
|
||||
assert(false);
|
||||
}
|
||||
|
||||
assert(result.type() == typeid(Object::Ptr));
|
||||
|
||||
Object::Ptr object = result.extract<Object::Ptr>();
|
||||
Var test = object->get("test");
|
||||
assert(test.isInteger());
|
||||
Int64 value = test;
|
||||
assert(value == 5000000000000000);
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
void JSONTest::testStringProperty()
|
||||
{
|
||||
@ -860,6 +894,9 @@ CppUnit::Test* JSONTest::suite()
|
||||
CppUnit_addTest(pSuite, JSONTest, testTrueProperty);
|
||||
CppUnit_addTest(pSuite, JSONTest, testFalseProperty);
|
||||
CppUnit_addTest(pSuite, JSONTest, testNumberProperty);
|
||||
#if defined(POCO_HAVE_INT64)
|
||||
CppUnit_addTest(pSuite, JSONTest, testNumber64Property);
|
||||
#endif
|
||||
CppUnit_addTest(pSuite, JSONTest, testStringProperty);
|
||||
CppUnit_addTest(pSuite, JSONTest, testEmptyObject);
|
||||
CppUnit_addTest(pSuite, JSONTest, testDoubleProperty);
|
||||
|
@ -51,6 +51,9 @@ public:
|
||||
void testTrueProperty();
|
||||
void testFalseProperty();
|
||||
void testNumberProperty();
|
||||
#if defined(POCO_HAVE_INT64)
|
||||
void testNumber64Property();
|
||||
#endif
|
||||
void testStringProperty();
|
||||
void testEmptyObject();
|
||||
void testDoubleProperty();
|
||||
|
Loading…
x
Reference in New Issue
Block a user