Skip to content

Commit 3fc591c

Browse files
committed
refactor(Pipeline): rapidjson to glaze
1 parent bf694dd commit 3fc591c

File tree

2 files changed

+74
-80
lines changed

2 files changed

+74
-80
lines changed

include/itkPipeline.h

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#include "itkImage.h"
2727
#include "itkVectorImage.h"
2828

29-
#include "rapidjson/document.h"
29+
#include "glaze/glaze.hpp"
3030

3131
#include "WebAssemblyInterfaceExport.h"
3232

@@ -119,22 +119,16 @@ using CLI::Success;
119119
using CLI::Config;
120120

121121
/**
122-
* @brief Create a rapidjson kArrayType value from an STL style container.
122+
* @brief Create a glaze arrat_t value from an STL style container.
123123
*
124124
* @tparam Iteratorable Any container type that supports STL style iterator.
125125
* @param container Container object.
126-
* @param allocator Rapidjson allocator.
127-
* @return rapidjson::Value rapidjson Value of kArrayType which contains all
128-
* the values from the input container.
126+
* @return glz::json_t::array_t with the values from the input container.
129127
*/
130128
template<typename Iteratorable>
131-
rapidjson::Value getArrayJson(Iteratorable container, rapidjson::Document::AllocatorType& allocator)
129+
glz::json_t::array_t getArrayJson(Iteratorable container)
132130
{
133-
rapidjson::Value value(rapidjson::kArrayType);
134-
for(auto iter = container.begin(); iter != container.end(); ++iter)
135-
{
136-
value.PushBack(rapidjson::Value(*iter), allocator);
137-
}
131+
glz::json_t::array_t value(container.begin(), container.end());
138132
return value;
139133
}
140134

src/itkPipeline.cxx

Lines changed: 69 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@
2020
#include <rang.hpp>
2121
#endif
2222
#include "CLI/Formatter.hpp"
23-
#include "rapidjson/document.h"
24-
#include "rapidjson/prettywriter.h"
25-
#include "rapidjson/ostreamwrapper.h"
2623

2724
namespace itk
2825
{
@@ -282,82 +279,70 @@ Pipeline
282279

283280
}
284281

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+
285304
void
286305
Pipeline
287306
::interface_json()
288307
{
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);
296308

297-
rapidjson::Value name;
298-
name.SetString(this->get_name().c_str(), allocator);
299-
document.AddMember("name", name.Move(), allocator);
309+
InterfaceJSON interfaceJSON;
300310

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();
304314

305-
rapidjson::Value inputs(rapidjson::kArrayType);
306-
rapidjson::Value outputs(rapidjson::kArrayType);
307-
rapidjson::Value parameters(rapidjson::kArrayType);
308315
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();
317319
if (singleName == "help")
318320
{
319321
continue;
320322
}
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();
344329
if (!opt->get_items_expected())
345330
{
346-
typeName = "BOOL";
331+
optionJSON.type = "BOOL";
332+
}
333+
if (!opt->get_default_str().empty())
334+
{
335+
optionJSON.defaultStr = opt->get_default_str();
347336
}
348-
rapidjson::Value optionTypeName;
349-
optionTypeName.SetString(typeName.c_str(), allocator);
350-
option.AddMember("type", optionTypeName.Move(), allocator);
351-
352337
if (opt->get_positional())
353338
{
354-
if (typeName.rfind("OUTPUT", 0) != std::string::npos)
339+
if (optionJSON.type.rfind("OUTPUT", 0) != std::string::npos)
355340
{
356-
outputs.PushBack(option, allocator);
341+
interfaceJSON.outputs.push_back(optionJSON);
357342
}
358343
else
359344
{
360-
inputs.PushBack(option, allocator);
345+
interfaceJSON.inputs.push_back(optionJSON);
361346
}
362347
}
363348
else
@@ -370,24 +355,39 @@ ::interface_json()
370355
}
371356
if (!opt->get_default_str().empty())
372357
{
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();
376359
}
377-
parameters.PushBack(option, allocator);
360+
interfaceJSON.parameters.push_back(optionJSON);
378361
}
379362
}
380-
document.AddMember("inputs", inputs.Move(), allocator);
381-
document.AddMember("outputs", outputs.Move(), allocator);
382-
document.AddMember("parameters", parameters.Move(), allocator);
383363

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;
388373
}
389374

390375
bool Pipeline::m_UseMemoryIO{false};
391376

392377
} // end namespace wasm
393378
} // 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

Comments
 (0)