codelord.net

Code, Angular, iOS and more by Aviv Ben-Yosef

Serializer Kata: Practicing DRY

| Comments

This kata is intended to help one practice the DRY principle (Don’t Repeat Yourself). You can read more about DRY here.

A few notes:

  • After completing a step in the Kata and before moving on to the next, take the time to make sure your code’s duplication ≤ 0
  • For the sake of focus, you may ignore matters of character escaping, encoding, error handling, object graph cycles and the likes
  • Our focus is on reducing duplication, it is not finishing the kata

In this Kata, our goal is to implement 2 simple object serializers. One serializer is to XML, the other is to JSON.

  1. Support serializing  a class without any members
    1. To XML: EmptyClass –> <EmptyClass></EmptyClass>
    2. To JSON: EmptyClass –> {}
  2. Add support for serializing a class’ integer members
    1. To XML: IntClass(a=1, b=2) –> <IntClass><a>1</a><b>2</b></IntClass>
    2. To JSON: IntClass(a=1, b=2) –> { “a”: 1, “b”: 2 }
  3. Add support for serializing a class’ string members
    1. To XML: StrClass(a=“first”, b=“second”) –> <StrClass><a>first</a><b>second</b></StrClass>
    2. To JSON: StrClass(a=“first”, b=“second”) –> { “a”: “first”, “b”: “second” }
  4. Add support for serializing a class’ other class members
    1. To XML: CompositeClass(inner=(a=1)) –> <CompositeClass><inner><a>1</a></inner></CompositeClass>
    2. To JSON: CompositeClass(inner=(a=1)) –> { “inner”: { “a”: 1 } }

If you found this interesting subscribe to my feed and follow me on twitter!

Comments