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
|
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
That's probably what I would do as well. There shouldn't be a compelling semantic difference between |
Beta Was this translation helpful? Give feedback.
-
I have a custom type I've created called optional which lets me distinguish between null and not-set values. |
Beta Was this translation helpful? Give feedback.
-
How to know
song
contains onlyid
,title
andfoo
, notbar
?Beta Was this translation helpful? Give feedback.
All reactions