Provide access to the raw byte data for a JsonElement for efficient transcoding of simple, custom types without allocating an intermediate string. #42839
Labels
api-suggestion
Early API idea and discussion, it is NOT ready for implementation
area-System.Text.Json
Milestone
Background and Motivation
There are a large number of datatypes that need to be implemented as formats of the string type. The canonical example is time handling, but there are many others. For instance, we choose to model our .NET Date/Time types using the NodaTime entities, to better map to the intent of JSON schema. (See this issue for details.)
On the writing side this is straightforward, as we can use
Utf8JsonWriter
to encode and write the values efficiently.On the reading side, we can do this efficiently if we drop to
Utf8JsonReader
; otherwise we have to allocate dotnet strings and then parse them into the target types.Dropping to
Utf8JsonReader
means we then we have to do all of the heavy lifting, all the time, when theJsonDocument
provides us with a perfectly efficient parse over the higher-level structure and efficient access to properties.We would like to be able to access to the raw bytes that back the
JsonElement
, when necessary to efficiently decode an instance of a type like this.JsonDocument
has parsed the data using its internal db/row structure, and decoding of values internally is handled viaGetRawValue()
which returns aReadOnlyMemory<byte>
based on the given start index, and searching for an end-index.JsonElement
offersGetRawText()
which usesJsonDocument.GetRawValue()
and then transcodes it to a string.We would like
JsonElement.GetRawValue()
which simply eliminates the transcoding to a string.The downside of this is that people could potentially misuse the content - it has not been unescaped/validated for example - but it would enable a lot of customization scenarios without throwing out all the good (efficient!) work
JsonDocument
has already done for usProposed API
Usage Examples
Alternative Designs
You can always fall back to
Utf8JsonReader
for this, but that entails dealing with theValueSequence
/ValueSpan
and managing the buffers.JsonDocument
has already done that for you with this approach.You could also use
WriteTo(Span<byte>)
- but that would require you to allocate (and potentially recycle, although that is not guaranteed) a target buffer. Whether or not you need to allocate, it also means making additional copies of data, which is never good for performanceRisks
The chief risk is that people try to use this low-level API without understanding how the underlying UTF8 byte stream actually works. Ensuring that people are pointed at
System.Buffers.Text.Utf8Parser
should help mitigate this.The text was updated successfully, but these errors were encountered: