Improve custom JsonConverters by allowing to call the default Read/Write implementation in System.Text.Json #46434
Unanswered
LazaroOnline
asked this question in
Ideas
Replies: 1 comment
-
This solution worked for my use case related to this issue. My JsonConverter Write method looks as follows: public override void Write(Utf8JsonWriter writer, MyClass value, JsonSerializerOptions options)
{
var doc = JsonSerializer.SerializeToDocument(value, value.GetType(), options);
doc.WriteTo(writer);
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
When trying to implement a custom JsonConverter in
System.Text.Json
, sometimes you only want to implement one of the 2 methodsRead
orWrite
, or you may want to call the default implementation (as if the converter that you are writing was not there) before further processing the data.But calling their default implementation is not currently possible as one would expect without issues or overly complicated solutions as seen in this StackOverflow question.
If you try to call the "default" like this:
Then you may fall in an infinite loop if you try to add the converter as an attribute to a class definition, example:
Can we have a way, provided by .Net runtime in System.Text.Json, to call the default implementation?
Something like:
So not only we can call leave the implementation to the default behaviour (as if the converter being written was not there), but also we could use the default implementation dynamically based on some condition.
Thank you!
Beta Was this translation helpful? Give feedback.
All reactions