aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/protobuf/json/proto2json_printer.cpp
diff options
context:
space:
mode:
authorbbiff <bbiff@yandex-team.com>2022-09-11 00:15:54 +0300
committerbbiff <bbiff@yandex-team.com>2022-09-11 00:15:54 +0300
commit67576a8962c2506b8ee10da48dffba311acb0042 (patch)
tree51a695bbddbb9be4e5a0cc5c7ec558ca233c2be5 /library/cpp/protobuf/json/proto2json_printer.cpp
parenta8945929eb162f6910b228697c3d2db3b7cb2ca9 (diff)
downloadydb-67576a8962c2506b8ee10da48dffba311acb0042.tar.gz
support duration and timestamp as string in json2proto
Diffstat (limited to 'library/cpp/protobuf/json/proto2json_printer.cpp')
-rw-r--r--library/cpp/protobuf/json/proto2json_printer.cpp23
1 files changed, 23 insertions, 0 deletions
diff --git a/library/cpp/protobuf/json/proto2json_printer.cpp b/library/cpp/protobuf/json/proto2json_printer.cpp
index 6123eab0f2..409147dc06 100644
--- a/library/cpp/protobuf/json/proto2json_printer.cpp
+++ b/library/cpp/protobuf/json/proto2json_printer.cpp
@@ -2,9 +2,13 @@
#include "config.h"
#include "util.h"
+#include <google/protobuf/util/time_util.h>
+
#include <util/generic/yexception.h>
#include <util/string/ascii.h>
#include <util/string/cast.h>
+#include <util/system/win_undef.h>
+
namespace NProtobufJson {
using namespace NProtoBuf;
@@ -176,6 +180,22 @@ namespace NProtobufJson {
}
}
+ bool HandleTimeConversion(const Message& proto, IJsonOutput& json) {
+ using namespace google::protobuf;
+ auto type = proto.GetDescriptor()->well_known_type();
+
+ if (type == Descriptor::WellKnownType::WELLKNOWNTYPE_DURATION) {
+ const auto& duration = static_cast<const Duration&>(proto);
+ json.Write(util::TimeUtil::ToString(duration));
+ return true;
+ } else if (type == Descriptor::WellKnownType::WELLKNOWNTYPE_TIMESTAMP) {
+ const auto& timestamp = static_cast<const Timestamp&>(proto);
+ json.Write(util::TimeUtil::ToString(timestamp));
+ return true;
+ }
+ return false;
+ }
+
void TProto2JsonPrinter::PrintSingleField(const Message& proto,
const FieldDescriptor& field,
IJsonOutput& json,
@@ -228,6 +248,9 @@ namespace NProtobufJson {
case FieldDescriptor::CPPTYPE_MESSAGE: {
json.WriteKey(key);
+ if (Config.ConvertTimeAsString && HandleTimeConversion(reflection->GetMessage(proto, &field), json)) {
+ break;
+ }
Print(reflection->GetMessage(proto, &field), json);
break;
}