Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Classes extending IReadOnlyDictionary are not serialized as a yaml mapping #606

Open
sfwester opened this issue Apr 19, 2021 · 0 comments
Open
Labels

Comments

@sfwester
Copy link

We are using a type that extends IReadOnlyDictionary and we would like to serialize that type. However, that type is serialized as a collection of key value pairs and not as an object.

I tried seeing about implementing an IYamlConverter for ExtendedReadOnlyDictionary but it seemed like I would have to re-implement the dictionary conversion.

Is there an extension point in the library that can solve this for me or is an update to the library required?

Example:
The test below returns the following output:

Assert.AreEqual failed. Expected:<foo: bar
>. Actual:<- Key: foo
  Value: bar
>. 
namespace Testing
{
    [TestClass]
    public class YamlDotNetTests
    {
        [TestMethod]
        public void Serialize_ShouldSerializeDictionaryExtensionsAsObjects()
        {
            // arrange
            IReadOnlyDictionary<string, string> roDict = new Dictionary<string, string>
            {
                {"foo", "bar"}
            };

            ExtendedReadOnlyDictionary<string, string> eroDict = new ExtendedReadOnlyDictionary<string, string>(roDict);

            // act
            var rodYaml = new SerializerBuilder()
                .Build()
                .Serialize(roDict);

            var erodYaml = new SerializerBuilder()
                .Build()
                .Serialize(eroDict);

            // assert
            Assert.AreEqual(rodYaml, erodYaml);
        }

        private class ExtendedReadOnlyDictionary<TKey, TValue> : IReadOnlyDictionary<TKey, TValue>
            where TKey : notnull
        {
            private readonly IReadOnlyDictionary<TKey, TValue> backingDictionary;

            public ExtendedReadOnlyDictionary(IReadOnlyDictionary<TKey, TValue> backingDictionary)
            {
                this.backingDictionary = backingDictionary;
            }

            public TValue this[TKey key] => this.backingDictionary[key];

            public IEnumerable<TKey> Keys => this.backingDictionary.Keys;

            public IEnumerable<TValue> Values => this.backingDictionary.Values;
            public int Count => throw new NotImplementedException();

            public bool ContainsKey(TKey key)
            {
                return this.backingDictionary.ContainsKey(key);
            }

            public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
            {
                return this.backingDictionary.GetEnumerator();
            }

            public bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value)
            {
                var result = this.backingDictionary.TryGetValue(key, out var foundValue);

                value = foundValue;

                return result;
            }

            IEnumerator IEnumerable.GetEnumerator()
            {
                return this.backingDictionary.GetEnumerator();
            }
        }
    }
}
@sfwester sfwester changed the title Classes extending IReadonlyDictionary are not serialized as a yaml mapping Classes extending IReadOnlyDictionary are not serialized as a yaml mapping Apr 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants