#include "json/filestream.h"
#include "json/stringbuffer.h"
#include "json/writer.h"
#include "JsonToolFor2dx.h"
//#include "json/encodings.h"
//#include <locale>
//#include <codecvt>
USING_NS_CC;
//using namespace rapidjson;
static JsonTool* jsonTool = nullptr;
JsonTool::JsonTool(){
}
JsonTool* JsonTool::getInstance(){
if (!jsonTool){
jsonTool = new(std::nothrow) JsonTool();
jsonTool->autorelease();
jsonTool->retain();
}
return jsonTool;
}
cocos2d::Value JsonTool::getJosnDataFromFileToValue(std::string filePath){
std::string jsonStr(FileUtils::getInstance()->getStringFromFile(filePath));
return getJsonDataFromStrToValue(jsonStr);
}
cocos2d::Value JsonTool::getJsonDataFromStrToValue(std::string jsonData){
rapidjson::Document* document = new rapidjson::Document();
document->Parse<0>(jsonData.c_str());
if (document->HasParseError()){
CCLOG("parse erro");
return cocos2d::Value();
}
if (document->IsObject()){
ValueMap valueMap;
for (auto iter = document->MemberBegin(); iter != document->MemberEnd(); iter++){
writerDataToValue(valueMap, iter);
}
return cocos2d::Value(valueMap);
}
else if (document->IsArray()){
ValueVector valueVector;
for (auto iter = document->Begin(); iter != document->End(); iter++){
writerDataToValue(valueVector, iter);
}
return cocos2d::Value(valueVector);
}
delete document;
return cocos2d::Value();
}
void JsonTool::writerDataToValue(ValueMap &ccValue, rapidjson::Value::MemberIterator iter){
std::string keyName = iter->name.GetString();
if (iter->value.IsArray()){
ValueVector valueVector;
for (auto iterThis = iter->value.Begin(); iterThis != iter->value.End(); iterThis++){
writerDataToValue(valueVector, iterThis);
}
std::pair<std::string,cocos2d::Value> var = std::make_pair(keyName, cocos2d::Value(valueVector));
ccValue.insert(var);
//ccValue[keyName] = cocos2d::Value(valueVector);
}
else if (iter->value.IsObject()){
ValueMap valueMap;
for (auto iterThis = iter->value.MemberBegin(); iterThis != iter->value.MemberEnd(); iterThis++){
writerDataToValue(valueMap, iterThis);
}
std::pair<std::string, cocos2d::Value> var = std::make_pair(keyName, cocos2d::Value(valueMap));
ccValue.insert(var);
//ccValue[keyName] = cocos2d::Value(valueMap);
}
else if (iter->value.IsBool()){
std::pair<std::string, cocos2d::Value> var = std::make_pair(keyName, cocos2d::Value(iter->value.GetBool()));
ccValue.insert(var);
//ccValue[keyName] = cocos2d::Value(iter->value.GetBool());
}
else if (iter->value.IsDouble()){
std::pair<std::string, cocos2d::Value> var = std::make_pair(keyName, cocos2d::Value(iter->value.GetDouble()));
ccValue.insert(var);
//ccValue[keyName] = cocos2d::Value(iter->value.GetDouble());
}
else if (iter->value.IsInt()){
std::pair<std::string, cocos2d::Value> var = std::make_pair(keyName, cocos2d::Value(iter->value.GetInt()));
ccValue.insert(var);
//ccValue[keyName] = cocos2d::Value(iter->value.GetInt());
}
else if (iter->value.IsString()){
std::pair<std::string, cocos2d::Value> var = std::make_pair(keyName, cocos2d::Value(iter->value.GetString()));
ccValue.insert(var);
//ccValue[keyName] = cocos2d::Value(iter->value.GetString());
}
else if (iter->value.IsNull()){
std::pair<std::string, cocos2d::Value> var = std::make_pair(keyName, cocos2d::Value());
ccValue.insert(var);
//ccValue[keyName] = cocos2d::Value(iter->value.GetString());
}
else{
}
}
void JsonTool::writerDataToValue(cocos2d::ValueVector &ccValue, rapidjson::Value::ValueIterator iterArray){
if (iterArray->IsArray()){
ValueVector valueVector;
for (auto iterThis = iterArray->Begin(); iterThis != iterArray->End(); iterThis++){
writerDataToValue(valueVector, iterThis);
}
//valueMap[keyName] = cocos2d::Value(valueVector);
ccValue.push_back(cocos2d::Value(valueVector));
}
else if (iterArray->IsObject()){
ValueMap valueMapOfObj;
for (auto iterThis = iterArray->MemberBegin(); iterThis != iterArray->MemberEnd(); iterThis++){
writerDataToValue(valueMapOfObj, iterThis);
}
ccValue.push_back(cocos2d::Value(valueMapOfObj));
}
else if (iterArray->IsBool()){
ccValue.push_back(cocos2d::Value(iterArray->GetBool()));
}
else if (iterArray->IsDouble()){
ccValue.push_back(cocos2d::Value(iterArray->GetDouble()));
}
else if (iterArray->IsInt()){
ccValue.push_back(cocos2d::Value(iterArray->GetInt()));
}
else if (iterArray->IsString()){
ccValue.push_back(cocos2d::Value(iterArray->GetString()));
}
else if (iterArray->IsNull()){
ccValue.push_back(cocos2d::Value());
}
else{
}
}
void JsonTool::writerDataToFile(cocos2d::Value& ccValue, std::string fullFilePath){
if (ccValue.isNull()){
return;
}
std::string& jsonStr = getJsonDataFromValueToStr(ccValue);
FileUtils::getInstance()->writeStringToFile(jsonStr, fullFilePath);
}
std::string JsonTool::getJsonDataFromValueToStr(cocos2d::Value& ccValue){
if (ccValue.isNull()){
return "";
}
rapidjson::Document* document = new rapidjson::Document();
if (ccValue.getType()==cocos2d::Value::Type::MAP){
document->SetObject();
writerValueToJson(ccValue.asValueMap(), *document, document);
}
else if (ccValue.getType() == cocos2d::Value::Type::VECTOR){
document->SetArray();
writerValueToJson(ccValue.asValueVector(), *document, document);
}
rapidjson::StringBuffer strBuffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(strBuffer);
document->Accept(writer);
return strBuffer.GetString();
}
void JsonTool::writerValueToJson(cocos2d::ValueMap &ccValue, rapidjson::Value& jsonValue, rapidjson::Document* document){
for (auto iter = ccValue.begin(); iter != ccValue.end();iter++){
if (iter->second.getType() == cocos2d::Value::Type::INTEGER){
jsonValue.AddMember(rapidjson::StringRef(iter->first.c_str(), iter->first.size()), iter->second.asInt(), document->GetAllocator());
}
else if (iter->second.getType() == cocos2d::Value::Type::STRING){
std::string strVar = iter->second.asString();
rapidjson::Value var;
var.SetString(strVar.c_str(), strVar.size(), document->GetAllocator());
jsonValue.AddMember(rapidjson::StringRef(iter->first.c_str(), iter->first.size()), var, document->GetAllocator());
}
else if (iter->second.getType() == cocos2d::Value::Type::DOUBLE){
jsonValue.AddMember(rapidjson::StringRef(iter->first.c_str(), iter->first.size()), iter->second.asDouble(), document->GetAllocator());
}
else if (iter->second.getType() == cocos2d::Value::Type::FLOAT){
jsonValue.AddMember(rapidjson::StringRef(iter->first.c_str(), iter->first.size()), iter->second.asFloat(), document->GetAllocator());
}
else if (iter->second.getType() == cocos2d::Value::Type::MAP){
rapidjson::Value jsonVar(rapidjson::kObjectType);
writerValueToJson(iter->second.asValueMap(), jsonVar, document);
jsonValue.AddMember(rapidjson::StringRef(iter->first.c_str(), iter->first.size()), jsonVar, document->GetAllocator());
}
else if (iter->second.getType() == cocos2d::Value::Type::VECTOR){
rapidjson::Value jsonVar(rapidjson::kArrayType);
writerValueToJson(iter->second.asValueVector(), jsonVar, document);
jsonValue.AddMember(rapidjson::StringRef(iter->first.c_str(), iter->first.size()), jsonVar, document->GetAllocator());
}
else if (iter->second.getType() == cocos2d::Value::Type::BOOLEAN){
jsonValue.AddMember(rapidjson::StringRef(iter->first.c_str(), iter->first.size()), iter->second.asBool(), document->GetAllocator());
}
else if (iter->second.getType() == cocos2d::Value::Type::NONE){
jsonValue.AddMember(rapidjson::StringRef(iter->first.c_str(), iter->first.size()), rapidjson::Value(rapidjson::kNullType), document->GetAllocator());
}
else{
}
}
}
void JsonTool::writerValueToJson(cocos2d::Valu
没有合适的资源?快使用搜索试试~ 我知道了~
cocos2d-x json字符串与cocos2d::Value转换工具
共2个文件
h:1个
cpp:1个
需积分: 14 10 下载量 16 浏览量
2017-06-17
19:10:36
上传
评论
收藏 2KB RAR 举报
温馨提示
该资源主要用于cocos2d-x中Value与json字符串的相互转换,提供从json文件读取为cocos2d::Value,cocos2d::Value写入到文件,cocos2d::Value转换为json字符串,json字符串转换为cocos2d::Value。json字符串转换成cocos::Value后就可以很方便的使用了,其本质就是json字符串的数组对应cocos2d::ValueVector,对象对应cocos2d::ValueMap.
资源推荐
资源详情
资源评论
收起资源包目录
JsonToolFor2d-x.rar (2个子文件)
JsonToolFor2dx.h 1KB
JsonToolFor2d-x.cpp 9KB
共 2 条
- 1
资源评论
__志__
- 粉丝: 3
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- (源码)基于ArcEngine的GIS数据处理系统.zip
- (源码)基于JavaFX和MySQL的医院挂号管理系统.zip
- (源码)基于IdentityServer4和Finbuckle.MultiTenant的多租户身份认证系统.zip
- (源码)基于Spring Boot和Vue3+ElementPlus的后台管理系统.zip
- (源码)基于C++和Qt框架的dearoot配置管理系统.zip
- (源码)基于 .NET 和 EasyHook 的虚拟文件系统.zip
- (源码)基于Python的金融文档智能分析系统.zip
- (源码)基于Java的医药管理系统.zip
- (源码)基于Java和MySQL的学生信息管理系统.zip
- (源码)基于ASP.NET Core的零售供应链管理系统.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功