Skip to content

Commit

Permalink
only allow to convert root array to single repeated pb
Browse files Browse the repository at this point in the history
  • Loading branch information
chenBright committed Dec 4, 2022
1 parent 1043b79 commit a0bc7db
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/json2pb/json_to_pb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,9 @@ inline bool convert_uint64_type(const BUTIL_RAPIDJSON_NAMESPACE::Value& item,

bool JsonValueToProtoMessage(const BUTIL_RAPIDJSON_NAMESPACE::Value& json_value,
google::protobuf::Message* message,
const Json2PbOptions& options, std::string* err);
const Json2PbOptions& options,
std::string* err,
bool root_val = false);

//Json value to protobuf convert rules for type:
//Json value type Protobuf type convert rules
Expand Down Expand Up @@ -516,9 +518,11 @@ bool JsonMapToProtoMap(const BUTIL_RAPIDJSON_NAMESPACE::Value& value,
bool JsonValueToProtoMessage(const BUTIL_RAPIDJSON_NAMESPACE::Value& json_value,
google::protobuf::Message* message,
const Json2PbOptions& options,
std::string* err) {
std::string* err,
bool root_val) {
const google::protobuf::Descriptor* descriptor = message->GetDescriptor();
if (!json_value.IsObject() && !(json_value.IsArray() && options.array_to_single_repeated)) {
if (!json_value.IsObject() &&
!(json_value.IsArray() && options.array_to_single_repeated && root_val)) {
J2PERROR_WITH_PB(message, err, "The input is not a json object");
return false;
}
Expand Down Expand Up @@ -627,7 +631,7 @@ inline bool JsonToProtoMessageInline(const std::string& json_string,
J2PERROR_WITH_PB(message, error, "Invalid json: %s", BUTIL_RAPIDJSON_NAMESPACE::GetParseError_En(d.GetParseError()));
return false;
}
return JsonValueToProtoMessage(d, message, options, error);
return JsonValueToProtoMessage(d, message, options, error, true);
}

bool JsonToProtoMessage(const std::string& json_string,
Expand Down Expand Up @@ -675,7 +679,7 @@ bool JsonToProtoMessage(ZeroCopyStreamReader* reader,
J2PERROR_WITH_PB(message, error, "Invalid json: %s", BUTIL_RAPIDJSON_NAMESPACE::GetParseError_En(d.GetParseError()));
return false;
}
return JsonValueToProtoMessage(d, message, options, error);
return JsonValueToProtoMessage(d, message, options, error, true);
}

bool JsonToProtoMessage(const std::string& json_string,
Expand Down

0 comments on commit a0bc7db

Please sign in to comment.