Created
January 8, 2018 00:01
-
-
Save schani/32d370b6ed5ea221bbc65534f2e5b473 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // To parse this JSON data, first install | |
| // | |
| // Boost http://www.boost.org | |
| // json.hpp https://github.com/nlohmann/json | |
| // | |
| // Then include this file, and then do | |
| // | |
| // TopLevel data = nlohmann::json::parse(jsonString); | |
| #include <boost/variant.hpp> | |
| #include "json.hpp" | |
| namespace quicktype { | |
| using nlohmann::json; | |
| typedef boost::variant<std::unique_ptr<struct TopLevel>, std::vector<struct TopLevel>> Foo; | |
| struct Bar { | |
| Foo foo; | |
| }; | |
| struct TopLevel { | |
| std::unique_ptr<struct Bar> bar; | |
| }; | |
| inline json get_untyped(const json &j, const char *property) { | |
| if (j.find(property) != j.end()) { | |
| return j.at(property).get<json>(); | |
| } | |
| return json(); | |
| } | |
| template <typename T> | |
| inline std::unique_ptr<T> get_optional(const json &j, const char *property) { | |
| if (j.find(property) != j.end()) | |
| return j.at(property).get<std::unique_ptr<T>>(); | |
| return std::unique_ptr<T>(); | |
| } | |
| } | |
| namespace nlohmann { | |
| template <typename T> | |
| struct adl_serializer<std::unique_ptr<T>> { | |
| static void to_json(json& j, const std::unique_ptr<T>& opt) { | |
| if (!opt) | |
| j = nullptr; | |
| else | |
| j = *opt; | |
| } | |
| static std::unique_ptr<T> from_json(const json& j) { | |
| if (j.is_null()) | |
| return std::unique_ptr<T>(); | |
| else | |
| return std::unique_ptr<T>(new T(j.get<T>())); | |
| } | |
| }; | |
| inline void from_json(const json& _j, struct quicktype::Bar& _x) { | |
| _x.foo = _j.at("foo").get<quicktype::Foo>(); | |
| } | |
| inline void to_json(json& _j, const struct quicktype::Bar& _x) { | |
| _j = json::object(); | |
| _j["foo"] = _x.foo; | |
| } | |
| inline void from_json(const json& _j, struct quicktype::TopLevel& _x) { | |
| _x.bar = quicktype::get_optional<struct quicktype::Bar>(_j, "bar"); | |
| } | |
| inline void to_json(json& _j, const struct quicktype::TopLevel& _x) { | |
| _j = json::object(); | |
| _j["bar"] = _x.bar; | |
| } | |
| inline void from_json(const json& _j, boost::variant<std::unique_ptr<struct quicktype::TopLevel>, std::vector<struct quicktype::TopLevel>>& _x) { | |
| if (_j.is_object()) | |
| _x = _j.get<std::unique_ptr<struct quicktype::TopLevel>>(); | |
| else if (_j.is_array()) | |
| _x = _j.get<std::vector<struct quicktype::TopLevel>>(); | |
| else throw "Could not deserialize"; | |
| } | |
| inline void to_json(json& _j, const boost::variant<std::unique_ptr<struct quicktype::TopLevel>, std::vector<struct quicktype::TopLevel>>& _x) { | |
| switch (_x.which()) { | |
| case 0: | |
| _j = boost::get<std::unique_ptr<struct quicktype::TopLevel>>(_x); | |
| break; | |
| case 1: | |
| _j = boost::get<std::vector<struct quicktype::TopLevel>>(_x); | |
| break; | |
| default: throw "Input JSON does not conform to schema"; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment