JSON

From Bitpost wiki
Revision as of 15:27, 6 September 2021 by M (talk | contribs)

JSON is as simple as possible but no less. Genius.

C++

We will use boost::json wherever possible. Keep updating it, and move to c++ std json if it becomes a thing. We still use nlohmann for quicktype and postgres-jsonb.

  • Configuration
   NOTE YOU CAN'T USE `using namespace boost::json` without fuckign up std::string!
  • Usage:
   when we need nlohmann:

       using namespace nlohmann::json;

   when we need boost:

       // boost/json.hpp
       using boost::json::object; // << most common!
       using boost::json::array;
       using boost::json::value; // << avoid, too generic
       using boost::json::value_from;
       using boost::json::parse;
       using boost::json::serialize;
       using boost::json::kind;

       // OR: Use auto to short-circuit the bullshit whenever possible.
       auto jBars = jResponse["bars"].as_array();
  • Use C++ string literals for hardcoded JSON whenever possible:
       R"(
{
  "name": "blah",
  "number": 123
}
       )";

Library history

  • rapidjson: I started with this. It is brutally low-level and cumbersome (fast? not to develop against...).
  • nlohmann: This was great to code with. Very elegant and straightforward. I moved towards boost to get better performance and a library with "more eyeballs". But this is still used by quicktype, which is used by my Postgres template code, which is really nice... and even tho quicktype is kinda dead... I'm keeping it for as long as I can.
  • boost: I started using boost::json on boost v 1.77, when it seemed to be generally not insane (like its predecessor spirit was...). It's slightly cumbersome, not that bad, and hopefully will get better.