gek0n gek0n - 1 year ago 170
JSON Question

Json.NET create schema for dictionary

I have a class


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:

"p1": "",
"p2": "",
"l1": ["", "", ..., ""],
"l2": ["", "", ..., ""]
"B": {...}

For now i deserialize json with
like this:

var alphabet = JsonConvert.DeserializeObject<Dictionary<char, Letter>>(jsonString);

So i can refer to values like this:

Now i want to check, is
, which i want to deserialize is valid.

I found some examples how to read
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?


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 },

Answer Source

You can generate schema from the type you're deserializing to. For example:

var schema = generator.Generate(typeof(Dictionary<string, Letter>))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download