{
file: {
key: "value",
array: [
{
key00: "value00",
key01: "value01"
},
{
key10: "value10",
key11: "value11"
}
]
}
}
#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;
}
- jsoncpp版本probuf转化成json
void FormatRepeatedField(Json::Value& value, const ::google::protobuf::Message& msg, const google::protobuf::FieldDescriptor *field, const ::google::protobuf::Reflection *reflection)
{
if (NULL == field || NULL == reflection)
{
FormatToJson(value, msg);
}
for (int i = 0; i < reflection->FieldSize(msg, field); ++i)
{
Json::Value tmp_value;
switch (field->type())
{
case FieldDescriptor::TYPE_MESSAGE:
{
const Message& tmp_msg = reflection->GetRepeatedMessage(msg, field, i);
if (0 != tmp_msg.ByteSize())
{
FormatToJson(tmp_value, tmp_msg);
}
}
break;
case FieldDescriptor::TYPE_INT32:
tmp_value = reflection->GetRepeatedInt32(msg, field, i);
break;
case FieldDescriptor::TYPE_UINT32:
tmp_value = reflection->GetRepeatedUInt32(msg, field, i);
break;
case FieldDescriptor::TYPE_INT64:
{
static char int64str[25];
memset(int64str, 0, sizeof(int64str));
_snprintf(int64str, sizeof(int64str), "%lld", (long long)reflection->GetRepeatedInt64(msg, field, i));
tmp_value = int64str;
}
break;
case FieldDescriptor::TYPE_UINT64:
{
static char uint64str[25];
memset(uint64str, 0, sizeof(uint64str));
_snprintf(uint64str, sizeof(uint64str), "%llu", (unsigned long long)reflection->GetRepeatedUInt64(msg, field, i));
tmp_value = uint64str;
}
break;
case FieldDescriptor::TYPE_STRING:
case FieldDescriptor::TYPE_BYTES:
tmp_value = reflection->GetRepeatedString(msg, field, i);
break;
default:
break;
}
value.append(tmp_value);
}
}
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;
}
}
}
- rapidjson版本probuf转化成json
void FormatRepeatedField(rapidjson::Value& value, const ::google::protobuf::Message& msg,
const google::protobuf::FieldDescriptor *field,
const ::google::protobuf::Reflection *reflection,
rapidjson::Document::AllocatorType& allocator)
{
if (NULL == field || NULL == reflection)
{
FormatToJson(value, msg, allocator);
}
rapidjson::Value optionsValue (rapidjson::kArrayType);
for (int i = 0; i < reflection->FieldSize(msg, field); ++i)
{
rapidjson::Value tmp_value;
switch (field->type())
{
case FieldDescriptor::TYPE_MESSAGE:
{
const Message& tmp_msg = reflection->GetRepeatedMessage(msg, field, i);
if (0 != tmp_msg.ByteSize())
{
rapidjson::Value root(rapidjson::kObjectType); // temp delete?
FormatToJson(root, tmp_msg, allocator);
//value.AddMember(rapidjson::StringRef(field->name().c_str()), root, allocator);
tmp_value = root;
}
}
break;
case FieldDescriptor::TYPE_INT32:
tmp_value = reflection->GetRepeatedInt32(msg, field, i);
break;
case FieldDescriptor::TYPE_UINT32:
tmp_value = reflection->GetRepeatedUInt32(msg, field, i);
break;
case FieldDescriptor::TYPE_INT64:
tmp_value = reflection->GetRepeatedInt64(msg, field, i);
break;
case FieldDescriptor::TYPE_UINT64:
tmp_value = reflection->GetRepeatedUInt64(msg, field, i);
break;
case FieldDescriptor::TYPE_STRING:
case FieldDescriptor::TYPE_BYTES:
tmp_value = rapidjson::Value().SetString(reflection->GetRepeatedString(msg, field, i).c_str(),allocator).Move();
break;
default:
break;
}
optionsValue.PushBack(tmp_value, allocator);
}
value = optionsValue;
}
void FormatToJson(rapidjson::Value& value, const ::google::protobuf::Message& msg, rapidjson::Document::AllocatorType& allocator)
{
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)
{
rapidjson::Value root(rapidjson::kObjectType); // temp delete?
FormatRepeatedField(root, msg, field, reflection, allocator);
value.AddMember(rapidjson::StringRef(field->name().c_str()), root, allocator);
}
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())
{
rapidjson::Value root(rapidjson::kObjectType); // temp delete?
FormatToJson(root, tmp_msg, allocator);
value.AddMember(rapidjson::StringRef(field->name().c_str()), root, allocator);
}
}
break;
case FieldDescriptor::TYPE_INT32:
value.AddMember(rapidjson::StringRef(field->name().c_str()), reflection->GetInt32(msg, field), allocator);
break;
case FieldDescriptor::TYPE_INT64:
value.AddMember(rapidjson::StringRef(field->name().c_str()), reflection->GetInt64(msg, field), allocator);
break;
case FieldDescriptor::TYPE_UINT32:
value.AddMember(rapidjson::StringRef(field->name().c_str()), reflection->GetUInt32(msg, field), allocator);
break;
case FieldDescriptor::TYPE_UINT64:
value.AddMember(rapidjson::StringRef(field->name().c_str()), reflection->GetUInt64(msg, field), allocator);
break;
case FieldDescriptor::TYPE_STRING:
case FieldDescriptor::TYPE_BYTES:
value.AddMember(rapidjson::StringRef(field->name().c_str()),
rapidjson::Value().SetString(reflection->GetString(msg, field).c_str(),allocator).Move(),
allocator);
break;
default:
break;
}
}
}
用法
#define JSON_LOG(msg, x) do { rapidjson::Document d; \
d.SetObject(); \
rapidjson::Document::AllocatorType& c = d.GetAllocator(); \
rapidjson::Value root(rapidjson::kObjectType); \
FormatToJson(root, msg, c); \
d.AddMember(rapidjson::StringRef("logrootlog"), root, c); \
rapidjson::StringBuffer buffer; \
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer); \
d.Accept(writer); \
std::string str = std::string(buffer.GetString());\
STATISTICAL_LOG("%s\t%s", x, str.c_str()); } while(0)
参考
https://blog.csdn.net/hudejun007/article/details/44852011