简体   繁体   中英

Validate object against a schema before serialization

I want to serialize a C# object as JSON into a stream, but to avoid the serialization if the object is not valid according to a schema. How should I proceed with this task using JSON.NET and Json.NET Schema? From what I see there is no method in the JSON.NET library which allows the validation of a C# object against a JSON schema. It seems somewhat weird that there is no direct method to just validate the C# object without encoding it. Do you have any idea why this method is not available?

It seems this API not currently available. At a guess, this might be because recursively generating the JSON values to validate involves most of the work of serializing the object. Or it could just be because no one at Newtonsoft ever designed, specified, implemented, tested, documented and shipped that feature .

If you want, you could file an enhancement request requesting this API, probably as a part of the SchemaExtensions class .

In the meantime, if you do need to test-validate a POCO without generating a complete serialization of it (because eg the result would be very large), you could grab NullJsonWriter from Reference to automatically created objects , wrap it in a JSchemaValidatingWriter and test-serialize your object as shown in Validate JSON with JSchemaValidatingWriter . NullJsonWriter doesn't actually write anything, and so using it eliminates the performance and memory overhead of generating a complete serialization (either as a string or as a JToken ).

First, add the following static method:

public static class JsonExtensions
{
    public static bool TestValidate<T>(T obj, JSchema schema, SchemaValidationEventHandler handler = null, JsonSerializerSettings settings = null)
    {
        using (var writer = new NullJsonWriter())
        using (var validatingWriter = new JSchemaValidatingWriter(writer) { Schema = schema })
        {
            int count = 0;
            if (handler != null)
                validatingWriter.ValidationEventHandler += handler;
            validatingWriter.ValidationEventHandler += (o, a) => count++;
            JsonSerializer.CreateDefault(settings).Serialize(validatingWriter, obj);
            return count == 0;
        }
    }
}

// Used to enable Json.NET to traverse an object hierarchy without actually writing any data.
class NullJsonWriter : JsonWriter
{
    public NullJsonWriter()
        : base()
    {
    }

    public override void Flush()
    {
        // Do nothing.
    }
}

Then use it like:

// Example adapted from 
// https://www.newtonsoft.com/jsonschema/help/html/JsonValidatingWriterAndSerializer.htm
// by James Newton-King

string schemaJson = @"{
   'description': 'A person',
   'type': 'object',
   'properties': {
     'name': {'type':'string'},
     'hobbies': {
       'type': 'array',
       'maxItems': 3,
       'items': {'type':'string'}
     }
  }
}";         
var schema = JSchema.Parse(schemaJson);

var person = new
{
    Name = "James",
    Hobbies = new [] { ".Net", "Blogging", "Reading", "XBox", "LOLCATS" },
};

var settings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() };
var isValid = JsonExtensions.TestValidate(person, schema, (o, a) => Console.WriteLine(a.Message), settings);
// Prints Array item count 5 exceeds maximum count of 3. Path 'hobbies'.

Console.WriteLine("isValid = {0}", isValid); 
// Prints isValid = False

Watch out for cases by the way. Json.NET schema is case sensitive so you will need to use an appropriate contract resolver when test-validating.

Sample fiddle .

You cannot do that from the JSON string, you need an object and a schema to compare with first..

public void Validate()
{
    //...
    JsonSchema schema = JsonSchema.Parse("{'pattern':'lol'}");
    JToken stringToken = JToken.FromObject("pie");
    stringToken.Validate(schema);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM