C#C
C#9mo ago
Core

✅ JsonSerializer - properties have null values

Hello,
For some reason the deserialization assigns
null
to every property, but the file is read, because there are exactly 3 entries in the list.
No exception is thrown.

c#
public static class FixtureLoader
{
    private static readonly JsonSerializerOptions SerializerOptions =
        new() { RespectRequiredConstructorParameters = true, };

    public async static Task<IEnumerable<T>> LoadAsync<T>(string fileName)
    {
        await using var stream = new FileStream(fileName, FileMode.Open);
        return await JsonSerializer.DeserializeAsync<IEnumerable<T>>(stream, SerializerOptions) ?? [];
    }
}


c#
public class DeviceFixture
{
    public string UserAgent { get; init; }

    public DeviceInfo Device { get; init; }

    public class DeviceInfo
    {
        public string Brand { get; init; }
        public string? Model { get; init; }
    }
}


[
  {
    "userAgent": "Mozilla/5.0 (Linux; U; Android 4.0; de-DE; EK-GC100 Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30",
    "device": {
      "brand": "Samsung",
      "model": "Galaxy Camera"
    }
  },
  {
    "userAgent": "Mozilla/5.0 (Linux; Android 4.1.2; EK-GC100 Build/JZO54K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.63 Mobile Safari/537.36 OPR/15.0.1162.60140",
    "device": {
      "brand": "Samsung",
      "model": "Galaxy Camera"
    }
  },
  {
    "userAgent": "Mozilla/5.0 (Linux; U; Android 2.3.3; ja-jp; COOLPIX S800c Build/CP01_WW) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1",
    "device": {
      "brand": "Nikon",
      "model": "Coolpix S800c"
    }
  }
]
Was this page helpful?