Skip to content

scalablecory/system-text-json-samples

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

System.Text.Json Samples

System.Text.Json is a high-performance JSON API added in .NET Core 3.

This looks at how one might use its various features.

Sample data used: http://data.consumerfinance.gov/api/views.json

JsonSerializer

TODO.

JsonDocument

TODO.

Utf8JsonReader

Utf8JsonReader is a pull parser similar to XmlReader. It is a bit low-level, presenting the document as a stream of tokens that you must interpret.

Parsing synchronously

Complaint.Read implements a synchronous parser on top of ReadOnlySpan.

This is the simplest parser you can write on top of Utf8JsonReader, with each "object" being parsed in its own method and a clear call stack as you go through the document.

Parsing asynchronously

Complaint.ReadAsync implements an asynchronous parser on top of Stream.

This does not load the entire Stream into memory: instead, it is parsed in reasonably sized chunks. We must handle the case where Utf8JsonReader has exhausted the current buffer, and read the next chunk. Because Utf8JsonReader is a ref struct, we can not use it in an async method and must instead implement our own state machine manually. This makes things a bit harder to follow.

This is implemented via a resumable, I/O-agnostic parser ComplaintParser that is passed to one of three methods which handle the I/O:

Releases

No releases published

Packages

No packages published

Languages