Replies: 4 comments
|
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue Detailspublic class Song {
public int Id {get; set;}
public string? Title {get; set;}
public string? Foo {get; set;}
public string? Bar {get; set;}
}
var song = JsonSerializer.Deserialize<Song>("""{"id": 1, "title": "Test", "foo": "a"}""");How to know
|
|
I thhink you have a few options. Just deserialize itDeserializing as you have in your code sample will result in Parse firstYou can parse the JSON text into I find JSON SchemaThis builds on the previous option since you still have to parse the JSON. Use something like JsonSchema.Net (disclaimer: it's mine) to define constraints that you want for the JSON with JSON Schema. Then you can evaluate the JSON with the schema, and you'll get output that indicates whether the JSON meets the required constraints. |
That's probably what I would do as well. There shouldn't be a compelling semantic difference between |
|
I have a custom type I've created called optional which lets me distinguish between null and not-set values. |
Uh oh!
There was an error while loading. Please reload this page.
How to know
songcontains onlyid,titleandfoo, notbar?All reactions