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. mybatis系列-11-一对多查询

    11.1     需求 查询订单及订单明细的信息. 11.2     sql语句 确定主查询表:订单表 确定关联查询表:订单明细表 在一对一查询基础上添加订单明细表关联即可. SELECT order ...

  2. nodejs API笔记

    一.URL 涉及到的方法 1.parse():解析地址 2.format():生成地址 3.resolve(from,to):组合成地址 举例说明: url.parse('http://baidu.c ...

  3. Struts2通配符映射

    1.一个Web 应用可能有成百上千个 action 声明. 可以利用 struts 提供的通配符映射机制把多个彼此相似的映射关系简化为一个映射关系 2.通配符映射规则 –若找到多个匹配, 没有通配符的 ...

  4. HDU 1142 A Walk Through the Forest (求最短路条数)

    A Walk Through the Forest 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1142 Description Jimmy exp ...

  5. HDU 1455 http://acm.hdu.edu.cn/showproblem.php?pid=1455

    #include<stdio.h> #include<stdlib.h> #include<math.h> #include<string.h> #de ...

  6. linux下查看端口的占用情况

    前提:首先你必须知道,端口不是独立存在的,它是依附于进程的.某个进程开启,那么它对应的端口就开启了,进程关闭,则该端口也就关闭了.下次若某个进程再次开启,则相应的端口也再次开启.而不要纯粹的理解为关闭 ...

  7. 如何更改c#项目的App.config文件

    动态修改App.Config 和web.Config 首先假设你的应用程序配置文件如下: <?xml version="1.0" encoding="utf-8&q ...

  8. myGeneration代码生成器

    转自:http://www.cnblogs.com/leitwolf/archive/2007/07/27/833255.html http://blog.csdn.net/happyhippy/ar ...

  9. Hibernate查询效率对比

    查询已知表名的实体时推荐使用getHibernateTemplate().executeWithNativeSession() + SQLQuery方式. 以下测试使用JUnit进行,仅查询一次,查询 ...

  10. 我所理解的设计模式(C++实现)——备忘录模式(Memento Pattern)

    概述: 我们玩单机游戏的时候总会遇到老婆大人的各位事情,一会去买瓶醋了,一会去打个酱油了,会耽误我们玩游戏的进程,但是此时我们能有“保存游戏”这个宝贝,我们的主基地不会在我们打酱油的时候被对手拆掉. ...