Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- std::string to_json(
- const google::protobuf::Message& m,
- std::function<std::string (const std::string&)> name_filter) {
- std::vector<std::pair<std::string, std::string>> key_value_pairs;
- auto refl = m.GetReflection();
- std::vector<const google::protobuf::FieldDescriptor*> field_descriptors;
- refl->ListFields(m, &field_descriptors);
- for (auto fd : field_descriptors) {
- if (!refl->HasField(m, fd)) {
- continue;
- }
- string value;
- if (fd->is_repeated()) {
- const int n = refl->FieldSize(m, fd);
- std::vector<std::string> vals(n);
- for (int i = 0; i < n; i++) {
- switch (fd->type()) {
- case google::protobuf::FieldDescriptor::TYPE_DOUBLE:
- vals.push_back(std::to_string(refl->GetRepeatedDouble(m, fd, i)));
- break;
- case google::protobuf::FieldDescriptor::TYPE_FLOAT:
- vals.push_back(std::to_string(refl->GetRepeatedFloat(m, fd, i)));
- break;
- case google::protobuf::FieldDescriptor::TYPE_INT64:
- vals.push_back(std::to_string(refl->GetRepeatedInt64(m, fd, i)));
- break;
- case google::protobuf::FieldDescriptor::TYPE_UINT64:
- vals.push_back(std::to_string(refl->GetRepeatedUInt64(m, fd, i)));
- break;
- case google::protobuf::FieldDescriptor::TYPE_INT32:
- vals.push_back(std::to_string(refl->GetRepeatedInt32(m, fd, i)));
- break;
- case google::protobuf::FieldDescriptor::TYPE_FIXED64:
- vals.push_back(std::to_string(refl->GetRepeatedInt64(m, fd, i)));
- break;
- case google::protobuf::FieldDescriptor::TYPE_FIXED32:
- vals.push_back(std::to_string(refl->GetRepeatedInt32(m, fd, i)));
- break;
- case google::protobuf::FieldDescriptor::TYPE_BOOL:
- vals.push_back(std::to_string(refl->GetRepeatedBool(m, fd, i)));
- break;
- case google::protobuf::FieldDescriptor::TYPE_STRING:
- vals.push_back(refl->GetRepeatedString(m, fd, i));
- break;
- case google::protobuf::FieldDescriptor::TYPE_GROUP:
- throw "Groups support not implemented.";
- break;
- case google::protobuf::FieldDescriptor::TYPE_MESSAGE:
- vals.push_back(
- to_json(refl->GetRepeatedMessage(m, fd, i), name_filter));
- break;
- case google::protobuf::FieldDescriptor::TYPE_BYTES:
- throw "Can't serialize binary to json.";
- break;
- case google::protobuf::FieldDescriptor::TYPE_UINT32:
- vals.push_back(std::to_string(refl->GetRepeatedUInt32(m, fd, i)));
- break;
- case google::protobuf::FieldDescriptor::TYPE_ENUM:
- throw "Can't serialize enums to json.";
- break;
- case google::protobuf::FieldDescriptor::TYPE_SFIXED32:
- vals.push_back(std::to_string(refl->GetRepeatedInt32(m, fd, i)));
- break;
- case google::protobuf::FieldDescriptor::TYPE_SFIXED64:
- vals.push_back(std::to_string(refl->GetRepeatedInt64(m, fd, i)));
- break;
- case google::protobuf::FieldDescriptor::TYPE_SINT32:
- vals.push_back(std::to_string(refl->GetRepeatedInt32(m, fd, i)));
- break;
- case google::protobuf::FieldDescriptor::TYPE_SINT64:
- vals.push_back(std::to_string(refl->GetRepeatedInt64(m, fd, i)));
- break;
- default:
- break;
- }
- value += '[';
- for (const auto& v : vals) {
- value += v;
- value += ',';
- }
- if (value[value.length() - 1] == ',') {
- value[value.length() - 1] = ']';
- } else {
- value += ']';
- }
- }
- } else {
- switch (fd->type()) {
- case google::protobuf::FieldDescriptor::TYPE_DOUBLE:
- value = std::to_string(refl->GetDouble(m, fd));
- break;
- case google::protobuf::FieldDescriptor::TYPE_FLOAT:
- value = std::to_string(refl->GetFloat(m, fd));
- break;
- case google::protobuf::FieldDescriptor::TYPE_INT64:
- value = std::to_string(refl->GetInt64(m, fd));
- break;
- case google::protobuf::FieldDescriptor::TYPE_UINT64:
- value = std::to_string(refl->GetUInt64(m, fd));
- break;
- case google::protobuf::FieldDescriptor::TYPE_INT32:
- value = std::to_string(refl->GetInt32(m, fd));
- break;
- case google::protobuf::FieldDescriptor::TYPE_FIXED64:
- value = std::to_string(refl->GetInt64(m, fd));
- break;
- case google::protobuf::FieldDescriptor::TYPE_FIXED32:
- value = std::to_string(refl->GetInt32(m, fd));
- break;
- case google::protobuf::FieldDescriptor::TYPE_BOOL:
- value = std::to_string(refl->GetBool(m, fd));
- break;
- case google::protobuf::FieldDescriptor::TYPE_STRING:
- value += '"';
- value += refl->GetString(m, fd);
- value += '"';
- break;
- case google::protobuf::FieldDescriptor::TYPE_GROUP:
- throw "Groups support not implemented.";
- break;
- case google::protobuf::FieldDescriptor::TYPE_MESSAGE:
- value += '"';
- value += to_json(refl->GetMessage(m, fd), name_filter);
- value += '"';
- break;
- case google::protobuf::FieldDescriptor::TYPE_BYTES:
- throw "Can't serialize binary to json.";
- break;
- case google::protobuf::FieldDescriptor::TYPE_UINT32:
- value = std::to_string(refl->GetUInt32(m, fd));
- break;
- case google::protobuf::FieldDescriptor::TYPE_ENUM:
- throw "Can't serialize enums to json.";
- break;
- case google::protobuf::FieldDescriptor::TYPE_SFIXED32:
- value = std::to_string(refl->GetInt32(m, fd));
- break;
- case google::protobuf::FieldDescriptor::TYPE_SFIXED64:
- value = std::to_string(refl->GetInt64(m, fd));
- break;
- case google::protobuf::FieldDescriptor::TYPE_SINT32:
- value = std::to_string(refl->GetInt32(m, fd));
- break;
- case google::protobuf::FieldDescriptor::TYPE_SINT64:
- value = std::to_string(refl->GetInt64(m, fd));
- break;
- default:
- break;
- }
- }
- key_value_pairs.push_back(std::make_pair(name_filter(fd->name()), value));
- }
- string ret = "{";
- for (const auto& p : key_value_pairs) {
- ret += '"';
- ret += p.first;
- ret += "\":";
- ret += p.second;
- ret += ',';
- }
- if (ret[ret.length() - 1] == ',') {
- ret[ret.length() - 1] = '}';
- } else {
- ret += '}';
- }
- return ret;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement