❔ Avoid infinite recursion in STJ converters
There are so-so many cases where I only want to customize reading up a type in System.Text.Json, but I'd want the default behavior for writing. Currently I'm doing this to avoid SO:
This is janky as hell IMO, is there a better/nicer way? I've found no obvious answer to it.
16 Replies
Well, you're not really meant to use
JsonSerializer.Serialize
in a converter. The whole idea of it is that you specify how things are serialized.Cool but that doesn't answer my question
I don't want to specify how the given type is serialized, I only want to customize the reading part
What...? Your post only ever talks about writing data, but now you ask about reading it?
post is about avoiding all that BS in Write
I have customized Reading
To avoid SO, I have to remove my converter for writing, because I do want the default write behavior
There are so-so many cases where I only want to customize reading up a type in System.Text.Json, but I'd want the default behavior for writing. Currently I'm doing this to avoid SOprovides snippet with the obvious hack
is there a better/nicer way?Please take your usual useless comments elsewhere 😅
?
when looking at all the different overloads i would guess thats exactly what to use to get back to the default behavior.
so where is that information from so i can read up on the details on it?
thb, this all sounds like u basically should just use different
JsonSerializerOptions
for serialization and deserialization in first place and let it throw NotSupportedException
on Write
(1)
the type checks in there are a bit weird as well imo, if u want to write it like this, each converter should have to deal only with itself, the condition seems to take care of more which has imo to much of an side effect here (2)
imo (1) would be the much cleaner way and it will also reduce the gc stressYes, using different options for the 2 is pretty fair, I haven't thought about that
I'll do that, thanks, it's essentially just one factory difference from reading
The type-checks were in for safety tbh, since I didn't know if the converters made by the factory get inserted. Prolly not but I was paranoid, I had to test on an env where I had almost no way of debugging (and could repro there only...)
Once I found the problem I left both in
i think there should be even an reference equality check usable
Likely yeah, I really don't know how factories behave in this case, I'll do some experimentation. But honestly, separating options just solves my problems as a whole
it really depends on the data you're working with, but breaking up the converter into ones that have less responsibility may be an option.
instead of a converter on the entire record
do u know what takes precedence here if u would have conflicting info on
JsonSerializerOptions
and the annotated version?hmm, conflicting?
do you mean 2 different converters for the same type?
what an inconsistent precedence
(just saw if u scroll down a bit there, they even use
JsonSerializer.Serialize()
inside the Write()
method theirself btw)Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.