cocos2d-x3.x使用rapidjson
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的更多相关文章
- cocos2d_x_06_游戏_一个都不能死
终于效果图: 环境版本号:cocos2d-x-3.3beta0 使用内置的物理引擎 游戏主场景 // // HeroScene.h // 01_cocos2d-x // // Created by b ...
- cocos2d-x3.0创建第一个jsb游戏
第一步: 最新的cocos2d-x.下载地址https://github.com/cocos2d/cocos2d-x github上最新的引擎,值得注意的是官网上发布的引擎是稳定版.选择哪种就看个人喜 ...
- cocos-2d解决rapidjson的string参数转换
解决方法 AddMember的传入的参数不是string, 所以会报错 本质就是把string类型转换成 参数的类型 username = "string"; rapidjson: ...
- cocos2dx 3.2 定义自己使用rapidjson阅读json数据
一.说明 我在这里得到的只是一个简单的定义string和Int种类,其他数据类型可以被替换向上. 两.头文件 class JsonReadUtils { public: static JsonRead ...
- rapidjson常见使用示例
目录 目录 1 1. 前言 2 2. Move语意 2 3. rapidjson::Document 2 4. 成员迭代器MemberIterator 3 5. 数组迭代器ValueIterator ...
- cocos2dx中的Rapidjson
1 Json基础 JSON 概念和特点: JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation) JSON 是轻量级的文本数据交换格式,类似 XML ...
- Rapidjson的简单使用示例
很早就想用用Markdown了,一直没机会.今天就来试一下 先放个目录: Rapidjson的简单使用示例 rapidjson官方教程 本示例所用环境 示例代码与注释 rapidjson官方教程 如果 ...
- Cocos2d-X网络编程(5) 使用Rapidjson解析数据
Json基础及28种c++解析库性能对比 JSON 概念和特点: JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation) JSON ...
- 小尝试一下 cocos2d
好奇 cocos2d 到底是怎样一个框架,正好有个项目需要一个游戏框架,所以稍微了解了一下.小结一下了解到的情况. 基本概念 首先呢,因为 cocos2d 是基于 pyglet 做的,你完全可以直接用 ...
- 采用cocos2d-x lua 制作数字滚动效果样例
require "Cocos2d"require "Cocos2dConstants"local testscene = class("testsce ...
随机推荐
- Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing ...
- Mahout应用(一)
Mahout应用(一) Mahout 是应用于hadoop上的数据挖掘工具(废话不多说) 这里先简单介绍一下mahout的一般使用方法. 拿kmeans为列子 Mahout中的kmeans所需要的输入 ...
- Codeforces 364
A 第一题明显统计,注意0和long long(我WA,RE好几次) /* * Problem: A. Matrix * Author: Shun Yao */ #include <string ...
- 【bz2002】弹飞绵羊
题意: 给出n个节点 及其父亲 和m个指令1:表示求节点i到根节点(n+1)的距离2:表示将节点i的父亲更换为j 题解: 动态树link.cut.access模板题 貌似没什么难度- - 代码: #i ...
- iOS事件机制(一)
运用的前提是掌握 掌握的本质是理解 本篇内容将围绕iOS中事件及其传递机制进行学习和分析.在iOS中,事件分为三类: 触控事件(单点.多点触控以及各种手势操作) 传感器事件(重力.加速度传感器等) 远 ...
- DelphiXE Android的所有权限按照分类总结说明
相关资料:http://www.delphitop.com/html/Android/2778.html 网络相关的: android.permission.INTERNET 允许程序打开网络套接 ...
- 绑定线程到特定CPU处理器
参考这篇文章 http://blog.chinaunix.net/uid-27761170-id-5050258.html 代码如下: #define _GNU_SOURCE #include < ...
- 通过OAF实现RTF多语言
职责: Oracle XML Publisher 管理员 -->主页 -->模板 -->创建模板 1.定义rtf模板时 在创建模板时,勾选可转换.
- Winfrom强大的自动更新程序
推荐一:.Net 小型软件自动更新库(SimpAutoUpdater) http://www.fishlee.net/soft/simple_autoupdater/usage.html 下载地址:h ...
- Cocos2d-x——Cocos2d-x 屏幕适配新解 – 兼容与扩展【转载】
Cocos2d-x 屏幕适配新解 – 兼容与扩展 本文出自[无间落叶](转载请保留出处):http://blog.leafsoar.com/archives/2013/05-13-08.html 在读 ...