-
Notifications
You must be signed in to change notification settings - Fork 12
/
pb2Json.h
39 lines (36 loc) · 1.32 KB
/
pb2Json.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//
// Created by [email protected]
// 5/26/2019
//
#ifndef PROTOBUF2JSON_H
#define PROTOBUF2JSON_H
#include <json/json.h>
#include <google/protobuf/descriptor.h>
#include <google/protobuf/message.h>
//protobuf与json之间转换
namespace PB2JSON {
class Pb2Json {
public:
typedef ::google::protobuf::Message ProtobufMessage;
typedef ::google::protobuf::Reflection ProtobufReflection;
typedef ::google::protobuf::FieldDescriptor ProtobufFieldDescriptor;
typedef ::google::protobuf::Descriptor ProtobufDescriptor;
public:
static void PbMsg2JsonStr(ProtobufMessage const& src, std::string& dst, bool enum2str = false);
static bool JsonStr2PbMsg(std::string const& src, ProtobufMessage& dst, bool str2enum = false);
static bool Json2PbMsg(Json::Value const& src, ProtobufMessage& dst, bool str2enum = false);
static void PbMsg2Json(ProtobufMessage const& src, Json::Value& dst, bool enum2str = false);
private:
static bool Json2RepeatedMessage(
Json::Value const& json, ProtobufMessage& message,
ProtobufFieldDescriptor const* field,
ProtobufReflection const* reflection,
bool str2enum = false);
static void RepeatedMessage2Json(
ProtobufMessage const& message,
ProtobufFieldDescriptor const* field,
ProtobufReflection const* reflection,
Json::Value& json, bool enum2str);
};
};
#endif