Add async serialization and deserialization support #2643
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds async serialization and deserialization support to Newtonsoft.Json (#1193). It uses a Roslyn source generator to generate async methods from their synchronous counterparts at build time. There is therefore no double code maintenance burden. Generated source is checked in for reference, easier debugging and PDB source links.
Async code paths are tested by the existing code suite by means of substituting calls to async methods at three key entry points -
JsonSerializer.SerializeInternal
,JsonSerializer.PopulateInternal
andJsonSerializer.DeserializeInternal
- whenTEST_ASYNC_AS_SYNC
preprocessor constant is defined. Completeness of async code generation coverage is ensured by wrappingJsonReader
andJsonWriter
objects passed into these methods with wrappers which throw exceptions from all sync I/O methods. WhenTEST_ASYNC_AS_SYNC
is not defined, tests use existing (synchronous) code paths. All tests pass in this mode.At the moment the PR is in draft stage. The main project is set up to always test the async code paths. Over 90% of the test suite passes in this mode, but tests that rely on custom converters in
Newtonsoft.Json.Converters
namespace fail because I haven't yet extended them with async implementations. I will add these if this PR has a chance of being merged.There are a few points where I had questions, marked with comments with the
XXX
tag. I have usedTask
rather thanValueTask
for simplicity, but it is easy to switch toValueTask
for greater performance and much fewer allocations provided that it is acceptable to introduce a dependency on theSystem.Threading.Task.Extensions
nuget package.