JsonSerializerOptions Ignore when null
Hey guys, I'm writing on an API and we have some values that can be null. I'm looking to hide those out if they are null. What I was doing so far was to use at property level the following:
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
As far as I've seen in stackoverflow and somewhere else is that I could have something like this for applying that to all of the properties:
This so far is not hiding the null values for all of the properties
This is an example of the response object on the api:
17 Replies
Hm, need more details. This works fine for me.
What .NET version are you using?
program.cs
controller
this correctly returns for me
this is using .net 8, for the record
I'm using .net 6.0
ok I'll try downgrading
works fine 🙂
could you perhaps make a minimal reproduction of your codebase with the error in it?
Yeah, give me a sec
Let me know if anything
well your props are not maked as nullable
string city
is not the same as string? city
but strings can be null?, shouldn't those be skipped?
wdym?
That I would assume that, all fields with null, should be hidden
Let me try with the
string?
instead
Still showing with string?
right. but if null is an acceptable value for the prop, it should be
string?
however, I specifically asked for a minimal reproduction, which isnt a video of the error, its a runnable example of the problem with the smallest possible amount of code where it still shows
a github link to the original project would also be goodhmm I don't think I can send that
I'll try to make a small example trying to reproduce my issue
I cannot share this codebase
great
hmm, trying to replicate it with the weather API works just fine also
My code then has to be doing something elsewhere
overriding the formatter anywhere? using newtonsoft json instead of STJ somewhere?
As far as I know we're using the STJ instead, but I could be wrong, I'm not the main codeowner for this
It's curious that we have this on some properties
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
and with that they are working
But not with the default oneyeah thats curious.