JSON使大小写不敏感 [英] JSON make case insensitive

查看:841
本文介绍了JSON使大小写不敏感的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从C#模型生成模式,到目前为止,以下各项进展顺利:

I'm trying to generate a schema from my C# models, and so far things are going well with the following:

JSchemaGenerator generator = new JSchemaGenerator();

JSchema schema = generator.Generate(typeof(MyClass));

schemachema.AllowAdditionalProperties = false;
schemachema.UniqueItems = false;

JObject update = JObject.Parse(@"{MYJSON}");

IList<string> messages;
bool IsValid = update.IsValid(clientSchema, out messages);

我无法弄清的一件事是如何使其不区分大小写.似乎应该自动执行此操作(首先区分大小写,然后不区分大小写),但对我而言却不是.

The one thing I have not been able to figure out is how to make it case insensitive. It seems it is supposed to do this automatically (case sensitive first, then case insensitive) but for me it does not.

关于我所缺少的东西有什么想法吗?

Any ideas on what I am missing?

推荐答案

不幸的是,JObject.Parse不允许您进行任何更改.

Unfortunately the JObject.Parse doesn't let do you change anything.

首先,设置架构. JSchema创建一个内部属性字典.

First, you set the schema. The JSchema creates an internal dictionary of properties.

JSchema schema = generator.Generate(typeof(MyClass));    
schema.AllowAdditionalProperties = false;
schema.UniqueItems = false;

我添加了自己的表示形式只是为了测试它并执行类反序列化.

I've added my own representation just to test it and executes the class deserialization.

string MyJson = "{\"PROPERTYONE\":\"Data\", \"PropertyTwo\":10}";

该类是故意不符合此定义的

The class is on purpose not matching this definition

public class MyClass
{
    public string PropertyOne { get; set; }
    public int PropertyTwo { get; set; }
}

在内部创建一个阅读器,传递您的字符串,并从您的定义中获取验证模式阅读器:

Internally a reader is created, your string is passed and the validating schema reader is obtained from your definition:

JsonTextReader reader = new JsonTextReader(new StringReader(MyJson));
JSchemaValidatingReader validatingReader = new JSchemaValidatingReader(reader);
validatingReader.Schema = JSchema.Parse(schema.ToString());

我已经手动创建了消息,这些消息与触发验证阅读器中每个验证的事件挂钩,因此您会收到验证消息:

I've create the messages manually hooking into the event that triggers every validation in the validating reader so you get your validation messages:

IList<string> messages = new List<string>();
validatingReader.ValidationEventHandler += (o, a) => messages.Add(a.Message);

如果您使用序列化程序反序列化您的类,则它可以工作并填充该类,因为反序列化并不关心大小写,但是您的验证阅读器将冒泡验证失败

If you Use a serializer to deserialize your class it works and populates the class because the deserialization doesn't care about the casing, but your validating reader is going to bubble up validation failures

JsonSerializer serializer = new JsonSerializer();
MyClass p = serializer.Deserialize<MyClass>(validatingReader);

在验证过程中,您的验证在以下部分中失败:

Inside of the validation process your validation fails in the following part:

private bool IsPropertyDefinied(JSchema schema, string propertyName)
{
    if (schema._properties != null && schema._properties.ContainsKey(propertyName))
    {
        return true;
    }
...

将属性名称与字典进行比较.这本字典是常规字典,没有使用InvariantCultureIgnoreCase属性,因此无法正确比较(根据您的要求) 唯一的方法是获取GitHub代码并更新它以支持您的功能,直到那时您都无法获得所需的东西.

The property name is compared to the dictionary. This dictionary is a regular dictionary and it's not using the property InvariantCultureIgnoreCase which would make it compare correctly (as per requested by you) The only way would be to get the GitHub code and update it to support your feature, until then it's not possible to obtain what you need.

这篇关于JSON使大小写不敏感的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