3

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!

  • here is the thing.. I am all for new and great ways to do things, but sometimes they create more problems then they are worth and are not for the faint heated. just saying... this is a very rare tag on stackoverflow, all i can do is wish you luck – TheGeneral Feb 08 '18 at 03:45
  • If the code is yours to change, you can probably avoid the issue by making the backing field read/write, and adding a private setter. – dumetrulo Feb 09 '18 at 08:23

0 Answers0