Skip to content

Commit

Permalink
Add TreatTimestampAsDateTime toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
Havret committed Mar 28, 2022
1 parent f5620ea commit 15a2125
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/Protobuf.System.Text.Json/JsonProtobufSerializerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,14 @@ public class JsonProtobufSerializerOptions
/// The default value is true.
/// </summary>
public bool TreatDurationAsTimeSpan { get; set; } = true;


/// <summary>
/// Controls how <see cref="Google.Protobuf.WellKnownTypes.Timestamp"/> fields are handled.
/// When set to true, <see cref="Google.Protobuf.WellKnownTypes.Timestamp"/> properties will
/// be converted to <see cref="DateTime"/> before serialization and will be expected in the
/// same format as <see cref="DateTime"/> during deserialization.
/// The default value is true.
/// </summary>
public bool TreatTimestampAsDateTime { get; set; } = true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down

0 comments on commit 15a2125

Please sign in to comment.