diff options
author | vadim-xd <vadim-xd@yandex-team.com> | 2023-10-24 17:24:29 +0300 |
---|---|---|
committer | vadim-xd <vadim-xd@yandex-team.com> | 2023-10-24 18:00:42 +0300 |
commit | 3547fbbc82a002091ef67079148ed1725dcd7d5e (patch) | |
tree | dc2f7762eab7d462d1c5ca19316c90c8a5336026 /library/cpp | |
parent | 6f0bed14b6dca2df7fff6bdeaea482f024b518eb (diff) | |
download | ydb-3547fbbc82a002091ef67079148ed1725dcd7d5e.tar.gz |
Optimize Json2Proto
Diffstat (limited to 'library/cpp')
-rw-r--r-- | library/cpp/protobuf/json/json2proto.cpp | 20 |
1 files changed, 12 insertions, 8 deletions
diff --git a/library/cpp/protobuf/json/json2proto.cpp b/library/cpp/protobuf/json/json2proto.cpp index b4a788a270..83b7891b9c 100644 --- a/library/cpp/protobuf/json/json2proto.cpp +++ b/library/cpp/protobuf/json/json2proto.cpp @@ -260,23 +260,29 @@ Json2SingleField(const NJson::TJsonValue& json, const Reflection* reflection = proto.GetReflection(); Y_ASSERT(!!reflection); - TString name; + const NJson::TJsonValue* fieldJsonPtr = &json; + TString nameHolder; + TStringBuf name; if (!isMapValue) { - name = GetFieldName(field, config); - if (!json.Has(name) || json[name].GetType() == NJson::JSON_UNDEFINED || json[name].GetType() == NJson::JSON_NULL) { + nameHolder = GetFieldName(field, config); + name = nameHolder; + const NJson::TJsonValue& fieldJson = json[name]; + if (auto fieldJsonType = fieldJson.GetType(); fieldJsonType == NJson::JSON_UNDEFINED || fieldJsonType == NJson::JSON_NULL) { if (field.is_required() && !field.has_default_value() && !reflection->HasField(proto, &field) && config.CheckRequiredFields) { ythrow yexception() << "JSON has no field for required field " << name << "."; } - return; } + if (name) { // For compatibility with previous implementation. Not sure if GetFieldName is allowed to return empty strings, + fieldJsonPtr = &fieldJson; + } } - const NJson::TJsonValue& fieldJson = name ? json[name] : json; + const NJson::TJsonValue& fieldJson = *fieldJsonPtr; if (name && config.UnknownFieldsCollector) { - config.UnknownFieldsCollector->OnEnterMapItem(name); + config.UnknownFieldsCollector->OnEnterMapItem(nameHolder); } switch (field.cpp_type()) { @@ -415,8 +421,6 @@ Json2RepeatedField(const NJson::TJsonValue& json, using namespace google::protobuf; TString name = GetFieldName(field, config); - if (!json.Has(name)) - return; const NJson::TJsonValue& fieldJson = json[name]; if (fieldJson.GetType() == NJson::JSON_UNDEFINED || fieldJson.GetType() == NJson::JSON_NULL) |