Skip to content

Commit d190278

Browse files
committed
Finished json array's so that they are complete
1 parent 713b1d5 commit d190278

File tree

3 files changed

+111
-52
lines changed

3 files changed

+111
-52
lines changed

include/json_to_cpp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace daw {
3737
boost::filesystem::path header_path;
3838
boost::filesystem::path json_path;
3939
bool separate_files;
40-
40+
bool hide_null_only;
4141
std::ostream &header_file( );
4242
std::ostream &cpp_file( );
4343
}; // config_t

src/json_to_cpp.cpp

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ namespace daw {
9191

9292
std::string name( ) const noexcept;
9393
std::string json_name( std::string member_name ) const noexcept;
94+
std::string array_member_info( ) const;
9495
daw::ordered_map<std::string, ti_value> const &children( ) const;
9596
daw::ordered_map<std::string, ti_value> &children( );
9697
bool &is_optional( ) noexcept;
@@ -167,6 +168,7 @@ namespace daw {
167168
virtual std::string name( ) const = 0;
168169
virtual std::string json_name( std::string member_name ) const = 0;
169170
virtual type_info_t *clone( ) const = 0;
171+
virtual std::string array_member_info( ) const = 0;
170172
}; // type_info_t
171173

172174
type_info_t::~type_info_t( ) {}
@@ -180,6 +182,10 @@ namespace daw {
180182
return value->json_name( std::move( member_name ) );
181183
}
182184

185+
std::string ti_value::array_member_info( ) const {
186+
return value->array_member_info( );
187+
}
188+
183189
size_t ti_value::type( ) const {
184190
return value->type( );
185191
}
@@ -222,6 +228,10 @@ namespace daw {
222228
return "json_custom<" + member_name + ">";
223229
}
224230

231+
virtual std::string array_member_info( ) const override {
232+
return "json_custom<no_name>";
233+
}
234+
225235
ti_null( )
226236
: type_info_t{} {
227237
is_optional = true;
@@ -242,6 +252,10 @@ namespace daw {
242252
return "int64_t";
243253
}
244254

255+
virtual std::string array_member_info( ) const override {
256+
return "json_number<no_name, int64_t>";
257+
}
258+
245259
std::string json_name( std::string member_name ) const override {
246260
return "json_number<" + member_name + ", intmax_t>";
247261
}
@@ -261,6 +275,10 @@ namespace daw {
261275
return "double";
262276
}
263277

278+
virtual std::string array_member_info( ) const override {
279+
return "json_number<no_name>";
280+
}
281+
264282
std::string json_name( std::string member_name ) const override {
265283
return "json_number<" + member_name + ">";
266284
}
@@ -280,6 +298,10 @@ namespace daw {
280298
return "bool";
281299
}
282300

301+
virtual std::string array_member_info( ) const override {
302+
return "json_bool<no_name>";
303+
}
304+
283305
std::string json_name( std::string member_name ) const override {
284306
return "json_bool<" + member_name + ">";
285307
}
@@ -299,6 +321,10 @@ namespace daw {
299321
return "std::string";
300322
}
301323

324+
virtual std::string array_member_info( ) const override {
325+
return "json_string<no_name>";
326+
}
327+
302328
std::string json_name( std::string member_name ) const override {
303329
return "json_string<" + member_name + ">";
304330
}
@@ -319,6 +345,7 @@ namespace daw {
319345
std::string name( ) const override {
320346
return object_name;
321347
}
348+
322349
ti_object( std::string obj_name )
323350
: type_info_t{}
324351
, object_name{std::move( obj_name )} {}
@@ -327,31 +354,15 @@ namespace daw {
327354
return new ti_object( *this );
328355
}
329356

357+
virtual std::string array_member_info( ) const override {
358+
return "json_class<no_name, " + name( ) + ">";
359+
}
360+
330361
std::string json_name( std::string member_name ) const override {
331362
return "json_class<" + member_name + ", " + name( ) + ">";
332363
}
333364
};
334365

335-
std::string make_json_type( std::string name, size_t id ) {
336-
switch( id ) {
337-
case json::json_value_t::index_of<json::json_value_t::string_t>( ):
338-
return "json_string<" + name;
339-
case json::json_value_t::index_of<json::json_value_t::boolean_t>( ):
340-
return "json_bool<" + name;
341-
case json::json_value_t::index_of<json::json_value_t::integer_t>( ):
342-
return "json_number<" + name + ", intmax_t";
343-
case json::json_value_t::index_of<json::json_value_t::real_t>( ):
344-
return "json_number<" + name;
345-
case json::json_value_t::index_of<json::json_value_t::null_t>( ):
346-
return "json_custom<" + name;
347-
case json::json_value_t::index_of<json::json_value_t::array_t>( ):
348-
return "json_array<" + name;
349-
case json::json_value_t::index_of<json::json_value_t::object_t>( ):
350-
return "json_class<" + name;
351-
}
352-
std::terminate( );
353-
}
354-
355366
struct ti_array : public type_info_t {
356367
size_t type( ) const override {
357368
return daw::json::json_value_t::index_of<
@@ -363,7 +374,13 @@ namespace daw {
363374
}
364375

365376
std::string json_name( std::string member_name ) const override {
366-
return "json_array<" + member_name + ", " + name( ) + "," + +">";
377+
return "json_array<" + member_name + ", " + name( ) + ", " +
378+
children.begin( )->second.array_member_info( ) + ">";
379+
}
380+
381+
std::string array_member_info( ) const override {
382+
return "json_array<no_name, " + name( ) + ", " +
383+
children.begin( )->second.array_member_info( ) + ">";
367384
}
368385

369386
type_info_t *clone( ) const override {
@@ -517,6 +534,9 @@ namespace daw {
517534
config.cpp_file( )
518535
<< "void " << cur_obj.object_name << "::json_link_map( ) {\n";
519536
for( auto const &child : cur_obj.children ) {
537+
if( config.hide_null_only and child.second.is_null( ) ) {
538+
continue;
539+
}
520540
config.cpp_file( )
521541
<< "\tlink_" << daw::json::to_string( child.second.type( ) );
522542
config.cpp_file( )
@@ -540,6 +560,9 @@ namespace daw {
540560
<< "inline auto describe_json_class( " << cur_obj.object_name
541561
<< " ) {\n\tusing namespace daw::json;\n";
542562
for( auto const &child : cur_obj.children ) {
563+
if( config.hide_null_only and child.second.is_null( ) ) {
564+
continue;
565+
}
543566
config.cpp_file( )
544567
<< "\tstatic constexpr char const " << child.first << "[] = \"";
545568
if( daw::string_view( child.first.data( ), child.first.size( ) )
@@ -558,6 +581,9 @@ namespace daw {
558581
bool is_first = true;
559582

560583
for( auto const &child : cur_obj.children ) {
584+
if( config.hide_null_only and child.second.is_null( ) ) {
585+
continue;
586+
}
561587
config.cpp_file( ) << "\t\t";
562588
if( !is_first ) {
563589
config.cpp_file( ) << ",";
@@ -581,6 +607,9 @@ namespace daw {
581607
config.cpp_file( ) << "\treturn std::forward_as_tuple( ";
582608
is_first = true;
583609
for( auto const &child : cur_obj.children ) {
610+
if( config.hide_null_only and child.second.is_null( ) ) {
611+
continue;
612+
}
584613
if( !is_first ) {
585614
config.cpp_file( ) << ", ";
586615
} else {
@@ -643,6 +672,9 @@ namespace daw {
643672
auto const obj_type = cur_obj.name( );
644673
config.header_file( ) << "struct " << obj_type << " {\n";
645674
for( auto const &child : cur_obj.children ) {
675+
if( config.hide_null_only and child.second.is_null( ) ) {
676+
continue;
677+
}
646678
auto const &member_name = child.first;
647679
auto const &member_type = child.second.name( );
648680
config.header_file( ) << "\t";

src/main.cpp

Lines changed: 57 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
// Copyright ( c ) 2016 Darrell Wright
44
//
55
// Permission is hereby granted, free of charge, to any person obtaining a copy
6-
// of this software and associated documentation files( the "Software" ), to deal
7-
// in the Software without restriction, including without limitation the rights
8-
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
9-
// copies of the Software, and to permit persons to whom the Software is
6+
// of this software and associated documentation files( the "Software" ), to
7+
// deal in the Software without restriction, including without limitation the
8+
// rights to use, copy, modify, merge, publish, distribute, sublicense, and / or
9+
// sell copies of the Software, and to permit persons to whom the Software is
1010
// furnished to do so, subject to the following conditions:
1111
//
12-
// The above copyright notice and this permission notice shall be included in all
13-
// copies or substantial portions of the Software.
12+
// The above copyright notice and this permission notice shall be included in
13+
// all copies or substantial portions of the Software.
1414
//
1515
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1616
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
@@ -27,40 +27,54 @@
2727
#include <curl/curl.h>
2828
#include <fstream>
2929
#include <iostream>
30-
#include <optional>
3130
#include <memory>
31+
#include <optional>
3232
#include <string>
3333

3434
#include <daw/daw_string_view.h>
3535

3636
#include "json_to_cpp.h"
3737

3838
namespace {
39-
std::optional<std::string> download( daw::string_view url, daw::string_view user_agent );
39+
std::optional<std::string> download( daw::string_view url,
40+
daw::string_view user_agent );
4041
bool is_url( daw::string_view path );
4142
} // namespace
4243

4344
int main( int argc, char **argv ) {
4445
using namespace daw::json_to_cpp;
4546
static std::string const default_user_agent =
46-
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.100 Safari/537.36";
47+
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) "
48+
"Chrome/54.0.2840.100 Safari/537.36";
4749

4850
boost::program_options::options_description desc{"Options"};
4951
desc.add_options( )( "help", "print option descriptions" )(
5052
"in_file", boost::program_options::value<boost::filesystem::path>( ),
51-
"json source file path or url" )( "use_jsonlink", boost::program_options::value<bool>( )->default_value( true ),
52-
"Use JsonLink serializaion/deserialization" )(
53-
"cpp_file", boost::program_options::value<boost::filesystem::path>( ), "output c++ file" )(
53+
"json source file path or url" )(
54+
"use_jsonlink",
55+
boost::program_options::value<bool>( )->default_value( true ),
56+
"Use JsonLink serializaion/deserialization" )(
57+
"cpp_file", boost::program_options::value<boost::filesystem::path>( ),
58+
"output c++ file" )(
5459
"header_file", boost::program_options::value<boost::filesystem::path>( ),
55-
"output c++ header file. If not specified uses cpp_file. Only valid when use_jsonlink=true" )(
56-
"allow_overwrite", boost::program_options::value<bool>( )->default_value( true ),
60+
"output c++ header file. If not specified uses cpp_file. Only valid when "
61+
"use_jsonlink=true" )(
62+
"allow_overwrite",
63+
boost::program_options::value<bool>( )->default_value( true ),
64+
5765
"Overwrite existing output files" )(
58-
"user_agent", boost::program_options::value<std::string>( )->default_value( default_user_agent ),
66+
"hide_null_only",
67+
boost::program_options::value<bool>( )->default_value( true ),
68+
"Do not output json entries that are only ever null" )(
69+
"user_agent",
70+
boost::program_options::value<std::string>( )->default_value(
71+
default_user_agent ),
5972
"User agent to use when downloading via URL" );
6073

6174
boost::program_options::variables_map vm;
6275
try {
63-
boost::program_options::store( boost::program_options::parse_command_line( argc, argv, desc ), vm );
76+
boost::program_options::store(
77+
boost::program_options::parse_command_line( argc, argv, desc ), vm );
6478
if( vm.count( "help" ) ) {
6579
std::cout << "Command line options\n" << desc << std::endl;
6680
return EXIT_SUCCESS;
@@ -81,11 +95,13 @@ int main( int argc, char **argv ) {
8195

8296
std::string json_str;
8397
if( is_url( config.json_path.string( ) ) ) {
84-
auto tmp = download( config.json_path.string( ), vm["user_agent"].as<std::string>( ) );
98+
auto tmp = download( config.json_path.string( ),
99+
vm["user_agent"].as<std::string>( ) );
85100
if( tmp ) {
86101
json_str = *tmp;
87102
} else {
88-
std::cerr << "Could not download json data from '" << canonical( config.json_path ) << "'\n";
103+
std::cerr << "Could not download json data from '"
104+
<< canonical( config.json_path ) << "'\n";
89105
exit( EXIT_FAILURE );
90106
}
91107
} else {
@@ -98,42 +114,51 @@ int main( int argc, char **argv ) {
98114
std::ifstream in_file;
99115
in_file.open( config.json_path.string( ) );
100116
if( !in_file ) {
101-
std::cerr << "Could not open json in_file '" << canonical( config.json_path ) << "'\n";
117+
std::cerr << "Could not open json in_file '"
118+
<< canonical( config.json_path ) << "'\n";
102119
exit( EXIT_FAILURE );
103120
}
104-
std::copy( std::istream_iterator<char>{in_file}, std::istream_iterator<char>{}, std::back_inserter( json_str ) );
121+
std::copy( std::istream_iterator<char>{in_file},
122+
std::istream_iterator<char>{}, std::back_inserter( json_str ) );
105123
in_file.close( );
106124
}
107125

108126
config.cpp_stream = &std::cout;
109127
config.header_stream = &std::cout;
110128
config.enable_jsonlink = vm["use_jsonlink"].as<bool>( );
129+
config.hide_null_only = vm["allow_overwrite"].as<bool>( );
111130
std::ofstream cpp_file;
112131
std::ofstream header_file;
113132

114133
if( vm.count( "cpp_file" ) > 0 ) {
115134
bool const allow_overwrite = vm["allow_overwrite"].as<bool>( );
116-
config.cpp_path = canonical( vm["cpp_file"].as<boost::filesystem::path>( ) );
135+
config.cpp_path =
136+
canonical( vm["cpp_file"].as<boost::filesystem::path>( ) );
117137
if( exists( config.cpp_path ) && !allow_overwrite ) {
118138
std::cerr << "cpp_file '" << config.cpp_path << "' already exists\n";
119139
exit( EXIT_FAILURE );
120140
}
121141
cpp_file.open( config.cpp_path.string( ), std::ios::out | std::ios::trunc );
122142
if( !cpp_file ) {
123-
std::cerr << "Could not open cpp_file '" << config.cpp_path << "' for writing\n";
143+
std::cerr << "Could not open cpp_file '" << config.cpp_path
144+
<< "' for writing\n";
124145
exit( EXIT_FAILURE );
125146
}
126147
config.cpp_stream = &cpp_file;
127148

128149
if( config.enable_jsonlink && vm.count( "header_file" ) > 0 ) {
129-
config.header_path = canonical( vm["header_file"].as<boost::filesystem::path>( ) );
150+
config.header_path =
151+
canonical( vm["header_file"].as<boost::filesystem::path>( ) );
130152
if( exists( config.header_path ) && !allow_overwrite ) {
131-
std::cerr << "header_file '" << config.header_path << "' already exists\n";
153+
std::cerr << "header_file '" << config.header_path
154+
<< "' already exists\n";
132155
exit( EXIT_FAILURE );
133156
}
134-
header_file.open( config.header_path.string( ), std::ios::out | std::ios::trunc );
157+
header_file.open( config.header_path.string( ),
158+
std::ios::out | std::ios::trunc );
135159
if( !header_file ) {
136-
std::cerr << "Could not open header_file '" << config.header_path << "' for writing\n";
160+
std::cerr << "Could not open header_file '" << config.header_path
161+
<< "' for writing\n";
137162
exit( EXIT_FAILURE );
138163
}
139164
config.header_stream = &header_file;
@@ -149,14 +174,16 @@ int main( int argc, char **argv ) {
149174
}
150175

151176
namespace {
152-
size_t callback( char const *in, size_t const size, size_t const num, std::string *const out ) {
177+
size_t callback( char const *in, size_t const size, size_t const num,
178+
std::string *const out ) {
153179
assert( out );
154180
size_t totalBytes = size * num;
155181
out->append( in, totalBytes );
156182
return totalBytes;
157183
}
158184

159-
std::optional<std::string> download( daw::string_view url, daw::string_view user_agent ) {
185+
std::optional<std::string> download( daw::string_view url,
186+
daw::string_view user_agent ) {
160187
struct curl_slist *headers = nullptr;
161188
curl_slist_append( headers, "Accept: application/json" );
162189
curl_slist_append( headers, "Content-Type: application/json" );
@@ -206,7 +233,7 @@ namespace {
206233
}
207234

208235
bool is_url( daw::string_view path ) {
209-
return boost::starts_with( path.data( ), "http://" ) || boost::starts_with( path.data( ), "https://" );
236+
return boost::starts_with( path.data( ), "http://" ) ||
237+
boost::starts_with( path.data( ), "https://" );
210238
}
211239
} // namespace
212-

0 commit comments

Comments
 (0)