C
C#5w ago
Core

✅ JSON - can't deserialize nullable property if it it's not present

Hello, Is if a nullable property is not present in the JSON the deserialization will fail. Is there a way to make deserialization work if a property is not present?
c#
return new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
};
c#
return new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
};
JSON deserialization for type 'UaDetector.Regexes.Models.Bot' was missing required properties including: 'producer'
JSON deserialization for type 'UaDetector.Regexes.Models.Bot' was missing required properties including: 'producer'
6 Replies
Jimmacle
Jimmacle5w ago
can you share the definition of the UaDetector.Regexes.Models.Bot class?
Core
CoreOP5w ago
Oops
c#
internal sealed class Bot
{
public required Regex Regex { get; init; }
public required string Name { get; init; }
public required BotCategory? Category { get; init; }
public required string? Url { get; init; }
public required BotProducer? Producer { get; init; }
}
c#
internal sealed class Bot
{
public required Regex Regex { get; init; }
public required string Name { get; init; }
public required BotCategory? Category { get; init; }
public required string? Url { get; init; }
public required BotProducer? Producer { get; init; }
}
sibber
sibber5w ago
the default behavior is to ignore missing properties dont mark it required
Core
CoreOP5w ago
oh :c damn I'm dumb
sibber
sibber5w ago
it happens
Core
CoreOP5w ago
Thank you!

Did you find this page helpful?