-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
How to write custom converters for JSON serialization (marshalling) in .NET
should explain how to handle complex types
#35020
Comments
I believe s_defaultConverter.Write(writer, value, options);
I'm not sure I understand the use case or how it is related to the issue. Given that this repo is for tracking documentation issues, I would recommend asking a question in the "Discussions" section of dotnet/runtime rather than here. |
@eiriktsarpalis it doesn't work on custom types. It will fail with I took the example from the doc, but I replaced To reproduce:
Stack trace snippet:
I confirmed the above-given code works if I modify it to be Hence my ask is to apply following changes to the page:
|
Ah yes, that issue is tracked here dotnet/runtime#50205. But it is not the case that a |
Also related to dotnet/runtime#63791. Given that this issue is not related to API documentation per se I'd be inclined to close it. Feel free to reopen or file a new issue if you were looking for a specific fix in the docs you cited. |
The
How to write custom converters for JSON serialization (marshalling) in .NET
page explains how to use default system converter here.What is not clear is that these converters are only for primitive types. I was hoping this article will explain how I can do this with my own custom built-in types, but it doesn't.
Following example is given for using a default converter in a custom converter:
but this doesn't work if
value
is notint
but a custom type. The custom converter will be null and just doingvalue.ToString()
won't produce the right value. Instead, for serialization one has to do:Similarly, for deserialization:
Additional context
You might ask: why I am trying to write a custom type converter for a complex type in the first place? Performance will take a hit.
This is an excellent question. It is because of this limitation:
Quote:
In addition, the documentation on ReferenceHandler.Preserve says:
I am trying to implement my own serialization that will preserve all references and also use constructors, without using
required
orinit
properties. This is because I want to forbid using an object initializer because I want to ensure constructor is called, as I am going to put precondition checks in the constructor which would be circumvented otherwise.Hence, if I could write a custom converter for my complex type I would make it know when to serialize its members fully and when just by reference, to avoid cycles and duplication. It would also have matching deserialization logic which knows when to deserialize given object and when to deserialize just a reference to it and call proper constructors in the right order.
@eiriktsarpalis if you perhaps have some insight how to best deal with my scenario I would be grateful. Maybe there is better approach than what I am thinking.
Related:
Consume decoded JSON strings
inHow to use a JSON document, Utf8JsonReader, and Utf8JsonWriter in System.Text.Json
does not compile #35019Utf8JsonReader
should explain how to obtain different kinds of objects from it, like e.g.JsonNode
. #35021Document Details
⚠ Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.
The text was updated successfully, but these errors were encountered: