diff --git a/include/flamegpu/io/JSONRunPlanReader.h b/include/flamegpu/io/JSONRunPlanReader.h index f19b76560..efd6aa45f 100644 --- a/include/flamegpu/io/JSONRunPlanReader.h +++ b/include/flamegpu/io/JSONRunPlanReader.h @@ -19,7 +19,7 @@ class JSONRunPlanReader { * @param input_filepath Path on disk to read the file from * @param model The model used to initialise the RunPlanVector */ - static RunPlanVector load(const std::string &input_filepath, const ModelDescription& model); + static RunPlanVector load(const std::string &input_filepath, const ModelDescription &model); }; } // namespace io } // namespace flamegpu diff --git a/include/flamegpu/io/JSONRunPlanWriter.h b/include/flamegpu/io/JSONRunPlanWriter.h index 0328b81ff..ce6a488f0 100644 --- a/include/flamegpu/io/JSONRunPlanWriter.h +++ b/include/flamegpu/io/JSONRunPlanWriter.h @@ -12,21 +12,29 @@ namespace io { * JSON format writer of RunPlanVector */ class JSONRunPlanWriter { + /** + * Utility method for writing out the outer structure of a RunPlanVector + * @param writer An initialised RapidJSON writer. + * @param rpv RunPlanVector to be written + * @tparam T Should be rapidjson::Writer or rapidjson::PrettyWriter (one does not inherit the other) + */ + template + static void writeCommon(std::unique_ptr &writer, const RunPlanVector &rpv); /** * Utility method for writing out a single RunPlan * @param writer An initialised RapidJSON writer. * @param rp RunPlan to be writer - * @tparam T Should be rapidjson::Writer + * @tparam T Should be rapidjson::Writer or rapidjson::PrettyWriter (one does not inherit the other) */ template - static void writeRunPlan(std::unique_ptr& writer, const RunPlan& rp); + static void writeRunPlan(std::unique_ptr &writer, const RunPlan &rp); public: /** * Exports the provided RunPlanVector in JSON format to the specified output_filepath * @param rpv The RunPlanVector to be exported * @param output_filepath Location on disk to export the file - * @param pretty Whether the exported JSON is "prettified" or "minified" + * @param pretty Whether the exported JSON is "prettified" (true) or "minified" (false), defaults true. */ static void save(const RunPlanVector &rpv, const std::string &output_filepath, bool pretty = true); }; diff --git a/src/flamegpu/io/JSONRunPlanWriter.cpp b/src/flamegpu/io/JSONRunPlanWriter.cpp index 50af12c16..3a42cb670 100644 --- a/src/flamegpu/io/JSONRunPlanWriter.cpp +++ b/src/flamegpu/io/JSONRunPlanWriter.cpp @@ -8,40 +8,22 @@ namespace flamegpu { namespace io { - // Typedef for the writer used, as the full template specification is way too long typedef rapidjson::Writer, rapidjson::UTF8<>, rapidjson::CrtAllocator, rapidjson::kWriteNanAndInfFlag> GenericJSONWriter; +typedef rapidjson::PrettyWriter, rapidjson::UTF8<>, rapidjson::CrtAllocator, rapidjson::kWriteNanAndInfFlag> PrettyJSONWriter; -void JSONRunPlanWriter::save(const RunPlanVector& rpv, const std::string& output_filepath, const bool pretty_print) { - // Init writer - auto buffer = rapidjson::StringBuffer(); - std::unique_ptr writer; - if (pretty_print) { - auto t_writer = std::make_unique, rapidjson::UTF8<>, rapidjson::CrtAllocator, rapidjson::kWriteNanAndInfFlag>>(buffer); - t_writer->SetIndent('\t', 1); - writer = std::move(t_writer); - } else { - writer = std::make_unique, rapidjson::UTF8<>, rapidjson::CrtAllocator, rapidjson::kWriteNanAndInfFlag>>(buffer); - } +template +void JSONRunPlanWriter::writeCommon(std::unique_ptr &writer, const RunPlanVector &rpv) { writer->StartObject(); writer->Key("RunPlanVector"); writer->StartArray(); // Write out RunPlan records - for (const auto &rp : rpv) { + for (const auto& rp : rpv) { writeRunPlan(writer, rp); } // Finalise and dump to file writer->EndArray(); writer->EndObject(); - std::ofstream out(output_filepath, std::ofstream::trunc); - if (!out.is_open()) { - THROW exception::InvalidFilePath("Unable to open '%s' for writing, in JSONRunPlanWriter::save().", output_filepath.c_str()); - } - out << buffer.GetString(); - out.close(); - // Cleanup (redundant in a static method) - writer.reset(); - buffer.Clear(); } template @@ -104,6 +86,25 @@ void JSONRunPlanWriter::writeRunPlan(std::unique_ptr &writer, const RunPlan & writer->EndObject(); writer->EndObject(); } -template void JSONRunPlanWriter::writeRunPlan(std::unique_ptr& writer, const RunPlan& rp); +void JSONRunPlanWriter::save(const RunPlanVector &rpv, const std::string &output_filepath, const bool pretty_print) { + // Init writer + auto buffer = rapidjson::StringBuffer(); + if (pretty_print) { + std::unique_ptr writer = std::make_unique(buffer); + writer->SetIndent('\t', 1); + writeCommon(writer, rpv); + } else { + std::unique_ptr writer = std::make_unique(buffer); + writeCommon(writer, rpv); + } + std::ofstream out(output_filepath, std::ofstream::trunc); + if (!out.is_open()) { + THROW exception::InvalidFilePath("Unable to open '%s' for writing, in JSONRunPlanWriter::save().", output_filepath.c_str()); + } + out << buffer.GetString(); + // Redundant cleanup + out.close(); + buffer.Clear(); +} } // namespace io } // namespace flamegpu