The interview question of the week for this week on Cassidoo’s weekly newsletter is,
Write a function that takes in a string of valid JSON and converts it to an object (or whatever your chosen language uses, dicts, maps, etc). Example input:
************ fakeParseJSON ('{” data ": {" fish ":" cake "," array ": [1,2,3]," children ": [ { "something": "else" }, { "candy": "cane" }, { "sponge": "bob" } ]}} ')
At one point, I was tempted to just to write:
)constfakeParseJSON=******************* (JSON) .parse;But, I thought, I’ve written quite a few articles about AST:
Creating custom JavaScript syntax with BabelStep-by-step guide for writing a custom babel transformation Manipulating AST with JavaScript which covers the overview of the compiler pipeline, as well as how to manipulate AST, but I haven't covered much on how to implement a parser.
That’s because, implementing a JavaScript compiler in an article is a task too daunting for me.
Well, fret not. JSON is also a language. It has its own grammar, which you can refer from the specifications (***********************. The knowledge and technique you need to write a JSON parser is transferrable to writing a JS parser.
So, let’s start writing a JSON parser!
************************** (********************************