|
6 | 6 | // Official repository: https://github.com/beached/daw_json_link |
7 | 7 | // |
8 | 8 |
|
9 | | -#include <algorithm> |
10 | | -#include <limits> |
11 | | -#include <string> |
12 | | -#include <typeindex> |
| 9 | +#include "json_to_cpp.h" |
| 10 | +#include "ti_value.h" |
| 11 | +#include "types/ti_types.h" |
13 | 12 |
|
14 | 13 | #include <daw/daw_bounded_hash_set.h> |
15 | 14 | #include <daw/daw_string_view.h> |
16 | 15 | #include <daw/json/daw_json_parser.h> |
17 | 16 |
|
18 | | -#include "json_to_cpp.h" |
19 | | -#include "ti_value.h" |
20 | | -#include "types/ti_types.h" |
| 17 | +#include <algorithm> |
| 18 | +#include <fmt/core.h> |
| 19 | +#include <limits> |
| 20 | +#include <string> |
| 21 | +#include <typeindex> |
21 | 22 |
|
22 | 23 | namespace daw::json_to_cpp { |
23 | 24 | namespace { |
@@ -57,7 +58,8 @@ namespace daw::json_to_cpp { |
57 | 58 | daw::make_bounded_hash_set<daw::string_view>( { |
58 | 59 | "alignas", "alignof", "and", "and_eq", "asm", "atomic_cancel", "atomic_commit", |
59 | 60 | "atomic_noexcept", "auto", "bitand", "bitor", "bool", "break", "case", "catch", |
60 | | - "char", "char16_t", "char32_t", "class", "compl", "concept", "const", "constexpr", |
| 61 | + "char", "char8_t", "char16_t", "char32_t", "class", "compl", "concept", "const", |
| 62 | + "consteval", "constexpr", "constinit", "co_await", "co_return", "co_yield", |
61 | 63 | "const_cast", "continue", "decltype", "default", "delete", "do", "double", "dynamic_cast", |
62 | 64 | "else", "enum", "explicit", "export", "extern", "false", "float", "for", "friend", |
63 | 65 | "goto", "if", "import", "", "int", "long", "module", "mutable", "namespace", |
@@ -73,7 +75,7 @@ namespace daw::json_to_cpp { |
73 | 75 | // JSON member names are strings. That is it, so empty looks |
74 | 76 | // like it is valid, as is all digits, or C++ keywords. |
75 | 77 | if( name.empty( ) or |
76 | | - !( std::isalpha( name.front( ) ) or name.front( ) == '_' ) or |
| 78 | + not( std::isalpha( name.front( ) ) or name.front( ) == '_' ) or |
77 | 79 | keywords.count( { name.data( ), name.size( ) } ) > 0 ) { |
78 | 80 |
|
79 | 81 | std::string const prefix = "_json"; |
@@ -85,7 +87,7 @@ namespace daw::json_to_cpp { |
85 | 87 | daw::algorithm::transform_it( |
86 | 88 | name.begin( ), name.end( ), std::back_inserter( new_name ), |
87 | 89 | []( char c, auto it ) { |
88 | | - if( !is_valid_id_char( c ) ) { |
| 90 | + if( not is_valid_id_char( c ) ) { |
89 | 91 | std::string const new_value = |
90 | 92 | "0x" + std::to_string( static_cast<int>( c ) ); |
91 | 93 | it = std::copy( new_value.begin( ), new_value.end( ), it ); |
@@ -335,88 +337,81 @@ namespace daw::json_to_cpp { |
335 | 337 | } |
336 | 338 |
|
337 | 339 | void generate_json_link_maps( std::integral_constant<int, 3>, |
338 | | - bool definition, config_t &config, |
| 340 | + config_t &config, |
339 | 341 | types::ti_object const &cur_obj ) { |
340 | | - if( !config.enable_jsonlink ) { |
| 342 | + if( not config.enable_jsonlink ) { |
341 | 343 | return; |
342 | 344 | } |
343 | | - if( !definition ) { |
344 | | - using daw::json::json_value_t; |
345 | | - config.cpp_file( ) << "namespace symbols_" << cur_obj.object_name |
346 | | - << " {\n"; |
347 | | - for( auto const &child : *cur_obj.children ) { |
348 | | - if( config.hide_null_only and is_null( child.second ) ) { |
349 | | - continue; |
350 | | - } |
351 | | - config.cpp_file( ) |
352 | | - << "\tstatic constexpr char const " << child.first << "[] = \""; |
353 | | - if( daw::string_view( child.first.data( ), child.first.size( ) ) |
354 | | - .starts_with( "_json" ) ) { |
355 | | - config.cpp_file( ) |
356 | | - << daw::string_view( child.first.data( ), child.first.size( ) ) |
357 | | - .substr( 5 ) |
358 | | - << "\";\n"; |
359 | | - } else { |
360 | | - config.cpp_file( ) << child.first << "\";\n"; |
361 | | - } |
| 345 | + using daw::json::json_value_t; |
| 346 | + config.cpp_file( ) << fmt::format( "namespace daw::json {{\n", |
| 347 | + cur_obj.object_name ); |
| 348 | + for( auto const &child : *cur_obj.children ) { |
| 349 | + if( config.hide_null_only and is_null( child.second ) ) { |
| 350 | + continue; |
362 | 351 | } |
363 | | - config.cpp_file( ) << "}\n\n"; |
364 | | - config.cpp_file( ) << "inline auto describe_json_class( " |
365 | | - << cur_obj.object_name |
366 | | - << " ) {\n\tusing namespace daw::json;\n"; |
367 | | - |
368 | | - config.cpp_file( ) << "\treturn daw::json::class_description_t<\n"; |
| 352 | + config.cpp_file( ) << fmt::format( |
| 353 | + "\ttemplate<>\n\tstruct json_data_contract<{}> {{\n", |
| 354 | + cur_obj.object_name ); |
| 355 | + config.cpp_file( ) << fmt::format( |
| 356 | + "\t\tstatic constexpr char const mem_{}[] = \"", child.first ); |
| 357 | + auto child_name = |
| 358 | + daw::string_view( child.first.data( ), child.first.size( ) ); |
| 359 | + if( child_name.starts_with( "_json" ) ) { |
| 360 | + child_name.remove_prefix( 5 ); |
| 361 | + } |
| 362 | + config.cpp_file( ) << child_name << "\";\n"; |
| 363 | + } |
| 364 | + config.cpp_file( ) << "\t\t using type = json_member_list<\n"; |
369 | 365 |
|
370 | | - bool is_first = true; |
| 366 | + bool is_first = true; |
371 | 367 |
|
372 | | - for( auto const &child : *cur_obj.children ) { |
373 | | - if( config.hide_null_only and is_null( child.second ) ) { |
374 | | - continue; |
375 | | - } |
376 | | - config.cpp_file( ) << "\t\t"; |
377 | | - if( !is_first ) { |
378 | | - config.cpp_file( ) << ","; |
379 | | - } else { |
380 | | - is_first = false; |
381 | | - } |
382 | | - if( is_optional( child.second ) ) { |
383 | | - config.cpp_file( ) << "json_nullable<"; |
384 | | - } |
385 | | - config.cpp_file( ) << types::ti_value( child.second ) |
386 | | - .json_name( child.first, config.has_cpp20, |
387 | | - cur_obj.object_name ); |
388 | | - if( is_optional( child.second ) ) { |
389 | | - config.cpp_file( ) << ">\n"; |
390 | | - } else { |
391 | | - config.cpp_file( ) << '\n'; |
392 | | - } |
| 368 | + for( auto const &child : *cur_obj.children ) { |
| 369 | + if( config.hide_null_only and is_null( child.second ) ) { |
| 370 | + continue; |
393 | 371 | } |
394 | | - config.cpp_file( ) << "\t>{};\n}\n\n"; |
| 372 | + config.cpp_file( ) << "\t\t\t\t"; |
| 373 | + if( not is_first ) { |
| 374 | + config.cpp_file( ) << ","; |
| 375 | + } else { |
| 376 | + is_first = false; |
| 377 | + } |
| 378 | + if( is_optional( child.second ) ) { |
| 379 | + config.cpp_file( ) << "json_nullable<"; |
| 380 | + } |
| 381 | + config.cpp_file( ) << types::ti_value( child.second ) |
| 382 | + .json_name( child.first, config.has_cpp20, |
| 383 | + cur_obj.object_name ); |
| 384 | + if( is_optional( child.second ) ) { |
| 385 | + config.cpp_file( ) << ">\n"; |
| 386 | + } else { |
| 387 | + config.cpp_file( ) << '\n'; |
| 388 | + } |
| 389 | + } |
| 390 | + config.cpp_file( ) << "\t>;\n\n"; |
395 | 391 |
|
396 | | - config.cpp_file( ) << "inline auto to_json_data( " |
397 | | - << cur_obj.object_name << " const & value ) {\n"; |
398 | | - config.cpp_file( ) << "\treturn std::forward_as_tuple( "; |
399 | | - is_first = true; |
400 | | - for( auto const &child : *cur_obj.children ) { |
401 | | - if( config.hide_null_only and is_null( child.second ) ) { |
402 | | - continue; |
403 | | - } |
404 | | - if( !is_first ) { |
405 | | - config.cpp_file( ) << ", "; |
406 | | - } else { |
407 | | - is_first = !is_first; |
408 | | - } |
409 | | - config.cpp_file( ) << "value." << child.first; |
| 392 | + config.cpp_file( ) << "\t\tstatic inline auto to_json_data( " << cur_obj.object_name |
| 393 | + << " const & value ) {\n"; |
| 394 | + config.cpp_file( ) << "\t\t\treturn std::forward_as_tuple( "; |
| 395 | + is_first = true; |
| 396 | + for( auto const &child : *cur_obj.children ) { |
| 397 | + if( config.hide_null_only and is_null( child.second ) ) { |
| 398 | + continue; |
| 399 | + } |
| 400 | + if( not is_first ) { |
| 401 | + config.cpp_file( ) << ", "; |
| 402 | + } else { |
| 403 | + is_first = not is_first; |
410 | 404 | } |
411 | | - config.cpp_file( ) << " );\n}\n\n"; |
| 405 | + config.cpp_file( ) << "value." << child.first; |
412 | 406 | } |
| 407 | + config.cpp_file( ) << " );\n}\n\t};\n}\n"; |
413 | 408 | } |
414 | 409 |
|
415 | | - void generate_json_link_maps( bool definition, config_t &config, |
| 410 | + void generate_json_link_maps( config_t &config, |
416 | 411 | types::ti_object const &cur_obj ) { |
417 | 412 |
|
418 | | - return generate_json_link_maps( std::integral_constant<int, 3>( ), |
419 | | - definition, config, cur_obj ); |
| 413 | + generate_json_link_maps( std::integral_constant<int, 3>( ), config, |
| 414 | + cur_obj ); |
420 | 415 | } |
421 | 416 |
|
422 | 417 | void generate_includes( bool definition, config_t &config, |
@@ -425,11 +420,11 @@ namespace daw::json_to_cpp { |
425 | 420 | std::string const header_message = |
426 | 421 | "// Code auto generated from json file '" + |
427 | 422 | config.json_path.string( ) + "'\n\n"; |
428 | | - if( !definition ) { |
| 423 | + if( not definition ) { |
429 | 424 | config.header_file( ) << header_message; |
430 | 425 | } |
431 | 426 | } |
432 | | - if( !definition ) { |
| 427 | + if( not definition ) { |
433 | 428 | config.header_file( ) << "#pragma once\n\n"; |
434 | 429 | if( config.enable_jsonlink ) { |
435 | 430 | config.header_file( ) << "#include <tuple>\n"; |
@@ -481,19 +476,16 @@ namespace daw::json_to_cpp { |
481 | 476 | } |
482 | 477 | config.header_file( ) << "};" |
483 | 478 | << "\t// " << obj_type << "\n\n"; |
484 | | - if( config.enable_jsonlink ) { |
485 | | - generate_json_link_maps( false, config, cur_obj ); |
486 | | - } |
487 | 479 | } |
488 | 480 | } |
489 | 481 |
|
490 | 482 | void generate_definitions( std::vector<types::ti_object> const &obj_info, |
491 | 483 | config_t &config ) { |
492 | | - if( !config.enable_jsonlink ) { |
| 484 | + if( not config.enable_jsonlink ) { |
493 | 485 | return; |
494 | 486 | } |
495 | 487 | for( auto const &cur_obj : obj_info ) { |
496 | | - generate_json_link_maps( true, config, cur_obj ); |
| 488 | + generate_json_link_maps( config, cur_obj ); |
497 | 489 | } |
498 | 490 | } |
499 | 491 |
|
|