Skip to content

Commit 0c8a284

Browse files
committed
.
Please enter the commit message for your changes. Lines starting
1 parent a4325d3 commit 0c8a284

2 files changed

Lines changed: 54 additions & 19 deletions

File tree

src/json_to_cpp.cpp

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,11 @@ namespace daw {
174174

175175
size_t type( ) const;
176176

177+
inline bool is_null( ) const {
178+
return type( ) ==
179+
json::json_value_t::index_of<json::json_value_t::null_t>( );
180+
}
181+
177182
constexpr ti_value( ) noexcept = default;
178183

179184
~ti_value( );
@@ -230,6 +235,11 @@ namespace daw {
230235
virtual ~type_info_t( );
231236

232237
virtual size_t type( ) const = 0;
238+
239+
bool is_null( ) const {
240+
return this->type( ) ==
241+
json::json_value_t::index_of<json::json_value_t::null_t>( );
242+
}
233243
virtual std::string name( ) const = 0;
234244
virtual std::string json_name( std::string member_name ) const = 0;
235245
virtual type_info_t *clone( ) const = 0;
@@ -249,6 +259,7 @@ namespace daw {
249259
size_t ti_value::type( ) const {
250260
return value->type( );
251261
}
262+
252263
std::map<std::string, ti_value> const &ti_value::children( ) const {
253264
return value->children;
254265
}
@@ -413,6 +424,7 @@ namespace daw {
413424
case json::json_value_t::index_of<json::json_value_t::object_t>( ):
414425
return "json_class<" + name;
415426
}
427+
std::terminate( );
416428
}
417429

418430
struct ti_array : public type_info_t {
@@ -456,7 +468,7 @@ namespace daw {
456468
}
457469

458470
types::ti_object &orig = *pos;
459-
std::vector<std::pair<std::string, types::ti_value>> diff;
471+
std::vector<std::pair<std::string, types::ti_value>> diff{};
460472

461473
static auto const comp = []( auto const &c1, auto const &c2 ) {
462474
return c1.first < c2.first;
@@ -466,35 +478,33 @@ namespace daw {
466478
std::back_inserter( diff ), comp );
467479

468480
for( auto &child : diff ) {
469-
child.second.is_optional( ) = true;
470-
if( json::json_value_t::index_of<json::json_value_t::null_t>( ) ==
471-
child.second.type( ) ) {
472-
if( json::json_value_t::index_of<json::json_value_t::null_t>( ) ==
473-
orig.children[child.first].type( ) ) {
481+
if( child.second.is_null( ) ) {
482+
if( !orig.children[child.first].is_null( ) ) {
474483
orig.children[child.first] = child.second;
475484
}
476485
orig.children[child.first].is_optional( ) = true;
477-
} else if( json::json_value_t::index_of<
478-
json::json_value_t::null_t>( ) ==
479-
orig.children[child.first].type( ) ) {
480-
orig.children[child.first].is_optional( ) = true;
481-
} else {
486+
} else if( orig.children[child.first].is_null( ) ) {
482487
orig.children[child.first] = child.second;
483-
}
488+
} /* else {
489+
orig.children[child.first] = child.second;
490+
}*/
491+
orig.children[child.first].is_optional( ) = true;
484492
}
485493
}
486494

487495
types::ti_value merge_array_values( types::ti_value const &a,
488496
types::ti_value const &b ) {
489497
using daw::json::json_value_t;
490-
types::ti_value result = b;
491-
static auto const null_type =
492-
daw::json::json_value_t::index_of<daw::json::json_value_t::null_t>( );
493-
if( null_type == a.type( ) ) {
498+
types::ti_value result{};
499+
if( a.is_null( ) ) {
494500
result = b;
495501
result.is_optional( ) = true;
496-
} else if( null_type == b.type( ) ) {
502+
} else if( b.is_null( ) ) {
503+
result = a;
497504
result.is_optional( ) = true;
505+
} else {
506+
result = a;
507+
result.is_optional( ) = a.is_optional( ) or b.is_optional( );
498508
}
499509
return result;
500510
}
@@ -563,7 +573,7 @@ namespace daw {
563573
parse_json_object( daw::json::json_value_t const &current_item,
564574
state_t &obj_state ) {
565575
using namespace daw::json;
566-
std::vector<types::ti_object> result;
576+
std::vector<types::ti_object> result{};
567577

568578
if( !current_item.is_object( ) ) {
569579
auto root_obj_member =
@@ -635,7 +645,15 @@ namespace daw {
635645
} else {
636646
is_first = false;
637647
}
638-
config.cpp_file( ) << child.second.json_name( child.first ) << "\n";
648+
if( child.second.is_optional( ) ) {
649+
config.cpp_file( ) << "json_nullable<";
650+
}
651+
config.cpp_file( ) << child.second.json_name( child.first );
652+
if( child.second.is_optional( ) ) {
653+
config.cpp_file( ) << ">\n";
654+
} else {
655+
config.cpp_file( ) << '\n';
656+
}
639657
}
640658
config.cpp_file( ) << ">{};\n}\n\n";
641659

test.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[
2+
{
3+
"a": null,
4+
"b": 12334
5+
},
6+
{
7+
"a": 1234,
8+
"b": 12334
9+
},
10+
{
11+
"a": null,
12+
"b": 12334
13+
},
14+
{
15+
"b": 12334
16+
}
17+
]

0 commit comments

Comments
 (0)