Json validation using the schema

Oct 15, 2009 at 6:36 AM
Edited Oct 15, 2009 at 6:38 AM

1.   Class definition

 public class A
    {
        public Boolean isThere=true;
    }

 

2. Schema for this class is generated using

Type type = Type.GetType("A");

Newtonsoft.Json.Schema.JsonSchemaGenerator jsonSchemaGenerator = new Newtonsoft.Json.Schema.JsonSchemaGenerator();
jsonSchemaGenerator.UndefinedSchemaIdHandling = Newtonsoft.Json.Schema.UndefinedSchemaIdHandling.UseTypeName;
Newtonsoft.Json.Schema.JsonSchema jsonSchema = jsonSchemaGenerator.Generate(type);

using (StreamWriter wr = new StreamWriter("SchemaA.json"))
{
  using (JsonWriter writer = new JsonTextWriter(wr))
  {
    writer.Formatting = Formatting.Indented;

    jsonSchema.WriteTo(writer);
  }
}

3. The contents of SchemaA.json turns out to be

{
  "id": "TemplateGenerator.A",
  "type": "object",
  "properties": {
    "isThere": {
      "type": "boolean"
    }
  }
}

4. I have created a instance of this schema as follows
 
{
   "isThere" : "false"
}

5. I an now validating the above json in step 4 with the schema shown in 3 using following code
using (StreamReader sr = new StreamReader("SchemaA.json"))
 {
  using (JsonReader sreader = new JsonTextReader(sr))
  {
   Newtonsoft.Json.Schema.JsonSchema jsonSchema = Newtonsoft.Json.Schema.JsonSchema.Read(sreader);
   using (StreamReader r = new StreamReader("DocA.json"))
    {
      using (JsonReader jr = new JsonTextReader(r))
      {
         using (JsonValidatingReader reader = new JsonValidatingReader(jr))
         {
           reader.Schema = jsonSchema;
           reader.ValidationEventHandler += new Newtonsoft.Json.Schema.ValidationEventHandler(
               delegate(object sender, Newtonsoft.Json.Schema.ValidationEventArgs aargs)
               {
                 System.Console.Write(sender.ToString() + " " + aargs.Message + "\n");
                });

                Newtonsoft.Json.Linq.JObject jo = Newtonsoft.Json.Linq.JObject.Load(reader);
                
            }
          }
        }
     }
 }

6. I am getting following error.

Newtonsoft.Json.JsonValidatingReader Invalid type. Expected Boolean but got String. Line 2, position 19.
7. Is this the correct way to use? I expected it to automatically convert the string "true", "false", "True", "False", 0, 1, "0", "1" to appropriate boolean types. Am I doing some thing wrong?

 

Coordinator
Oct 18, 2009 at 2:44 AM

It expects this JSON

{
"isThere" : false
}

There shouldn't be any quotes around the boolean, otherwise it is a string.
Oct 23, 2009 at 10:07 AM

A
deserializedProduct = JsonConvert.DeserializeObject<A>(output);

The above method is able to convert the string "true" "false" to appropriate boolean type values.
But the method specified in previous post gives error.

Thank you by the way, your suggested way does work. But I was thinking that all the ways of deserialization would work similarly.
Coordinator
Oct 23, 2009 at 9:35 PM

The schema generator has no way of knowing at runtime whether the string property will have a valid boolean. The serializer is more forgiving and does its best. Using a schema is the way to be exact with exactly what you want.