© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•11mo ago•
4 replies
Core

✅ JsonSerializer - properties have null values

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

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) ?? [];
    }
}
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) ?? [];
    }
}


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; }
    }
}
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"
    }
  }
]
[
  {
    "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"
    }
  }
]
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ Change Null Values
C#CC# / help
3y ago
JsonSerializer escaping '&'
C#CC# / help
2y ago
Unit Testing, AcionResult Metadata Properties and JsonSerializer.Deserialize [Answered]
C#CC# / help
4y ago
Anomaly with JsonSerializer
C#CC# / help
16mo ago