Today, I took the challenge of converting YAML to JSON. How hard can it be right ? Well, I thought the same until I started brainstorming the design. It took me a couple of hours until I had something correct with a lot of limited features of YAML. Do note that most programming languages comes with default YAML parser which performs extremely well and handles all features of YAML.
The algorithm goes like this:
Base case for recursion:
I do want to point out that this approach comes with limitation. For example: empty values like key1: ''
is not possible.
The implementation follows exactly how I laid the design. However I ran into couple of issues when performing python split
. For example, if I have key1:
, splitting this with :
as delimiter returns ['key1', '']
. The empty string, second item of the array, made it challenging to add a feature where key can have empty strings as a value.
def yaml2Dict(texts: str) -> Dict: textsByNewLine = texts.split('\n')[1:] def util(texts: List[str], space: int) -> Dict: converted = {} for idx, text in enumerate(texts): splitText = text.split(':') if len(splitText) < 2: continue key = splitText[0] val = splitText[1] lSpaceKey = countLTrimSpaces(key) if lSpaceKey != space: continue if val == '': nextLine = texts[idx+1].split(':') nextKeyLSpace = countLTrimSpaces(nextLine[0]) converted[key.strip()] = util(texts[idx+1:], nextKeyLSpace) else: converted[key.strip()] = val.strip() return converted return util(textsByNewLine, 0)
This code with tests and comments can be found here
This was a hard coding challenge. We are very fortunate to have all major programming languages ship with a YAML converter which saves us a lot of trouble in writing a converter that can get very tricky. The same applies to all the items that are present in standard libraries. Let us take this moment to appreciate the standard libraries that programming language provides and the beautiful APIs that abstract the complexity.