❔ ✅ JSON Serialization not working?
Can someone help me with this one, in the MS docs this seems to work just fine, what am i doing wrong..
18 Replies
What do you see which makes you think that isn't working?
{}
Ah, you're using fields rather than properties
is that the issue?
Yes
in that case, thanks a lot !
Most serializers will ignore fields, and just process properties. You should never have a public field, really
yeah it wasn't public at first, it thought it had something to do with it, so i tried everything
Yeah, it also needs to be public
the one from newtonsoft seems to work fine so that's why i was confused
Yeah, different defaults.
Looks like you can configure S.T.J to use fields: https://stackoverflow.com/a/58139922/1086121
Stack Overflow
How to use class fields with System.Text.Json.JsonSerializer?
I recently upgraded a solution to be all .NET Core 3 and I have a class that requires the class variables to be fields. This is a problem since the new System.Text.Json.JsonSerializer doesn't support
oh neat
thanks!
Looks like you can use
[JsonInclude]
to use properties with private setters, but doesn't seem to work on private properties
But, you should normally just use public properties in classes intended for serializationyeah, i'm quite satisfied with this result, at first i thought the dictionary i had included was being faulty, cus i heard some serializers don't like dictionaries much
but this one works nicely, + it's simple and can just be serialized directly to a string, exactly what i need
Sweet!
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.