forked from leewaya/pb2jsoncpp
/
pb2json.cpp
212 lines (200 loc) · 5.15 KB
/
pb2json.cpp
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include "pb2json.h"
#include <boost/format.hpp>
using std::string;
string pb2json(const Message *msg)
{
if (!msg) {
return NULL;
}
Json::Value root;
bool result = parse_msg(*msg, root);
if (result) {
return root.toStyledString();
} else {
return NULL;
}
}
string pb2json(Message *msg, const char *buf, int len)
{
if (!msg) {
return NULL;
}
string s (buf,len);
msg->ParseFromString(s);
return pb2json(msg);
}
static bool parse_repeated_field(const Message& msg, const Reflection& ref, const FieldDescriptor* field, Json::Value& node)
{
size_t count = ref.FieldSize(msg, field);
switch(field->cpp_type())
{
case FieldDescriptor::CPPTYPE_DOUBLE:
for(size_t i = 0 ; i != count ; ++i)
{
double value1 = ref.GetRepeatedDouble(msg, field,i);
node.append(Json::Value(value1));
}
break;
case FieldDescriptor::CPPTYPE_FLOAT:
for(size_t i = 0 ; i != count ; ++i)
{
float value2 = ref.GetRepeatedFloat(msg, field,i);
node.append(Json::Value(value2));
}
break;
case FieldDescriptor::CPPTYPE_INT64:
for(size_t i = 0 ; i != count ; ++i)
{
int64_t value3 = ref.GetRepeatedInt64(msg, field,i);
node.append(Json::Value((boost::format("%d") % value3).str()));
}
break;
case FieldDescriptor::CPPTYPE_UINT64:
for(size_t i = 0 ; i != count ; ++i)
{
uint64_t value4 = ref.GetRepeatedUInt64(msg, field,i);
node.append(Json::Value((boost::format("%d") % value4).str()));
}
break;
case FieldDescriptor::CPPTYPE_INT32:
for(size_t i = 0 ; i != count ; ++i)
{
int32_t value5 = ref.GetRepeatedInt32(msg, field,i);
node.append(Json::Value(value5));
}
break;
case FieldDescriptor::CPPTYPE_UINT32:
for(size_t i = 0 ; i != count ; ++i)
{
uint32_t value6 = ref.GetRepeatedUInt32(msg, field,i);
node.append(Json::Value(value6));
}
break;
case FieldDescriptor::CPPTYPE_BOOL:
for(size_t i = 0 ; i != count ; ++i)
{
bool value7 = ref.GetRepeatedBool(msg, field,i);
node.append(Json::Value(value7)) ;
}
break;
case FieldDescriptor::CPPTYPE_STRING:
for(size_t i = 0 ; i != count ; ++i)
{
string value8 = ref.GetRepeatedString(msg, field,i);
node.append(Json::Value(value8.c_str()));
}
break;
case FieldDescriptor::CPPTYPE_MESSAGE:
for(size_t i = 0 ; i != count ; ++i)
{
const Message *value9 = &(ref.GetRepeatedMessage(msg,field,i));
Json::Value repeatedNode;
parse_msg(*value9, repeatedNode);
node.append(repeatedNode);
}
break;
case FieldDescriptor::CPPTYPE_ENUM:
for(size_t i = 0 ; i != count ; ++i)
{
const EnumValueDescriptor* value10 = ref.GetRepeatedEnum(msg, field,i);
node.append(Json::Value(value10->number()));
}
break;
default:
break;
}
return true;
}
static bool parse_msg(const Message& msg, Json::Value& root)
{
const Descriptor *d = msg.GetDescriptor();
if(!d)return false;
size_t count = d->field_count();
for (size_t i = 0; i != count ; ++i)
{
const FieldDescriptor *field = d->field(i);
if(!field) return false;
const Reflection *ref = msg.GetReflection();
if(!ref)return false;
const string name = field->name().c_str();
if(field->is_repeated()) {
Json::Value node;
parse_repeated_field(msg, *ref, field, node);
root[name] = node;
continue;
}
if(ref->HasField(msg,field))
{
switch (field->cpp_type())
{
case FieldDescriptor::CPPTYPE_DOUBLE:
{
double value1 = ref->GetDouble(msg,field);
root[name] = Json::Value(value1);
break;
}
case FieldDescriptor::CPPTYPE_FLOAT:
{
float value2 = ref->GetFloat(msg,field);
root[name] = Json::Value(value2);
break;
}
case FieldDescriptor::CPPTYPE_INT64:
{
int64_t value3 = ref->GetInt64(msg,field);
root[name] = Json::Value((boost::format("%d") % value3).str());
break;
}
case FieldDescriptor::CPPTYPE_UINT64:
{
uint64_t value4 = ref->GetUInt64(msg,field);
root[name] = Json::Value((boost::format("%d") % value4).str());
break;
}
case FieldDescriptor::CPPTYPE_INT32:
{
int32_t value5 = ref->GetInt32(msg,field);
root[name] = Json::Value(value5);
break;
}
case FieldDescriptor::CPPTYPE_UINT32:
{
uint32_t value6 = ref->GetUInt32(msg,field);
root[name] = Json::Value(value6);
break;
}
case FieldDescriptor::CPPTYPE_BOOL:
{
bool value7 = ref->GetBool(msg,field);
root[name] = Json::Value(value7);
break;
}
case FieldDescriptor::CPPTYPE_STRING:
{
string value8 = ref->GetString(msg,field);
if (value8.length() > 0) {
root[name] = Json::Value(value8.c_str());
}
break;
}
case FieldDescriptor::CPPTYPE_MESSAGE:
{
const Message *value9 = &(ref->GetMessage(msg,field));
Json::Value subNode;
parse_msg(*value9, subNode);
root[name] = subNode;
break;
}
case FieldDescriptor::CPPTYPE_ENUM:
{
const EnumValueDescriptor *value10 = ref->GetEnum(msg,field);
root[name] = Json::Value(value10->number());
break;
}
default:
break;
}
}
}
return true;
}