json-cpp longlong 类型的扩展

本文详细介绍了在使用Cocos2d-x进行跨平台开发时,遇到JSON库不支持int64类型的问题,并提供了解决方案。通过自定义类型和方法,实现了对int64类型的正确解析,确保了项目的顺利进行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

          用cocos2dx做跨平台项目的时候,自然就用了c++版本的json库,这个json-cpp是用的比较多的,总体用起来还算可以,有一个很不方便的地方就是不支持long long(int64)类型,一开始我使用double类型来强制转换,后来出了bug,double精度只有16位,超过20位的就会被四舍五入,试过了官网上bug列表里的方法都不行,

没办法,花点时间添加longValue才解决这个问题,加地方实在太多了,附上主要扩展代码备忘:


一,定义一个long类型

//lancer add long

   typedef long long Long;


二、

//类型里添加longValue   

   enum ValueType

   {

      nullValue = 0, ///< 'null' value

      intValue,      ///< signed integer value

      uintValue,     ///< unsigned integer value

      //lancer add for longlong

      longValue,     //signed int 64

      realValue,     ///< double value

      stringValue,   ///< UTF-8 string value

      booleanValue,  ///< bool value

      arrayValue,    ///< array value (ordered list)

      objectValue    ///< object value (collection of name/value pairs).


   };


三、添加一个解析long类型的方法

bool 

Reader::decodeLongLong( Token &token )

{

    Long value = 0;

    const int bufferSize = 32;

    int count;

    int length = int(token.end_ - token.start_);

    if ( length <= bufferSize )

    {

        Char buffer[bufferSize];

        memcpy( buffer, token.start_, length );

        buffer[length] = 0;

        count = sscanf( buffer, "%lld", &value );

    }

    else

    {

        std::string buffer( token.start_, token.end_ );

        count = sscanf( buffer.c_str(), "%lld", &value );

    }

    

    if ( count != 1 )

        return addError( "'" + std::string( token.start_, token.end_ ) + "' is not a number.", token );

    currentValue() = value;

    return true;

}


四、解析数字

bool 

Reader::decodeNumber( Token &token )方法里,超出int型数据范围的时候使用decodeLongLong方法,而不是之前的decodeDouble

   while ( current < token.end_ )

   {

      Char c = *current++;

      if ( c < '0'  ||  c > '9' )

         return addError( "'" + std::string( token.start_, token.end_ ) + "' is not a number.", token );

      if ( value >= threshold )

//         return decodeDouble( token );

          //lancer add for long long

          return decodeLongLong( token );

      value = value * 10 + Value::UInt(c - '0');

   }


五,添加一个asLongLong方法供调用

//lancer as long

Long Value::asLongLong() const

{

    switch ( type_ )

    {

        case nullValue:

            return 0;

        case intValue:

            return value_.int_;

        case uintValue:

            JSON_ASSERT_MESSAGE( value_.uint_ < (unsigned)maxInt, "integer out of signed integer range" );

            return value_.uint_;

        case longValue:

            return value_.long_;

        case realValue:

            JSON_ASSERT_MESSAGE( value_.real_ >= minLong  &&  value_.real_ <= maxLong, "Real out of signed long range" );

            return Long( value_.real_ );

        case booleanValue:

            return value_.bool_ ? 1 : 0;

        case stringValue:

        {

            //lancer safe long long

            return atoll(value_.string_);

        }

        case arrayValue:

        case objectValue:

            JSON_ASSERT_MESSAGE( false, "Type is not convertible to int" );

        default:

            JSON_ASSERT_UNREACHABLE;

    }

    return 0; // unreachable;    

}


其他地方细节要改的太多了,我都是全局搜索realValue,然后对应的地方改掉


另外,由于后台传输数据的时候数字可能会被传成字符串,所以解析数字的地方都需要加上对字符串的判断,

比如:

Value::Int 

Value::asInt() const

