rapidjson效率高,所以之前cocostudio里面解析用的jsoncpp也换成了rapidjson。

引擎又带有rapidjson库,所以不用单独去下载,直接就可以用。

这里主要写一下关于解析和存储的代码笔记。

1. 解析

#include "json/rapidjson.h"
#include "json/document.h"
#include "json/writer.h"
#include "json/stringbuffer.h" #define FILE_Name "EmailInfo.txt" #define KEY_Root       "EmailList"
#define KEY_Id "id"
#define KEY_Type    "type"
#define KEY_Content "content"
#define KEY_BonusId "bonus_unitID"
#define KEY_BonusCount   "bonusCount"
#define KEY_IsRead "isAlreadyRead"
#define KEY_RecvTime   "recvTime" //将要解析的json格式
// {
// "EmailList":[
// {
// "id":1,
// "type":1,
// "content":"string",
// "bonus_unitID":99,
// "bonusCount":10,
// "isAlreadyRead":false,
// "recvTime":11111
// }
// ]
// } //解析函数
bool EmailHelper::initData()
{
string filePath = FileUtils::getInstance()->getWritablePath();
filePath += FILE_Name;
Data dat = FileUtils::getInstance()->getDataFromFile(filePath);
if (dat.getSize() <= )
{
// no file
CCLOG("%s ---> no file [%s]", __FUNCTION__, filePath.c_str());
return false;
}
char *buf = new char[dat.getSize()+];
strncpy(buf, (char*)dat.getBytes(), dat.getSize());
buf[dat.getSize()] = ; //重新分配内存就是为了这一步,否则直接用Data来解析会解析错误。
CCLOG("%s ---> %s\n", __FUNCTION__, buf); rapidjson::Document d;
d.Parse<>(buf);
delete [] buf;
if (d.HasParseError()) //解析错误
{
CCLOG("%s ---> GetParseError %s\n", __FUNCTION__, d.GetParseError());
return false;
}
else if (d.IsObject())
{
if (d.HasMember(KEY_Root) && d[KEY_Root].IsArray()/* && d[KEY_Root].Size() > 0*/)
{
const rapidjson::Value& arr = d[KEY_Root];
for (int i=,count=arr.Size(); i<count; ++i)
{
Email temp;
if (arr[i].HasMember(KEY_Id)) temp.id = arr[i][KEY_Id].GetUint();
if (arr[i].HasMember(KEY_Type)) temp.type = (Email::EmailType)arr[i][KEY_Type].GetUint();
if (arr[i].HasMember(KEY_Content)) temp.content = arr[i][KEY_Content].GetString();
if (arr[i].HasMember(KEY_BonusId)) temp.bonus_unitID = arr[i][KEY_BonusId].GetUint();
if (arr[i].HasMember(KEY_BonusCount)) temp.bonusCount = arr[i][KEY_BonusCount].GetUint();
if (arr[i].HasMember(KEY_IsRead)) temp.isAlreadyRead = arr[i][KEY_IsRead].GetBool();
if (arr[i].HasMember(KEY_RecvTime)) temp.recvTime = arr[i][KEY_RecvTime].GetInt();
m_vecEmailList.push_back(temp);
}
return true;
}
} return false;
}

2. 存储

bool EmailHelper::saveData()
{
rapidjson::Document document;
document.SetObject(); //Set this value as an empty object.
rapidjson::Document::AllocatorType& allocator = document.GetAllocator();
//value
rapidjson::Value emailArr(rapidjson::kArrayType);
for (int i=,count=m_vecEmailList.size(); i<count; ++i)
{
rapidjson::Value object(rapidjson::kObjectType);
object.AddMember<unsigned int>(KEY_Id, m_vecEmailList[i].id, allocator);
object.AddMember<unsigned int>(KEY_Type, m_vecEmailList[i].type, allocator);
object.AddMember<const char*>(KEY_Content, m_vecEmailList[i].content.c_str(), allocator);
object.AddMember<unsigned int>(KEY_BonusId, m_vecEmailList[i].bonus_unitID, allocator);
object.AddMember<unsigned int>(KEY_BonusCount, m_vecEmailList[i].bonusCount, allocator);
object.AddMember<bool>(KEY_IsRead, m_vecEmailList[i].isAlreadyRead, allocator);
object.AddMember<int>(KEY_RecvTime, m_vecEmailList[i].recvTime, allocator);
emailArr.PushBack(object, allocator);
}
document.AddMember(KEY_Root, emailArr, allocator); rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
document.Accept(writer); string filePath = FileUtils::getInstance()->getWritablePath();
filePath += FILE_Name;
FILE* fp = fopen(FileUtils::getInstance()->fullPathForFilename(filePath).c_str(),"w+");//具有读写属性,写的时候如果文件存在,会被清空,从头开始写。
if (!fp)
{
CCASSERT(fp,"file open faild!");
return false;
}
fwrite(buffer.GetString(),sizeof(char),buffer.Size(),fp);
fclose(fp); CCLOG("%s %s ---> %s", __FUNCTION__, filePath.c_str(), buffer.GetString());
return true;
}

