Collection Serializer Working

I might have to revisit this for more advanced scenarios, but it’s working for basic purposes. Currently, it requires a collection property to be read/write, but I’ll work on that. The serializer only serializes read/write properties. It’ll have to be smarter about collections and append to the collection if it already exists, which is normally how collection properties work. Also, it’s not going to work for arrays, so that’ll have to be handled separately.

I’ve been saying “collection”, but it’s actually “IList”. That’s the highest interface with the add method. List implements it. Collection implements it. Sounds good to me.

BlahCollection is Collection<Blah>. BlahCollection has 2 additional properties: a property of another BlahCollection, and an integer property. Those are to make sure it does the whole graph, and that other properties are handled too.

        public void ShouldSerializeAndDeserializeCollection()
            BlahCollection collection = new BlahCollection
                new Blah { FirstName = "first 1", LastName = "last 1" },
                new Blah { FirstName = "first 2", LastName = "last 2" }
            collection.MyFavoriteNumber = 57;

            collection.NestedWithSetter = new BlahCollection
                new Blah {FirstName = "nested first 1", LastName = "nested last 1"},
                new Blah {FirstName = "nested first 2", LastName = "nested last 2"},
            collection.NestedWithSetter.MyFavoriteNumber = 1003;

            string serialized = new Serializer().Serialize(collection);
            BlahCollection deserialized = (BlahCollection)new Serializer().Deserialize(serialized);

            // root collection
            Assert.AreEqual(2, deserialized.Count);
            Assert.AreEqual("first 1", deserialized[0].FirstName);
            Assert.AreEqual("last 1", deserialized[0].LastName);
            Assert.AreEqual("first 2", deserialized[1].FirstName);
            Assert.AreEqual("last 2", deserialized[1].LastName);
            Assert.AreEqual(57, deserialized.MyFavoriteNumber);

            // nested with setter
            Assert.AreEqual(2, deserialized.NestedWithSetter.Count);
            Assert.AreEqual("nested first 1", deserialized.NestedWithSetter[0].FirstName);
            Assert.AreEqual("nested last 1", deserialized.NestedWithSetter[0].LastName);
            Assert.AreEqual("nested first 2", deserialized.NestedWithSetter[1].FirstName);
            Assert.AreEqual("nested last 2", deserialized.NestedWithSetter[1].LastName);
            Assert.AreEqual(1003, deserialized.NestedWithSetter.MyFavoriteNumber);


Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: