Json.Convert return 创建对象的默认值

Json.Convert return default values of an object created

假设我有一个名为 Cat 的简单 class,我想序列化和反序列化这个 class

猫Class:

 public class Cat {
   public Cat () : base (Type.Cat)
     { 
       this.Origin = "American";
       this.Color = "Black";
     }
    [JsonConstructor]
    public Cat (bool Serialize) : base (Type.Cat) // base is Animal Class
     { 
      
     }
 }

动物Class:

public class Animal
     public abstract class Animal {
       public string Color {
         get;
         set;
       }
       public string Origin {
         get;
         set;
       }
        public Animal(Type AnimalType)
         { 
            
         }
     }

序列化方法:

   public void SerializeObj(Cat cat)
    {
        var mysettings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto, 
        Formatting = Newtonsoft.Json.Formatting.Indented };
    
                var json = JsonConvert.SerializeObject(cat, typeof(Cat), mysettings );
    
                using (StreamWriter stream = new StreamWriter(path))
                {
                    stream.WriteLine(json);
                    stream.Close();
                }
    
    }

反序列化方法:

 public object Deserialize(string object)
    {
        var settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto };

        var cat = JsonConvert.DeserializeObject<Cat>(object, settings);

        return motor;

    }

调用了两种方法:

Cat cat = new Cat();
cat.Color = "White";
cat.Origin = "Europe";
var fileString = Serialize(cat);

// file created with fileString

Cat cat = Deserialize(myfile);
Console.WriteLine(cat.Origin); // it will print American
Console.WriteLine(cat.Color); // it will print black

问题是反序列化产生的是默认值,而不是序列化时修改的值

请注意,我的文件包含更新后的值(Color = "White" 和 Origin = "Europe")

我该如何解决这个问题?

修复后我测试了你的代码,因为你发布的代码无法编译。 一切正常

void Main()
{
    Cat cat = new Cat();
    cat.Color = "White";
    cat.Origin = "Europe";
    var json = Serialize(cat);

     cat = Deserialize<Cat>(json);
    Console.WriteLine(cat.Origin); // it  printed Europe
    Console.WriteLine(cat.Color); // it printed White
}

方法

public string Serialize<T>(T obj)
{
    var mysettings = new JsonSerializerSettings
    {
        TypeNameHandling = TypeNameHandling.Auto,
        Formatting = Newtonsoft.Json.Formatting.Indented
    };

    return JsonConvert.SerializeObject(obj, mysettings);
}


 public T Deserialize<T>(string obj)
{
    var settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto };

    return JsonConvert.DeserializeObject<T>(obj, settings);
}

public class Cat : Animal
{
    public Cat () : base (AnimalType.Cat)
    {
        this.Origin = "American";
        this.Color = "Black";
    }
    [JsonConstructor]
    public Cat(bool Serialize) : base (AnimalType.Cat) {}
}

     public abstract class Animal
    {
        public string Color
        {
            get;
            set;
        }
        public string Origin
        {
            get;
            set;
        }
        public Animal(AnimalType anymalType)
        {

        }
    }

public enum AnimalType
{
    Cat
}