C
C#16mo ago
MaeFire

❔ JSON serialization nullable warnings (best approach?)

Kind of have a "best approach" question for the following:
using System.Text.Json;

class FileVerification
{
private readonly string quackenFilePath = @"C:\Users\maske\Documents\dev\CSharp\TelegramBots\quacken.json";

private readonly JsonSerializerOptions _options = new()
{
PropertyNameCaseInsensitive = true
};
public List<QuackenJson> JsonReadWithSystemText()
{
using FileStream json = File.OpenRead(quackenFilePath);
List<QuackenJson> token = JsonSerializer.Deserialize<List<QuackenJson>>(json, _options)!;
return token;
}
}
using System.Text.Json;

class FileVerification
{
private readonly string quackenFilePath = @"C:\Users\maske\Documents\dev\CSharp\TelegramBots\quacken.json";

private readonly JsonSerializerOptions _options = new()
{
PropertyNameCaseInsensitive = true
};
public List<QuackenJson> JsonReadWithSystemText()
{
using FileStream json = File.OpenRead(quackenFilePath);
List<QuackenJson> token = JsonSerializer.Deserialize<List<QuackenJson>>(json, _options)!;
return token;
}
}
Would you make it nullable using ? or ?? in the appropriate spots, or (in this particular case) since I know the value will never be null, would you do as I have and use ! to suppress the warning?
7 Replies
ero
ero16mo ago
It would only ever return null if the contents of the quacken.json file were literally just
null
null
If you know that's never the case, I'd suppress it with ! A more proper way would likely be ?? new() But that crosses the territory of inappropriately handling null inputs and hiding said handling away from whoever uses it The most proper way would obviously be to make the return type nullable But now you have some options and some whoopie
MaeFire
MaeFireOP16mo ago
So the two good options of these three would be either what I did above or
public List<QuackenJson>? JsonReadWithSystemText()
{
using FileStream json = File.OpenRead(quackenFilePath);
List<QuackenJson>? token = JsonSerializer.Deserialize<List<QuackenJson>>(json, _options);
return token;
}
public List<QuackenJson>? JsonReadWithSystemText()
{
using FileStream json = File.OpenRead(quackenFilePath);
List<QuackenJson>? token = JsonSerializer.Deserialize<List<QuackenJson>>(json, _options);
return token;
}
I think this is what you meant by making the return type nullable, right?
ero
ero16mo ago
Mhm
MaeFire
MaeFireOP16mo ago
Awesome. Thank you for the insight. I was not aware of the ?? new() hiding the handling away, so I'll just try to keep in mind to either suppress or make it into a nullable type(preferably the latter) ⭐
JakenVeina
JakenVeina16mo ago
if you really don't like Deserialize<>()! you can always do Deserialize<>() ?? throw new InvalidOperationException() one could argue that's the most "proper", even if it's an exception that should never occur as it fails early
ero
ero16mo ago
I would perhaps choose FormatException
Accord
Accord16mo ago
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.
Want results from more Discord servers?
Add your server