Skip to content

Commit 87716bf

Browse files
committed
sort order is now in depth first of object graph so dependencies are first
1 parent 139a1d1 commit 87716bf

2 files changed

Lines changed: 33 additions & 15 deletions

File tree

src/json_to_cpp.cpp

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323

2424
#include <boost/utility/string_view.hpp>
2525
#include <unordered_map>
26-
26+
#include <limits>
27+
#include <tuple>
28+
#include <algorithm>
2729
#include <daw/json/daw_json_link.h>
2830

2931
#include "json_to_cpp.h"
@@ -98,9 +100,13 @@ namespace daw {
98100
return "unknown_" + std::to_string( unknown_count( ) );
99101
}
100102

101-
void parse_json_array( boost::string_view cur_name, daw::json::impl::value_t const & cur_item, std::unordered_map<std::string, obj_info_t> & obj_info );
103+
void parse_json_array( boost::string_view cur_name, daw::json::impl::value_t const & cur_item, std::vector<obj_info_t> & obj_info );
104+
105+
auto find_by_name( std::vector<obj_info_t> & obj_info, std::string const & name ) {
106+
return std::find_if( obj_info.begin( ), obj_info.end( ), [&]( auto const & v ) { return v.name == name; } );
107+
}
102108

103-
void parse_json_object( boost::string_view cur_name, daw::json::impl::object_value const & cur_item, std::unordered_map<std::string, obj_info_t> & obj_info ) {
109+
void parse_json_object( boost::string_view cur_name, daw::json::impl::object_value const & cur_item, std::vector<obj_info_t> & obj_info ) {
104110
using daw::json::impl::value_t;
105111

106112
obj_info_t cur_obj;
@@ -121,14 +127,18 @@ namespace daw {
121127
}
122128

123129
}
124-
// This will merge or create
125-
obj_info[cur_obj.name].name = cur_obj.name;
126-
for( auto const & member: cur_obj.members ) {
127-
obj_info[cur_obj.name].members[member.first] = member.second;
130+
131+
auto old_item = find_by_name( obj_info, cur_obj.name );
132+
if( old_item != obj_info.end( ) ) {
133+
for( auto const & member: cur_obj.members ) {
134+
old_item->members[member.first] = member.second;
135+
}
136+
} else {
137+
obj_info.push_back( cur_obj );
128138
}
129139
}
130140

131-
void parse_json_array( boost::string_view cur_name, daw::json::impl::value_t const & cur_item, std::unordered_map<std::string, obj_info_t> & obj_info ) {
141+
void parse_json_array( boost::string_view cur_name, daw::json::impl::value_t const & cur_item, std::vector<obj_info_t> & obj_info ) {
132142
using daw::json::impl::value_t;
133143
obj_info_t cur_obj;
134144
cur_obj.is_array = true;
@@ -150,11 +160,19 @@ namespace daw {
150160
}
151161
}
152162
cur_obj.members[val_info.name] = val_info;
153-
obj_info[cur_obj.name] = cur_obj;
163+
164+
auto old_item = find_by_name( obj_info, cur_obj.name );
165+
if( old_item != obj_info.end( ) ) {
166+
for( auto const & member: cur_obj.members ) {
167+
old_item->members[member.first] = member.second;
168+
}
169+
} else {
170+
obj_info.push_back( cur_obj );
171+
}
154172
}
155173

156-
std::unordered_map<std::string, obj_info_t> parse_json_object( daw::json::impl::value_t const & json_obj ) {
157-
std::unordered_map<std::string, obj_info_t> result;
174+
std::vector<obj_info_t> parse_json_object( daw::json::impl::value_t const & json_obj ) {
175+
std::vector<obj_info_t> result;
158176
if( json_obj.type( ) == daw::json::impl::value_t::value_types::object ) {
159177
parse_json_object( "root_type", json_obj.get_object( ), result );
160178
} else if( json_obj.type( ) == daw::json::impl::value_t::value_types::array ) {
@@ -287,11 +305,10 @@ namespace daw {
287305
}
288306
}
289307

290-
std::string generate_code( std::unordered_map<std::string, obj_info_t> obj_info, config_t const & config ) {
308+
std::string generate_code( std::vector<obj_info_t> obj_info, config_t const & config ) {
291309
using daw::json::impl::value_t;
292310
std::stringstream ss;
293-
for( auto const & obj_info_item: obj_info ) {
294-
auto const & cur_obj = obj_info_item.second;
311+
for( auto const & cur_obj: obj_info ) {
295312
auto const obj_type = cur_obj.name + "_t";
296313

297314
ss << "struct " << obj_type;

src/main.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ int main( int argc, char ** argv ) {
3838
std::string json_blob;
3939
std::copy( std::istream_iterator<char>{ in_file }, std::istream_iterator<char>{ }, std::back_inserter( json_blob ) );
4040
in_file.close( );
41-
auto code = generate_cpp( json_blob, config_t{ } );
41+
config_t config{ };
42+
auto code = generate_cpp( json_blob, config );
4243
std::cout << code << '\n';
4344

4445
return EXIT_SUCCESS;

0 commit comments

Comments
 (0)