-
Notifications
You must be signed in to change notification settings - Fork 5k
Proposal: Utf8JsonWriter.WriteUtf8Json #32849
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I've also had a use for something like this, particularly when interacting with multiple serializers. (With different behaviour to System.Text.Json, which is fairly hard to implement without writing a lot of custom code.) The workaround I came up with was to serialize to a JsonDocument and write that to the output. A better name is probably something like |
Yeah I ended up deserializing to a As for naming, I'm sure that the team would have some strong opinions as well so I'm happy to leave that open to discussion. My original thinking was along the lines that you may need to convey to the caller than the method does not do any transcoding; the input |
Writing raw data can be unsafe. I would propose a method that writes all tokens from a Utf8JsonReader. That would implicitly validate the content. |
That was a pattern I was hoping existed when I was first looking for a solution. Something like public ref struct Utf8JsonReader {
void CopyTo(Utf8JsonWriter writer);
} Alternatively (and this seems like a objectively worse API) only allow Or implement both options; have |
We are looking into this for 5.0. Closing as a duplicate of #1784. |
I would like to propose an addition to the
System.Text.Json.Utf8JsonWriter
API which would allow writing existing UTF8-encoded JSON fragments into the current JSON document.Something like the following:
An example of usage would be:
I believe this would be useful when you are trying to augment or wrap existing JSON payloads (i.e. from external services) in a parent document without needing to deserialize and reserialize them.
I have read through all the documentation I can find and as far as I can see there doesn't currently seem to be any way of doing what I'm proposing. If there is already a better way of doing this please let me know.
The text was updated successfully, but these errors were encountered: