我可以用Newtonsoft.Json严格的反序列化? [英] Can I make a strict deserialization with Newtonsoft.Json?

查看:195
本文介绍了我可以用Newtonsoft.Json严格的反序列化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Newtonsoft.Json序列化/反序列化对象。

据我知道一个反序列化是不能成功的,如果类没有参数的构造函数。例如,

 公共类犬
{
公共字符串名称;

公狗(字符串n)
{
名称= N;
}
}

有关这个类下面的代码正确生成的对象。

 狗DOG1 = Newtonsoft.Json.JsonConvert.DeserializeObject<狗和GT({\Name\:\ Dog1\}); 



对于我来说,令人惊讶的是正确下面的代码还生成的对象。

 狗DOG2 = Newtonsoft.Json.JsonConvert.DeserializeObject<狗和GT({\name\:\Dog2\} ); 
狗dog3 = Newtonsoft.Json.JsonConvert.DeserializeObject<狗和GT({\n\:\Dog3\});
狗dog4 = Newtonsoft.Json.JsonConvert.DeserializeObject<狗和GT({\N\:\Dog4\});

现在所有我能想到的就是




  1. 的Json转换器被忽略大小写,而这样做的反思。

  2. 此外,如果它面临它填补使用JSON字符串参数(如同参数名称是JSON构造串)。我不知道,但也许这就是他们称这种灵活的原因。



下面我的问题是:



如果我的课是这样的

 公共类犬
{
公共字符串名称;

公狗(字符串名称)
{
名称=名称+AAA;
}
}

和生成的对象与

 狗DOG1 = Newtonsoft.Json.JsonConvert.DeserializeObject<狗和GT({\Name\:\Dog1\} ); 



然后创建的对象给了我 dog1.Name =Dog1aaa,而不是 dog1.Name =DOG1。我怎样才能正确地反序列化对象(也许覆盖名称创建对象)?是否有严格的反序列化的方法?



在此先感谢


解决方案

我怎样才能正确地反序列化对象(可能重写名称创建对象)?是否有严格的反序列化的方法?




您可以声明另一个构造和力Json.Net使用它

 公共类犬
{
公共字符串名称;

[JsonConstructor]
公狗()
{

}

公狗(字符串名称)
{
名称=名称+AAA;
}
}


I am using Newtonsoft.Json to serialize/deserialize objects.
As far as I know a deserialization can not be successful if the class does not have parameterless constructor. Example,

public class Dog
{
    public string Name;

    public Dog(string n)
    {
        Name = n;
    }
}

For this class below code generates the object correctly.

Dog dog1 = Newtonsoft.Json.JsonConvert.DeserializeObject<Dog>("{\"Name\":\"Dog1\"}");

For me, surprisingly it generates the object correctly with below codes also.

Dog dog2 = Newtonsoft.Json.JsonConvert.DeserializeObject<Dog>("{\"name\":\"Dog2\"}");
Dog dog3 = Newtonsoft.Json.JsonConvert.DeserializeObject<Dog>("{\"n\":\"Dog3\"}");
Dog dog4 = Newtonsoft.Json.JsonConvert.DeserializeObject<Dog>("{\"N\":\"Dog4\"}");

Now all I can think is

  1. Json converter is ignoring case-sensitivity while doing reflection.
  2. Moreover if it faces a constructor it fills parameters with json string(as if the parameter names are in json string). I am not sure, but maybe this is the reason they call this flexible.

Here comes my question:

If my class is something like this,

public class Dog
{
    public string Name;

    public Dog(string name)
    {
        Name = name + "aaa";
    }
}

and generating object with

Dog dog1 = Newtonsoft.Json.JsonConvert.DeserializeObject<Dog>("{\"Name\":\"Dog1\"}");

then created object gives me dog1.Name = "Dog1aaa" instead of dog1.Name = "Dog1". How can I deserialize the object correctly(maybe overriding Name after creating the object)? Is there a way for strict deserialization?

Thanks in advance

解决方案

How can I deserialize the object correctly(maybe overriding Name after creating the object)? Is there a way for strict deserialization?

You can declare another constructor and force Json.Net to use it

public class Dog
{
    public string Name;

    [JsonConstructor]
    public Dog()
    {

    }

    public Dog(string name)
    {
        Name = name + "aaa";
    }
}

这篇关于我可以用Newtonsoft.Json严格的反序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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