-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
CustomConverterTests.NullValueType.cs
226 lines (184 loc) · 7.98 KB
/
CustomConverterTests.NullValueType.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using System.Globalization;
using Xunit;
namespace System.Text.Json.Serialization.Tests
{
public static partial class CustomConverterTests
{
/// <summary>
/// Allow a conversion of "null" to a 0 value.
/// </summary>
private class Int32NullConverter : JsonConverter<int>
{
public override int Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null)
{
return 0;
}
return reader.GetInt32();
}
public override void Write(Utf8JsonWriter writer, int value, JsonSerializerOptions options)
{
writer.WriteNumberValue(value);
}
}
[Fact]
public static void ValueTypeConverterForNull()
{
// Baseline
Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<int>("null"));
Assert.Equal(1, JsonSerializer.Deserialize<int>("1"));
var options = new JsonSerializerOptions();
options.Converters.Add(new Int32NullConverter());
Assert.Equal(0, JsonSerializer.Deserialize<int>("null", options));
Assert.Equal(1, JsonSerializer.Deserialize<int>("1", options));
}
[Fact]
public static void ValueTypeConverterForNullWithArray()
{
const string json = "[null, 1, null]";
// Baseline
Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<int[]>(json));
var options = new JsonSerializerOptions();
options.Converters.Add(new Int32NullConverter());
int[] arr = JsonSerializer.Deserialize<int[]>(json, options);
Assert.Equal(0, arr[0]);
Assert.Equal(1, arr[1]);
Assert.Equal(0, arr[2]);
}
/// <summary>
/// Allow a conversion of empty string to a null DateTimeOffset?.
/// </summary>
public class JsonNullableDateTimeOffsetConverter : JsonConverter<DateTimeOffset?>
{
public override DateTimeOffset? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null)
{
return default;
}
string value = reader.GetString();
if (value == string.Empty)
{
return default;
}
return DateTimeOffset.ParseExact(value, "yyyy/MM/dd HH:mm:ss", CultureInfo.InvariantCulture);
}
public override void Write(Utf8JsonWriter writer, DateTimeOffset? value, JsonSerializerOptions options)
{
if (!value.HasValue)
{
writer.WriteNullValue();
}
else
{
writer.WriteStringValue(value.Value.ToString("yyyy/MM/dd HH:mm:ss"));
}
}
}
private class ClassWithNullableAndJsonConverterAttribute
{
[JsonConverter(typeof(JsonNullableDateTimeOffsetConverter))]
public DateTimeOffset? NullableValue { get; set; }
}
[Fact]
public static void ValueConverterForNullableWithJsonConverterAttribute()
{
ClassWithNullableAndJsonConverterAttribute obj;
const string BaselineJson = @"{""NullableValue"":""1989/01/01 11:22:33""}";
obj = JsonSerializer.Deserialize<ClassWithNullableAndJsonConverterAttribute>(BaselineJson);
Assert.NotNull(obj.NullableValue);
const string Json = @"{""NullableValue"":""""}";
obj = JsonSerializer.Deserialize<ClassWithNullableAndJsonConverterAttribute>(Json);
Assert.Null(obj.NullableValue);
string json = JsonSerializer.Serialize(obj);
Assert.Contains(@"""NullableValue"":null", json);
}
private class ClassWithNullableAndWithoutJsonConverterAttribute
{
public DateTimeOffset? NullableValue { get; set; }
public List<DateTimeOffset?> NullableValues { get; set; }
}
[Fact]
public static void ValueConverterForNullableWithoutJsonConverterAttribute()
{
const string Json = @"{""NullableValue"":"""", ""NullableValues"":[""""]}";
ClassWithNullableAndWithoutJsonConverterAttribute obj;
// The json is not valid with the default converter.
Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<ClassWithNullableAndWithoutJsonConverterAttribute>(Json));
JsonSerializerOptions options = new JsonSerializerOptions();
options.Converters.Add(new JsonNullableDateTimeOffsetConverter());
obj = JsonSerializer.Deserialize<ClassWithNullableAndWithoutJsonConverterAttribute>(Json, options);
Assert.Null(obj.NullableValue);
Assert.Null(obj.NullableValues[0]);
string json = JsonSerializer.Serialize(obj);
Assert.Contains(@"""NullableValue"":null", json);
Assert.Contains(@"""NullableValues"":[null]", json);
}
[JsonConverter(typeof(ClassThatCanBeNullDependingOnContentConverter))]
private class ClassThatCanBeNullDependingOnContent
{
public int MyInt { get; set; }
}
/// <summary>
/// Allow a conversion of ClassThatCanBeNullDependingOnContent to null when its MyInt property is 0.
/// </summary>
private class ClassThatCanBeNullDependingOnContentConverter : JsonConverter<ClassThatCanBeNullDependingOnContent>
{
public override ClassThatCanBeNullDependingOnContent Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null)
{
return null;
}
// Assume a single property.
reader.Read();
Assert.Equal(JsonTokenType.PropertyName, reader.TokenType);
reader.Read();
int myInt = reader.GetInt16();
reader.Read();
Assert.Equal(JsonTokenType.EndObject, reader.TokenType);
if (myInt == 0)
{
return null;
}
return new ClassThatCanBeNullDependingOnContent
{
MyInt = myInt
};
}
public override void Write(Utf8JsonWriter writer, ClassThatCanBeNullDependingOnContent value, JsonSerializerOptions options)
{
writer.WriteStartObject();
if (value.MyInt == 0)
{
writer.WriteNull("MyInt");
}
else
{
writer.WriteNumber("MyInt", value.MyInt);
}
writer.WriteEndObject();
}
}
[Fact]
public static void ConverterForClassThatCanBeNullDependingOnContent()
{
ClassThatCanBeNullDependingOnContent obj;
obj = JsonSerializer.Deserialize<ClassThatCanBeNullDependingOnContent>(@"{""MyInt"":5}");
Assert.Equal(5, obj.MyInt);
string json;
json = JsonSerializer.Serialize(obj);
Assert.Contains(@"""MyInt"":5", json);
obj.MyInt = 0;
json = JsonSerializer.Serialize(obj);
Assert.Contains(@"""MyInt"":null", json);
obj = JsonSerializer.Deserialize<ClassThatCanBeNullDependingOnContent>(@"{""MyInt"":0}");
Assert.Null(obj);
}
}
}