C
C#2w 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 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; }
}
}
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"
}
}
]
[
{
"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"
}
}
]
2 Replies
ero
ero2w ago
I mean it's just because the names don't match right You need to either set the naming policy (camelCase) for the deserialization in the JsonSerializerOptions or use JsonPropertyName on the properties
Core
CoreOP2w ago
Wow, I have completely missed that. I thought it uses camelCase by default... Thank you

Did you find this page helpful?