Skip to content

Commit 7c16cbc

Browse files
committed
Add Timestamp support
1 parent 19a1c35 commit 7c16cbc

12 files changed

Lines changed: 130 additions & 4 deletions

src/System.Text.Json.Protobuf/FieldInfo.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Text.Json.Protobuf.InternalConverters;
12
using Google.Protobuf.Reflection;
23

34
namespace System.Text.Json.Protobuf;

src/System.Text.Json.Protobuf/FieldConverter.cs renamed to src/System.Text.Json.Protobuf/InternalConverters/FieldConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using Google.Protobuf;
33
using Google.Protobuf.Reflection;
44

5-
namespace System.Text.Json.Protobuf;
5+
namespace System.Text.Json.Protobuf.InternalConverters;
66

77
internal class FieldConverter<T> : InternalConverter
88
{

src/System.Text.Json.Protobuf/InternalConverter.cs renamed to src/System.Text.Json.Protobuf/InternalConverters/InternalConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Google.Protobuf;
22
using Google.Protobuf.Reflection;
33

4-
namespace System.Text.Json.Protobuf;
4+
namespace System.Text.Json.Protobuf.InternalConverters;
55

66
internal abstract class InternalConverter
77
{

src/System.Text.Json.Protobuf/InternalConverterFactory.cs renamed to src/System.Text.Json.Protobuf/InternalConverters/InternalConverterFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace System.Text.Json.Protobuf;
1+
namespace System.Text.Json.Protobuf.InternalConverters;
22

33
internal class InternalConverterFactory
44
{

src/System.Text.Json.Protobuf/RepeatedFieldConverter.cs renamed to src/System.Text.Json.Protobuf/InternalConverters/RepeatedFieldConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using Google.Protobuf.Collections;
44
using Google.Protobuf.Reflection;
55

6-
namespace System.Text.Json.Protobuf;
6+
namespace System.Text.Json.Protobuf.InternalConverters;
77

88
internal class RepeatedFieldConverter<T> : InternalConverter
99
{

src/System.Text.Json.Protobuf/JsonSerializerOptionsExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public static class JsonSerializerOptionsExtensions
77
{
88
public static void AddProtobufSupport(this JsonSerializerOptions options)
99
{
10+
options.Converters.Add(new TimestampConverter());
1011
options.Converters.Add(new ProtobufJsonConverterFactory());
1112
}
1213
}

src/System.Text.Json.Protobuf/ProtobufConverter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Reflection;
2+
using System.Text.Json.Protobuf.InternalConverters;
23
using System.Text.Json.Serialization;
34
using Google.Protobuf;
45
using Google.Protobuf.Reflection;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System.Text.Json.Serialization;
2+
using Google.Protobuf.WellKnownTypes;
3+
4+
namespace System.Text.Json.Protobuf;
5+
6+
public class TimestampConverter : JsonConverter<Timestamp?>
7+
{
8+
private JsonConverter<DateTime>? _converter;
9+
10+
public override Timestamp? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
11+
{
12+
if (reader.TryGetDateTime(out var dateTime))
13+
{
14+
return dateTime.ToTimestamp();
15+
}
16+
17+
return null;
18+
}
19+
20+
public override void Write(Utf8JsonWriter writer, Timestamp? value, JsonSerializerOptions options)
21+
{
22+
if (value != null)
23+
{
24+
var dateTime = value.ToDateTime();
25+
_converter ??= (JsonConverter<DateTime>) options.GetConverter(typeof(DateTime));
26+
_converter.Write(writer, dateTime, options);
27+
}
28+
}
29+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"timestampProperty": "1991-01-25T00:00:00Z"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

0 commit comments

Comments
 (0)