在序列化之前根据模式验证对象

时间:2017-12-22 21:12:57

标签: c# .net json json.net jsonschema

我想将C#对象作为JSON序列化为流,但是如果对象根据模式无效则要避免序列化。我应该如何使用JSON.NET和Json.NET Schema继续执行此任务?根据我的看法,JSON.NET库中没有允许根据JSON模式验证C#对象的方法。似乎有点奇怪的是,没有直接方法来验证C#对象而不对其进行编码。你知道为什么这种方法不可用吗?

2 个答案:

答案 0 :(得分:3)

似乎此API目前无法使用。猜测,这可能是因为递归生成要验证的JSON值涉及序列化对象的大部分工作。或者可能只是因为Newtonsoft ever designed, specified, implemented, tested, documented and shipped that feature没有人。

如果您愿意,可以file an enhancement request申请此API,可能是SchemaExtensions class的一部分。

与此同时,如果您确实需要对POCO进行测试验证而不生成完整的序列化(因为结果会非常大),您可以从Reference to automatically created objects抓取NullJsonWriter,将其包装在JSchemaValidatingWriter中并测试序列化您的对象,如Validate JSON with JSchemaValidatingWriter所示。 NullJsonWriter实际上并没有写任何内容,因此使用它会消除生成完整序列化的性能和内存开销(stringJToken)。

首先,添加以下静态方法:

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

然后使用它:

// 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

顺便注意案例。 Json.NET架构为case sensitive,因此在测试验证时需要使用适当的合同解析器。

示例fiddle

答案 1 :(得分:0)

你不能从JSON字符串中做到这一点,你需要一个对象和一个模式来与第一个进行比较..

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