20
20
#include < rang.hpp>
21
21
#endif
22
22
#include " CLI/Formatter.hpp"
23
- #include " rapidjson/document.h"
24
- #include " rapidjson/prettywriter.h"
25
- #include " rapidjson/ostreamwrapper.h"
26
23
27
24
namespace itk
28
25
{
@@ -282,82 +279,70 @@ Pipeline
282
279
283
280
}
284
281
282
+ struct CLIOptionJSON
283
+ {
284
+ std::string description;
285
+ std::string name;
286
+ std::string type;
287
+ bool required;
288
+ int itemsExpected;
289
+ int itemsExpectedMin;
290
+ int itemsExpectedMax;
291
+ std::string defaultStr;
292
+ };
293
+
294
+ struct InterfaceJSON
295
+ {
296
+ std::string description;
297
+ std::string name;
298
+ std::string version;
299
+ std::vector<CLIOptionJSON> inputs;
300
+ std::vector<CLIOptionJSON> outputs;
301
+ std::vector<CLIOptionJSON> parameters;
302
+ };
303
+
285
304
void
286
305
Pipeline
287
306
::interface_json ()
288
307
{
289
- rapidjson::Document document;
290
- document.SetObject ();
291
- rapidjson::Document::AllocatorType& allocator = document.GetAllocator ();
292
-
293
- rapidjson::Value description;
294
- description.SetString (this ->get_description ().c_str (), allocator);
295
- document.AddMember (" description" , description.Move (), allocator);
296
308
297
- rapidjson::Value name;
298
- name.SetString (this ->get_name ().c_str (), allocator);
299
- document.AddMember (" name" , name.Move (), allocator);
309
+ InterfaceJSON interfaceJSON;
300
310
301
- rapidjson::Value version ;
302
- version. SetString ( this ->version (). c_str (), allocator );
303
- document. AddMember ( " version" , version. Move (), allocator );
311
+ interfaceJSON. description = this -> get_description () ;
312
+ interfaceJSON. name = this ->get_name ( );
313
+ interfaceJSON. version = this -> version ( );
304
314
305
- rapidjson::Value inputs (rapidjson::kArrayType );
306
- rapidjson::Value outputs (rapidjson::kArrayType );
307
- rapidjson::Value parameters (rapidjson::kArrayType );
308
315
for (CLI::Option *opt : this ->get_options ({})) {
309
- rapidjson::Value option;
310
- option.SetObject ();
311
-
312
- rapidjson::Value optionDescription;
313
- optionDescription.SetString (opt->get_description ().c_str (), allocator);
314
- option.AddMember (" description" , optionDescription.Move (), allocator);
315
-
316
- auto singleName = opt->get_single_name ();
316
+ CLIOptionJSON optionJSON;
317
+ optionJSON.description = opt->get_description ();
318
+ const auto singleName = opt->get_single_name ();
317
319
if (singleName == " help" )
318
320
{
319
321
continue ;
320
322
}
321
-
322
- rapidjson::Value optionName;
323
- optionName.SetString (opt->get_single_name ().c_str (), allocator);
324
- option.AddMember (" name" , optionName.Move (), allocator);
325
-
326
- rapidjson::Value required;
327
- required.SetBool (opt->get_required ());
328
- option.AddMember (" required" , required.Move (), allocator);
329
-
330
- rapidjson::Value itemsExpected;
331
- itemsExpected.SetInt (opt->get_items_expected ());
332
- option.AddMember (" itemsExpected" , itemsExpected.Move (), allocator);
333
-
334
- rapidjson::Value itemsExpectedMin;
335
- itemsExpectedMin.SetInt (opt->get_items_expected_min ());
336
- option.AddMember (" itemsExpectedMin" , itemsExpectedMin.Move (), allocator);
337
-
338
- rapidjson::Value itemsExpectedMax;
339
- itemsExpectedMax.SetInt (opt->get_items_expected_max ());
340
- option.AddMember (" itemsExpectedMax" , itemsExpectedMax.Move (), allocator);
341
-
342
- auto typeName = opt->get_type_name ();
343
- // flag
323
+ optionJSON.name = opt->get_single_name ();
324
+ optionJSON.required = opt->get_required ();
325
+ optionJSON.itemsExpected = opt->get_items_expected ();
326
+ optionJSON.itemsExpectedMin = opt->get_items_expected_min ();
327
+ optionJSON.itemsExpectedMax = opt->get_items_expected_max ();
328
+ optionJSON.type = opt->get_type_name ();
344
329
if (!opt->get_items_expected ())
345
330
{
346
- typeName = " BOOL" ;
331
+ optionJSON.type = " BOOL" ;
332
+ }
333
+ if (!opt->get_default_str ().empty ())
334
+ {
335
+ optionJSON.defaultStr = opt->get_default_str ();
347
336
}
348
- rapidjson::Value optionTypeName;
349
- optionTypeName.SetString (typeName.c_str (), allocator);
350
- option.AddMember (" type" , optionTypeName.Move (), allocator);
351
-
352
337
if (opt->get_positional ())
353
338
{
354
- if (typeName .rfind (" OUTPUT" , 0 ) != std::string::npos)
339
+ if (optionJSON. type .rfind (" OUTPUT" , 0 ) != std::string::npos)
355
340
{
356
- outputs.PushBack (option, allocator );
341
+ interfaceJSON. outputs .push_back (optionJSON );
357
342
}
358
343
else
359
344
{
360
- inputs.PushBack (option, allocator );
345
+ interfaceJSON. inputs .push_back (optionJSON );
361
346
}
362
347
}
363
348
else
@@ -370,24 +355,39 @@ ::interface_json()
370
355
}
371
356
if (!opt->get_default_str ().empty ())
372
357
{
373
- rapidjson::Value defaultStr;
374
- defaultStr.SetString (opt->get_default_str ().c_str (), allocator);
375
- option.AddMember (" default" , defaultStr.Move (), allocator);
358
+ optionJSON.defaultStr = opt->get_default_str ();
376
359
}
377
- parameters.PushBack (option, allocator );
360
+ interfaceJSON. parameters .push_back (optionJSON );
378
361
}
379
362
}
380
- document.AddMember (" inputs" , inputs.Move (), allocator);
381
- document.AddMember (" outputs" , outputs.Move (), allocator);
382
- document.AddMember (" parameters" , parameters.Move (), allocator);
383
363
384
- rapidjson::OStreamWrapper ostreamWrapper ( std::cout );
385
- rapidjson::PrettyWriter< rapidjson::OStreamWrapper > writer ( ostreamWrapper );
386
- document.Accept ( writer );
387
- std::cout << std::endl;
364
+ std::string serialized{};
365
+ auto ec = glz::write<glz::opts{ .prettify = true , .concatenate = false }>(interfaceJSON, serialized);
366
+ if (ec)
367
+ {
368
+ const std::string descriptiveError = glz::format_error (ec, serialized);
369
+ std::cerr << " Error during interface JSON serialization: " << descriptiveError << std::endl;
370
+ return ;
371
+ }
372
+ std::cout << serialized << std::endl;
388
373
}
389
374
390
375
bool Pipeline::m_UseMemoryIO{false };
391
376
392
377
} // end namespace wasm
393
378
} // end namespace itk
379
+
380
+ template <>
381
+ struct glz ::meta<itk::wasm::CLIOptionJSON> {
382
+ using T = itk::wasm::CLIOptionJSON;
383
+ static constexpr auto value = glz::object(
384
+ " description" , &T::description,
385
+ " name" , &T::name,
386
+ " type" , &T::type,
387
+ " required" , &T::required,
388
+ " itemsExpected" , &T::itemsExpected,
389
+ " itemsExpectedMin" , &T::itemsExpectedMin,
390
+ " itemsExpectedMax" , &T::itemsExpectedMax,
391
+ " default" , &T::defaultStr
392
+ );
393
+ };
0 commit comments