|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license.
|
3 | 3 | // See the LICENSE file in the project root for more information.
|
4 | 4 |
|
| 5 | +using System.Collections.Generic; |
5 | 6 | using System.IO;
|
6 | 7 | using System.Threading.Tasks;
|
7 | 8 | using Xunit;
|
@@ -149,6 +150,112 @@ public static async Task WritePrimitivesAsync()
|
149 | 150 | int i = await JsonSerializer.DeserializeAsync<int>(stream, options);
|
150 | 151 | Assert.Equal(1, i);
|
151 | 152 | }
|
| 153 | + |
| 154 | + private class Session |
| 155 | + { |
| 156 | + public int Id { get; set; } |
| 157 | + public string Title { get; set; } |
| 158 | + public virtual string Abstract { get; set; } |
| 159 | + public virtual DateTimeOffset? StartTime { get; set; } |
| 160 | + public virtual DateTimeOffset? EndTime { get; set; } |
| 161 | + public TimeSpan Duration => EndTime?.Subtract(StartTime ?? EndTime ?? DateTimeOffset.MinValue) ?? TimeSpan.Zero; |
| 162 | + public int? TrackId { get; set; } |
| 163 | + } |
| 164 | + |
| 165 | + private class SessionResponse : Session |
| 166 | + { |
| 167 | + public Track Track { get; set; } |
| 168 | + public List<Speaker> Speakers { get; set; } = new List<Speaker>(); |
| 169 | + } |
| 170 | + |
| 171 | + private class Track |
| 172 | + { |
| 173 | + public int Id { get; set; } |
| 174 | + public string Name { get; set; } |
| 175 | + } |
| 176 | + |
| 177 | + private class Speaker |
| 178 | + { |
| 179 | + public int Id { get; set; } |
| 180 | + public string Name { get; set; } |
| 181 | + public string Bio { get; set; } |
| 182 | + public virtual string WebSite { get; set; } |
| 183 | + } |
| 184 | + |
| 185 | + [Theory] |
| 186 | + [InlineData(0)] |
| 187 | + [InlineData(10)] |
| 188 | + [InlineData(1000)] |
| 189 | + [InlineData(4000)] |
| 190 | + [InlineData(8000)] |
| 191 | + [InlineData(16000)] |
| 192 | + public static async Task LargeJsonFile(int bufferSize) |
| 193 | + { |
| 194 | + // Build up a large list to serialize. |
| 195 | + var list = new List<SessionResponse>(); |
| 196 | + for (int i = 0; i < 100; i++) |
| 197 | + { |
| 198 | + SessionResponse response = new SessionResponse |
| 199 | + { |
| 200 | + Id = i, |
| 201 | + Abstract = new string('A', i * 2), |
| 202 | + Title = new string('T', i), |
| 203 | + StartTime = new DateTime(i), |
| 204 | + EndTime = new DateTime(i * 10000), |
| 205 | + TrackId = i, |
| 206 | + Track = new Track() |
| 207 | + { |
| 208 | + Id = i, |
| 209 | + Name = new string('N', i), |
| 210 | + }, |
| 211 | + }; |
| 212 | + |
| 213 | + for (int j = 0; j < 5; j++) |
| 214 | + { |
| 215 | + response.Speakers.Add(new Speaker() |
| 216 | + { |
| 217 | + Bio = new string('B', 50), |
| 218 | + Id = j, |
| 219 | + Name = new string('N', i), |
| 220 | + WebSite = new string('W', 20), |
| 221 | + }); |
| 222 | + } |
| 223 | + |
| 224 | + list.Add(response); |
| 225 | + } |
| 226 | + |
| 227 | + // Adjust buffer length to encourage buffer flusing at several levels. |
| 228 | + JsonSerializerOptions options = new JsonSerializerOptions(); |
| 229 | + if (bufferSize != 0) |
| 230 | + { |
| 231 | + options.DefaultBufferSize = bufferSize; |
| 232 | + } |
| 233 | + |
| 234 | + string json = JsonSerializer.Serialize(list, options); |
| 235 | + Assert.True(json.Length > 100_000); // Verify data is large and will cause buffer flushing. |
| 236 | + Assert.True(json.Length < 200_000); // But not too large for memory considerations. |
| 237 | + |
| 238 | + // Sync case. |
| 239 | + { |
| 240 | + List<SessionResponse> deserializedList = JsonSerializer.Deserialize<List<SessionResponse>>(json, options); |
| 241 | + Assert.Equal(100, deserializedList.Count); |
| 242 | + |
| 243 | + string jsonSerialized = JsonSerializer.Serialize(deserializedList, options); |
| 244 | + Assert.Equal(json, jsonSerialized); |
| 245 | + } |
| 246 | + |
| 247 | + // Async case. |
| 248 | + using (var memoryStream = new MemoryStream()) |
| 249 | + { |
| 250 | + await JsonSerializer.SerializeAsync(memoryStream, list, options); |
| 251 | + string jsonSerialized = Encoding.UTF8.GetString(memoryStream.ToArray()); |
| 252 | + Assert.Equal(json, jsonSerialized); |
| 253 | + |
| 254 | + memoryStream.Position = 0; |
| 255 | + List<SessionResponse> deserializedList = await JsonSerializer.DeserializeAsync<List<SessionResponse>>(memoryStream, options); |
| 256 | + Assert.Equal(100, deserializedList.Count); |
| 257 | + } |
| 258 | + } |
152 | 259 | }
|
153 | 260 |
|
154 | 261 | public sealed class TestStream : Stream
|
|
0 commit comments