C/C++ JSON – Adding NLohmann to your environment

Still find it fascinating that after all these years that C/C++ doesnt have native JSON libraries. I have been working with various public APIs recently for some personal projects and just thought I would put this out there for quick reference for folks. For my purposes the Niels Lohmann is perfect. There may be other projects out there and you are welcome to explore at your leisure.

I recommend folks check out the official project repository on Github called JSON for Modern C++.

Feel free to grab it via the command line but for purposes of this example we will download it. Simply click on the Code Dropdown (in Green) and select the Download ZIP.

Now that you have it down, feel free to extract it to where-ever you like. For me, I always extract these things to my /home/src directory. Once it is extracted you will will have a directory called json-develop. So in my case:

/home/src/json-develop/

You will then type in the instructions below:

  • cd json-develop
  • mkdir build
  • cd build
  • cmake .. -DJSON_BuildTests=ON
  • cmake .. –build .
  • ctest –output-on-failure

Its important to note that these steps are listed at the bottom of the JSON project on github, and although they are labeled as ‘unit tests’ are absolutely needed to perform the installation. These steps can take some time. Once complete you are ready to go ahead and install it. Make sure you use sudo:

  • sudo make install

That should do it. You will now be able to include the nlohmann Json libraries and headers into your code so you can pull to your hearts content.

\Mm

Leave a Reply

Your email address will not be published.