C#C
C#3y ago
Maeve

❔ 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;
    }
}

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?
Was this page helpful?