|
| 1 | +using System.Text.Json; |
| 2 | +using System.Text.Json.Protobuf.Tests; |
| 3 | +using SmartAnalyzers.ApprovalTestsExtensions; |
| 4 | +using Xunit; |
| 5 | + |
| 6 | +namespace Protobuf.System.Text.Json.Tests; |
| 7 | + |
| 8 | +public class PropertyNamingSourceTests |
| 9 | +{ |
| 10 | + [Fact] |
| 11 | + public void Should_use_custom_json_name_when_PropertyNamingSource_is_ProtobufJsonName() |
| 12 | + { |
| 13 | + // Arrange |
| 14 | + var msg = new MessageWithCustomJsonName |
| 15 | + { |
| 16 | + DoubleProperty = 2.5d, |
| 17 | + StringProperty = "test" |
| 18 | + }; |
| 19 | + var jsonSerializerOptions = new JsonSerializerOptions(); |
| 20 | + jsonSerializerOptions.AddProtobufSupport(options => options.PropertyNamingSource = PropertyNamingSource.ProtobufJsonName); |
| 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_use_proto_field_name_when_PropertyNamingSource_is_ProtobufFieldName() |
| 32 | + { |
| 33 | + // Arrange |
| 34 | + var msg = new MessageWithCustomJsonName |
| 35 | + { |
| 36 | + DoubleProperty = 2.5d, |
| 37 | + StringProperty = "test" |
| 38 | + }; |
| 39 | + var jsonSerializerOptions = new JsonSerializerOptions(); |
| 40 | + jsonSerializerOptions.AddProtobufSupport(options => options.PropertyNamingSource = PropertyNamingSource.ProtobufFieldName); |
| 41 | + |
| 42 | + // Act |
| 43 | + var serialized = JsonSerializer.Serialize(msg, jsonSerializerOptions); |
| 44 | + |
| 45 | + // Assert |
| 46 | + var approver = new ExplicitApprover(); |
| 47 | + approver.VerifyJson(serialized); |
| 48 | + } |
| 49 | + |
| 50 | + [Fact] |
| 51 | + public void Should_deserialize_using_custom_json_name_when_PropertyNamingSource_is_ProtobufJsonName() |
| 52 | + { |
| 53 | + // Arrange |
| 54 | + var json = "{\"customDoubleProperty\": 2.5, \"stringProperty\": \"test\"}"; |
| 55 | + var jsonSerializerOptions = new JsonSerializerOptions(); |
| 56 | + jsonSerializerOptions.AddProtobufSupport(options => options.PropertyNamingSource = PropertyNamingSource.ProtobufJsonName); |
| 57 | + |
| 58 | + // Act |
| 59 | + var deserialized = JsonSerializer.Deserialize<MessageWithCustomJsonName>(json, jsonSerializerOptions); |
| 60 | + |
| 61 | + // Assert |
| 62 | + Assert.NotNull(deserialized); |
| 63 | + Assert.Equal(2.5d, deserialized.DoubleProperty); |
| 64 | + Assert.Equal("test", deserialized.StringProperty); |
| 65 | + } |
| 66 | + |
| 67 | + [Fact] |
| 68 | + public void Should_deserialize_using_proto_field_name_when_PropertyNamingSource_is_ProtobufFieldName() |
| 69 | + { |
| 70 | + // Arrange |
| 71 | + var json = "{\"double_property\": 2.5, \"string_property\": \"test\"}"; |
| 72 | + var jsonSerializerOptions = new JsonSerializerOptions(); |
| 73 | + jsonSerializerOptions.AddProtobufSupport(options => options.PropertyNamingSource = PropertyNamingSource.ProtobufFieldName); |
| 74 | + |
| 75 | + // Act |
| 76 | + var deserialized = JsonSerializer.Deserialize<MessageWithCustomJsonName>(json, jsonSerializerOptions); |
| 77 | + |
| 78 | + // Assert |
| 79 | + Assert.NotNull(deserialized); |
| 80 | + Assert.Equal(2.5d, deserialized.DoubleProperty); |
| 81 | + Assert.Equal("test", deserialized.StringProperty); |
| 82 | + } |
| 83 | + |
| 84 | + [Fact] |
| 85 | + public void Should_round_trip_with_ProtobufFieldName() |
| 86 | + { |
| 87 | + // Arrange |
| 88 | + var original = new MessageWithCustomJsonName |
| 89 | + { |
| 90 | + DoubleProperty = 2.5d, |
| 91 | + StringProperty = "test" |
| 92 | + }; |
| 93 | + var jsonSerializerOptions = new JsonSerializerOptions(); |
| 94 | + jsonSerializerOptions.AddProtobufSupport(options => options.PropertyNamingSource = PropertyNamingSource.ProtobufFieldName); |
| 95 | + |
| 96 | + // Act |
| 97 | + var serialized = JsonSerializer.Serialize(original, jsonSerializerOptions); |
| 98 | + var deserialized = JsonSerializer.Deserialize<MessageWithCustomJsonName>(serialized, jsonSerializerOptions); |
| 99 | + |
| 100 | + // Assert |
| 101 | + Assert.NotNull(deserialized); |
| 102 | + Assert.Equal(original.DoubleProperty, deserialized.DoubleProperty); |
| 103 | + Assert.Equal(original.StringProperty, deserialized.StringProperty); |
| 104 | + } |
| 105 | +} |
0 commit comments