The following code:
#include <fstream>
#include <iomanip>
#include <iostream>
#include <unordered_map>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
enum class Species {
DOG,
OCTOPUS
};
void from_json(const json& j, Species& sp) {
std::string s = j;
if (s == "DOG") {
sp = Species::DOG;
} else if (s == "OCTOPUS") {
sp = Species::OCTOPUS;
} else {
throw std::invalid_argument("Unknown species");
}
}
void to_json(json& j, const Species sp) {
if (sp == Species::DOG) {
j = "DOG";
} else {
j = "OCTOPUS";
}
}
int main() {
json j;
std::unordered_map<Species, bool> is_aquatic;
is_aquatic[Species::DOG] = false;
is_aquatic[Species::OCTOPUS] = true;
j = is_aquatic;
std::cout << j << std::endl;
}
produces the output [["DOG",false],["OCTOPUS",true]], which is not what the user would expect IMHO. Would it be possible to render this map as {"DOG":false,"OCTOPUS":true}?
The following code:
produces the output
[["DOG",false],["OCTOPUS",true]], which is not what the user would expect IMHO. Would it be possible to render this map as{"DOG":false,"OCTOPUS":true}?