In YamlDotNet, how can I deserialize a getter-only property?

天涯浪子 提交于 2020-02-06 08:49:55

问题


I would like to serialize/deserialize objects that have readonly private fields with public getter-only properties, whose readonly fields are set in the objects' constructor. However, the following code fails in unit testing:

using System.ComponentModel;
using FluentAssertions;
using Xunit;
using YamlDotNet.Serialization;

namespace Utilities.ComputerInventory {

    public class CPU {
        readonly CPUMaker cPUMaker;
        public CPU() : this(CPUMaker.Generic) { }
        public CPU(CPUMaker cPUMaker) {
            this.cPUMaker = cPUMaker;
        }
        public CPUMaker CPUMaker => cPUMaker;
    }

    public enum CPUMaker {
        [Description("Generic")]
        Generic,
        [Description("Intel")]
        Intel,
        [Description("AMD")]
        AMD
    }
}

namespace Utilities.ComputerInventory.UnitTests {

    public class ComputerInventoryUnitTests001 {

        [Fact]
        public void CPUDeserializeFromYAMLSO() {
            Serializer serializer = new SerializerBuilder().Build();
            Deserializer deserializer = new DeserializerBuilder().Build();
            CPU cPU = new CPU(CPUMaker.Intel);
            CPU cPUAfterRoundTrip = deserializer.Deserialize<CPU>(serializer.Serialize(cPU));
            cPUAfterRoundTrip.Should().Be(cPU);
        }
    }
}

This fails with the message "Test Failed"

Message: YamlDotNet.Core.YamlException : (Line: 1, Col: 1, Idx: 0) - (Line: 1, Col: 1, Idx: 0): Exception during deserialization
---- System.Runtime.Serialization.SerializationException : Property 'CPUMaker' not found on type 'Utilities.ComputerInventory.CPU'.

And stack trace showing

Test Name:  CPUDeserializeFromYAMLSO
Test FullName:  Utilities.ComputerInventory.UnitTests.ComputerInventoryUnitTests001.CPUDeserializeFromYAMLSO
Test Source:    <obfuscated>\test1.cs
Test Outcome:   Failed
Test Duration:  0:00:00.11

Result 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.Deserializer.Deserialize(IParser parser, Type type)
   at YamlDotNet.Serialization.Deserializer.Deserialize(TextReader input, Type type)
   at YamlDotNet.Serialization.Deserializer.Deserialize[T](String input)
   at Utilities.ComputerInventory.UnitTests.ComputerInventoryUnitTests001.CPUDeserializeFromYAMLSO() in <obfuscated>\test1.cs:line 43
----- Inner Stack Trace -----
   at YamlDotNet.Serialization.TypeInspectors.TypeInspectorSkeleton.GetProperty(Type type, Object container, String name, Boolean ignoreUnmatched)
   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)
Result Message: 
YamlDotNet.Core.YamlException : (Line: 1, Col: 1, Idx: 0) - (Line: 1, Col: 1, Idx: 0): Exception during deserialization
---- System.Runtime.Serialization.SerializationException : Property 'CPUMaker' not found on type 'Utilities.ComputerInventory.CPU'.

Simply serializing the cPU instance of the CPU object produces YAML that looks like this: "CPUMaker: Intel\r\n"

There appears to be a similar SO question (YamlDotNet can not find property), asked 12/23/16 (13 months ago), but no one has answered or commented.

Summary: Using YamlDotNet, what is the proper way to serialize/deserialize objects with getter-only properties backed by a readonly field?

TIA for any help!

来源:https://stackoverflow.com/questions/48677081/in-yamldotnet-how-can-i-deserialize-a-getter-only-property

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