jsoncpp,rapidjson語法比較以及範例:probuf轉化成json
阿新 • • 發佈:2018-12-17
{
file: {
key: "value",
array: [
{
key00: "value00",
key01: "value01"
},
{
key10: "value10",
key11: "value11"
}
]
}
}
rapidjson
#include <iostream>
#include <fstream>
#include <sstream>
#include "rapidjson/rapidjson.h"
#include "rapidjson/document.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"
int main()
{
//讀取檔案
std::fstream fs("file.json");
if(!fs)
std::cout<<std::endl<<"file open failed"<<std::endl;
std::stringstream ss;
ss << fs.rdbuf();
std::string str = ss.str();
//解析
rapidjson::Document doc;
if (doc.Parse(str.c_str()).HasParseError())
std::cout<<std::endl<<"parse failed"<<std::endl;
rapidjson::Document::AllocatorType&allocator = doc.GetAllocator(); // 獲取最初資料的分配器
//增加元素
doc["file"].AddMember("key2", "value2", allocator);
//刪除元素
doc[ "file"].RemoveMember("key");
//修改值
doc["file"]["key2"].SetString("newValue");
//陣列操作//
rapidjson::Value& array = doc["file"]["array"];//獲取陣列物件
//新增陣列物件
rapidjson::Value item(rapidjson::kObjectType); //建立數組裡面對象
item.AddMember("key20", "value20", allocator);
item.AddMember("key21", "value21", allocator);
array.PushBack(item, allocator);
//讀取陣列元素
if(array[2].HasMember("key20"))//查詢是否存在
std::cout<<std::endl<<array[2]["key20"].GetString()<<std::endl;
//刪除陣列最後一個元素
array.PopBack();
//輸出
rapidjson::StringBuffer buffer;
rapidjson::Writer< rapidjson::StringBuffer > writer(buffer);
doc.Accept(writer);
std::string out = buffer.GetString();
std::cout<<std::endl<<out<<std::endl;
}
jsconcpp
#include <iostream>
#include <fstream>
#include <jsoncpp/jsoncpp.h>
int main()
{
//讀取檔案
std::fstream fs("file.json");
if(!fs)
std::cout<<std::endl<<"file open failed"<<std::endl;
//解析
Json::Reader reader;
Json::Value root;
if(!reader.parse(fs, root))
std::cout<<std::endl<<"parse failed"<<std::endl;
//修改元素值
root["file"]["key"] = Json::Value("1");
//刪除元素
root["file"].removeMember("key");
//新增元素
root["file"]["key2"] = Json::Value("value2");
//陣列操作
Json::Value& array = root["file"]["array"]; //獲取陣列
//刪除,讀取陣列元素
for(int i = 0; i < array.size(); i++)
{
if(array[i].isMember("key00"))
array[i].removeMember("key00");
if(!array[i].isMember("key01"))
continue;
std::string out = array[i]["key01"].asString();
std::cout<<std::endl<<"key01: "<<out<<std::endl;
}
//插入陣列元素
Json::Value item(Json::objectValue);
item["key20"] = Json::Value("value20");
item["key21"] = Json::Value("value21");
array.append(item);
//輸出
std::string out = root.toStyledString();
std::cout<<std::endl<<out<<std::endl;
}
void FormatToJson(Json::Value& value, const ::google::protobuf::Message& msg)
{
const Descriptor* descriptor = msg.GetDescriptor();
const Reflection* reflection = msg.GetReflection();
const int count = descriptor->field_count();
for (int i = 0; i < count; ++i)
{
const FieldDescriptor* field = descriptor->field(i);
if (field->is_repeated())
{
if (reflection->FieldSize(msg, field) > 0)
{
FormatRepeatedField(value[field->name()], msg, field, reflection);
}
continue;
}
if (!reflection->HasField(msg, field))
{
continue;
}
switch (field->type())
{
case FieldDescriptor::TYPE_MESSAGE:
{
const Message& tmp_msg = reflection->GetMessage(msg, field);
if (0 != tmp_msg.ByteSize())
{
FormatToJson(value[field->name()], tmp_msg);
}
}
break;
case FieldDescriptor::TYPE_INT32:
value[field->name()] = reflection->GetInt32(msg, field);
break;
case FieldDescriptor::TYPE_UINT32:
value[field->name()] = reflection->GetUInt32(msg, field);
break;
case FieldDescriptor::TYPE_INT64:
{
static char int64str[25];
memset(int64str, 0, sizeof(int64str));
_snprintf(int64str, sizeof(int64str), "%lld", (long long)reflection->GetInt64(msg, field));
value[field->name()] = int64str;
}
break;
case FieldDescriptor::TYPE_UINT64:
{
static char uint64str[25];
memset(uint64str, 0, sizeof(uint64str));
_snprintf(uint64str, sizeof(uint64str), "%llu", (unsigned long long)reflection->GetUInt64(msg, field));
value[field->name()] = uint64str;
}
break;
case FieldDescriptor::TYPE_STRING:
case FieldDescriptor::TYPE_BYTES:
{
value[field->name()] = reflection->GetString(msg, field);
}
break;
default:
break;
}
}
}