-
Notifications
You must be signed in to change notification settings - Fork 129
/
json2pb.cc
273 lines (235 loc) · 7.77 KB
/
json2pb.cc
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
* Copyright (c) 2013 Pavel Shramov <shramov@mexmat.net>
*
* json2pb is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
#include <errno.h>
#include <jansson.h>
#include <google/protobuf/message.h>
#include <google/protobuf/descriptor.h>
#include <json2pb.h>
#include <stdexcept>
namespace {
#include "bin2ascii.h"
}
using google::protobuf::Message;
using google::protobuf::MessageFactory;
using google::protobuf::Descriptor;
using google::protobuf::FieldDescriptor;
using google::protobuf::EnumDescriptor;
using google::protobuf::EnumValueDescriptor;
using google::protobuf::Reflection;
struct json_autoptr {
json_t * ptr;
json_autoptr(json_t *json) : ptr(json) {}
~json_autoptr() { if (ptr) json_decref(ptr); }
json_t * release() { json_t *tmp = ptr; ptr = 0; return tmp; }
};
class j2pb_error : public std::exception {
std::string _error;
public:
j2pb_error(const std::string &e) : _error(e) {}
j2pb_error(const FieldDescriptor *field, const std::string &e) : _error(field->name() + ": " + e) {}
virtual ~j2pb_error() throw() {};
virtual const char *what() const throw () { return _error.c_str(); };
};
static json_t * _pb2json(const Message& msg);
static json_t * _field2json(const Message& msg, const FieldDescriptor *field, size_t index)
{
const Reflection *ref = msg.GetReflection();
const bool repeated = field->is_repeated();
json_t *jf = 0;
switch (field->cpp_type())
{
#define _CONVERT(type, ctype, fmt, sfunc, afunc) \
case FieldDescriptor::type: { \
const ctype value = (repeated)? \
ref->afunc(msg, field, index): \
ref->sfunc(msg, field); \
jf = fmt(value); \
break; \
}
_CONVERT(CPPTYPE_DOUBLE, double, json_real, GetDouble, GetRepeatedDouble);
_CONVERT(CPPTYPE_FLOAT, double, json_real, GetFloat, GetRepeatedFloat);
_CONVERT(CPPTYPE_INT64, json_int_t, json_integer, GetInt64, GetRepeatedInt64);
_CONVERT(CPPTYPE_UINT64, json_int_t, json_integer, GetUInt64, GetRepeatedUInt64);
_CONVERT(CPPTYPE_INT32, json_int_t, json_integer, GetInt32, GetRepeatedInt32);
_CONVERT(CPPTYPE_UINT32, json_int_t, json_integer, GetUInt32, GetRepeatedUInt32);
_CONVERT(CPPTYPE_BOOL, bool, json_boolean, GetBool, GetRepeatedBool);
#undef _CONVERT
case FieldDescriptor::CPPTYPE_STRING: {
std::string scratch;
const std::string &value = (repeated)?
ref->GetRepeatedStringReference(msg, field, index, &scratch):
ref->GetStringReference(msg, field, &scratch);
if (field->type() == FieldDescriptor::TYPE_BYTES)
jf = json_string(b64_encode(value).c_str());
else
jf = json_string(value.c_str());
break;
}
case FieldDescriptor::CPPTYPE_MESSAGE: {
const Message& mf = (repeated)?
ref->GetRepeatedMessage(msg, field, index):
ref->GetMessage(msg, field);
jf = _pb2json(mf);
break;
}
case FieldDescriptor::CPPTYPE_ENUM: {
const EnumValueDescriptor* ef = (repeated)?
ref->GetRepeatedEnum(msg, field, index):
ref->GetEnum(msg, field);
jf = json_integer(ef->number());
break;
}
default:
break;
}
if (!jf) throw j2pb_error(field, "Fail to convert to json");
return jf;
}
static json_t * _pb2json(const Message& msg)
{
const Descriptor *d = msg.GetDescriptor();
const Reflection *ref = msg.GetReflection();
if (!d || !ref) return 0;
json_t *root = json_object();
json_autoptr _auto(root);
std::vector<const FieldDescriptor *> fields;
ref->ListFields(msg, &fields);
for (size_t i = 0; i != fields.size(); i++)
{
const FieldDescriptor *field = fields[i];
json_t *jf = 0;
if(field->is_repeated()) {
size_t count = ref->FieldSize(msg, field);
if (!count) continue;
json_autoptr array(json_array());
for (size_t j = 0; j < count; j++)
json_array_append_new(array.ptr, _field2json(msg, field, j));
jf = array.release();
} else if (ref->HasField(msg, field))
jf = _field2json(msg, field, 0);
else
continue;
const std::string &name = (field->is_extension())?field->full_name():field->name();
json_object_set_new(root, name.c_str(), jf);
}
return _auto.release();
}
static void _json2pb(Message& msg, json_t *root);
static void _json2field(Message &msg, const FieldDescriptor *field, json_t *jf)
{
const Reflection *ref = msg.GetReflection();
const bool repeated = field->is_repeated();
json_error_t error;
switch (field->cpp_type())
{
#define _SET_OR_ADD(sfunc, afunc, value) \
do { \
if (repeated) \
ref->afunc(&msg, field, value); \
else \
ref->sfunc(&msg, field, value); \
} while (0)
#define _CONVERT(type, ctype, fmt, sfunc, afunc) \
case FieldDescriptor::type: { \
ctype value; \
int r = json_unpack_ex(jf, &error, JSON_STRICT, fmt, &value); \
if (r) throw j2pb_error(field, std::string("Failed to unpack: ") + error.text); \
_SET_OR_ADD(sfunc, afunc, value); \
break; \
}
_CONVERT(CPPTYPE_DOUBLE, double, "F", SetDouble, AddDouble);
_CONVERT(CPPTYPE_FLOAT, double, "F", SetFloat, AddFloat);
_CONVERT(CPPTYPE_INT64, json_int_t, "I", SetInt64, AddInt64);
_CONVERT(CPPTYPE_UINT64, json_int_t, "I", SetUInt64, AddUInt64);
_CONVERT(CPPTYPE_INT32, json_int_t, "I", SetInt32, AddInt32);
_CONVERT(CPPTYPE_UINT32, json_int_t, "I", SetUInt32, AddUInt32);
_CONVERT(CPPTYPE_BOOL, int, "b", SetBool, AddBool);
case FieldDescriptor::CPPTYPE_STRING: {
if (!json_is_string(jf))
throw j2pb_error(field, "Not a string");
const char * value = json_string_value(jf);
if(field->type() == FieldDescriptor::TYPE_BYTES)
_SET_OR_ADD(SetString, AddString, b64_decode(value));
else
_SET_OR_ADD(SetString, AddString, value);
break;
}
case FieldDescriptor::CPPTYPE_MESSAGE: {
Message *mf = (repeated)?
ref->AddMessage(&msg, field):
ref->MutableMessage(&msg, field);
_json2pb(*mf, jf);
break;
}
case FieldDescriptor::CPPTYPE_ENUM: {
const EnumDescriptor *ed = field->enum_type();
const EnumValueDescriptor *ev = 0;
if (json_is_integer(jf)) {
ev = ed->FindValueByNumber(json_integer_value(jf));
} else if (json_is_string(jf)) {
ev = ed->FindValueByName(json_string_value(jf));
} else
throw j2pb_error(field, "Not an integer or string");
if (!ev)
throw j2pb_error(field, "Enum value not found");
_SET_OR_ADD(SetEnum, AddEnum, ev);
break;
}
default:
break;
}
}
static void _json2pb(Message& msg, json_t *root)
{
const Descriptor *d = msg.GetDescriptor();
const Reflection *ref = msg.GetReflection();
if (!d || !ref) throw j2pb_error("No descriptor or reflection");
for (void *i = json_object_iter(root); i; i = json_object_iter_next(root, i))
{
const char *name = json_object_iter_key(i);
json_t *jf = json_object_iter_value(i);
const FieldDescriptor *field = d->FindFieldByName(name);
if (!field)
field = ref->FindKnownExtensionByName(name);
//field = d->file()->FindExtensionByName(name);
if (!field) throw j2pb_error("Unknown field: " + std::string(name));
int r = 0;
if (field->is_repeated()) {
if (!json_is_array(jf))
throw j2pb_error(field, "Not array");
for (size_t j = 0; j < json_array_size(jf); j++)
_json2field(msg, field, json_array_get(jf, j));
} else
_json2field(msg, field, jf);
}
}
void json2pb(Message &msg, const char *buf, size_t size)
{
json_t *root;
json_error_t error;
root = json_loadb(buf, size, 0, &error);
if (!root)
throw j2pb_error(std::string("Load failed: ") + error.text);
json_autoptr _auto(root);
if (!json_is_object(root))
throw j2pb_error("Malformed JSON: not an object");
_json2pb(msg, root);
}
int json_dump_std_string(const char *buf, size_t size, void *data)
{
std::string *s = (std::string *) data;
s->append(buf, size);
return 0;
}
std::string pb2json(const Message &msg)
{
std::string r;
json_t *root = _pb2json(msg);
json_autoptr _auto(root);
json_dump_callback(root, json_dump_std_string, &r, 0);
return r;
}