Skip to content

Instantly share code, notes, and snippets.

@schani
Last active October 29, 2017 22:18
Show Gist options
  • Select an option

  • Save schani/140a13bfd1df9e6dda1a6aee483d5a6a to your computer and use it in GitHub Desktop.

Select an option

Save schani/140a13bfd1df9e6dda1a6aee483d5a6a to your computer and use it in GitHub Desktop.
// 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 = json::parse(jsonString);
#include <boost/optional.hpp>
#include <boost/variant.hpp>
#include "json.hpp"
namespace quicktype {
using nlohmann::json;
typedef boost::variant<int64_t, std::string> intOrString;
struct person {
std::string name;
boost::optional<bool> optionalValue;
intOrString purpleIntOrString;
};
struct differentThing {
std::string name;
};
typedef boost::variant<int64_t, std::string, struct differentThing> differentThings;
typedef boost::variant<int64_t, std::string> tuples;
struct topLevel {
bool booleanValue;
std::string dateValue;
double doubleValue;
int64_t intValue;
std::map<std::string, int64_t> mapValue;
nlohmann::json nameWithSpaces;
nlohmann::json nullValue;
std::vector<struct person> people;
std::vector<differentThings> purpleDifferentThings;
struct person purplePerson;
std::vector<std::vector<tuples>> purpleTuples;
std::string stringValue;
std::string uuidValue;
};
static 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>
static boost::optional<T> get_optional(const json &j, const char *property) {
if (j.find(property) != j.end()) {
return j.at(property).get<boost::optional<T>>();
}
return boost::optional<T>();
}
}
namespace nlohmann {
template <typename T>
struct adl_serializer<boost::optional<T>> {
static void to_json(json& j, const boost::optional<T>& opt) {
if (opt == boost::none) {
j = nullptr;
} else {
j = *opt; // this will call adl_serializer<T>::to_json which will
// find the free function to_json in T's namespace!
}
}
static void from_json(const json& j, boost::optional<T>& opt) {
if (j.is_null()) {
opt = boost::none;
} else {
opt = j.get<T>(); // same as above, but with
// adl_serializer<T>::from_json
}
}
};
void from_json(const json& _j, struct quicktype::topLevel& _x) {
_x.booleanValue = _j.at("booleanValue").get<bool>();
_x.dateValue = _j.at("dateValue").get<std::string>();
_x.doubleValue = _j.at("doubleValue").get<double>();
_x.intValue = _j.at("intValue").get<int64_t>();
_x.mapValue = _j.at("mapValue").get<std::map<std::string, int64_t>>();
_x.nameWithSpaces = quicktype::get_untyped(_j, "name with spaces");
_x.nullValue = quicktype::get_untyped(_j, "nullValue");
_x.people = _j.at("people").get<std::vector<struct quicktype::person>>();
_x.purpleDifferentThings = _j.at("differentThings").get<std::vector<quicktype::differentThings>>();
_x.purplePerson = _j.at("person").get<struct quicktype::person>();
_x.purpleTuples = _j.at("tuples").get<std::vector<std::vector<quicktype::tuples>>>();
_x.stringValue = _j.at("stringValue").get<std::string>();
_x.uuidValue = _j.at("uuidValue").get<std::string>();
}
void to_json(json& _j, const struct quicktype::topLevel& _x) {
_j = json{{"booleanValue", _x.booleanValue}, {"dateValue", _x.dateValue}, {"doubleValue", _x.doubleValue}, {"intValue", _x.intValue}, {"mapValue", _x.mapValue}, {"name with spaces", _x.nameWithSpaces}, {"nullValue", _x.nullValue}, {"people", _x.people}, {"differentThings", _x.purpleDifferentThings}, {"person", _x.purplePerson}, {"tuples", _x.purpleTuples}, {"stringValue", _x.stringValue}, {"uuidValue", _x.uuidValue}};
}
void from_json(const json& _j, struct quicktype::differentThing& _x) {
_x.name = _j.at("name").get<std::string>();
}
void to_json(json& _j, const struct quicktype::differentThing& _x) {
_j = json{{"name", _x.name}};
}
void from_json(const json& _j, struct quicktype::person& _x) {
_x.name = _j.at("name").get<std::string>();
_x.optionalValue = quicktype::get_optional<bool>(_j, "optionalValue");
_x.purpleIntOrString = _j.at("intOrString").get<quicktype::intOrString>();
}
void to_json(json& _j, const struct quicktype::person& _x) {
_j = json{{"name", _x.name}, {"optionalValue", _x.optionalValue}, {"intOrString", _x.purpleIntOrString}};
}
void from_json(const json& _j, boost::variant<int64_t, std::string>& _x) {
if (_j.is_number_integer())
_x = _j.get<int64_t>();
else if (_j.is_string())
_x = _j.get<std::string>();
else throw "Could not deserialize";
}
void to_json(json& _j, const boost::variant<int64_t, std::string>& _x) {
switch (_x.which()) {
case 0:
_j = boost::get<int64_t>(_x);
break;
case 1:
_j = boost::get<std::string>(_x);
break;
default: throw "This should not happen";
}
}
void from_json(const json& _j, boost::variant<int64_t, std::string, struct quicktype::differentThing>& _x) {
if (_j.is_number_integer())
_x = _j.get<int64_t>();
else if (_j.is_string())
_x = _j.get<std::string>();
else if (_j.is_object())
_x = _j.get<struct quicktype::differentThing>();
else throw "Could not deserialize";
}
void to_json(json& _j, const boost::variant<int64_t, std::string, struct quicktype::differentThing>& _x) {
switch (_x.which()) {
case 0:
_j = boost::get<int64_t>(_x);
break;
case 1:
_j = boost::get<std::string>(_x);
break;
case 2:
_j = boost::get<struct quicktype::differentThing>(_x);
break;
default: throw "This should not happen";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment