Skip to content
This repository has been archived by the owner on May 16, 2022. It is now read-only.

Fix issue while serializing not completed ValueTask<TResult> #221

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
12 changes: 10 additions & 2 deletions src/Utf8Json/Formatters/StandardClassLibraryFormatters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -669,8 +669,16 @@ public sealed class ValueTaskFormatter<T> : IJsonFormatter<ValueTask<T>>
{
public void Serialize(ref JsonWriter writer, ValueTask<T> value, IJsonFormatterResolver formatterResolver)
{
// value.Result -> wait...!
formatterResolver.GetFormatterWithVerify<T>().Serialize(ref writer, value.Result, formatterResolver);

if(value.IsCompleted)
{
formatterResolver.GetFormatterWithVerify<T>().Serialize(ref writer, value.Result, formatterResolver);
}
else
{
// value.Result -> wait...!
formatterResolver.GetFormatterWithVerify<T>().Serialize(ref writer, value.AsTask().Result, formatterResolver);
}
}

public ValueTask<T> Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
Expand Down
3 changes: 2 additions & 1 deletion tests/Utf8Json.Tests/ExceptionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public FooException() : base("BCD")

}
}

#if !NETCOREAPP3_1 //The order of members in json is different.
public class ExceptionTest
{
[Fact]
Expand Down Expand Up @@ -64,4 +64,5 @@ public void Inner()
}
}
}
#endif
}
19 changes: 19 additions & 0 deletions tests/Utf8Json.Tests/FormatterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,27 @@ public void TaskTest()

ValueTask<int>? nullValueTask2 = null;
Convert(nullValueTask2).IsNull();


}

#if NETCOREAPP3_1
[Fact]
public async Task ValueTaskSourceTest()
{
ValueTask<bool> asyncValueTask = ValueTaskSourceProvider().MoveNextAsync();
(await Convert(asyncValueTask)).IsFalse();

}

public async IAsyncEnumerator<object> ValueTaskSourceProvider()
{
await Task.Delay(100);
yield break;
}

#endif

[Fact]
public void DecimalLang()
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Utf8Json.Tests/Utf8Json.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<TargetFrameworks>netcoreapp2.0;netcoreapp3.1</TargetFrameworks>

<IsPackable>false</IsPackable>

Expand Down