Skip to content

Commit f03df0c

Browse files
committed
Clean up
1 parent 5b4fbfe commit f03df0c

8 files changed

Lines changed: 96 additions & 53 deletions
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System.Text.Json.Protobuf;
2+
3+
// ReSharper disable once CheckNamespace
4+
namespace System.Text.Json;
5+
6+
public static class JsonSerializerOptionsExtensions
7+
{
8+
public static void AddProtobufSupport(this JsonSerializerOptions options)
9+
{
10+
options.Converters.Add(new ProtobufJsonConverterFactory());
11+
}
12+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace System.Text.Json.Protobuf;
55

6-
public class ProtobufJsonConverterFactory : JsonConverterFactory
6+
internal class ProtobufJsonConverterFactory : JsonConverterFactory
77
{
88
public override bool CanConvert(Type typeToConvert)
99
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"int32List": [
3+
1,
4+
2,
5+
3
6+
],
7+
"int64List": [
8+
2147483648,
9+
2147483649,
10+
2147483650
11+
]
12+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System.Text.Json.Protobuf.Tests.Utils;
2+
using Shouldly;
3+
using SmartAnalyzers.ApprovalTestsExtensions;
4+
using Xunit;
5+
6+
namespace System.Text.Json.Protobuf.Tests;
7+
8+
public class MessageWithRepeatedListTests
9+
{
10+
[Fact]
11+
public void Should_serialize_message_with_repeated_list()
12+
{
13+
// Arrange
14+
var msg = new MessageWithRepeatedList
15+
{
16+
Int32List = {1, 2, 3},
17+
Int64List = {int.MaxValue + 1L, int.MaxValue + 2L, int.MaxValue + 3L}
18+
};
19+
20+
var jsonSerializerOptions = TestHelper.CreateJsonSerializerOptions();
21+
22+
// Act
23+
var serialized = JsonSerializer.Serialize(msg, jsonSerializerOptions);
24+
25+
// Assert
26+
var approver = new ExplicitApprover();
27+
approver.VerifyJson(serialized);
28+
}
29+
30+
[Fact]
31+
public void Should_deserialize_message_with_repeated_list()
32+
{
33+
// Arrange
34+
var msg = new MessageWithRepeatedList
35+
{
36+
Int32List = {1, 2, 3},
37+
Int64List = {int.MaxValue + 1L, int.MaxValue + 2L, int.MaxValue + 3L}
38+
};
39+
40+
var jsonSerializerOptions = TestHelper.CreateJsonSerializerOptions();
41+
42+
// Act
43+
var serialized = JsonSerializer.Serialize(msg, jsonSerializerOptions);
44+
var deserialized = JsonSerializer.Deserialize<MessageWithRepeatedList>(serialized, jsonSerializerOptions);
45+
46+
// Assert
47+
deserialized.ShouldNotBeNull();
48+
49+
deserialized.Int32List.ShouldBe(msg.Int32List);
50+
deserialized.Int64List.ShouldBe(msg.Int64List);
51+
}
52+
}

test/System.Text.Json.Protobuf.Tests/Protos/message_with_reptead_list.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ option csharp_namespace = "System.Text.Json.Protobuf.Tests";
44

55
message MessageWithRepeatedList {
66
repeated int32 int_32_list = 1;
7+
8+
repeated int64 int_64_list = 2;
79
}

test/System.Text.Json.Protobuf.Tests/SimpleMessageTests.Should_serialize_message_with_repeated_list.approved.json

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 6 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Text.Json.Protobuf.Tests.Utils;
12
using Shouldly;
23
using SmartAnalyzers.ApprovalTestsExtensions;
34
using Xunit;
@@ -15,8 +16,9 @@ public void Should_serialize_message_with_primitive_types()
1516
Int32Property = 1,
1617
Int64Property = 2,
1718
};
18-
var jsonSerializerOptions = CreateJsonSerializerOptions();
19-
19+
var jsonSerializerOptions = TestHelper.CreateJsonSerializerOptions();
20+
21+
// Act
2022
var serialized = JsonSerializer.Serialize(msg, jsonSerializerOptions);
2123

2224
// Assert
@@ -33,8 +35,9 @@ public void Should_deserialize_message_with_primitive_types()
3335
Int32Property = 1,
3436
Int64Property = 2,
3537
};
36-
var jsonSerializerOptions = CreateJsonSerializerOptions();
38+
var jsonSerializerOptions = TestHelper.CreateJsonSerializerOptions();
3739

40+
// Act
3841
var serialized = JsonSerializer.Serialize(msg, jsonSerializerOptions);
3942
var deserialized = JsonSerializer.Deserialize<SimpleMessage>(serialized, jsonSerializerOptions);
4043

@@ -43,46 +46,4 @@ public void Should_deserialize_message_with_primitive_types()
4346
deserialized.Int32Property.ShouldBe(msg.Int32Property);
4447
deserialized.Int64Property.ShouldBe(msg.Int64Property);
4548
}
46-
47-
[Fact]
48-
public void Should_serialize_message_with_repeated_list()
49-
{
50-
var msg = new MessageWithRepeatedList
51-
{
52-
Int32List = {1, 2, 3}
53-
};
54-
55-
var jsonSerializerOptions = CreateJsonSerializerOptions();
56-
57-
var serialized = JsonSerializer.Serialize(msg, jsonSerializerOptions);
58-
59-
// Assert
60-
var approver = new ExplicitApprover();
61-
approver.VerifyJson(serialized);
62-
}
63-
64-
[Fact]
65-
public void Should_deserialize_message_with_repeated_list()
66-
{
67-
var msg = new MessageWithRepeatedList
68-
{
69-
Int32List = {1, 2, 3}
70-
};
71-
72-
var jsonSerializerOptions = CreateJsonSerializerOptions();
73-
74-
var serialized = JsonSerializer.Serialize(msg, jsonSerializerOptions);
75-
var deserialized = JsonSerializer.Deserialize<MessageWithRepeatedList>(serialized, jsonSerializerOptions);
76-
77-
// Assert
78-
deserialized.ShouldNotBeNull();
79-
deserialized.Int32List.ShouldBeEquivalentTo(msg.Int32List);
80-
}
81-
82-
private static JsonSerializerOptions CreateJsonSerializerOptions()
83-
{
84-
var jsonSerializerOptions = new JsonSerializerOptions();
85-
jsonSerializerOptions.Converters.Add(new ProtobufJsonConverterFactory());
86-
return jsonSerializerOptions;
87-
}
8849
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace System.Text.Json.Protobuf.Tests.Utils;
2+
3+
public class TestHelper
4+
{
5+
public static JsonSerializerOptions CreateJsonSerializerOptions()
6+
{
7+
var jsonSerializerOptions = new JsonSerializerOptions();
8+
jsonSerializerOptions.AddProtobufSupport();
9+
return jsonSerializerOptions;
10+
}
11+
}

0 commit comments

Comments
 (0)