|
1 |
| -# json_to_cpp |
2 |
| -Generate boilerplate C++ code, that uses parse_json library, from example json |
| 1 | +# JSON to C++ |
| 2 | +This program will take either a json file or a URL to a web service and build C++ classes to work with that data. By default it will create the serialization linkage for the JsonLink library that is part of https://github.com/beached/parse_json . |
3 | 3 |
|
| 4 | +# Requirements |
| 5 | +* CMake >= 2.8.12 https://cmake.org/ |
| 6 | +* Boost >= 1.58 https://www.boost.org/ |
| 7 | +* Curl https://curl.haxx.se/ |
| 8 | + |
| 9 | +Both Boost and Curl are often packaged within a Linux distribution and available for Windows/Mac |
| 10 | + |
| 11 | +# Building |
| 12 | +The build system uses cmake. Often a build can be accomplished by creating a build folder in the project source folder and typing |
| 13 | +``` |
| 14 | +cmake .. |
| 15 | +cmake --build . |
| 16 | +``` |
| 17 | +# Running |
| 18 | +To output the C++ code to the terminal one just needs to type ```json_to_cpp_bin --in_file jsonfile.json``` |
| 19 | + |
| 20 | +``` |
| 21 | +Command line options |
| 22 | +Options: |
| 23 | + --help print option descriptions |
| 24 | + --in_file arg json source file path or url |
| 25 | + --out_file arg output c++ file |
| 26 | + --use_jsonlink arg (=1) Use JsonLink serializaion/deserialization |
| 27 | +``` |
| 28 | +# Example |
| 29 | +## H2 JSON Data |
| 30 | +``` |
| 31 | +{ |
| 32 | + "glossary": { |
| 33 | + "title": "example glossary", |
| 34 | + "GlossDiv": { |
| 35 | + "title": "S", |
| 36 | + "GlossList": { |
| 37 | + "GlossEntry": { |
| 38 | + "ID": "SGML", |
| 39 | + "SortAs": "SGML", |
| 40 | + "GlossTerm": "Standard Generalized Markup Language", |
| 41 | + "Acronym": "SGML", |
| 42 | + "Abbrev": "ISO 8879:1986", |
| 43 | + "GlossDef": { |
| 44 | + "para": "A meta-markup language, used to create markup languages such as DocBook.", |
| 45 | + "GlossSeeAlso": ["GML", "XML"] |
| 46 | + }, |
| 47 | + "GlossSee": "markup" |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | +``` |
| 54 | +## Generated C++ Code |
| 55 | +``` |
| 56 | +#include <string> |
| 57 | +#include <vector> |
| 58 | +#include <daw/json/daw_json_link.h> |
| 59 | +
|
| 60 | +struct GlossDef_t: public daw::json::JsonLink<GlossDef_t> { |
| 61 | + std::vector<std::string> GlossSeeAlso; |
| 62 | + std::string para; |
| 63 | +
|
| 64 | + GlossDef_t( ); |
| 65 | + GlossDef_t( GlossDef_t const & other ); |
| 66 | + GlossDef_t( GlossDef_t && other ); |
| 67 | + ~GlossDef_t( ); |
| 68 | +
|
| 69 | + GlossDef_t & operator=( GlossDef_t const & ) = default; |
| 70 | + GlossDef_t & operator=( GlossDef_t && ) = default; |
| 71 | +private: |
| 72 | + void set_links( ); |
| 73 | +}; // GlossDef_t |
| 74 | +
|
| 75 | +struct GlossEntry_t: public daw::json::JsonLink<GlossEntry_t> { |
| 76 | + std::string GlossSee; |
| 77 | + GlossDef_t GlossDef; |
| 78 | + std::string Abbrev; |
| 79 | + std::string ID; |
| 80 | + std::string SortAs; |
| 81 | + std::string GlossTerm; |
| 82 | + std::string Acronym; |
| 83 | +
|
| 84 | + GlossEntry_t( ); |
| 85 | + GlossEntry_t( GlossEntry_t const & other ); |
| 86 | + GlossEntry_t( GlossEntry_t && other ); |
| 87 | + ~GlossEntry_t( ); |
| 88 | +
|
| 89 | + GlossEntry_t & operator=( GlossEntry_t const & ) = default; |
| 90 | + GlossEntry_t & operator=( GlossEntry_t && ) = default; |
| 91 | +private: |
| 92 | + void set_links( ); |
| 93 | +}; // GlossEntry_t |
| 94 | +
|
| 95 | +struct GlossList_t: public daw::json::JsonLink<GlossList_t> { |
| 96 | + GlossEntry_t GlossEntry; |
| 97 | +
|
| 98 | + GlossList_t( ); |
| 99 | + GlossList_t( GlossList_t const & other ); |
| 100 | + GlossList_t( GlossList_t && other ); |
| 101 | + ~GlossList_t( ); |
| 102 | +
|
| 103 | + GlossList_t & operator=( GlossList_t const & ) = default; |
| 104 | + GlossList_t & operator=( GlossList_t && ) = default; |
| 105 | +private: |
| 106 | + void set_links( ); |
| 107 | +}; // GlossList_t |
| 108 | +
|
| 109 | +struct GlossDiv_t: public daw::json::JsonLink<GlossDiv_t> { |
| 110 | + GlossList_t GlossList; |
| 111 | + std::string title; |
| 112 | +
|
| 113 | + GlossDiv_t( ); |
| 114 | + GlossDiv_t( GlossDiv_t const & other ); |
| 115 | + GlossDiv_t( GlossDiv_t && other ); |
| 116 | + ~GlossDiv_t( ); |
| 117 | +
|
| 118 | + GlossDiv_t & operator=( GlossDiv_t const & ) = default; |
| 119 | + GlossDiv_t & operator=( GlossDiv_t && ) = default; |
| 120 | +private: |
| 121 | + void set_links( ); |
| 122 | +}; // GlossDiv_t |
| 123 | +
|
| 124 | +struct glossary_t: public daw::json::JsonLink<glossary_t> { |
| 125 | + GlossDiv_t GlossDiv; |
| 126 | + std::string title; |
| 127 | +
|
| 128 | + glossary_t( ); |
| 129 | + glossary_t( glossary_t const & other ); |
| 130 | + glossary_t( glossary_t && other ); |
| 131 | + ~glossary_t( ); |
| 132 | +
|
| 133 | + glossary_t & operator=( glossary_t const & ) = default; |
| 134 | + glossary_t & operator=( glossary_t && ) = default; |
| 135 | +private: |
| 136 | + void set_links( ); |
| 137 | +}; // glossary_t |
| 138 | +
|
| 139 | +struct root_object_t: public daw::json::JsonLink<root_object_t> { |
| 140 | + glossary_t glossary; |
| 141 | +
|
| 142 | + root_object_t( ); |
| 143 | + root_object_t( root_object_t const & other ); |
| 144 | + root_object_t( root_object_t && other ); |
| 145 | + ~root_object_t( ); |
| 146 | +
|
| 147 | + root_object_t & operator=( root_object_t const & ) = default; |
| 148 | + root_object_t & operator=( root_object_t && ) = default; |
| 149 | +private: |
| 150 | + void set_links( ); |
| 151 | +}; // root_object_t |
| 152 | +
|
| 153 | +GlossDef_t::GlossDef_t( ): |
| 154 | + daw::json::JsonLink<GlossDef_t>{ }, |
| 155 | + GlossSeeAlso{ }, |
| 156 | + para{ } { |
| 157 | +
|
| 158 | + set_links( ); |
| 159 | +} |
| 160 | +
|
| 161 | +GlossDef_t::GlossDef_t( GlossDef_t const & other ): |
| 162 | + daw::json::JsonLink<GlossDef_t>{ }, |
| 163 | + GlossSeeAlso{ other.GlossSeeAlso }, |
| 164 | + para{ other.para } { |
| 165 | +
|
| 166 | + set_links( ); |
| 167 | +} |
| 168 | +
|
| 169 | +GlossDef_t::GlossDef_t( GlossDef_t && other ): |
| 170 | + daw::json::JsonLink<GlossDef_t>{ }, |
| 171 | + GlossSeeAlso{ std::move( other.GlossSeeAlso ) }, |
| 172 | + para{ std::move( other.para ) } { |
| 173 | +
|
| 174 | + set_links( ); |
| 175 | +} |
| 176 | +
|
| 177 | +GlossDef_t::~GlossDef_t( ) { } |
| 178 | +
|
| 179 | +void GlossDef_t::set_links( ) { |
| 180 | + link_array( "GlossSeeAlso", GlossSeeAlso ); |
| 181 | + link_string( "para", para ); |
| 182 | +} |
| 183 | +
|
| 184 | +GlossEntry_t::GlossEntry_t( ): |
| 185 | + daw::json::JsonLink<GlossEntry_t>{ }, |
| 186 | + GlossSee{ }, |
| 187 | + GlossDef{ }, |
| 188 | + Abbrev{ }, |
| 189 | + ID{ }, |
| 190 | + SortAs{ }, |
| 191 | + GlossTerm{ }, |
| 192 | + Acronym{ } { |
| 193 | +
|
| 194 | + set_links( ); |
| 195 | +} |
| 196 | +
|
| 197 | +GlossEntry_t::GlossEntry_t( GlossEntry_t const & other ): |
| 198 | + daw::json::JsonLink<GlossEntry_t>{ }, |
| 199 | + GlossSee{ other.GlossSee }, |
| 200 | + GlossDef{ other.GlossDef }, |
| 201 | + Abbrev{ other.Abbrev }, |
| 202 | + ID{ other.ID }, |
| 203 | + SortAs{ other.SortAs }, |
| 204 | + GlossTerm{ other.GlossTerm }, |
| 205 | + Acronym{ other.Acronym } { |
| 206 | +
|
| 207 | + set_links( ); |
| 208 | +} |
| 209 | +
|
| 210 | +GlossEntry_t::GlossEntry_t( GlossEntry_t && other ): |
| 211 | + daw::json::JsonLink<GlossEntry_t>{ }, |
| 212 | + GlossSee{ std::move( other.GlossSee ) }, |
| 213 | + GlossDef{ std::move( other.GlossDef ) }, |
| 214 | + Abbrev{ std::move( other.Abbrev ) }, |
| 215 | + ID{ std::move( other.ID ) }, |
| 216 | + SortAs{ std::move( other.SortAs ) }, |
| 217 | + GlossTerm{ std::move( other.GlossTerm ) }, |
| 218 | + Acronym{ std::move( other.Acronym ) } { |
| 219 | +
|
| 220 | + set_links( ); |
| 221 | +} |
| 222 | +
|
| 223 | +GlossEntry_t::~GlossEntry_t( ) { } |
| 224 | +
|
| 225 | +void GlossEntry_t::set_links( ) { |
| 226 | + link_string( "GlossSee", GlossSee ); |
| 227 | + link_object( "GlossDef", GlossDef ); |
| 228 | + link_string( "Abbrev", Abbrev ); |
| 229 | + link_string( "ID", ID ); |
| 230 | + link_string( "SortAs", SortAs ); |
| 231 | + link_string( "GlossTerm", GlossTerm ); |
| 232 | + link_string( "Acronym", Acronym ); |
| 233 | +} |
| 234 | +
|
| 235 | +GlossList_t::GlossList_t( ): |
| 236 | + daw::json::JsonLink<GlossList_t>{ }, |
| 237 | + GlossEntry{ } { |
| 238 | +
|
| 239 | + set_links( ); |
| 240 | +} |
| 241 | +
|
| 242 | +GlossList_t::GlossList_t( GlossList_t const & other ): |
| 243 | + daw::json::JsonLink<GlossList_t>{ }, |
| 244 | + GlossEntry{ other.GlossEntry } { |
| 245 | +
|
| 246 | + set_links( ); |
| 247 | +} |
| 248 | +
|
| 249 | +GlossList_t::GlossList_t( GlossList_t && other ): |
| 250 | + daw::json::JsonLink<GlossList_t>{ }, |
| 251 | + GlossEntry{ std::move( other.GlossEntry ) } { |
| 252 | +
|
| 253 | + set_links( ); |
| 254 | +} |
| 255 | +
|
| 256 | +GlossList_t::~GlossList_t( ) { } |
| 257 | +
|
| 258 | +void GlossList_t::set_links( ) { |
| 259 | + link_object( "GlossEntry", GlossEntry ); |
| 260 | +} |
| 261 | +
|
| 262 | +GlossDiv_t::GlossDiv_t( ): |
| 263 | + daw::json::JsonLink<GlossDiv_t>{ }, |
| 264 | + GlossList{ }, |
| 265 | + title{ } { |
| 266 | +
|
| 267 | + set_links( ); |
| 268 | +} |
| 269 | +
|
| 270 | +GlossDiv_t::GlossDiv_t( GlossDiv_t const & other ): |
| 271 | + daw::json::JsonLink<GlossDiv_t>{ }, |
| 272 | + GlossList{ other.GlossList }, |
| 273 | + title{ other.title } { |
| 274 | +
|
| 275 | + set_links( ); |
| 276 | +} |
| 277 | +
|
| 278 | +GlossDiv_t::GlossDiv_t( GlossDiv_t && other ): |
| 279 | + daw::json::JsonLink<GlossDiv_t>{ }, |
| 280 | + GlossList{ std::move( other.GlossList ) }, |
| 281 | + title{ std::move( other.title ) } { |
| 282 | +
|
| 283 | + set_links( ); |
| 284 | +} |
| 285 | +
|
| 286 | +GlossDiv_t::~GlossDiv_t( ) { } |
| 287 | +
|
| 288 | +void GlossDiv_t::set_links( ) { |
| 289 | + link_object( "GlossList", GlossList ); |
| 290 | + link_string( "title", title ); |
| 291 | +} |
| 292 | +
|
| 293 | +glossary_t::glossary_t( ): |
| 294 | + daw::json::JsonLink<glossary_t>{ }, |
| 295 | + GlossDiv{ }, |
| 296 | + title{ } { |
| 297 | +
|
| 298 | + set_links( ); |
| 299 | +} |
| 300 | +
|
| 301 | +glossary_t::glossary_t( glossary_t const & other ): |
| 302 | + daw::json::JsonLink<glossary_t>{ }, |
| 303 | + GlossDiv{ other.GlossDiv }, |
| 304 | + title{ other.title } { |
| 305 | +
|
| 306 | + set_links( ); |
| 307 | +} |
| 308 | +
|
| 309 | +glossary_t::glossary_t( glossary_t && other ): |
| 310 | + daw::json::JsonLink<glossary_t>{ }, |
| 311 | + GlossDiv{ std::move( other.GlossDiv ) }, |
| 312 | + title{ std::move( other.title ) } { |
| 313 | +
|
| 314 | + set_links( ); |
| 315 | +} |
| 316 | +
|
| 317 | +glossary_t::~glossary_t( ) { } |
| 318 | +
|
| 319 | +void glossary_t::set_links( ) { |
| 320 | + link_object( "GlossDiv", GlossDiv ); |
| 321 | + link_string( "title", title ); |
| 322 | +} |
| 323 | +
|
| 324 | +root_object_t::root_object_t( ): |
| 325 | + daw::json::JsonLink<root_object_t>{ }, |
| 326 | + glossary{ } { |
| 327 | +
|
| 328 | + set_links( ); |
| 329 | +} |
| 330 | +
|
| 331 | +root_object_t::root_object_t( root_object_t const & other ): |
| 332 | + daw::json::JsonLink<root_object_t>{ }, |
| 333 | + glossary{ other.glossary } { |
| 334 | +
|
| 335 | + set_links( ); |
| 336 | +} |
| 337 | +
|
| 338 | +root_object_t::root_object_t( root_object_t && other ): |
| 339 | + daw::json::JsonLink<root_object_t>{ }, |
| 340 | + glossary{ std::move( other.glossary ) } { |
| 341 | +
|
| 342 | + set_links( ); |
| 343 | +} |
| 344 | +
|
| 345 | +root_object_t::~root_object_t( ) { } |
| 346 | +
|
| 347 | +void root_object_t::set_links( ) { |
| 348 | + link_object( "glossary", glossary ); |
| 349 | +} |
| 350 | +``` |
0 commit comments