用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.csdn.net/smilelance
分享到:
相关推荐
JsonRpc-Cpp - JSON-RPC implementation. * Copyright (C) 2008-2011 Sebastien Vincent * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser ...
json-lib-2.4-jdk15.jar下载,json-lib是一款简单易用的Java JSON库。它可以将Java对象转换成JSON字符串,也可以将JSON字符串转换成Java对象。json-lib体积小、无依赖,性能表现也不错,适合在各种Java项目中使用。 ...
json-c-0.12.1.tar.gz,json-c库离线安装包
JSON-lib包json-lib-0.8.jar和json-lib-0.9.jar包
jquery和struts2结合使用的jar包
json-smart-2.3.jar
里面包含json-lib所需的全部jar文件. json-lib-2.1-jdk15.jar ezmorph-1.0.3.jar commons-beanutils.jar commons-collections-3.2.1.jar commons-lang-2.4.jar commons-logging-1.1.1.jar
json架包,包好json-lib-2.2.3-jdk13.jar,json-lib-2.4-jdk14.jar以及附带的支持包ezmorph-1.0.6.jar,commons-logging.jar,commons-lang.jar等
json-smart-2.0-RC3.jar 亲测,可用!
JSON Schema 生成库——json-schema-inferrer(java版).rar
json-lib-2.4-jdk15所需全部JAR包,资源清单 json-lib-2.4-jdk15.jar 依赖: ezmorph-1.0.6.jar commons-beanutils-1.8.3.jar commons-lang-2.5.jar morph-1.1.1.jar commons-logging-1.1.1.jar commons-collections-...
json的核心包:json-lib-2.4-jdk15.jar;json-2.4所依赖的jar包:commons-beanutils-1.8.0.jar,commons-collections-3.2.1.jar,commons-lang-2.5.jar,commons-logging-1.1.1.jar,ezmorph-1.0.6.jar;以及和...
libjson-rpc-cpp此框架为C ++提供跨平台的JSON-RPC(远程过程调用)支持。 它与JSON-RPC 2.0和1.0完全兼容。 在下一个RPC项目中使用libjson-rpc-cpp的5个很好的理由libjson-rpc-cpp此框架为C ++提供跨平台的...
json-lib-2.4-jdk15.jar json-lib-2.4-jdk15-sources.jar json-lib-2.4-jdk15-javadoc.jar 包含文件有:json-lib-2.4-jdk15.jar json-lib-2.4-jdk15-sources.jar json-lib-2.4-jdk15-javadoc.jar
JSON-C库 JSON 库的选取 现在JSON解析库满天飞,没有必要再自己写个了,除非是商业程序 。我试过JsonCPP, QJson, Json-Spirit, jaula等C++的库。遇到总总问题,有不支持中文(UTF-8编码也不行)、不方便在Windows下...
struts2-json-plugin,Struts JSON插件
json-simple-1.1.jar json-simple-1.1-sources.jar (json-simple-1.1.jar)&(json-simple-1.1-sources.jar).jar (json-simple-1.1.jar)&(json-simple-1.1-sources.jar).jar
json-cpp-示例在C ++中使用json
编译好的 windows 版的 json-c-0.8 库。压缩包里面包含三个文件夹..\include,..\lib,..\src,”lib\“下有.lib和.dll。要使用该库,需要包含 json.h, json_object.h, json_tokener.h.
json-lib-2.2.3-jdk13.jar commons-beanutils-1.7.0.jar commons-collections-3.2.1.jar commons-lang-2.4.jar commons-logging-1.1.1.jar ezmorph-1.0.6.jar