May 23, 2018

A Json Parser Part 1 : Reading a file

The year is 2021. A rainy Sunday afternoon. It’s getting increasingly more difficult to ignore the looming shadow of The Great Rewrite.

But you ran out of cacao and there is nothing on HBO. So you guzzle a fourth cup of coffee and decide to start a new project. In C++ of course. You sacrificed too much to reevaluate your life choices now.

But you need to do something new. Something to impress, Something to shine, like a diamond.

Something bold. Pionerring:

A command line parser ! Unfortunately, someone already did that, apparently.

A console logger ! That too was already done.

A console emulator …. too complicated….

That’s it! Let’s build a JSON parser. Truly novel, and simple.

First, you setup some cmake scripts.

The year is 2022.

Reading a file

You figure that to make any kind of parser, you will probably need to read a file, so you can lex it. Let’s open a file:

#include <fstream>

You reevaluate your life choices.

<fstream> and all of <iostream> is terrible. It hard to say exactly why. In some case, a benchmark reveals it may have worse performance than manipuling a file handle directly, but results aren’t exactly clear cut.

Share on