How can I convert null entries in JSON data to a default value?
I have JSON data that contains null entries for some values. It would make the business code a lot easier if all the data that comes out of deserialization is uniform. For example, the property "strength" could be null, but I would like it to be deserialized to 0. It would also be ideal if I could do something similar so that I construct null members by default, for example a "friend = null" property would instead do "new Friend()". I looked into
JsonSerializerOptions.DefaultIgnoreCondition
but that seems to only affect serialization, and not deserialization. I could transform all the data from a deserialized to a business ready form, but that seems like extra work if I can just get deserialization right and not have to double the class count.11 Replies
If you want it to be a non-null vallue when displaying you can use the null check i think.. Check this [link from Mozilla] (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing)
MDN Web Docs
Nullish coalescing operator (??) - JavaScript | MDN
The nullish coalescing (??) operator is a logical
operator that returns its right-hand side operand when its left-hand side operand is
null or undefined, and otherwise returns its left-hand side
operand.
right, but I would rather not infect the type of the data to be nullable. I'd rather the type be int instead of int? and have to check for null everytime I use it
Im assuming you want to null check when displaying data then keep the json data as is without affecting it, since you might want to use it somewhere else or just to keep it's original integrity
Do you want to edit the data or you want to display it, then use defaults when passing through null values
I want to edit the data
the data comes from a 3rd party source and the defaults I want to set are good enough the rest of the logic
Ooh, alright alright... Then i guess you can use the null check when inserting into the data.
Ooh i see,... So i guess u can, get the data, edit it using the null check then use it as you want
but there isn't a way to do it through like annotations or serealizer options?
But since editing the whole data might take a longer time if you only use a portion of it in practice ... Then i guess you can keep a "defaults object" to use when it's time to render the object in need so your code ends up checking the nulls then replacing from the "defaults object" properties
Mmmmmmm.... I'm honestly not well vexed in those unfortunately...
I just do simple stuff rn..... Maybe you can check on stack overflow or something like that..
Or pass it to gpt then kinda see if it's regurgitated poop is useful or just straight up garbage
What in the half ass design batman
I haven't used annotations in JS yet... And for serialization are you using another library which isn't the JSON library or maybe the app requires a mandatory serialization to another format of sort
Or you get the info,.. Replace the null values then serialize it to your format of choice or back to JSON, since there is a need to replace the null values either way
All you need to do is make a JsonConverter<T> and override HandleNull to true. Then if null return default(t).
Here is an example of a hex converter I wrote
oh perfect, thanks