MCPcopy Create free account
hub / github.com/apache/brpc / JsonValueToProtoMessage

Function JsonValueToProtoMessage

src/json2pb/json_to_pb.cpp:535–629  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

533}
534
535bool JsonValueToProtoMessage(const BUTIL_RAPIDJSON_NAMESPACE::Value& json_value,
536 google::protobuf::Message* message,
537 const Json2PbOptions& options,
538 std::string* err,
539 int depth) {
540 if (depth > FLAGS_json2pb_max_recursion_depth) {
541 J2PERROR_WITH_PB(message, err, "Exceeded maximum recursion depth");
542 return false;
543 }
544 const google::protobuf::Descriptor* descriptor = message->GetDescriptor();
545 if (!json_value.IsObject() &&
546 !(json_value.IsArray() && options.array_to_single_repeated && depth == 0)) {
547 J2PERROR_WITH_PB(message, err, "The input is not a json object");
548 return false;
549 }
550
551 const google::protobuf::Reflection* reflection = message->GetReflection();
552
553 std::vector<const google::protobuf::FieldDescriptor*> fields;
554 fields.reserve(64);
555 for (int i = 0; i < descriptor->extension_range_count(); ++i) {
556 const google::protobuf::Descriptor::ExtensionRange*
557 ext_range = descriptor->extension_range(i);
558#if GOOGLE_PROTOBUF_VERSION < 4024000
559 for (int tag_number = ext_range->start; tag_number < ext_range->end; ++tag_number)
560#else
561 for (int tag_number = ext_range->start_number(); tag_number < ext_range->end_number(); ++tag_number)
562#endif
563 {
564 const google::protobuf::FieldDescriptor* field =
565 reflection->FindKnownExtensionByNumber(tag_number);
566 if (field) {
567 fields.push_back(field);
568 }
569 }
570 }
571 for (int i = 0; i < descriptor->field_count(); ++i) {
572 fields.push_back(descriptor->field(i));
573 }
574
575 if (json_value.IsArray()) {
576 if (fields.size() == 1 && fields.front()->is_repeated()) {
577 return JsonValueToProtoField(json_value, fields.front(), message, options, err, depth);
578 }
579
580 J2PERROR_WITH_PB(message, err, "the input json can't be array here");
581 return false;
582 }
583
584 std::string field_name_str_temp;
585 const BUTIL_RAPIDJSON_NAMESPACE::Value* value_ptr = NULL;
586 for (size_t i = 0; i < fields.size(); ++i) {
587 const google::protobuf::FieldDescriptor* field = fields[i];
588
589 const std::string orig_name = butil::EnsureString(field->name());
590 bool res = decode_name(orig_name, field_name_str_temp);
591 const std::string& field_name_str = (res ? field_name_str_temp : orig_name);
592

Callers 3

JsonValueToProtoFieldFunction · 0.85
JsonToProtoMessageInlineFunction · 0.85
JsonToProtoMessageFunction · 0.85

Calls 15

JsonValueToProtoFieldFunction · 0.85
EnsureStringFunction · 0.85
decode_nameFunction · 0.85
IsProtobufMapFunction · 0.85
JsonMapToProtoMapFunction · 0.85
IsObjectMethod · 0.80
IsArrayMethod · 0.80
field_countMethod · 0.80
fieldMethod · 0.80
GetDescriptorMethod · 0.45
reserveMethod · 0.45
push_backMethod · 0.45

Tested by

no test coverage detected