diff options
author | Alexander Smirnov <alex@ydb.tech> | 2024-10-02 11:40:21 +0000 |
---|---|---|
committer | Alexander Smirnov <alex@ydb.tech> | 2024-10-02 11:40:21 +0000 |
commit | 4a43f3fbfda5a2eee2af081bd76ae023afd481db (patch) | |
tree | ae27208d4452705b2c0ec19efdcd8132c8e8de20 /library/cpp/protobuf/json/proto2json_printer.cpp | |
parent | 2a5dadb53a806ed944a3a3e1dfcaef886838360d (diff) | |
parent | 2084bac66bb1c8d3013d8ef6c61867726c4188e2 (diff) | |
download | ydb-4a43f3fbfda5a2eee2af081bd76ae023afd481db.tar.gz |
Merge branch 'rightlib' into mergelibs-241002-1139
Diffstat (limited to 'library/cpp/protobuf/json/proto2json_printer.cpp')
-rw-r--r-- | library/cpp/protobuf/json/proto2json_printer.cpp | 19 |
1 files changed, 17 insertions, 2 deletions
diff --git a/library/cpp/protobuf/json/proto2json_printer.cpp b/library/cpp/protobuf/json/proto2json_printer.cpp index 5d0e140615..a9f8c3fce9 100644 --- a/library/cpp/protobuf/json/proto2json_printer.cpp +++ b/library/cpp/protobuf/json/proto2json_printer.cpp @@ -8,6 +8,7 @@ #include <library/cpp/protobuf/json/proto/enum_options.pb.h> +#include <util/generic/map.h> #include <util/generic/yexception.h> #include <util/string/ascii.h> #include <util/string/cast.h> @@ -394,8 +395,22 @@ namespace NProtobufJson { case FieldDescriptor::CPPTYPE_MESSAGE: { if (isMap) { - for (size_t i = 0, endI = reflection->FieldSize(proto, &field); i < endI; ++i) { - PrintKeyValue(reflection->GetRepeatedMessage(proto, &field, i), json); + if (GetConfig().SortMapKeys) { + TMap<TString, size_t> keyToIndex; + for (size_t i = 0, endI = reflection->FieldSize(proto, &field); i < endI; ++i) { + const Message& fieldMessage = reflection->GetRepeatedMessage(proto, &field, i); + const FieldDescriptor* keyField = fieldMessage.GetDescriptor()->map_key(); + Y_ABORT_UNLESS(keyField, "Map entry key field not found."); + TString key = MakeKey(fieldMessage, *keyField); + keyToIndex[key] = i; + } + for (const auto& [_, i] : keyToIndex) { + PrintKeyValue(reflection->GetRepeatedMessage(proto, &field, i), json); + } + } else { + for (size_t i = 0, endI = reflection->FieldSize(proto, &field); i < endI; ++i) { + PrintKeyValue(reflection->GetRepeatedMessage(proto, &field, i), json); + } } } else { for (size_t i = 0, endI = reflection->FieldSize(proto, &field); i < endI; ++i) { |