JSON

From Bitpost wiki
Revision as of 15:29, 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.

Warnings

   NOTE YOU CAN'T USE `using namespace boost::json` without fuckign up std::string!

Header

   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();

Examples

       use these:

           // CONSTRUCTING
           object j;
           j["my-name"] = "Heisenburg";
           j["my-number"] = 42;
           object jv = 
           {
               { "pi", 3.141 },
               { "happy", true },
               { "name", "Boost" },
               { "nothing", nullptr },
               { "answer", 
                   {
                       { "everything", 42 }
                   }
               },
               {"list", {1, 0, 2}},
               {
                   "object", 
                   {
                       { "currency", "USD" },
                       { "value", 42.99 }
                   } 
               }
           };

           // PARSING
           object jBody = parse(payload).as_object();
           if ( !jBody.if_contains( "sub" ) || !jBody.if_contains( "role" )) return;      
           sub_  = jBody["sub"].as_string();
           role_ = jBody["role"].as_int64();

           // NOTE you can't do an assignment for some reason...?
           FAIL: string sub = jBody["sub"].as_string();
           // But you can provide a constructor param.  Just weird.
           string sub( jBody["sub"].as_string() );

           // OUTPUT
           ss << j;
           string strMyJ = serialize( j );

String literals for hardcoded JSON

  • 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.