cocos2d-x3.x使用rapidjson的更多相关文章

  1. cocos2d_x_06_游戏_一个都不能死

    终于效果图: 环境版本号:cocos2d-x-3.3beta0 使用内置的物理引擎 游戏主场景 // // HeroScene.h // 01_cocos2d-x // // Created by b ...

  2. cocos2d-x3.0创建第一个jsb游戏

    第一步: 最新的cocos2d-x.下载地址https://github.com/cocos2d/cocos2d-x github上最新的引擎,值得注意的是官网上发布的引擎是稳定版.选择哪种就看个人喜 ...

  3. cocos-2d解决rapidjson的string参数转换

    解决方法 AddMember的传入的参数不是string, 所以会报错 本质就是把string类型转换成 参数的类型 username = "string"; rapidjson: ...

  4. cocos2dx 3.2 定义自己使用rapidjson阅读json数据

    一.说明 我在这里得到的只是一个简单的定义string和Int种类,其他数据类型可以被替换向上. 两.头文件 class JsonReadUtils { public: static JsonRead ...

  5. rapidjson常见使用示例

    目录 目录 1 1. 前言 2 2. Move语意 2 3. rapidjson::Document 2 4. 成员迭代器MemberIterator 3 5. 数组迭代器ValueIterator ...

  6. cocos2dx中的Rapidjson

    1 Json基础 JSON 概念和特点: JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation) JSON 是轻量级的文本数据交换格式,类似 XML ...

  7. Rapidjson的简单使用示例

    很早就想用用Markdown了,一直没机会.今天就来试一下 先放个目录: Rapidjson的简单使用示例 rapidjson官方教程 本示例所用环境 示例代码与注释 rapidjson官方教程 如果 ...

  8. Cocos2d-X网络编程(5) 使用Rapidjson解析数据

    Json基础及28种c++解析库性能对比 JSON 概念和特点:     JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation)     JSON ...

  9. 小尝试一下 cocos2d

    好奇 cocos2d 到底是怎样一个框架,正好有个项目需要一个游戏框架,所以稍微了解了一下.小结一下了解到的情况. 基本概念 首先呢,因为 cocos2d 是基于 pyglet 做的,你完全可以直接用 ...

  10. 采用cocos2d-x lua 制作数字滚动效果样例

    require "Cocos2d"require "Cocos2dConstants"local testscene = class("testsce ...

随机推荐

  1. Ruby多字节字符的设计

    Perl.Python的多字节字符处理方式是UCS(Universal Code Set),Ruby的多字节字符处理方式是CSI(Code Set Independent).UCS的做法是,不管你读取 ...

  2. 快速建立Linux c/c++编译环境

    sudo apt-get install build-essential 省时又省心~

  3. Java ArrayList Sort

    Collections.sort(hits_list, new Comparator<ScoreDoc>(){ @Override public int compare(ScoreDoc ...

  4. Failed to allocate the network(s), not rescheduling

    Failed to allocate the network(s), not rescheduling 在计算节点的/etc/nova/nova.conf中添加下面两句 #Fail instance ...

  5. Nginx和Tengine的详细安装图文教程(Linux下)

    简洁安装 安装依赖 yum -y install gcc openssl-devel pcre-devel zlib-devel 编译三步走./configure \ --prefix=/opt/sx ...

  6. 软件工程个人作业——Agile Software Development读后感

    昨天利用了半天的时间看了下老师给的网页下的8篇文章和一段宣言,将感悟整理为下面的一篇博客. 首先先介绍一下这个网页.记得我们上学期上过一门课叫做面向对象建模方法,在这门课上刘超老师极力推荐的一本教材— ...

  7. EasyUI样式在IE下无法显示原因总结

    1.js css顺序错误 <script type="text/javascript" charset="utf-8" src="js/jque ...

  8. UVA 11354 Bond(MST + LCA)

    n<=50000, m<=100000的无向图,对于Q<=50000个询问,每次求q->p的瓶颈路. 其实求瓶颈路数组maxcost[u][v]有用邻接矩阵prim的方法.但是 ...

  9. java commons-lang 工具包 逃脱工具 转unicode 及其他

    <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</ar ...

  10. vs2010生成Dll文件并引用dll(C#)

    1.创建新C#控制台应用程序,项目命名createBll,打开Program.cs重命名为TestA(可以不重命名)并修改代码,如图: 写好后,可以写其它的类.cs文件 2.完成后,点击菜单栏的“项目 ...