From 15a212574c83ebdf95769d2176f025123233b4e9 Mon Sep 17 00:00:00 2001 From: Havret Date: Mon, 28 Mar 2022 16:22:21 +0200 Subject: [PATCH] Add TreatTimestampAsDateTime toggle --- .../JsonProtobufSerializerOptions.cs | 10 ++++++++++ .../JsonSerializerOptionsExtensions.cs | 6 ++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Protobuf.System.Text.Json/JsonProtobufSerializerOptions.cs b/src/Protobuf.System.Text.Json/JsonProtobufSerializerOptions.cs index 4dc8ed5..6fe6797 100644 --- a/src/Protobuf.System.Text.Json/JsonProtobufSerializerOptions.cs +++ b/src/Protobuf.System.Text.Json/JsonProtobufSerializerOptions.cs @@ -19,4 +19,14 @@ public class JsonProtobufSerializerOptions /// The default value is true. /// public bool TreatDurationAsTimeSpan { get; set; } = true; + + + /// + /// Controls how fields are handled. + /// When set to true, properties will + /// be converted to before serialization and will be expected in the + /// same format as during deserialization. + /// The default value is true. + /// + public bool TreatTimestampAsDateTime { get; set; } = true; } \ No newline at end of file diff --git a/src/Protobuf.System.Text.Json/JsonSerializerOptionsExtensions.cs b/src/Protobuf.System.Text.Json/JsonSerializerOptionsExtensions.cs index de7909e..c36b0c4 100644 --- a/src/Protobuf.System.Text.Json/JsonSerializerOptionsExtensions.cs +++ b/src/Protobuf.System.Text.Json/JsonSerializerOptionsExtensions.cs @@ -19,8 +19,10 @@ public static void AddProtobufSupport(this JsonSerializerOptions options, Action { options.Converters.Add(new DurationConverter()); } - - options.Converters.Add(new TimestampConverter()); + if (jsonProtobufSerializerOptions.TreatTimestampAsDateTime) + { + options.Converters.Add(new TimestampConverter()); + } options.Converters.Add(new ProtobufJsonConverterFactory(jsonProtobufSerializerOptions)); }