Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal override bool OnTryRead(
scoped ref ReadStack state,
out Memory<T> value)
{
if (reader.TokenType is JsonTokenType.Null)
if (reader.TokenType is JsonTokenType.Null && state.Current.ReturnValue is null)
{
value = default;
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal override bool OnTryRead(
scoped ref ReadStack state,
out ReadOnlyMemory<T> value)
{
if (reader.TokenType is JsonTokenType.Null)
if (reader.TokenType is JsonTokenType.Null && state.Current.ReturnValue is null)
{
value = default;
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Xunit;

Expand Down Expand Up @@ -137,5 +139,39 @@ public async Task DeserializeMemoryByteClassAsync()
MemoryOfTClass<byte> memoryOfByteClass = await Serializer.DeserializeWrapper<MemoryOfTClass<byte>>(json);
AssertExtensions.SequenceEqual<byte>(s_testData.AsSpan(), memoryOfByteClass.Memory.Span);
}

[Fact]
public async Task DeserializeReadOnlyMemoryFromStreamWithManyNulls()
{
// Regression test for https://github.com/dotnet/runtime/issues/118346
if (StreamingSerializer is null)
{
return;
}

// Create an array of ~200 null elements to force resumptions with smallest buffer size
string json = $"[{string.Join(",", Enumerable.Repeat("null", 200))}]";
using var stream = new Utf8MemoryStream(json);
ReadOnlyMemory<string?> result = await StreamingSerializer.DeserializeWrapper<ReadOnlyMemory<string?>>(stream);

Assert.Equal(200, result.Length);
}

[Fact]
public async Task DeserializeMemoryFromStreamWithManyNulls()
{
// Regression test for https://github.com/dotnet/runtime/issues/118346
if (StreamingSerializer is null)
{
return;
}

// Create an array of ~200 null elements to force resumptions with smallest buffer size
string json = $"[{string.Join(",", Enumerable.Repeat("null", 200))}]";
using var stream = new Utf8MemoryStream(json);
Memory<string?> result = await StreamingSerializer.DeserializeWrapper<Memory<string?>>(stream);

Assert.Equal(200, result.Length);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,8 @@ public async Task DeserializeAsyncEnumerable()
[JsonSerializable(typeof(StackWrapper))]
[JsonSerializable(typeof(ClassWithRecursiveCollectionTypes))]
[JsonSerializable(typeof(MemoryOfTClass<byte>))]
[JsonSerializable(typeof(ReadOnlyMemory<string>))]
[JsonSerializable(typeof(Memory<string>))]
[JsonSerializable(typeof(ReadOnlyMemoryOfTClass<byte>))]
[JsonSerializable(typeof(MemoryOfTClass<int>))]
[JsonSerializable(typeof(ReadOnlyMemoryOfTClass<int>))]
Expand Down Expand Up @@ -883,6 +885,8 @@ public CollectionTests_Default()
[JsonSerializable(typeof(StackWrapper))]
[JsonSerializable(typeof(ClassWithRecursiveCollectionTypes))]
[JsonSerializable(typeof(MemoryOfTClass<byte>))]
[JsonSerializable(typeof(ReadOnlyMemory<string>))]
[JsonSerializable(typeof(Memory<string>))]
[JsonSerializable(typeof(ReadOnlyMemoryOfTClass<byte>))]
[JsonSerializable(typeof(MemoryOfTClass<int>))]
[JsonSerializable(typeof(ReadOnlyMemoryOfTClass<int>))]
Expand Down
Loading