JSON make case insensitive

假如想象 提交于 2020-01-04 05:25:14

问题


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?


回答1:


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

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

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.



来源:https://stackoverflow.com/questions/43188628/json-make-case-insensitive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!