Is it possible to find unmapped properties with the System.Text.Json.JsonSerializer
?
I'm accessing an API which returns an array of documents. I want to know if there is a way to know if there is an property in the json document which is not mapped by my C# type. At best a method that returns a list of unmapped properties.
Example
JSON Document
{
"docs": [
{
"foo": "a",
"bar": "b",
"baz": "c",
}
]
}
C# types
public class Wrapper
{
[JsonPropertyName("docs")]
public List<MyDocument> Documents { get; set; }
}
public class MyDocument
{
[JsonPropertyName("foo")]
public string Foo { get; set; }
[JsonPropertyName("baz")]
public string Baz { get; set; }
}
Parser
using System.Text.Json;
var body = "{ ... }";
var documents = JsonSerializer.Deserialize<Documents>(body);
List<JsonElement> unmappedProperties
= JsonSerializer.FindUnmappedProperties<Document>(body);