Skip to content

Commit f27ed58

Browse files
committed
Now will output daw_json_link 2 bindings
1 parent 21bd10f commit f27ed58

File tree

5 files changed

+87
-88
lines changed

5 files changed

+87
-88
lines changed

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
cmake_minimum_required(VERSION 3.13)
1111

1212
project("json_to_cpp"
13-
VERSION "1.0.1"
13+
VERSION "2.0.0"
1414
DESCRIPTION "Try to use the JSON structures to make C++ structures and bindings"
1515
HOMEPAGE_URL "https://github.com/beached/json_to_cpp"
1616
LANGUAGES C CXX)
@@ -64,6 +64,7 @@ target_link_libraries( ${PROJECT_NAME} PRIVATE
6464
OpenSSL::SSL
6565
OpenSSL::Crypto
6666
CURL::libcurl
67+
fmt::fmt
6768
)
6869
target_include_directories( ${PROJECT_NAME} PRIVATE ${HEADER_FOLDER} )
6970

extern/CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,17 @@ FetchContent_Declare(
3535
GIT_REPOSITORY https://github.com/beached/libtemp_file.git
3636
)
3737

38+
FetchContent_Declare(
39+
fmtlib
40+
GIT_REPOSITORY https://github.com/fmtlib/fmt.git
41+
)
42+
3843
option( ENABLE_DATE_TESTING "" OFF )
3944
option( USE_SYSTEM_TZ_DB "" ON )
4045
FetchContent_Declare(
4146
date
4247
GIT_REPOSITORY https://github.com/howardhinnant/date.git
4348
)
4449

45-
FetchContent_MakeAvailable(daw_header_libraries daw_utf_range daw_parse_json daw_libtemp_file date )
50+
FetchContent_MakeAvailable(daw_header_libraries daw_utf_range daw_parse_json daw_libtemp_file date fmtlib )
4651

include/json_to_cpp_config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include <filesystem>
1212
#include <iostream>
13+
#include <optional>
1314
#include <string>
1415
#include <vector>
1516

include/types/ti_base.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ namespace daw::json_to_cpp::types::impl {
5757
if( parent_name.empty( ) ) {
5858
return name;
5959
}
60-
std::string result = "symbols_" + parent_name.to_string( ) + "::" + name;
60+
std::string result = "mem_" + name;
6161
return result;
6262
}
6363
} // namespace daw::json_to_cpp::types::impl

src/json_to_cpp.cpp

Lines changed: 77 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@
66
// Official repository: https://github.com/beached/daw_json_link
77
//
88

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"
1312

1413
#include <daw/daw_bounded_hash_set.h>
1514
#include <daw/daw_string_view.h>
1615
#include <daw/json/daw_json_parser.h>
1716

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>
2122

2223
namespace daw::json_to_cpp {
2324
namespace {
@@ -57,7 +58,8 @@ namespace daw::json_to_cpp {
5758
daw::make_bounded_hash_set<daw::string_view>( {
5859
"alignas", "alignof", "and", "and_eq", "asm", "atomic_cancel", "atomic_commit",
5960
"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",
6163
"const_cast", "continue", "decltype", "default", "delete", "do", "double", "dynamic_cast",
6264
"else", "enum", "explicit", "export", "extern", "false", "float", "for", "friend",
6365
"goto", "if", "import", "", "int", "long", "module", "mutable", "namespace",
@@ -73,7 +75,7 @@ namespace daw::json_to_cpp {
7375
// JSON member names are strings. That is it, so empty looks
7476
// like it is valid, as is all digits, or C++ keywords.
7577
if( name.empty( ) or
76-
!( std::isalpha( name.front( ) ) or name.front( ) == '_' ) or
78+
not( std::isalpha( name.front( ) ) or name.front( ) == '_' ) or
7779
keywords.count( { name.data( ), name.size( ) } ) > 0 ) {
7880

7981
std::string const prefix = "_json";
@@ -85,7 +87,7 @@ namespace daw::json_to_cpp {
8587
daw::algorithm::transform_it(
8688
name.begin( ), name.end( ), std::back_inserter( new_name ),
8789
[]( char c, auto it ) {
88-
if( !is_valid_id_char( c ) ) {
90+
if( not is_valid_id_char( c ) ) {
8991
std::string const new_value =
9092
"0x" + std::to_string( static_cast<int>( c ) );
9193
it = std::copy( new_value.begin( ), new_value.end( ), it );
@@ -335,88 +337,81 @@ namespace daw::json_to_cpp {
335337
}
336338

337339
void generate_json_link_maps( std::integral_constant<int, 3>,
338-
bool definition, config_t &config,
340+
config_t &config,
339341
types::ti_object const &cur_obj ) {
340-
if( !config.enable_jsonlink ) {
342+
if( not config.enable_jsonlink ) {
341343
return;
342344
}
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;
362351
}
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";
369365

370-
bool is_first = true;
366+
bool is_first = true;
371367

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;
393371
}
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";
395391

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;
410404
}
411-
config.cpp_file( ) << " );\n}\n\n";
405+
config.cpp_file( ) << "value." << child.first;
412406
}
407+
config.cpp_file( ) << " );\n}\n\t};\n}\n";
413408
}
414409

415-
void generate_json_link_maps( bool definition, config_t &config,
410+
void generate_json_link_maps( config_t &config,
416411
types::ti_object const &cur_obj ) {
417412

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 );
420415
}
421416

422417
void generate_includes( bool definition, config_t &config,
@@ -425,11 +420,11 @@ namespace daw::json_to_cpp {
425420
std::string const header_message =
426421
"// Code auto generated from json file '" +
427422
config.json_path.string( ) + "'\n\n";
428-
if( !definition ) {
423+
if( not definition ) {
429424
config.header_file( ) << header_message;
430425
}
431426
}
432-
if( !definition ) {
427+
if( not definition ) {
433428
config.header_file( ) << "#pragma once\n\n";
434429
if( config.enable_jsonlink ) {
435430
config.header_file( ) << "#include <tuple>\n";
@@ -481,19 +476,16 @@ namespace daw::json_to_cpp {
481476
}
482477
config.header_file( ) << "};"
483478
<< "\t// " << obj_type << "\n\n";
484-
if( config.enable_jsonlink ) {
485-
generate_json_link_maps( false, config, cur_obj );
486-
}
487479
}
488480
}
489481

490482
void generate_definitions( std::vector<types::ti_object> const &obj_info,
491483
config_t &config ) {
492-
if( !config.enable_jsonlink ) {
484+
if( not config.enable_jsonlink ) {
493485
return;
494486
}
495487
for( auto const &cur_obj : obj_info ) {
496-
generate_json_link_maps( true, config, cur_obj );
488+
generate_json_link_maps( config, cur_obj );
497489
}
498490
}
499491

0 commit comments

Comments
 (0)