Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow json unknown field #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions bin2ascii.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ inline std::string hex2bin(const std::string &s)
std::string r;
r.reserve(s.size() / 2);
for (size_t i = 0; i < s.size(); i += 2) {
char hi = lookup[s[i]];
char lo = lookup[s[i+1]];
char hi = lookup[(size_t)s[i]];
char lo = lookup[(size_t)s[i+1]];
if (0x80 & (hi | lo))
throw std::runtime_error("Invalid hex data: " + s.substr(i, 6));
r.push_back((hi << 4) | lo);
Expand All @@ -53,8 +53,8 @@ inline std::string bin2hex(const std::string &s)
for (size_t i = 0; i < s.size(); i++) {
char hi = s[i] >> 4;
char lo = s[i] & 0xf;
r.push_back(lookup[hi]);
r.push_back(lookup[lo]);
r.push_back(lookup[(size_t)hi]);
r.push_back(lookup[(size_t)lo]);
}
return r;
}
Expand All @@ -81,7 +81,7 @@ inline std::string b64_encode(const std::string &s)
if (i + 1 < s.size()) r.push_back(lookup[n2]);
if (i + 2 < s.size()) r.push_back(lookup[n3]);
}
for (int i = 0; i < (3 - s.size() % 3) % 3; i++)
for (size_t i = 0; i < (3 - s.size() % 3) % 3; i++)
r.push_back('=');
return r;
}
Expand Down
27 changes: 17 additions & 10 deletions json2pb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,9 @@ static json_t * _pb2json(const Message& msg)
return _auto.release();
}

static void _json2pb(Message& msg, json_t *root);
static void _json2field(Message &msg, const FieldDescriptor *field, json_t *jf)
static void _json2pb(Message& msg, json_t *root, bool allow_unknown_field);

static void _json2field(Message &msg, const FieldDescriptor *field, json_t *jf, bool allow_unknown_field)
{
const Reflection *ref = msg.GetReflection();
const bool repeated = field->is_repeated();
Expand Down Expand Up @@ -186,7 +187,7 @@ static void _json2field(Message &msg, const FieldDescriptor *field, json_t *jf)
Message *mf = (repeated)?
ref->AddMessage(&msg, field):
ref->MutableMessage(&msg, field);
_json2pb(*mf, jf);
_json2pb(*mf, jf, allow_unknown_field);
break;
}
case FieldDescriptor::CPPTYPE_ENUM: {
Expand All @@ -208,7 +209,7 @@ static void _json2field(Message &msg, const FieldDescriptor *field, json_t *jf)
}
}

static void _json2pb(Message& msg, json_t *root)
static void _json2pb(Message& msg, json_t *root, bool allow_unknown_field)
{
const Descriptor *d = msg.GetDescriptor();
const Reflection *ref = msg.GetReflection();
Expand All @@ -224,20 +225,26 @@ static void _json2pb(Message& msg, json_t *root)
field = ref->FindKnownExtensionByName(name);
//field = d->file()->FindExtensionByName(name);

if (!field) throw j2pb_error("Unknown field: " + std::string(name));
if (!field) {
if (!allow_unknown_field) {
throw j2pb_error("Unknown field: " + std::string(name));
}
else {
continue;
}
}

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));
_json2field(msg, field, json_array_get(jf, j), allow_unknown_field);
} else
_json2field(msg, field, jf);
_json2field(msg, field, jf, allow_unknown_field);
}
}

void json2pb(Message &msg, const char *buf, size_t size)
void json2pb(Message &msg, const char *buf, size_t size, bool allow_unknown_field)
{
json_t *root;
json_error_t error;
Expand All @@ -252,7 +259,7 @@ void json2pb(Message &msg, const char *buf, size_t size)
if (!json_is_object(root))
throw j2pb_error("Malformed JSON: not an object");

_json2pb(msg, root);
_json2pb(msg, root, allow_unknown_field);
}

int json_dump_std_string(const char *buf, size_t size, void *data)
Expand Down
2 changes: 1 addition & 1 deletion json2pb.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Message;
}
}

void json2pb(google::protobuf::Message &msg, const char *buf, size_t size);
void json2pb(google::protobuf::Message &msg, const char *buf, size_t size, bool allow_unknown_field = false);
std::string pb2json(const google::protobuf::Message &msg);

#endif//__JSON2PB_H__