Menu

Support for int64/uint64 numbers

2010-04-05
2013-04-22
  • Pavel Shevaev

    Pavel Shevaev - 2010-04-05

    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!

     
  • Pavel Shevaev

    Pavel Shevaev - 2010-04-05

    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:

    diff -r efb00fffb9a3 shared/lib/jsoncpp/include/json/forwards.h
    --- a/shared/lib/jsoncpp/include/json/forwards.h    Mon Apr 05 11:23:32 2010 +0400
    +++ b/shared/lib/jsoncpp/include/json/forwards.h    Mon Apr 05 13:32:43 2010 +0400
    @@ -1,7 +1,8 @@
     #ifndef JSON_FORWARDS_H_INCLUDED
     # define JSON_FORWARDS_H_INCLUDED
    
    -# include "config.h"
    +#include "config.h"
    +#include <sys/types.h>
    
     namespace Json {
    
    @@ -16,8 +17,10 @@
        class Features;
    
        // value.h
    -   typedef int Int;
    -   typedef unsigned int UInt;
    +   //typedef int Int;
    +   //typedef unsigned int UInt;
    +   typedef __int64_t Int;
    +   typedef __uint64_t UInt;
        class StaticString;
        class Path;
        class PathArgument;
    diff -r efb00fffb9a3 shared/lib/jsoncpp/include/json/value.h
    --- a/shared/lib/jsoncpp/include/json/value.h   Mon Apr 05 11:23:32 2010 +0400
    +++ b/shared/lib/jsoncpp/include/json/value.h   Mon Apr 05 13:32:43 2010 +0400
    @@ -140,20 +140,20 @@
                 duplicate,
                 duplicateOnCopy
              };
    -         CZString( int index );
    +         CZString( Int index );
              CZString( const char *cstr, DuplicationPolicy allocate );
              CZString( const CZString &other );
              ~CZString();
              CZString &operator =( const CZString &other );
              bool operator<( const CZString &other ) const;
              bool operator==( const CZString &other ) const;
    -         int index() const;
    +         Int index() const;
              const char *c_str() const;
              bool isStaticString() const;
           private:
              void swap( CZString &other );
              const char *cstr_;
    -         int index_;
    +         Int index_;
           };
    
        public:
    diff -r efb00fffb9a3 shared/lib/jsoncpp/src/lib_json/json_value.cpp
    --- a/shared/lib/jsoncpp/src/lib_json/json_value.cpp    Mon Apr 05 11:23:32 2010 +0400
    +++ b/shared/lib/jsoncpp/src/lib_json/json_value.cpp    Mon Apr 05 13:32:43 2010 +0400
    @@ -171,7 +171,7 @@
     // Notes: index_ indicates if the string was allocated when
     // a string is stored.
    
    -Value::CZString::CZString( int index )
    +Value::CZString::CZString( Int index )
        : cstr_( 0 )
        , index_( index )
     {
    @@ -231,7 +231,7 @@
     }
    
    -int 
    +Int
     Value::CZString::index() const
     {
        return index_;
    
     
  • Pavel Shevaev

    Pavel Shevaev - 2010-04-05

    Damn SourceForge, it added some weird garbage(just ignore it) to the top of the patch, and I can't edit the post

     
  • Tim Drijvers

    Tim Drijvers - 2010-04-05

    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.

     
  • Baptiste Lepilleur

    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).

     
  • Anonymous

    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 *&current )
     {
        *--current = 0;
    
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.