Article 44E2H CodeSOD: Stringed Out

CodeSOD: Stringed Out

by
Remy Porter
from The Daily WTF on (#44E2H)

The line between objects and maps can sometimes get a little blurry. In languages like JavaScript, there's really no difference between the two. In Python, the deep internals of your classes are implemented essentially as dicts, though there are ways around that behavior.

In a language like C#, however, you've got types, you've got property definitions. This can offer a lot of advantages. When you layer on features like reflection, you can inspect your objects. Combine all this, and it means that if you want to serialize a data object to XML, you can usually do it in a way that's both typesafe and generally doesn't require much code on your part. A handful of annotations and a few method calls, and boom- any object gets serialized.

Unless you work at Kara's office, of course. When they have an object that requires serialization, they must inherit from SerializableObjectBase.

 public abstract class SerializableObjectBase { public Dictionary<string, string> properties = new Dictionary<string, string>(); public virtual void SerializeMe(XmlElement parent) { foreach (KeyValuePair<string, string> item in properties) { parent.AppendChild( parent.OwnerDocument.CreateElement(item.Key) ).InnerText = item.Value; } } // Deserializer omitted for brevity. }

All serializable properties must be stored in the properties dictionary. This dictionary is conveniently public, and stringly typed. The serialization method also produces a conveniently stringly-type XML document, so we don't have to worry about anything so pedantic as schemas.

So, for example, if you wanted to create a serializable object, you might do something like this:

 public class Foo : SerializableObjectBase { }

Look how easy that is! Of course, if your custom class has any reference types, they can't be stored in the properties dictionary, so you'll have to write that yourself. Something like:

 public class Foo : SerializableObjectBase { public override void SerializeMe(XmlElement parent) { base.SerializeMe(parent); if (this.BarReference != null) { var elem = parent.OwnerDocument.CreateElement("Bar") parent.AppendChild(elem) this.BarReference.SerializeMe(elem); } } }

Enjoy doing that for every property that can't be stored as a string. You may have noticed that, since the properties dictionary is public, I didn't add any property accessors to my class. 90% of the classes in their codebase followed that pattern. You were lucky to find a class that actually bothered to implement typed accessors. Of course, since you had to store any serializable property in your properties dictionary, the property accessors usually took the form:

 public int myProperty { get { if (properties.ContainsKey("myProperty")) return int.Parse(properties["myProperty"]); return 0; } set { properties["myProperty"] = value.ToString(); } }

What could be simpler?

otter-icon.png [Advertisement] Continuously monitor your servers for configuration changes, and report when there's configuration drift. Get started with Otter today! TheDailyWtf?d=yIl2AUoC8zAGrx73dnEuNU
External Content
Source RSS or Atom Feed
Feed Location http://syndication.thedailywtf.com/TheDailyWtf
Feed Title The Daily WTF
Feed Link http://thedailywtf.com/
Reply 0 comments