I'm working with Facebook Json API where all ids are 64 bit values and support for big ints is essential. I wonder whether big int numbers are actually supported by jsoncpp on 32 bit platforms? Looks like no, since forwards.h declares Int and UInt as "int" and "unsigned int" respectively.
As a possible workaround I could retrieve a number string and then convert it manually into 64 bit value. But I'm not sure if it's possible with the current jsoncpp API: for big ints asString() method throws "Type is not convertible to string".
Probably the best solution is to use platform specific int64/uint64 types for Int and UInt, or provide some config option… What do you think?
Thanks!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I agree, i'm using the twitter API and all IDs are in int64. I already posted a feature request: http://sourceforge.net/tracker/?func=detail&aid=2976540&group_id=144446&atid=758826 Secondly, wouldn't it be easier to use the c++ (string)stream to convert from and to specific types? For example, it should be possible to convert any json value into a string.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I added support for 64 bits integer to the roadmap. This will be done by changing the Int type definition as you proposed, though array will still be limited to a simple int (Json::Value is not designed for such large volume).
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2010-09-15
I did my own patch, ended up with a few differences in order to pass the unit tests. Note: this probably isn't portable due to the use of %lld/%llu in the unit test code.
diff -r fa8ac9d77879 jsoncpp/include/json/forwards.h--- a/jsoncpp/include/json/forwards.h Tue Sep 14 18:14:27 2010 -0400+++ b/jsoncpp/include/json/forwards.h Wed Sep 15 11:12:35 2010 -0400@@ -2,6 +2,7 @@
# define JSON_FORWARDS_H_INCLUDED
# include "config.h"
+#include <sys/types.h>
namespace Json {
@@ -16,8 +17,8 @@
class Features;
// value.h
- typedef int Int;- typedef unsigned int UInt;+ typedef __int64_t Int;+ typedef __uint64_t UInt;
class StaticString;
class Path;
class PathArgument;
diff -r fa8ac9d77879 jsoncpp/include/json/value.h--- a/jsoncpp/include/json/value.h Tue Sep 14 18:14:27 2010 -0400+++ b/jsoncpp/include/json/value.h Wed Sep 15 11:12:35 2010 -0400@@ -184,6 +184,8 @@
Value( ValueType type = nullValue );
Value( Int value );
Value( UInt value );
+ Value( int value );+ Value( unsigned int value );
Value( double value );
Value( const char *value );
Value( const char *beginValue, const char *endValue );
@@ -248,7 +250,7 @@
bool isConvertibleTo( ValueType other ) const;
/// Number of values in array or object
- UInt size() const;+ unsigned int size() const;
/// \brief Return true if empty array, empty object, or null;
/// otherwise, false.
diff -r fa8ac9d77879 jsoncpp/src/jsontestrunner/main.cpp--- a/jsoncpp/src/jsontestrunner/main.cpp Tue Sep 14 18:14:27 2010 -0400+++ b/jsoncpp/src/jsontestrunner/main.cpp Wed Sep 15 11:12:35 2010 -0400@@ -35,10 +35,10 @@
fprintf( fout, "%s=null\n", path.c_str() );
break;
case Json::intValue:
- fprintf( fout, "%s=%d\n", path.c_str(), value.asInt() );+ fprintf( fout, "%s=%lld\n", path.c_str(), value.asInt() );
break;
case Json::uintValue:
- fprintf( fout, "%s=%u\n", path.c_str(), value.asUInt() );+ fprintf( fout, "%s=%llu\n", path.c_str(), value.asUInt() );
break;
case Json::realValue:
fprintf( fout, "%s=%.16g\n", path.c_str(), value.asDouble() );
diff -r fa8ac9d77879 jsoncpp/src/lib_json/json_value.cpp--- a/jsoncpp/src/lib_json/json_value.cpp Tue Sep 14 18:14:27 2010 -0400+++ b/jsoncpp/src/lib_json/json_value.cpp Wed Sep 15 11:12:35 2010 -0400@@ -330,6 +330,27 @@
value_.uint_ = value;
}
+Value::Value( int value )+ : type_( intValue )+ , comments_( 0 )+# ifdef JSON_VALUE_USE_INTERNAL_MAP+ , itemIsUsed_( 0 )+#endif+{+ value_.int_ = value;+}+++Value::Value( unsigned int value )+ : type_( uintValue )+ , comments_( 0 )+# ifdef JSON_VALUE_USE_INTERNAL_MAP+ , itemIsUsed_( 0 )+#endif+{+ value_.uint_ = value;+}+
Value::Value( double value )
: type_( realValue )
, comments_( 0 )
@@ -869,7 +890,7 @@
/// Number of values in array or object
-Value::UInt +unsigned int
Value::size() const
{
switch ( type_ )
diff -r fa8ac9d77879 jsoncpp/src/lib_json/json_writer.cpp--- a/jsoncpp/src/lib_json/json_writer.cpp Tue Sep 14 18:14:27 2010 -0400+++ b/jsoncpp/src/lib_json/json_writer.cpp Wed Sep 15 11:12:35 2010 -0400@@ -27,7 +27,7 @@
}
return false;
}
-static void uintToString( unsigned int value, +static void uintToString( UInt value,
char *¤t )
{
*--current = 0;
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I'm working with Facebook Json API where all ids are 64 bit values and support for big ints is essential. I wonder whether big int numbers are actually supported by jsoncpp on 32 bit platforms? Looks like no, since forwards.h declares Int and UInt as "int" and "unsigned int" respectively.
As a possible workaround I could retrieve a number string and then convert it manually into 64 bit value. But I'm not sure if it's possible with the current jsoncpp API: for big ints asString() method throws "Type is not convertible to string".
Probably the best solution is to use platform specific int64/uint64 types for Int and UInt, or provide some config option… What do you think?
Thanks!
Ok, here is the "proof of the concept" - a small patch which defines Int and UInt as 64 bit numbers. This was tested on linux only:
Damn SourceForge, it added some weird garbage(just ignore it) to the top of the patch, and I can't edit the post
Hi there,
I agree, i'm using the twitter API and all IDs are in int64. I already posted a feature request: http://sourceforge.net/tracker/?func=detail&aid=2976540&group_id=144446&atid=758826 Secondly, wouldn't it be easier to use the c++ (string)stream to convert from and to specific types? For example, it should be possible to convert any json value into a string.
I added support for 64 bits integer to the roadmap. This will be done by changing the Int type definition as you proposed, though array will still be limited to a simple int (Json::Value is not designed for such large volume).
I did my own patch, ended up with a few differences in order to pass the unit tests. Note: this probably isn't portable due to the use of %lld/%llu in the unit test code.