{

   switch ( type_ )

   {

case stringValue:

    {

      //lancer safe int

      return atoi(value_.string_);

    }


转载请保留以下信息:

作者(Author):smilelance

出处( From ):http://blog.youkuaiyun.com/smilelance




转载于:https://www.cnblogs.com/secbook/archive/2012/06/13/2655387.html

35 0 F:\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\include\c++\random In file included from F:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/random 4 C:\Users\余伟杰\Desktop\RSA.cpp from C:\Users\余伟杰\Desktop\RSA.cpp 32 2 F:\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\include\c++\bits\c++0x_warning.h [Error] #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options. C:\Users\余伟杰\Desktop\RSA.cpp In function 'bool isPrime(long long int, int)': 33 5 C:\Users\余伟杰\Desktop\RSA.cpp [Error] 'random_device' was not declared in this scope 34 5 C:\Users\余伟杰\Desktop\RSA.cpp [Error] 'mt19937' was not declared in this scope 35 5 C:\Users\余伟杰\Desktop\RSA.cpp [Error] 'uniform_int_distribution' was not declared in this scope 35 30 C:\Users\余伟杰\Desktop\RSA.cpp [Error] expected primary-expression before 'long' 38 27 C:\Users\余伟杰\Desktop\RSA.cpp [Error] 'gen' was not declared in this scope 38 30 C:\Users\余伟杰\Desktop\RSA.cpp [Error] 'dis' was not declared in this scope C:\Users\余伟杰\Desktop\RSA.cpp In function 'long long int generateLargePrime(int)': 59 5 C:\Users\余伟杰\Desktop\RSA.cpp [Error] 'random_device' was not declared in this scope 60 5 C:\Users\余伟杰\Desktop\RSA.cpp [Error] 'mt19937' was not declared in this scope 61 5 C:\Users\余伟杰\Desktop\RSA.cpp [Error] 'uniform_int_distribution' was not declared in this scope 61 30 C:\Users\余伟杰\Desktop\RSA.cpp [Error] expected primary-expression before 'long' 65 21 C:\Users\余伟杰\Desktop\RSA.cpp [Error] 'gen' was not declared in this scope 65 24 C:\Users\余伟杰\Desktop\RSA.cpp [Error] 'dis' was not declared in this scope C:\Users\余伟杰\Desktop\RSA.cpp In function 'void generateRSAKeys(RSAKey&, RSAKey&, int)': 103 22 C:\Users\余伟杰\Desktop\RSA.cpp [Error] 'gcd' was not declared in this scope 108 22 C:\Users\余伟杰\Desktop\RSA.cpp [Warning] extended initializer lists only available with -std=c++11 or -std=gnu++11 108 15 C:\Users\余伟杰\Desktop\RSA.cpp [Warning] extended initializer lists only available with -std=c++11 or -std=gnu++11 109 23 C:\Users\余伟杰\Desktop\RSA.cpp [Warning] extended initializer lists only available with -std=c++11 or -std=gnu++11 109 16 C:\Users\余伟杰\Desktop\RSA.cpp [Warning] extended initializer lists only available with -std=c++11 or -std=gnu++11 C:\Users\余伟杰\Desktop\RSA.cpp In function 'std::vector<long long int> rsaEncrypt(const string&, const RSAKey&)': 115 19 C:\Users\余伟杰\Desktop\RSA.cpp [Error] range-based 'for' loops are not allowed in C++98 mode 117 19 C:\Users\余伟杰\Desktop\RSA.cpp [Error] redeclaration of 'long long int c' 115 15 C:\Users\余伟杰\Desktop\RSA.cpp [Note] 'char c' previously declared here C:\Users\余伟杰\Desktop\RSA.cpp In function 'std::string rsaDecrypt(const std::vector<long long int>&, const RSAKey&)': 126 24 C:\Users\余伟杰\Desktop\RSA.cpp [Error] range-based 'for' loops are not allowed in C++98 mode C:\Users\余伟杰\Desktop\RSA.cpp In function 'int main()': 147 24 C:\Users\余伟杰\Desktop\RSA.cpp [Error] range-based 'for' loops are not allowed in C++98 mode
最新发布
07-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值