-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathJsonNumberHandlingTests.cs
More file actions
241 lines (210 loc) · 8.26 KB
/
JsonNumberHandlingTests.cs
File metadata and controls
241 lines (210 loc) · 8.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
using System;
using System.Text.Json;
using System.Text.Json.Protobuf.Tests;
using System.Text.Json.Serialization;
using Protobuf.System.Text.Json.Tests.Utils;
using Xunit;
namespace Protobuf.System.Text.Json.Tests;
public class JsonNumberHandlingTests
{
[Fact]
public void Should_deserialize_numbers_as_strings_when_NumberHandling_set_to_AllowReadingFromString()
{
// Arrange
var json =
"""
{
"doubleProperty" : "0.12",
"floatProperty" : "7.89",
"int32Property" : "123",
"int64Property" : "456",
"uint32Property" : "789",
"uint64Property" : "101112",
"sint32Property" : "1314",
"sint64Property" : "151617",
"fixed32Property" : "1819",
"fixed64Property" : "202122",
"sfixed32Property" : "2324",
"sfixed64Property" : "252627"
}
""";
var options = TestHelper.CreateJsonSerializerOptions();
options.NumberHandling = JsonNumberHandling.AllowReadingFromString;
// Act
var deserialized = JsonSerializer.Deserialize<SimpleMessage>(json, options);
// Assert
Assert.NotNull(deserialized);
Assert.Equal(0.12d, deserialized.DoubleProperty);
Assert.Equal(7.89f, deserialized.FloatProperty);
Assert.Equal(123, deserialized.Int32Property);
Assert.Equal(456L, deserialized.Int64Property);
Assert.Equal(789u, deserialized.Uint32Property);
Assert.Equal(101112ul, deserialized.Uint64Property);
Assert.Equal(1314, deserialized.Sint32Property);
Assert.Equal(151617L, deserialized.Sint64Property);
Assert.Equal(1819u, deserialized.Fixed32Property);
Assert.Equal(202122ul, deserialized.Fixed64Property);
Assert.Equal(2324, deserialized.Sfixed32Property);
Assert.Equal(252627L, deserialized.Sfixed64Property);
}
[Fact]
public void Should_fail_deserializing_numbers_as_strings_when_NumberHandling_not_set_to_AllowReadingFromString()
{
// Arrange
var json =
"""
{
"doubleProperty" : "0.12",
"floatProperty" : "7.89",
"int32Property" : "123",
"int64Property" : "456",
"uint32Property" : "789",
"uint64Property" : "101112",
"sint32Property" : "1314",
"sint64Property" : "151617",
"fixed32Property" : "1819",
"fixed64Property" : "202122",
"sfixed32Property" : "2324",
"sfixed64Property" : "252627"
}
""";
var options = TestHelper.CreateJsonSerializerOptions();
// Explicitly disable reading numbers from strings
options.NumberHandling = JsonNumberHandling.Strict;
// Act & Assert
Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<SimpleMessage>(json, options));
}
[Fact]
public void Should_serialize_numbers_as_strings_when_NumberHandling_set_to_WriteAsString()
{
// Arrange
var message = new SimpleMessage
{
DoubleProperty = 0.12d,
FloatProperty = 7.89f,
Int32Property = 123,
Int64Property = 456L,
Uint32Property = 789u,
Uint64Property = 101112u, // Note: this is actually uint32 in proto
Sint32Property = 1314,
Sint64Property = 151617L,
Fixed32Property = 1819u,
Fixed64Property = 202122ul,
Sfixed32Property = 2324,
Sfixed64Property = 252627L
};
var options = TestHelper.CreateJsonSerializerOptions();
options.NumberHandling = JsonNumberHandling.WriteAsString;
// Act
var json = JsonSerializer.Serialize(message, options);
// Assert
Assert.Contains("\"doubleProperty\":\"0.12\"", json);
Assert.Contains("\"floatProperty\":\"7.89\"", json);
Assert.Contains("\"int32Property\":\"123\"", json);
Assert.Contains("\"int64Property\":\"456\"", json);
Assert.Contains("\"uint32Property\":\"789\"", json);
Assert.Contains("\"uint64Property\":\"101112\"", json);
Assert.Contains("\"sint32Property\":\"1314\"", json);
Assert.Contains("\"sint64Property\":\"151617\"", json);
Assert.Contains("\"fixed32Property\":\"1819\"", json);
Assert.Contains("\"fixed64Property\":\"202122\"", json);
Assert.Contains("\"sfixed32Property\":\"2324\"", json);
Assert.Contains("\"sfixed64Property\":\"252627\"", json);
}
[Fact]
public void Should_not_serialize_numbers_as_strings_when_NumberHandling_set_to_Strict()
{
// Arrange
var message = new SimpleMessage
{
DoubleProperty = 0.12d,
FloatProperty = 7.89f,
Int32Property = 123
};
var options = TestHelper.CreateJsonSerializerOptions();
options.NumberHandling = JsonNumberHandling.Strict;
// Act
var json = JsonSerializer.Serialize(message, options);
// Assert - numbers should be JSON numbers, not strings
Assert.Contains("\"doubleProperty\":0.12", json);
Assert.Contains("\"floatProperty\":7.89", json);
Assert.Contains("\"int32Property\":123", json);
}
[Fact]
public void Should_deserialize_named_floating_point_literals_when_AllowNamedFloatingPointLiterals_flag_set()
{
// Arrange
var json =
"""
{
"doubleProperty": "NaN",
"floatProperty": "Infinity"
}
""";
var options = TestHelper.CreateJsonSerializerOptions();
options.NumberHandling = JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.AllowNamedFloatingPointLiterals;
// Act
var deserialized = JsonSerializer.Deserialize<SimpleMessage>(json, options);
// Assert
Assert.NotNull(deserialized);
Assert.True(double.IsNaN(deserialized.DoubleProperty));
Assert.True(float.IsPositiveInfinity(deserialized.FloatProperty));
}
[Fact]
public void Should_deserialize_negative_infinity_when_AllowNamedFloatingPointLiterals_flag_set()
{
// Arrange
var json =
"""
{
"doubleProperty": "-Infinity",
"floatProperty": "-Infinity"
}
""";
var options = TestHelper.CreateJsonSerializerOptions();
options.NumberHandling = JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.AllowNamedFloatingPointLiterals;
// Act
var deserialized = JsonSerializer.Deserialize<SimpleMessage>(json, options);
// Assert
Assert.NotNull(deserialized);
Assert.True(double.IsNegativeInfinity(deserialized.DoubleProperty));
Assert.True(float.IsNegativeInfinity(deserialized.FloatProperty));
}
[Fact]
public void Should_fail_deserializing_named_floating_point_literals_when_flag_not_set()
{
// Arrange
var json =
"""
{
"doubleProperty": "NaN"
}
""";
var options = TestHelper.CreateJsonSerializerOptions();
// Only AllowReadingFromString, but not AllowNamedFloatingPointLiterals
options.NumberHandling = JsonNumberHandling.AllowReadingFromString;
// Act & Assert
Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<SimpleMessage>(json, options));
}
[Fact]
public void Should_round_trip_with_WriteAsString_and_AllowReadingFromString()
{
// Arrange
var message = new SimpleMessage
{
DoubleProperty = 123.456d,
FloatProperty = 78.9f,
Int32Property = 999
};
var options = TestHelper.CreateJsonSerializerOptions();
options.NumberHandling = JsonNumberHandling.WriteAsString | JsonNumberHandling.AllowReadingFromString;
// Act
var json = JsonSerializer.Serialize(message, options);
var deserialized = JsonSerializer.Deserialize<SimpleMessage>(json, options);
// Assert
Assert.NotNull(deserialized);
Assert.Equal(message.DoubleProperty, deserialized.DoubleProperty);
Assert.Equal(message.FloatProperty, deserialized.FloatProperty);
Assert.Equal(message.Int32Property, deserialized.Int32Property);
}
}