From a07c7298d1986512e103da831fbd30b7518fde94 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 17 Sep 2015 11:24:11 +0800 Subject: [PATCH] allow json unknown field --- bin2ascii.h | 10 +++++----- json2pb.cc | 27 +++++++++++++++++---------- json2pb.h | 2 +- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/bin2ascii.h b/bin2ascii.h index 15f5126..70e9496 100644 --- a/bin2ascii.h +++ b/bin2ascii.h @@ -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); @@ -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; } @@ -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; } diff --git a/json2pb.cc b/json2pb.cc index 8ed7116..73360f4 100644 --- a/json2pb.cc +++ b/json2pb.cc @@ -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(); @@ -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: { @@ -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(); @@ -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; @@ -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) diff --git a/json2pb.h b/json2pb.h index d134318..f96dc6f 100644 --- a/json2pb.h +++ b/json2pb.h @@ -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__