'create json object for vector<bool> using nlohman json

I want to convert a vector<bool> vec = {true, false,true} to json object using nlohman json lib to send through restapi.

I expect the converted json object in the form

{
  "data" : [true, false, true]
}


Solution 1:[1]

To do this in nlohmann/json, all that needs doing is creating an empty nlohmann JSON object and assigning the boolean vector to a field, "data".

e.g.

std::vector<bool> vec = {true, false, true};

nlohmann::json j;
j["data"] = vec;

yields the json object

{
    "data":[true,false,true]
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 C2P1