https://github.com/scalablecory/system-text-json-samples
System.Text.Json samples
https://github.com/scalablecory/system-text-json-samples
dotnet dotnet-core dotnet-core3 json system-text-json
Last synced: 3 months ago
JSON representation
System.Text.Json samples
- Host: GitHub
- URL: https://github.com/scalablecory/system-text-json-samples
- Owner: scalablecory
- Created: 2019-06-24T00:58:55.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2019-07-05T21:55:31.000Z (about 7 years ago)
- Last Synced: 2025-03-28T04:36:58.800Z (over 1 year ago)
- Topics: dotnet, dotnet-core, dotnet-core3, json, system-text-json
- Language: C#
- Homepage:
- Size: 11.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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:
* See [JsonParser.Memory.cs](json-test/JsonParser.Memory.cs) for a simple `Span`-based implementation that uses that parser.
* See [JsonParser.ParseSimpleAsync.cs](json-test/JsonParser.ParseSimpleAsync.cs) for a `Stream`-based implementation that implements trivial copy & grow buffering.
* See [Jsonparser.ParseNoCopyAsync.cs](json-test/JsonParser.ParseNoCopyAsync.cs) for a `Stream`-based implementation that implements sequences to avoid copying when growing buffers.