反序列化yaml文件时出错

时间:2017-11-28 10:18:50

标签: c# serialization deserialization yaml yamldotnet

序列化没有问题但反序列化抛出错误如下所示。我分享了所有相关的类和错误。我该怎么做才能反序化这个文件。

yaml文件(由yaml序列化创建):

  Edits:   0:
        Id: 20
        Type: AddOrUpdate
        Operation:
          book:
            ElementName: a
            ArithmeticExpression: asd
            ConditionExpression: a

c#c​​lasses:

 public class DbContext
        {
            public Dictionary<int, DbModel> Edits { get; set; }
        }



      public class DbModel
        {
            public int Id { get; set; }
            public string Type { get; set; }
            public Dictionary<string, Operation> Operation { get; set; }


            public DbModel(int id, string type, Dictionary<string, Operation> op)
            {
                Id = id;
                Type = type;
                Operation = op;
            }
        }
    public class Operation
    {
        public string ElementName { get; set; }
        public string ArithmeticExpression { get; set; }
        public string ConditionExpression { get; set; }

        public Operation(string elemName, string ArithmetticExp, string ConditionExp)
        {
            ElementName = elemName;
            ArithmeticExpression = ArithmetticExp;
            ConditionExpression = ConditionExp;
        }

    }

class DatabaseOperation
{
    public static DbContext DbContext { get; set; }
    public const string yamlEditString = "C:\\Data\\setting.yml";

    static DatabaseOperation()
    {
        DbContext = ReadAllDataFromFile().Result;
    }

    public static Task<DbContext> ReadAllDataFromFile()
    {
        DbContext dbCon = new DbContext();
        if (File.Exists(yamlEditString))
        {

            var deserializer = new Deserializer();

            string readedData = File.ReadAllText(yamlEditString);
            //dbCon.Edits = deserializer.Deserialize<List<DbModel>>(readedData);
            try
            {
                dbCon = deserializer.Deserialize<DbContext>(readedData);
            }
            catch (Exception ex)
            {

                throw;
            }

        }
        else
        {
            dbCon.Edits = new Dictionary<int, DbModel>();
        }
        return Task.FromResult(dbCon);
    }
    public static Task WriteAllDataToFile(DbContext dbCon)
    {
        var serializer = new Serializer();
        File.WriteAllText(yamlEditString, serializer.Serialize(dbCon));
    }
}
  

收到此错误:   (行:7,Col:9,Idx:86) - (行:7,Col:9,Idx:86):反序列化期间的例外

&#34; YamlDotNet&#34;

内部异常:

  

无法创建类型&#39; YamlEdit.Operation&#39;的类型的实例。

     

在   YamlDotNet.Serialization.ObjectFactories.DefaultObjectFactory.Create(类型   输入)   YamlDotNet.Serialization.NodeDeserializers.ObjectNodeDeserializer.YamlDotNet.Serialization.INodeDeserializer.Deserialize(IParser   解析器,类型expectedType,Func`3 nestedObjectDeserializer,Object&amp;   价值)   YamlDotNet.Serialization.ValueDeserializers.NodeValueDeserializer.DeserializeValue(IParser   解析器,类型expectedType,SerializerState状态,IValueDeserializer   nestedObjectDeserializer)

StackTrace:

   at YamlDotNet.Serialization.ValueDeserializers.NodeValueDeserializer.DeserializeValue(IParser parser, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer)
   at YamlDotNet.Serialization.ValueDeserializers.AliasValueDeserializer.DeserializeValue(IParser parser, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer)
   at YamlDotNet.Serialization.ValueDeserializers.NodeValueDeserializer.<>c__DisplayClass3_0.<DeserializeValue>b__0(IParser r, Type t)
   at YamlDotNet.Serialization.NodeDeserializers.DictionaryNodeDeserializer.DeserializeHelper(Type tKey, Type tValue, IParser parser, Func`3 nestedObjectDeserializer, IDictionary result)
   at YamlDotNet.Serialization.NodeDeserializers.DictionaryNodeDeserializer.YamlDotNet.Serialization.INodeDeserializer.Deserialize(IParser parser, Type expectedType, Func`3 nestedObjectDeserializer, Object& value)
   at YamlDotNet.Serialization.ValueDeserializers.NodeValueDeserializer.DeserializeValue(IParser parser, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer)
   at YamlDotNet.Serialization.ValueDeserializers.AliasValueDeserializer.DeserializeValue(IParser parser, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer)
   at YamlDotNet.Serialization.ValueDeserializers.NodeValueDeserializer.<>c__DisplayClass3_0.<DeserializeValue>b__0(IParser r, Type t)
   at YamlDotNet.Serialization.NodeDeserializers.ObjectNodeDeserializer.YamlDotNet.Serialization.INodeDeserializer.Deserialize(IParser parser, Type expectedType, Func`3 nestedObjectDeserializer, Object& value)
   at YamlDotNet.Serialization.ValueDeserializers.NodeValueDeserializer.DeserializeValue(IParser parser, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer)
   at YamlDotNet.Serialization.ValueDeserializers.AliasValueDeserializer.DeserializeValue(IParser parser, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer)
   at YamlDotNet.Serialization.Deserializer.Deserialize(IParser parser, Type type)
   at YamlDotNet.Serialization.Deserializer.Deserialize[T](String input)
   at YamlEdit.DatabaseOperation.ReadAllDataFromFile() in D:\Repositories\YamlEdit\DatabaseOperation.cs:line 35

1 个答案:

答案 0 :(得分:0)

您需要向YamlEdit.Operation添加无参数构造函数。 YamlDotNet不知道如何传递构造函数参数。