Reputation: 1333
I have a class Letter
:
public class Letter
{
public string p1 {get; set;}
public string p2 {get; set;}
public List<string> l1 {get; set;}
public List<string> l2 {get; set;}
}
And i have json file:
{
"A":
{
"p1": "",
"p2": "",
"l1": ["", "", ..., ""],
"l2": ["", "", ..., ""]
}
"B": {...}
...
}
For now i deserialize json with Json.NET
from NewtonSoft
like this:
var alphabet = JsonConvert.DeserializeObject<Dictionary<char, Letter>>(jsonString);
So i can refer to values like this: alphabet["A"]
Now i want to check, is jsonString
, which i want to deserialize is valid.
I found some examples how to read JsonSchema
from file, or how to generate it in code. But i can't find out, how generate shema for my example of json file.
Can anyone help me?
P.S.
For now i do it like this. Is this best way?
...
var schemaGenerator = new JSchemaGenerator();
var schemaForLetter = schemaGenerator.Generate(typeof (Letter));
var schema = new JSchema
{
Type = JSchemaType.Object,
Properties =
{
{ "A", schemaForLetter },
{ "B", schemaForLetter },
...
}
}
...
Upvotes: 0
Views: 1613
Reputation: 38046
You can generate schema from the type you're deserializing to. For example:
var schema = generator.Generate(typeof(Dictionary<string, Letter>))
Upvotes: 3