Merge pull request #33 from syvex/JSONParse64

Fix JSON Parser to handle 64-bit integers (Issue #32)
This commit is contained in:
Aleksandar Fabijanic
2012-12-13 20:02:46 -08:00
5 changed files with 72 additions and 11 deletions

View File

@@ -105,8 +105,13 @@ public:
virtual std::string asString() const; virtual std::string asString() const;
/// Returns a string representation of the token. /// 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; virtual int asInteger() const;
/// Returns an integer representation of the token. /// Returns an integer representation of the token.
#endif
virtual double asFloat() const; virtual double asFloat() const;
/// Returns a floating-point representation of the token. /// Returns a floating-point representation of the token.

View File

@@ -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 int Token::asInteger() const
{ {
return NumberParser::parse(_value); return NumberParser::parse(_value);
} }
#endif
double Token::asFloat() const double Token::asFloat() const
{ {
return NumberParser::parseFloat(_value); return NumberParser::parseFloat(_value);

View File

@@ -575,18 +575,20 @@ void Parser::readValue(const Token* token)
case Token::INTEGER_LITERAL_TOKEN: case Token::INTEGER_LITERAL_TOKEN:
if ( _handler != NULL ) if ( _handler != NULL )
{ {
int value = token->asInteger();
#if defined(POCO_HAVE_INT64) #if defined(POCO_HAVE_INT64)
if ( value == std::numeric_limits<int>::max() Int64 value = token->asInteger();
|| value == std::numeric_limits<int>::min() ) // if number is 32-bit, then handle as such
{ if ( value > std::numeric_limits<int>::max()
_handler->value(NumberParser::parse64(token->asString())); || value < std::numeric_limits<int>::min() )
} {
else _handler->value(value);
{ }
_handler->value(token->asInteger()); else
} {
_handler->value(static_cast<int>(value));
}
#else #else
int value = token->asInteger();
_handle->value(value); _handle->value(value);
#endif #endif
} }
@@ -697,5 +699,4 @@ bool Parser::readElements(bool firstCall)
throw JSONException(format("Invalid token '%s' found.", token->asString())); throw JSONException(format("Invalid token '%s' found.", token->asString()));
} }
} } // namespace Poco::JSON } } // namespace Poco::JSON

View File

@@ -203,6 +203,40 @@ void JSONTest::testNumberProperty()
assert(value == 1969); 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() void JSONTest::testStringProperty()
{ {
@@ -860,6 +894,9 @@ CppUnit::Test* JSONTest::suite()
CppUnit_addTest(pSuite, JSONTest, testTrueProperty); CppUnit_addTest(pSuite, JSONTest, testTrueProperty);
CppUnit_addTest(pSuite, JSONTest, testFalseProperty); CppUnit_addTest(pSuite, JSONTest, testFalseProperty);
CppUnit_addTest(pSuite, JSONTest, testNumberProperty); 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, testStringProperty);
CppUnit_addTest(pSuite, JSONTest, testEmptyObject); CppUnit_addTest(pSuite, JSONTest, testEmptyObject);
CppUnit_addTest(pSuite, JSONTest, testDoubleProperty); CppUnit_addTest(pSuite, JSONTest, testDoubleProperty);

View File

@@ -51,6 +51,9 @@ public:
void testTrueProperty(); void testTrueProperty();
void testFalseProperty(); void testFalseProperty();
void testNumberProperty(); void testNumberProperty();
#if defined(POCO_HAVE_INT64)
void testNumber64Property();
#endif
void testStringProperty(); void testStringProperty();
void testEmptyObject(); void testEmptyObject();
void testDoubleProperty(); void testDoubleProperty();