C
C#10mo ago
Yelnats

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
Mc Samuel
Mc Samuel10mo ago
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.
Yelnats
YelnatsOP10mo ago
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
Mc Samuel
Mc Samuel10mo ago
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
Yelnats
YelnatsOP10mo ago
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
Mc Samuel
Mc Samuel10mo ago
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
Yelnats
YelnatsOP10mo ago
but there isn't a way to do it through like annotations or serealizer options?
Mc Samuel
Mc Samuel10mo ago
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
Mkp
Mkp10mo ago
What in the half ass design batman
Mc Samuel
Mc Samuel10mo ago
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
Mkp
Mkp10mo ago
All you need to do is make a JsonConverter<T> and override HandleNull to true. Then if null return default(t).
public class HexNumberConverter : JsonConverter<uint>
{
public override uint Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.String)
{
string? str = reader.GetString();
if (string.IsNullOrEmpty(str))
return 0u;
if (str.StartsWith("0x"))
str = str[2..];
if (!uint.TryParse(str, NumberStyles.HexNumber, null, out uint result))
throw new JsonException("Json hexstring can't be converted to a uint");
return result;
}

return reader.GetUInt32();
}

public override void Write(Utf8JsonWriter writer, uint value, JsonSerializerOptions options)
{
writer.WriteStringValue($"0x{value:X6}");
}
}
public class HexNumberConverter : JsonConverter<uint>
{
public override uint Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.String)
{
string? str = reader.GetString();
if (string.IsNullOrEmpty(str))
return 0u;
if (str.StartsWith("0x"))
str = str[2..];
if (!uint.TryParse(str, NumberStyles.HexNumber, null, out uint result))
throw new JsonException("Json hexstring can't be converted to a uint");
return result;
}

return reader.GetUInt32();
}

public override void Write(Utf8JsonWriter writer, uint value, JsonSerializerOptions options)
{
writer.WriteStringValue($"0x{value:X6}");
}
}
Here is an example of a hex converter I wrote
public override bool HandleNull { get; } = true;
public override bool HandleNull { get; } = true;
Yelnats
YelnatsOP10mo ago
oh perfect, thanks
Want results from more Discord servers?
Add your server