#include #include #include #include #include #include class JSON_Field; using JSON_List = std::vector; class JSON { public: JSON_Field& operator[](std::string const& key) { return fields[key]; } JSON_Field const& operator[](std::string const& key) const { return fields.at(key); } void print(std::ostream& os, int indent = 0) const; private: std::map fields { }; }; // Implement JSON_Field here void JSON::print(std::ostream& os, int indent) const { os << "{" << std::endl; bool first { true }; for (auto&& [key, value] : fields) { // print a if (first) { first = false; } else { os << "," << std::endl; } os << std::setw(indent + 4) << ""; os << '"' << key << "\" : "; value.print(os, indent + 4); } os << std::endl << std::setw(indent) << "" << "}"; } int main() { JSON json1; json1["d"] = 0.0; json1["e"] = 6.0; JSON json2; json2["a"] = 5.0; json2["b"] = JSON_List{ 1.0, JSON_List{ 2.0, 3.0 }, 4.0 }; json2["c"] = std::move(json1); json2.print(std::cout); std::cout << std::endl; std::cout << "====" << std::endl; JSON json3; json3["list"] = JSON_List{ 0.5, std::move(json2), 1.5 }; json3["number"] = -1.5; json3.print(std::cout); std::cout << std::endl; }