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;
0 commit comments