Test case
1 generated file · +18 −1test/inputs/schema/mutually-recursive.schema
Mschema-cplusplusdefault / quicktype.hpp+18 −1
| @@ -89,11 +89,17 @@ namespace quicktype { | ||
| 89 | 89 | } |
| 90 | 90 | #endif |
| 91 | 91 | |
| 92 | + struct Foo; | |
| 93 | + | |
| 92 | 94 | class Bar; |
| 93 | 95 | |
| 94 | 96 | class TopLevel; |
| 95 | 97 | |
| 96 | - using Foo = std::variant<std::vector<TopLevel>, std::shared_ptr<TopLevel>>; | |
| 98 | + struct Foo : std::variant<std::vector<TopLevel>, std::shared_ptr<TopLevel>> { | |
| 99 | + using base = std::variant<std::vector<TopLevel>, std::shared_ptr<TopLevel>>; | |
| 100 | + using base::base; | |
| 101 | + using base::operator=; | |
| 102 | + }; | |
| 97 | 103 | |
| 98 | 104 | class Bar { |
| 99 | 105 | public: |
| @@ -130,6 +136,9 @@ void to_json(json & j, const Bar & x); | ||
| 130 | 136 | |
| 131 | 137 | void from_json(const json & j, TopLevel & x); |
| 132 | 138 | void to_json(json & j, const TopLevel & x); |
| 139 | + | |
| 140 | +void from_json(const json & j, Foo & x); | |
| 141 | +void to_json(json & j, const Foo & x); | |
| 133 | 142 | } |
| 134 | 143 | namespace nlohmann { |
| 135 | 144 | template <> |
| @@ -156,6 +165,14 @@ namespace quicktype { | ||
| 156 | 165 | j = json::object(); |
| 157 | 166 | j["bar"] = x.get_bar(); |
| 158 | 167 | } |
| 168 | + | |
| 169 | + inline void from_json(const json & j, Foo & x) { | |
| 170 | + x = j.get<Foo::base>(); | |
| 171 | + } | |
| 172 | + | |
| 173 | + inline void to_json(json & j, const Foo & x) { | |
| 174 | + j = static_cast<const Foo::base &>(x); | |
| 175 | + } | |
| 159 | 176 | } |
| 160 | 177 | namespace nlohmann { |
| 161 | 178 | inline void adl_serializer<std::variant<std::vector<quicktype::TopLevel>, std::shared_ptr<quicktype::TopLevel>>>::from_json(const json & j, std::variant<std::vector<quicktype::TopLevel>, std::shared_ptr<quicktype::TopLevel>> & x) { |
Test case
1 generated file · +373 −0test/inputs/schema/recursive-union-flattening.schema
Aschema-cplusplusdefault / quicktype.hpp+373 −0
| @@ -0,0 +1,373 @@ | ||
| 1 | +// To parse this JSON data, first install | |
| 2 | +// | |
| 3 | +// json.hpp https://github.com/nlohmann/json | |
| 4 | +// | |
| 5 | +// Then include this file, and then do | |
| 6 | +// | |
| 7 | +// TopLevel data = nlohmann::json::parse(jsonString); | |
| 8 | + | |
| 9 | +#pragma once | |
| 10 | + | |
| 11 | +#include <optional> | |
| 12 | +#include <variant> | |
| 13 | +#include "json.hpp" | |
| 14 | + | |
| 15 | +#include <optional> | |
| 16 | +#include <stdexcept> | |
| 17 | +#include <regex> | |
| 18 | + | |
| 19 | +#ifndef NLOHMANN_OPT_HELPER | |
| 20 | +#define NLOHMANN_OPT_HELPER | |
| 21 | +namespace nlohmann { | |
| 22 | + template <typename T> | |
| 23 | + struct adl_serializer<std::shared_ptr<T>> { | |
| 24 | + static void to_json(json & j, const std::shared_ptr<T> & opt) { | |
| 25 | + if (!opt) j = nullptr; else j = *opt; | |
| 26 | + } | |
| 27 | + | |
| 28 | + static std::shared_ptr<T> from_json(const json & j) { | |
| 29 | + if (j.is_null()) return std::shared_ptr<T>(); else return std::make_shared<T>(j.get<T>()); | |
| 30 | + } | |
| 31 | + }; | |
| 32 | + template <typename T> | |
| 33 | + struct adl_serializer<std::optional<T>> { | |
| 34 | + static void to_json(json & j, const std::optional<T> & opt) { | |
| 35 | + if (!opt) j = nullptr; else j = *opt; | |
| 36 | + } | |
| 37 | + | |
| 38 | + static std::optional<T> from_json(const json & j) { | |
| 39 | + if (j.is_null()) return std::optional<T>(); else return std::make_optional<T>(j.get<T>()); | |
| 40 | + } | |
| 41 | + }; | |
| 42 | +} | |
| 43 | +#endif | |
| 44 | + | |
| 45 | +namespace quicktype { | |
| 46 | + using nlohmann::json; | |
| 47 | + | |
| 48 | + #ifndef NLOHMANN_UNTYPED_quicktype_HELPER | |
| 49 | + #define NLOHMANN_UNTYPED_quicktype_HELPER | |
| 50 | + inline json get_untyped(const json & j, const char * property) { | |
| 51 | + if (j.find(property) != j.end()) { | |
| 52 | + return j.at(property).get<json>(); | |
| 53 | + } | |
| 54 | + return json(); | |
| 55 | + } | |
| 56 | + | |
| 57 | + inline json get_untyped(const json & j, std::string property) { | |
| 58 | + return get_untyped(j, property.data()); | |
| 59 | + } | |
| 60 | + #endif | |
| 61 | + | |
| 62 | + #ifndef NLOHMANN_OPTIONAL_quicktype_HELPER | |
| 63 | + #define NLOHMANN_OPTIONAL_quicktype_HELPER | |
| 64 | + template <typename T> | |
| 65 | + inline std::shared_ptr<T> get_heap_optional(const json & j, const char * property) { | |
| 66 | + auto it = j.find(property); | |
| 67 | + if (it != j.end() && !it->is_null()) { | |
| 68 | + return j.at(property).get<std::shared_ptr<T>>(); | |
| 69 | + } | |
| 70 | + return std::shared_ptr<T>(); | |
| 71 | + } | |
| 72 | + | |
| 73 | + template <typename T> | |
| 74 | + inline std::shared_ptr<T> get_heap_optional(const json & j, std::string property) { | |
| 75 | + return get_heap_optional<T>(j, property.data()); | |
| 76 | + } | |
| 77 | + template <typename T> | |
| 78 | + inline std::optional<T> get_stack_optional(const json & j, const char * property) { | |
| 79 | + auto it = j.find(property); | |
| 80 | + if (it != j.end() && !it->is_null()) { | |
| 81 | + return j.at(property).get<std::optional<T>>(); | |
| 82 | + } | |
| 83 | + return std::optional<T>(); | |
| 84 | + } | |
| 85 | + | |
| 86 | + template <typename T> | |
| 87 | + inline std::optional<T> get_stack_optional(const json & j, std::string property) { | |
| 88 | + return get_stack_optional<T>(j, property.data()); | |
| 89 | + } | |
| 90 | + #endif | |
| 91 | + | |
| 92 | + struct X; | |
| 93 | + struct XElement; | |
| 94 | + | |
| 95 | + class XClass; | |
| 96 | + | |
| 97 | + struct X : std::shared_ptr<std::variant<std::vector<XElement>, bool, double, int64_t, std::map<std::string, nlohmann::json>, std::string>> { | |
| 98 | + using base = std::shared_ptr<std::variant<std::vector<XElement>, bool, double, int64_t, std::map<std::string, nlohmann::json>, std::string>>; | |
| 99 | + using base::base; | |
| 100 | + using base::operator=; | |
| 101 | + }; | |
| 102 | + | |
| 103 | + struct XElement : std::optional<std::variant<std::vector<nlohmann::json>, bool, std::shared_ptr<XClass>, double, int64_t, std::string>> { | |
| 104 | + using base = std::optional<std::variant<std::vector<nlohmann::json>, bool, std::shared_ptr<XClass>, double, int64_t, std::string>>; | |
| 105 | + using base::base; | |
| 106 | + using base::operator=; | |
| 107 | + }; | |
| 108 | + | |
| 109 | + class XClass { | |
| 110 | + public: | |
| 111 | + XClass() = default; | |
| 112 | + virtual ~XClass() = default; | |
| 113 | + | |
| 114 | + private: | |
| 115 | + X x; | |
| 116 | + | |
| 117 | + public: | |
| 118 | + X get_x() const { return x; } | |
| 119 | + void set_x(X value) { this->x = value; } | |
| 120 | + }; | |
| 121 | + | |
| 122 | + class TopLevelClass { | |
| 123 | + public: | |
| 124 | + TopLevelClass() = default; | |
| 125 | + virtual ~TopLevelClass() = default; | |
| 126 | + | |
| 127 | + private: | |
| 128 | + X x; | |
| 129 | + | |
| 130 | + public: | |
| 131 | + X get_x() const { return x; } | |
| 132 | + void set_x(X value) { this->x = value; } | |
| 133 | + }; | |
| 134 | + | |
| 135 | + using TopLevelElement = std::optional<std::variant<std::vector<nlohmann::json>, bool, TopLevelClass, double, int64_t, std::string>>; | |
| 136 | + | |
| 137 | + using TopLevel = std::optional<std::variant<std::vector<TopLevelElement>, bool, double, int64_t, std::map<std::string, nlohmann::json>, std::string>>; | |
| 138 | +} | |
| 139 | + | |
| 140 | +namespace quicktype { | |
| 141 | +void from_json(const json & j, XClass & x); | |
| 142 | +void to_json(json & j, const XClass & x); | |
| 143 | + | |
| 144 | +void from_json(const json & j, TopLevelClass & x); | |
| 145 | +void to_json(json & j, const TopLevelClass & x); | |
| 146 | + | |
| 147 | +void from_json(const json & j, X & x); | |
| 148 | +void to_json(json & j, const X & x); | |
| 149 | + | |
| 150 | +void from_json(const json & j, XElement & x); | |
| 151 | +void to_json(json & j, const XElement & x); | |
| 152 | +} | |
| 153 | +namespace nlohmann { | |
| 154 | +template <> | |
| 155 | +struct adl_serializer<std::variant<std::vector<quicktype::XElement>, bool, double, int64_t, std::map<std::string, json>, std::string>> { | |
| 156 | + static void from_json(const json & j, std::variant<std::vector<quicktype::XElement>, bool, double, int64_t, std::map<std::string, json>, std::string> & x); | |
| 157 | + static void to_json(json & j, const std::variant<std::vector<quicktype::XElement>, bool, double, int64_t, std::map<std::string, json>, std::string> & x); | |
| 158 | +}; | |
| 159 | + | |
| 160 | +template <> | |
| 161 | +struct adl_serializer<std::variant<std::vector<json>, bool, std::shared_ptr<quicktype::XClass>, double, int64_t, std::string>> { | |
| 162 | + static void from_json(const json & j, std::variant<std::vector<json>, bool, std::shared_ptr<quicktype::XClass>, double, int64_t, std::string> & x); | |
| 163 | + static void to_json(json & j, const std::variant<std::vector<json>, bool, std::shared_ptr<quicktype::XClass>, double, int64_t, std::string> & x); | |
| 164 | +}; | |
| 165 | + | |
| 166 | +template <> | |
| 167 | +struct adl_serializer<std::variant<std::vector<json>, bool, quicktype::TopLevelClass, double, int64_t, std::string>> { | |
| 168 | + static void from_json(const json & j, std::variant<std::vector<json>, bool, quicktype::TopLevelClass, double, int64_t, std::string> & x); | |
| 169 | + static void to_json(json & j, const std::variant<std::vector<json>, bool, quicktype::TopLevelClass, double, int64_t, std::string> & x); | |
| 170 | +}; | |
| 171 | + | |
| 172 | +template <> | |
| 173 | +struct adl_serializer<std::variant<std::vector<quicktype::TopLevelElement>, bool, double, int64_t, std::map<std::string, json>, std::string>> { | |
| 174 | + static void from_json(const json & j, std::variant<std::vector<quicktype::TopLevelElement>, bool, double, int64_t, std::map<std::string, json>, std::string> & x); | |
| 175 | + static void to_json(json & j, const std::variant<std::vector<quicktype::TopLevelElement>, bool, double, int64_t, std::map<std::string, json>, std::string> & x); | |
| 176 | +}; | |
| 177 | +} | |
| 178 | +namespace quicktype { | |
| 179 | + inline void from_json(const json & j, XClass& x) { | |
| 180 | + x.set_x(j.find("x") != j.end() ? j.at("x").get<X>() : X()); | |
| 181 | + } | |
| 182 | + | |
| 183 | + inline void to_json(json & j, const XClass & x) { | |
| 184 | + j = json::object(); | |
| 185 | + j["x"] = x.get_x(); | |
| 186 | + } | |
| 187 | + | |
| 188 | + inline void from_json(const json & j, TopLevelClass& x) { | |
| 189 | + x.set_x(j.find("x") != j.end() ? j.at("x").get<X>() : X()); | |
| 190 | + } | |
| 191 | + | |
| 192 | + inline void to_json(json & j, const TopLevelClass & x) { | |
| 193 | + j = json::object(); | |
| 194 | + j["x"] = x.get_x(); | |
| 195 | + } | |
| 196 | + | |
| 197 | + inline void from_json(const json & j, X & x) { | |
| 198 | + x = j.get<X::base>(); | |
| 199 | + } | |
| 200 | + | |
| 201 | + inline void to_json(json & j, const X & x) { | |
| 202 | + j = static_cast<const X::base &>(x); | |
| 203 | + } | |
| 204 | + | |
| 205 | + inline void from_json(const json & j, XElement & x) { | |
| 206 | + x = j.get<XElement::base>(); | |
| 207 | + } | |
| 208 | + | |
| 209 | + inline void to_json(json & j, const XElement & x) { | |
| 210 | + j = static_cast<const XElement::base &>(x); | |
| 211 | + } | |
| 212 | +} | |
| 213 | +namespace nlohmann { | |
| 214 | + inline void adl_serializer<std::variant<std::vector<quicktype::XElement>, bool, double, int64_t, std::map<std::string, json>, std::string>>::from_json(const json & j, std::variant<std::vector<quicktype::XElement>, bool, double, int64_t, std::map<std::string, json>, std::string> & x) { | |
| 215 | + if (j.is_boolean()) | |
| 216 | + x = j.get<bool>(); | |
| 217 | + else if (j.is_number_integer()) | |
| 218 | + x = j.get<int64_t>(); | |
| 219 | + else if (j.is_number()) | |
| 220 | + x = j.get<double>(); | |
| 221 | + else if (j.is_string()) | |
| 222 | + x = j.get<std::string>(); | |
| 223 | + else if (j.is_object()) | |
| 224 | + x = j.get<std::map<std::string, json>>(); | |
| 225 | + else if (j.is_array()) | |
| 226 | + x = j.get<std::vector<quicktype::XElement>>(); | |
| 227 | + else throw std::runtime_error("Could not deserialise!"); | |
| 228 | + } | |
| 229 | + | |
| 230 | + inline void adl_serializer<std::variant<std::vector<quicktype::XElement>, bool, double, int64_t, std::map<std::string, json>, std::string>>::to_json(json & j, const std::variant<std::vector<quicktype::XElement>, bool, double, int64_t, std::map<std::string, json>, std::string> & x) { | |
| 231 | + switch (x.index()) { | |
| 232 | + case 0: | |
| 233 | + j = std::get<std::vector<quicktype::XElement>>(x); | |
| 234 | + break; | |
| 235 | + case 1: | |
| 236 | + j = std::get<bool>(x); | |
| 237 | + break; | |
| 238 | + case 2: | |
| 239 | + j = std::get<double>(x); | |
| 240 | + break; | |
| 241 | + case 3: | |
| 242 | + j = std::get<int64_t>(x); | |
| 243 | + break; | |
| 244 | + case 4: | |
| 245 | + j = std::get<std::map<std::string, json>>(x); | |
| 246 | + break; | |
| 247 | + case 5: | |
| 248 | + j = std::get<std::string>(x); | |
| 249 | + break; | |
| 250 | + default: throw std::runtime_error("Input JSON does not conform to schema!"); | |
| 251 | + } | |
| 252 | + } | |
| 253 | + | |
| 254 | + inline void adl_serializer<std::variant<std::vector<json>, bool, std::shared_ptr<quicktype::XClass>, double, int64_t, std::string>>::from_json(const json & j, std::variant<std::vector<json>, bool, std::shared_ptr<quicktype::XClass>, double, int64_t, std::string> & x) { | |
| 255 | + if (j.is_boolean()) | |
| 256 | + x = j.get<bool>(); | |
| 257 | + else if (j.is_number_integer()) | |
| 258 | + x = j.get<int64_t>(); | |
| 259 | + else if (j.is_number()) | |
| 260 | + x = j.get<double>(); | |
| 261 | + else if (j.is_string()) | |
| 262 | + x = j.get<std::string>(); | |
| 263 | + else if (j.is_object()) | |
| 264 | + x = j.get<std::shared_ptr<quicktype::XClass>>(); | |
| 265 | + else if (j.is_array()) | |
| 266 | + x = j.get<std::vector<json>>(); | |
| 267 | + else throw std::runtime_error("Could not deserialise!"); | |
| 268 | + } | |
| 269 | + | |
| 270 | + inline void adl_serializer<std::variant<std::vector<json>, bool, std::shared_ptr<quicktype::XClass>, double, int64_t, std::string>>::to_json(json & j, const std::variant<std::vector<json>, bool, std::shared_ptr<quicktype::XClass>, double, int64_t, std::string> & x) { | |
| 271 | + switch (x.index()) { | |
| 272 | + case 0: | |
| 273 | + j = std::get<std::vector<json>>(x); | |
| 274 | + break; | |
| 275 | + case 1: | |
| 276 | + j = std::get<bool>(x); | |
| 277 | + break; | |
| 278 | + case 2: | |
| 279 | + j = std::get<std::shared_ptr<quicktype::XClass>>(x); | |
| 280 | + break; | |
| 281 | + case 3: | |
| 282 | + j = std::get<double>(x); | |
| 283 | + break; | |
| 284 | + case 4: | |
| 285 | + j = std::get<int64_t>(x); | |
| 286 | + break; | |
| 287 | + case 5: | |
| 288 | + j = std::get<std::string>(x); | |
| 289 | + break; | |
| 290 | + default: throw std::runtime_error("Input JSON does not conform to schema!"); | |
| 291 | + } | |
| 292 | + } | |
| 293 | + | |
| 294 | + inline void adl_serializer<std::variant<std::vector<json>, bool, quicktype::TopLevelClass, double, int64_t, std::string>>::from_json(const json & j, std::variant<std::vector<json>, bool, quicktype::TopLevelClass, double, int64_t, std::string> & x) { | |
| 295 | + if (j.is_boolean()) | |
| 296 | + x = j.get<bool>(); | |
| 297 | + else if (j.is_number_integer()) | |
| 298 | + x = j.get<int64_t>(); | |
| 299 | + else if (j.is_number()) | |
| 300 | + x = j.get<double>(); | |
| 301 | + else if (j.is_string()) | |
| 302 | + x = j.get<std::string>(); | |
| 303 | + else if (j.is_object()) | |
| 304 | + x = j.get<quicktype::TopLevelClass>(); | |
| 305 | + else if (j.is_array()) | |
| 306 | + x = j.get<std::vector<json>>(); | |
| 307 | + else throw std::runtime_error("Could not deserialise!"); | |
| 308 | + } | |
| 309 | + | |
| 310 | + inline void adl_serializer<std::variant<std::vector<json>, bool, quicktype::TopLevelClass, double, int64_t, std::string>>::to_json(json & j, const std::variant<std::vector<json>, bool, quicktype::TopLevelClass, double, int64_t, std::string> & x) { | |
| 311 | + switch (x.index()) { | |
| 312 | + case 0: | |
| 313 | + j = std::get<std::vector<json>>(x); | |
| 314 | + break; | |
| 315 | + case 1: | |
| 316 | + j = std::get<bool>(x); | |
| 317 | + break; | |
| 318 | + case 2: | |
| 319 | + j = std::get<quicktype::TopLevelClass>(x); | |
| 320 | + break; | |
| 321 | + case 3: | |
| 322 | + j = std::get<double>(x); | |
| 323 | + break; | |
| 324 | + case 4: | |
| 325 | + j = std::get<int64_t>(x); | |
| 326 | + break; | |
| 327 | + case 5: | |
| 328 | + j = std::get<std::string>(x); | |
| 329 | + break; | |
| 330 | + default: throw std::runtime_error("Input JSON does not conform to schema!"); | |
| 331 | + } | |
| 332 | + } | |
| 333 | + | |
| 334 | + inline void adl_serializer<std::variant<std::vector<quicktype::TopLevelElement>, bool, double, int64_t, std::map<std::string, json>, std::string>>::from_json(const json & j, std::variant<std::vector<quicktype::TopLevelElement>, bool, double, int64_t, std::map<std::string, json>, std::string> & x) { | |
| 335 | + if (j.is_boolean()) | |
| 336 | + x = j.get<bool>(); | |
| 337 | + else if (j.is_number_integer()) | |
| 338 | + x = j.get<int64_t>(); | |
| 339 | + else if (j.is_number()) | |
| 340 | + x = j.get<double>(); | |
| 341 | + else if (j.is_string()) | |
| 342 | + x = j.get<std::string>(); | |
| 343 | + else if (j.is_object()) | |
| 344 | + x = j.get<std::map<std::string, json>>(); | |
| 345 | + else if (j.is_array()) | |
| 346 | + x = j.get<std::vector<quicktype::TopLevelElement>>(); | |
| 347 | + else throw std::runtime_error("Could not deserialise!"); | |
| 348 | + } | |
| 349 | + | |
| 350 | + inline void adl_serializer<std::variant<std::vector<quicktype::TopLevelElement>, bool, double, int64_t, std::map<std::string, json>, std::string>>::to_json(json & j, const std::variant<std::vector<quicktype::TopLevelElement>, bool, double, int64_t, std::map<std::string, json>, std::string> & x) { | |
| 351 | + switch (x.index()) { | |
| 352 | + case 0: | |
| 353 | + j = std::get<std::vector<quicktype::TopLevelElement>>(x); | |
| 354 | + break; | |
| 355 | + case 1: | |
| 356 | + j = std::get<bool>(x); | |
| 357 | + break; | |
| 358 | + case 2: | |
| 359 | + j = std::get<double>(x); | |
| 360 | + break; | |
| 361 | + case 3: | |
| 362 | + j = std::get<int64_t>(x); | |
| 363 | + break; | |
| 364 | + case 4: | |
| 365 | + j = std::get<std::map<std::string, json>>(x); | |
| 366 | + break; | |
| 367 | + case 5: | |
| 368 | + j = std::get<std::string>(x); | |
| 369 | + break; | |
| 370 | + default: throw std::runtime_error("Input JSON does not conform to schema!"); | |
| 371 | + } | |
| 372 | + } | |
| 373 | +} |
Test case
1 generated file · +20 −3test/inputs/schema/rust-cycle-breaker-union.schema
Mschema-cplusplusdefault / quicktype.hpp+20 −3
| @@ -89,9 +89,15 @@ namespace quicktype { | ||
| 89 | 89 | } |
| 90 | 90 | #endif |
| 91 | 91 | |
| 92 | + struct Next; | |
| 93 | + | |
| 92 | 94 | class Node; |
| 93 | 95 | |
| 94 | - using Next = std::shared_ptr<std::variant<std::shared_ptr<Node>, std::string>>; | |
| 96 | + struct Next : std::shared_ptr<std::variant<std::shared_ptr<Node>, std::string>> { | |
| 97 | + using base = std::shared_ptr<std::variant<std::shared_ptr<Node>, std::string>>; | |
| 98 | + using base::base; | |
| 99 | + using base::operator=; | |
| 100 | + }; | |
| 95 | 101 | |
| 96 | 102 | class Node { |
| 97 | 103 | public: |
| @@ -136,6 +142,9 @@ void to_json(json & j, const Node & x); | ||
| 136 | 142 | |
| 137 | 143 | void from_json(const json & j, TopLevel & x); |
| 138 | 144 | void to_json(json & j, const TopLevel & x); |
| 145 | + | |
| 146 | +void from_json(const json & j, Next & x); | |
| 147 | +void to_json(json & j, const Next & x); | |
| 139 | 148 | } |
| 140 | 149 | namespace nlohmann { |
| 141 | 150 | template <> |
| @@ -146,7 +155,7 @@ struct adl_serializer<std::variant<std::shared_ptr<quicktype::Node>, std::string | ||
| 146 | 155 | } |
| 147 | 156 | namespace quicktype { |
| 148 | 157 | inline void from_json(const json & j, Node& x) { |
| 149 | - x.set_next(get_heap_optional<std::variant<std::shared_ptr<Node>, std::string>>(j, "next")); | |
| 158 | + x.set_next(j.find("next") != j.end() ? j.at("next").get<Next>() : Next()); | |
| 150 | 159 | x.set_value(j.at("value").get<std::string>()); |
| 151 | 160 | } |
| 152 | 161 | |
| @@ -157,7 +166,7 @@ namespace quicktype { | ||
| 157 | 166 | } |
| 158 | 167 | |
| 159 | 168 | inline void from_json(const json & j, TopLevel& x) { |
| 160 | - x.set_next(get_heap_optional<std::variant<std::shared_ptr<Node>, std::string>>(j, "next")); | |
| 169 | + x.set_next(j.find("next") != j.end() ? j.at("next").get<Next>() : Next()); | |
| 161 | 170 | x.set_value(j.at("value").get<std::string>()); |
| 162 | 171 | } |
| 163 | 172 | |
| @@ -166,6 +175,14 @@ namespace quicktype { | ||
| 166 | 175 | j["next"] = x.get_next(); |
| 167 | 176 | j["value"] = x.get_value(); |
| 168 | 177 | } |
| 178 | + | |
| 179 | + inline void from_json(const json & j, Next & x) { | |
| 180 | + x = j.get<Next::base>(); | |
| 181 | + } | |
| 182 | + | |
| 183 | + inline void to_json(json & j, const Next & x) { | |
| 184 | + j = static_cast<const Next::base &>(x); | |
| 185 | + } | |
| 169 | 186 | } |
| 170 | 187 | namespace nlohmann { |
| 171 | 188 | inline void adl_serializer<std::variant<std::shared_ptr<quicktype::Node>, std::string>>::from_json(const json & j, std::variant<std::shared_ptr<quicktype::Node>, std::string> & x) { |
Test case
1 generated file · +18 −1test/inputs/schema/union-list.schema
Mschema-cplusplusdefault / quicktype.hpp+18 −1
| @@ -88,9 +88,15 @@ namespace quicktype { | ||
| 88 | 88 | } |
| 89 | 89 | #endif |
| 90 | 90 | |
| 91 | + struct UnionList; | |
| 92 | + | |
| 91 | 93 | class UnionListClass; |
| 92 | 94 | |
| 93 | - using UnionList = std::variant<std::shared_ptr<UnionListClass>, double>; | |
| 95 | + struct UnionList : std::variant<std::shared_ptr<UnionListClass>, double> { | |
| 96 | + using base = std::variant<std::shared_ptr<UnionListClass>, double>; | |
| 97 | + using base::base; | |
| 98 | + using base::operator=; | |
| 99 | + }; | |
| 94 | 100 | |
| 95 | 101 | class UnionListClass { |
| 96 | 102 | public: |
| @@ -127,6 +133,9 @@ void to_json(json & j, const UnionListClass & x); | ||
| 127 | 133 | |
| 128 | 134 | void from_json(const json & j, TopLevel & x); |
| 129 | 135 | void to_json(json & j, const TopLevel & x); |
| 136 | + | |
| 137 | +void from_json(const json & j, UnionList & x); | |
| 138 | +void to_json(json & j, const UnionList & x); | |
| 130 | 139 | } |
| 131 | 140 | namespace nlohmann { |
| 132 | 141 | template <> |
| @@ -153,6 +162,14 @@ namespace quicktype { | ||
| 153 | 162 | j = json::object(); |
| 154 | 163 | j["list"] = x.get_list(); |
| 155 | 164 | } |
| 165 | + | |
| 166 | + inline void from_json(const json & j, UnionList & x) { | |
| 167 | + x = j.get<UnionList::base>(); | |
| 168 | + } | |
| 169 | + | |
| 170 | + inline void to_json(json & j, const UnionList & x) { | |
| 171 | + j = static_cast<const UnionList::base &>(x); | |
| 172 | + } | |
| 156 | 173 | } |
| 157 | 174 | namespace nlohmann { |
| 158 | 175 | inline void adl_serializer<std::variant<std::shared_ptr<quicktype::UnionListClass>, double>>::from_json(const json & j, std::variant<std::shared_ptr<quicktype::UnionListClass>, double> & x) { |
Test case
1 generated file · +47 −14test/inputs/schema/vega-lite.schema
Mschema-cplusplusdefault / quicktype.hpp+47 −14
| @@ -221,6 +221,9 @@ namespace quicktype { | ||
| 221 | 221 | } |
| 222 | 222 | #endif |
| 223 | 223 | |
| 224 | + struct SelectionOperand; | |
| 225 | + struct LogicalOperandPredicate; | |
| 226 | + | |
| 224 | 227 | /** |
| 225 | 228 | * Determines how size calculation should be performed, one of `"content"` or `"padding"`. |
| 226 | 229 | * The default setting (`"content"`) interprets the width and height settings as the data |
| @@ -5039,7 +5042,11 @@ namespace quicktype { | ||
| 5039 | 5042 | |
| 5040 | 5043 | class Selection; |
| 5041 | 5044 | |
| 5042 | - using SelectionOperand = std::variant<std::shared_ptr<Selection>, std::string>; | |
| 5045 | + struct SelectionOperand : std::variant<std::shared_ptr<Selection>, std::string> { | |
| 5046 | + using base = std::variant<std::shared_ptr<Selection>, std::string>; | |
| 5047 | + using base::base; | |
| 5048 | + using base::operator=; | |
| 5049 | + }; | |
| 5043 | 5050 | |
| 5044 | 5051 | class Selection { |
| 5045 | 5052 | public: |
| @@ -5204,7 +5211,11 @@ namespace quicktype { | ||
| 5204 | 5211 | |
| 5205 | 5212 | class Predicate; |
| 5206 | 5213 | |
| 5207 | - using LogicalOperandPredicate = std::variant<std::shared_ptr<Predicate>, std::string>; | |
| 5214 | + struct LogicalOperandPredicate : std::variant<std::shared_ptr<Predicate>, std::string> { | |
| 5215 | + using base = std::variant<std::shared_ptr<Predicate>, std::string>; | |
| 5216 | + using base::base; | |
| 5217 | + using base::operator=; | |
| 5218 | + }; | |
| 5208 | 5219 | |
| 5209 | 5220 | class Predicate { |
| 5210 | 5221 | public: |
| @@ -9845,6 +9856,12 @@ void to_json(json & j, const ResolveMode & x); | ||
| 9845 | 9856 | |
| 9846 | 9857 | void from_json(const json & j, SelectionDefType & x); |
| 9847 | 9858 | void to_json(json & j, const SelectionDefType & x); |
| 9859 | + | |
| 9860 | +void from_json(const json & j, SelectionOperand & x); | |
| 9861 | +void to_json(json & j, const SelectionOperand & x); | |
| 9862 | + | |
| 9863 | +void from_json(const json & j, LogicalOperandPredicate & x); | |
| 9864 | +void to_json(json & j, const LogicalOperandPredicate & x); | |
| 9848 | 9865 | } |
| 9849 | 9866 | namespace nlohmann { |
| 9850 | 9867 | template <> |
| @@ -11078,7 +11095,7 @@ namespace quicktype { | ||
| 11078 | 11095 | } |
| 11079 | 11096 | |
| 11080 | 11097 | inline void from_json(const json & j, Selection& x) { |
| 11081 | - x.set_selection_not(get_heap_optional<std::variant<std::shared_ptr<Selection>, std::string>>(j, "not")); | |
| 11098 | + x.set_selection_not(get_heap_optional<SelectionOperand>(j, "not")); | |
| 11082 | 11099 | x.set_selection_and(get_stack_optional<std::vector<SelectionOperand>>(j, "and")); |
| 11083 | 11100 | x.set_selection_or(get_stack_optional<std::vector<SelectionOperand>>(j, "or")); |
| 11084 | 11101 | } |
| @@ -11118,7 +11135,7 @@ namespace quicktype { | ||
| 11118 | 11135 | } |
| 11119 | 11136 | |
| 11120 | 11137 | inline void from_json(const json & j, Predicate& x) { |
| 11121 | - x.set_predicate_not(get_heap_optional<std::variant<std::shared_ptr<Predicate>, std::string>>(j, "not")); | |
| 11138 | + x.set_predicate_not(get_heap_optional<LogicalOperandPredicate>(j, "not")); | |
| 11122 | 11139 | x.set_predicate_and(get_stack_optional<std::vector<LogicalOperandPredicate>>(j, "and")); |
| 11123 | 11140 | x.set_predicate_or(get_stack_optional<std::vector<LogicalOperandPredicate>>(j, "or")); |
| 11124 | 11141 | x.set_equal(get_stack_optional<std::variant<bool, DateTime, double, std::string>>(j, "equal")); |
| @@ -11126,7 +11143,7 @@ namespace quicktype { | ||
| 11126 | 11143 | x.set_time_unit(get_stack_optional<TimeUnit>(j, "timeUnit")); |
| 11127 | 11144 | x.set_range(get_stack_optional<std::vector<RangeElement>>(j, "range")); |
| 11128 | 11145 | x.set_one_of(get_stack_optional<std::vector<Equal>>(j, "oneOf")); |
| 11129 | - x.set_selection(get_heap_optional<std::variant<std::shared_ptr<Selection>, std::string>>(j, "selection")); | |
| 11146 | + x.set_selection(get_heap_optional<SelectionOperand>(j, "selection")); | |
| 11130 | 11147 | } |
| 11131 | 11148 | |
| 11132 | 11149 | inline void to_json(json & j, const Predicate & x) { |
| @@ -11143,9 +11160,9 @@ namespace quicktype { | ||
| 11143 | 11160 | } |
| 11144 | 11161 | |
| 11145 | 11162 | inline void from_json(const json & j, ConditionalValueDef& x) { |
| 11146 | - x.set_test(get_heap_optional<std::variant<std::shared_ptr<Predicate>, std::string>>(j, "test")); | |
| 11163 | + x.set_test(get_heap_optional<LogicalOperandPredicate>(j, "test")); | |
| 11147 | 11164 | x.set_value(j.at("value").get<ConditionalValueDefValue>()); |
| 11148 | - x.set_selection(get_heap_optional<std::variant<std::shared_ptr<Selection>, std::string>>(j, "selection")); | |
| 11165 | + x.set_selection(get_heap_optional<SelectionOperand>(j, "selection")); | |
| 11149 | 11166 | } |
| 11150 | 11167 | |
| 11151 | 11168 | inline void to_json(json & j, const ConditionalValueDef & x) { |
| @@ -11288,9 +11305,9 @@ namespace quicktype { | ||
| 11288 | 11305 | } |
| 11289 | 11306 | |
| 11290 | 11307 | inline void from_json(const json & j, ConditionalPredicateMarkPropFieldDefClass& x) { |
| 11291 | - x.set_test(get_heap_optional<std::variant<std::shared_ptr<Predicate>, std::string>>(j, "test")); | |
| 11308 | + x.set_test(get_heap_optional<LogicalOperandPredicate>(j, "test")); | |
| 11292 | 11309 | x.set_value(get_stack_optional<std::variant<bool, double, std::string>>(j, "value")); |
| 11293 | - x.set_selection(get_heap_optional<std::variant<std::shared_ptr<Selection>, std::string>>(j, "selection")); | |
| 11310 | + x.set_selection(get_heap_optional<SelectionOperand>(j, "selection")); | |
| 11294 | 11311 | x.set_aggregate(get_stack_optional<AggregateOp>(j, "aggregate")); |
| 11295 | 11312 | x.set_bin(get_stack_optional<std::variant<bool, BinParams>>(j, "bin")); |
| 11296 | 11313 | x.set_field(get_stack_optional<std::variant<RepeatRef, std::string>>(j, "field")); |
| @@ -11395,9 +11412,9 @@ namespace quicktype { | ||
| 11395 | 11412 | } |
| 11396 | 11413 | |
| 11397 | 11414 | inline void from_json(const json & j, ConditionalPredicateFieldDefClass& x) { |
| 11398 | - x.set_test(get_heap_optional<std::variant<std::shared_ptr<Predicate>, std::string>>(j, "test")); | |
| 11415 | + x.set_test(get_heap_optional<LogicalOperandPredicate>(j, "test")); | |
| 11399 | 11416 | x.set_value(get_stack_optional<std::variant<bool, double, std::string>>(j, "value")); |
| 11400 | - x.set_selection(get_heap_optional<std::variant<std::shared_ptr<Selection>, std::string>>(j, "selection")); | |
| 11417 | + x.set_selection(get_heap_optional<SelectionOperand>(j, "selection")); | |
| 11401 | 11418 | x.set_aggregate(get_stack_optional<AggregateOp>(j, "aggregate")); |
| 11402 | 11419 | x.set_bin(get_stack_optional<std::variant<bool, BinParams>>(j, "bin")); |
| 11403 | 11420 | x.set_field(get_stack_optional<std::variant<RepeatRef, std::string>>(j, "field")); |
| @@ -11458,9 +11475,9 @@ namespace quicktype { | ||
| 11458 | 11475 | } |
| 11459 | 11476 | |
| 11460 | 11477 | inline void from_json(const json & j, ConditionalPredicateTextFieldDefClass& x) { |
| 11461 | - x.set_test(get_heap_optional<std::variant<std::shared_ptr<Predicate>, std::string>>(j, "test")); | |
| 11478 | + x.set_test(get_heap_optional<LogicalOperandPredicate>(j, "test")); | |
| 11462 | 11479 | x.set_value(get_stack_optional<std::variant<bool, double, std::string>>(j, "value")); |
| 11463 | - x.set_selection(get_heap_optional<std::variant<std::shared_ptr<Selection>, std::string>>(j, "selection")); | |
| 11480 | + x.set_selection(get_heap_optional<SelectionOperand>(j, "selection")); | |
| 11464 | 11481 | x.set_aggregate(get_stack_optional<AggregateOp>(j, "aggregate")); |
| 11465 | 11482 | x.set_bin(get_stack_optional<std::variant<bool, BinParams>>(j, "bin")); |
| 11466 | 11483 | x.set_field(get_stack_optional<std::variant<RepeatRef, std::string>>(j, "field")); |
| @@ -11926,7 +11943,7 @@ namespace quicktype { | ||
| 11926 | 11943 | } |
| 11927 | 11944 | |
| 11928 | 11945 | inline void from_json(const json & j, Transform& x) { |
| 11929 | - x.set_filter(get_heap_optional<std::variant<std::shared_ptr<Predicate>, std::string>>(j, "filter")); | |
| 11946 | + x.set_filter(get_heap_optional<LogicalOperandPredicate>(j, "filter")); | |
| 11930 | 11947 | x.set_as(get_stack_optional<std::variant<std::vector<std::string>, std::string>>(j, "as")); |
| 11931 | 11948 | x.set_calculate(get_stack_optional<std::string>(j, "calculate")); |
| 11932 | 11949 | x.set_transform_default(get_stack_optional<std::string>(j, "default")); |
| @@ -12957,6 +12974,22 @@ namespace quicktype { | ||
| 12957 | 12974 | default: throw std::runtime_error("Unexpected value in enumeration \"SelectionDefType\": " + std::to_string(static_cast<int>(x))); |
| 12958 | 12975 | } |
| 12959 | 12976 | } |
| 12977 | + | |
| 12978 | + inline void from_json(const json & j, SelectionOperand & x) { | |
| 12979 | + x = j.get<SelectionOperand::base>(); | |
| 12980 | + } | |
| 12981 | + | |
| 12982 | + inline void to_json(json & j, const SelectionOperand & x) { | |
| 12983 | + j = static_cast<const SelectionOperand::base &>(x); | |
| 12984 | + } | |
| 12985 | + | |
| 12986 | + inline void from_json(const json & j, LogicalOperandPredicate & x) { | |
| 12987 | + x = j.get<LogicalOperandPredicate::base>(); | |
| 12988 | + } | |
| 12989 | + | |
| 12990 | + inline void to_json(json & j, const LogicalOperandPredicate & x) { | |
| 12991 | + j = static_cast<const LogicalOperandPredicate::base &>(x); | |
| 12992 | + } | |
| 12960 | 12993 | } |
| 12961 | 12994 | namespace nlohmann { |
| 12962 | 12995 | inline void adl_serializer<std::variant<quicktype::AutoSizeParams, quicktype::AutosizeType>>::from_json(const json & j, std::variant<quicktype::AutoSizeParams, quicktype::AutosizeType> & x) { |
No generated files match these filters.