2

Is there a way to serialize an object so that it could then be rehydrated by .Net Core Configuration Binder?

Basically, I'd like to get this Test to pass:

[Test]
public void Can_Serialize_And_Rehydrate()
{
   var foo = new Foo{ Prop1 = 42; Prop2 = "Test" }

   Dictionary<string, string> serialized = Serialize(Foo);

   var deserializedFoo = new Foo();

    new ConfigurationBuilder()
        .AddInMemoryCollection(serialized)
        .Build()
        .Bind(deserializedFoo);

   Assert.AreEqual(deserializedFoo.Prop1, 42);
   Assert.AreEqual(deserializedFoo.Prop2, "Test");
}

Is there a Serializer out-of-the-box, or am I'm going to need to write my own Serialize() method?

Philip Pittle
  • 11,821
  • 8
  • 59
  • 123
  • have you seen the docs, esp the part about Using Options and Configuration objects? https://docs.asp.net/en/latest/fundamentals/configuration.html – Joe Audette Jul 11 '16 at 10:53
  • @JoeAudette - I've looked at the documentation, yes. I haven't seen anything in there about converting an object into a configuration (only the other way around), and from what I've seen of the Tests in their source code, I haven't seen a 'serializer' anywhere either. And yes, what I'm trying to do is a bit unorthodox. – Philip Pittle Jul 12 '16 at 01:19

2 Answers2

0

AddInMemoryCollection's signature is like below, so why are you trying to serialize your dictionary here? You could just use it as it is.

public static IConfigurationBuilder AddInMemoryCollection(
            this IConfigurationBuilder configurationBuilder,
            IEnumerable<KeyValuePair<string, string>> initialData)

If you like to know more about how to test your custom configurations, I would suggest to look here: https://github.com/aspnet/Configuration/blob/1.0.0/test/Microsoft.Extensions.Configuration.Binder.Test/ConfigurationBinderTests.cs

Kiran
  • 56,921
  • 15
  • 176
  • 161
  • "Why are tyring ot serialize your dictionary here" - I'm not trying to serialize a dictionary, I'm trying to serialize `Foo` into a Dictionary. Yes, this is a little unorthodox :). I'm experimenting with using the ConfigurationBuilder to serialize/deserialize items in a message passing system as the ConfigKey structure plays nice when building up type schema. – Philip Pittle Jul 12 '16 at 01:17
0

I was able to get this working by "hijacking" a JsonConfigurationProvider and plugging serialized Json directly into it. Not sure if this is the best way, but it does work:

public class ConfigurationSerializer 
{
    private class CustomJsonProvider : JsonConfigurationProvider
    {
        public CustomJsonProvider() : base(new JsonConfigurationSource())
        {
        }

        public IDictionary<string, string> GetData(Stream s)
        {
            Load(s);
            // Return the Configuration Dictionary
            return Data;
        }
    }

    public Dictionary<string, string> Serialize(object o)
    {
        var serialized = 
            JsonConvert.SerializeObject(
                o,
                new JsonSerializerSettings {NullValueHandling = NullValueHandling.Ignore});

        using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(serialized)))
        {
            var jsonProvider = new CustomJsonProvider();

            return jsonProvider
                .GetData(ms)
                .ToDictionary(key => key.Key, value => value.Value);
        }
    }
}
Philip Pittle
  • 11,821
  • 8
  • 59
  • 123