Skip to content

Commit a5c56f2

Browse files
committed
Started adapting to daw_json_link project for json_links
1 parent 2476625 commit a5c56f2

1 file changed

Lines changed: 66 additions & 136 deletions

File tree

src/json_to_cpp.cpp

Lines changed: 66 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <tuple>
3030
#include <unordered_map>
3131
#include <unordered_set>
32+
#include <typeindex>
3233

3334
#include <daw/json/daw_json_link.h>
3435

@@ -90,7 +91,8 @@ namespace daw {
9091
std::map<std::string, ti_value> & children( );
9192
bool & is_optional( ) noexcept;
9293
bool const & is_optional( ) const noexcept;
93-
daw::json::impl::value_t::value_types type( ) const;
94+
95+
std::type_index type( ) const;
9496

9597
ti_value( ): value{ nullptr } { }
9698
~ti_value( );
@@ -142,7 +144,7 @@ namespace daw {
142144
type_info_t & operator=( type_info_t && ) = default;
143145
virtual ~type_info_t( );
144146

145-
virtual daw::json::impl::value_t::value_types type( ) const = 0;
147+
virtual std::type_index type( ) const = 0;
146148
virtual std::string name( ) const = 0;
147149

148150
virtual type_info_t * clone( ) const = 0;
@@ -154,7 +156,7 @@ namespace daw {
154156
return value->name( );
155157
}
156158

157-
daw::json::impl::value_t::value_types ti_value::type( ) const {
159+
std::type_index ti_value::type( ) const {
158160
return value->type( );
159161
}
160162
std::map<std::string, ti_value> const & ti_value::children( ) const {
@@ -174,8 +176,8 @@ namespace daw {
174176
}
175177

176178
struct ti_null: public type_info_t {
177-
daw::json::impl::value_t::value_types type( ) const override {
178-
return daw::json::impl::value_t::value_types::null;
179+
std::type_index type( ) const override {
180+
return std::type_index( typeid( daw::json::json_value_t::null_t ) );
179181
}
180182

181183
std::string name( ) const override {
@@ -202,8 +204,8 @@ namespace daw {
202204
}
203205

204206
struct ti_integral: public type_info_t {
205-
daw::json::impl::value_t::value_types type( ) const override {
206-
return daw::json::impl::value_t::value_types::integral;
207+
std::type_index type( ) const override {
208+
return std::type_index( typeid( daw::json::json_value_t::integer_t ) );
207209
}
208210

209211
std::string name( ) const override {
@@ -216,8 +218,8 @@ namespace daw {
216218
};
217219

218220
struct ti_real: public type_info_t {
219-
daw::json::impl::value_t::value_types type( ) const override {
220-
return daw::json::impl::value_t::value_types::real;
221+
std::type_index type( ) const override {
222+
return std::type_index( typeid( daw::json::json_value_t::real_t ) );
221223
}
222224

223225
std::string name( ) const override {
@@ -230,8 +232,8 @@ namespace daw {
230232
};
231233

232234
struct ti_boolean: public type_info_t {
233-
daw::json::impl::value_t::value_types type( ) const override {
234-
return daw::json::impl::value_t::value_types::boolean;
235+
std::type_index type( ) const override {
236+
return std::type_index( typeid( daw::json::json_value_t::boolean_t ) );
235237
}
236238

237239
std::string name( ) const override {
@@ -244,8 +246,8 @@ namespace daw {
244246
};
245247

246248
struct ti_string: public type_info_t {
247-
daw::json::impl::value_t::value_types type( ) const override {
248-
return daw::json::impl::value_t::value_types::string;
249+
std::type_index type( ) const override {
250+
return std::type_index( typeid( daw::json::json_value_t::string_t ) );
249251
}
250252

251253
std::string name( ) const override {
@@ -260,8 +262,8 @@ namespace daw {
260262
struct ti_object: public type_info_t {
261263
std::string object_name;
262264

263-
daw::json::impl::value_t::value_types type( ) const override {
264-
return daw::json::impl::value_t::value_types::object;
265+
std::type_index type( ) const override {
266+
return std::type_index( typeid( daw::json::json_value_t::object_t ) );
265267
}
266268

267269
std::string name( ) const override {
@@ -277,8 +279,8 @@ namespace daw {
277279
};
278280

279281
struct ti_array: public type_info_t {
280-
daw::json::impl::value_t::value_types type( ) const override {
281-
return daw::json::impl::value_t::value_types::array;
282+
std::type_index type( ) const override {
283+
return std::type_index( typeid( daw::json::json_value_t::array_t ) );
282284
}
283285

284286
std::string name( ) const override {
@@ -319,43 +321,48 @@ namespace daw {
319321
}
320322

321323
types::ti_value merge_array_values( types::ti_value const & a, types::ti_value const & b) {
322-
using daw::json::impl::value_t;
324+
using daw::json::json_value_t;
323325
types::ti_value result = b;
324-
if( a.type( ) == value_t::value_types::null ) {
326+
static auto const null_type = std::type_index( typeid( daw::json::json_value_t::null_t ) );
327+
if( null_type == a.type( ) ) {
325328
result = b;
326329
result.is_optional( ) = true;
327-
} else if( b.type( ) == value_t::value_types::null ) {
330+
} else if( null_type == b.type( ) ) {
328331
result.is_optional( ) = true;
329332
}
330333
return result;
331334
}
332335

333-
types::ti_value parse_json_object( daw::json::impl::value_t const & current_item, boost::string_view cur_name, std::vector<types::ti_object> & obj_info, state_t & obj_state ) {
334-
using daw::json::impl::value_t;
335-
switch( current_item.type( ) ) {
336-
case value_t::value_types::integral:
337-
obj_state.has_integrals = true;
338-
return types::create_ti_value<types::ti_integral>( );
339-
case value_t::value_types::real:
340-
return types::create_ti_value<types::ti_real>( );
341-
case value_t::value_types::boolean:
342-
return types::create_ti_value<types::ti_boolean>( );
343-
case value_t::value_types::string:
344-
obj_state.has_strings = true;
345-
return types::create_ti_value<types::ti_string>( );
346-
case value_t::value_types::null:
347-
obj_state.has_optionals = true;
348-
return types::create_ti_value<types::ti_null>( );
349-
case value_t::value_types::object: {
350-
auto result = types::ti_object{ cur_name.to_string( ) + "_t" };
351-
for( auto const & child: current_item.get_object( ) ) {
352-
std::string const child_name = replace_keywords( child.first.to_string( ) );
353-
result.children[child_name] = parse_json_object( child.second, child_name, obj_info, obj_state );
354-
}
355-
add_or_merge( obj_info, result );
356-
return result;
357-
}
358-
case value_t::value_types::array: {
336+
types::ti_value parse_json_object( daw::json::json_value_t const & current_item, boost::string_view cur_name, std::vector<types::ti_object> & obj_info, state_t & obj_state ) {
337+
using daw::json::json_value_t;
338+
if( current_item.is_integer( ) ) {
339+
obj_state.has_integrals = true;
340+
return types::create_ti_value<types::ti_integral>( );
341+
}
342+
if( current_item.is_real( ) ) {
343+
return types::create_ti_value<types::ti_real>( );
344+
}
345+
if( current_item.is_boolean( ) ) {
346+
return types::create_ti_value<types::ti_boolean>( );
347+
}
348+
if( current_item.is_string( ) ) {
349+
obj_state.has_strings = true;
350+
return types::create_ti_value<types::ti_string>( );
351+
}
352+
if( current_item.is_null( ) ) {
353+
obj_state.has_optionals = true;
354+
return types::create_ti_value<types::ti_null>( );
355+
}
356+
if( current_item.is_object( ) ) {
357+
auto result = types::ti_object{ cur_name.to_string( ) + "_t" };
358+
for( auto const & child: current_item.get_object( ) ) {
359+
std::string const child_name = replace_keywords( child.first.to_string( ) );
360+
result.children[child_name] = parse_json_object( child.second, child_name, obj_info, obj_state );
361+
}
362+
add_or_merge( obj_info, result );
363+
return result;
364+
}
365+
if( current_item.is_array( ) ) {
359366
obj_state.has_arrays = true;
360367
auto result = types::create_ti_value<types::ti_array>( );
361368
auto arry = current_item.get_array( );
@@ -372,120 +379,43 @@ namespace daw {
372379
result.children( )[child_name] = child;
373380
}
374381
return result;
375-
}
376-
}
382+
383+
}
377384
throw std::runtime_error( "Unexpected exit point to parse_json_object2" );
378385
}
379386

380-
std::vector<types::ti_object> parse_json_object( daw::json::impl::value_t const & current_item, state_t & obj_state ) {
381-
using namespace daw::json::impl;
387+
std::vector<types::ti_object> parse_json_object( daw::json::json_value_t const & current_item, state_t & obj_state ) {
388+
using namespace daw::json;
382389
std::vector<types::ti_object> result;
383390

384391
if( !current_item.is_object( ) ) {
385-
auto root_obj_member = daw::json::impl::make_object_value_item( "root_obj", current_item );
386-
object_value root_object;
392+
auto root_obj_member = daw::json::make_object_value_item( "root_obj", current_item );
393+
json_object_value root_object;
387394
root_object.members_v.push_back( std::move( root_obj_member ) );
388-
value_t root_value{ std::move( root_object ) };
395+
json_value_t root_value{ std::move( root_object ) };
389396
parse_json_object( root_value, "root_object", result, obj_state );
390397
} else {
391398
parse_json_object( current_item, "root_object", result, obj_state );
392399
}
393400
return result;
394401
}
395402

396-
void generate_default_constructor( bool definition, config_t & config, types::ti_object const & cur_obj ) {
397-
if( !config.enable_jsonlink ) {
398-
return;
399-
}
400-
if( definition ) {
401-
config.cpp_file( ) << cur_obj.object_name << "::" << cur_obj.object_name << "( ):\n";
402-
config.cpp_file( ) << "\t\tdaw::json::JsonLink<" << cur_obj.object_name << ">{ }";
403-
for( auto const & child: cur_obj.children ) {
404-
config.cpp_file( ) << ",\n\t\t" << child.first << "{ }";
405-
}
406-
config.cpp_file( ) << " {\n\n\tset_links( );\n}\n\n";
407-
} else {
408-
config.header_file( ) << "\t" << cur_obj.object_name << "( );\n";
409-
}
410-
}
411-
412-
void generate_copy_constructor( bool definition, config_t & config, types::ti_object const & cur_obj ) {
403+
void generate_json_link_maps( bool definition, config_t & config, types::ti_object const & cur_obj ) {
413404
if( !config.enable_jsonlink ) {
414405
return;
415406
}
416407
if( definition ) {
417-
config.cpp_file( ) << cur_obj.object_name << "::" << cur_obj.object_name << "( " << cur_obj.object_name << " const & other ):\n";
418-
config.cpp_file( ) << "\t\tdaw::json::JsonLink<" << cur_obj.object_name << ">{ }";
408+
config.cpp_file( ) << "void " << cur_obj.object_name << "::json_link_map( ) {\n";
419409
for( auto const & child: cur_obj.children ) {
420-
config.cpp_file( ) << ",\n\t\t" << child.first << "{ other." << child.first << " }";
421-
}
422-
config.cpp_file( ) << " {\n\n\tset_links( );\n}\n\n";
423-
} else {
424-
config.header_file( ) << "\t" << cur_obj.object_name << "( " << cur_obj.object_name << " const & other );\n";
425-
}
426-
}
427-
428-
void generate_move_constructor( bool definition, config_t & config, types::ti_object const & cur_obj ) {
429-
if( !config.enable_jsonlink ) {
430-
return;
431-
}
432-
if( definition ) {
433-
config.cpp_file( ) << cur_obj.object_name << "::" << cur_obj.object_name << "( " << cur_obj.object_name << " && other ):\n";
434-
config.cpp_file( ) << "\t\tdaw::json::JsonLink<" << cur_obj.object_name << ">{ }";
435-
for( auto const & child: cur_obj.children ) {
436-
config.cpp_file( ) << ",\n\t\t" << child.first << "{ std::move( other." << child.first << " ) }";
437-
}
438-
config.cpp_file( ) << " {\n\n\tset_links( );\n}\n\n";
439-
} else {
440-
config.header_file( ) << "\t" << cur_obj.object_name << "( " << cur_obj.object_name << " && other );\n";
441-
}
442-
}
443-
444-
void generate_destructor( bool definition, config_t & config, types::ti_object const & cur_obj ) {
445-
if( !config.enable_jsonlink ) {
446-
return;
447-
}
448-
if( definition ) {
449-
config.cpp_file( ) << cur_obj.object_name << "::~" << cur_obj.object_name << "( ) { }\n\n";
450-
} else {
451-
config.header_file( ) << "\t~" << cur_obj.object_name << "( );\n";
452-
}
453-
}
454-
455-
void generate_set_links( bool definition, config_t & config, types::ti_object const & cur_obj ) {
456-
if( !config.enable_jsonlink ) {
457-
return;
458-
}
459-
if( definition ) {
460-
config.cpp_file( ) << "void " << cur_obj.object_name << "::set_links( ) {\n";
461-
for( auto const & child: cur_obj.children ) {
462-
config.cpp_file( ) << "\tlink_" << to_string( child.second.type( ) );
410+
config.cpp_file( ) << "\tlink_" << daw::json::to_string( child.second.type( ) );
463411
config.cpp_file( ) << "( \"" << child.first << "\", " << child.first << " );\n";
464412
}
465413
config.cpp_file( ) << "}\n\n";
466414
} else {
467-
config.header_file( ) << "private:\n";
468-
config.header_file( ) << "\tvoid set_links( );\n";
415+
config.header_file( ) << "\tstatic void json_link_map( );\n";
469416
}
470417
}
471418

472-
void generate_jsonlink( bool definition, config_t & config, types::ti_object const & cur_obj ) {
473-
if( !config.enable_jsonlink ) {
474-
return;
475-
}
476-
generate_default_constructor( definition, config, cur_obj );
477-
generate_copy_constructor( definition, config, cur_obj );
478-
generate_move_constructor( definition, config, cur_obj );
479-
generate_destructor( definition, config, cur_obj );
480-
if( !definition ) {
481-
auto const obj_type = cur_obj.name( );
482-
config.header_file( ) << "\n\t" << obj_type << " & operator=( " << obj_type << " const & ) = default;\n";
483-
config.header_file( ) << "\t" << obj_type << " & operator=( " << obj_type << " && ) = default;\n";
484-
}
485-
generate_set_links( definition, config, cur_obj );
486-
}
487-
488-
489419
void generate_includes( bool definition, config_t & config, state_t const & obj_state ) {
490420
{
491421
std::string const header_message = "// Code auto generated from json file '" + config.json_path.string( ) + "'\n\n";
@@ -535,7 +465,7 @@ namespace daw {
535465
}
536466
if( config.enable_jsonlink ) {
537467
config.header_file( ) << "\n";
538-
generate_jsonlink( false, config, cur_obj );
468+
generate_json_link_maps( false, config, cur_obj );
539469
}
540470
config.header_file( ) << "};" << "\t// " << obj_type << "\n\n";
541471
}
@@ -546,7 +476,7 @@ namespace daw {
546476
return;
547477
}
548478
for( auto const & cur_obj: obj_info ) {
549-
generate_jsonlink( true, config, cur_obj );
479+
generate_json_link_maps( true, config, cur_obj );
550480
}
551481
}
552482

0 commit comments

Comments
 (0)