Can I make a strict deserialization with Newtonsoft.Json?

后端 未结 2 669
滥情空心
滥情空心 2021-01-18 05:28

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 construct

2条回答
  •  离开以前
    2021-01-18 05:34

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

提交回复
热议问题