From 1bc0a65106e340aeff3ce43012c5eb2a05ee3160 Mon Sep 17 00:00:00 2001 From: YuSanka Date: Tue, 13 Feb 2024 13:40:19 +0100 Subject: [PATCH] Added JsonUtils to post-process written json (backported from master) --- src/libslic3r/CMakeLists.txt | 2 ++ src/libslic3r/Utils/JsonUtils.cpp | 24 ++++++++++++++++++++++++ src/libslic3r/Utils/JsonUtils.hpp | 12 ++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 src/libslic3r/Utils/JsonUtils.cpp create mode 100644 src/libslic3r/Utils/JsonUtils.hpp diff --git a/src/libslic3r/CMakeLists.txt b/src/libslic3r/CMakeLists.txt index a8268e226d..3876a19e15 100644 --- a/src/libslic3r/CMakeLists.txt +++ b/src/libslic3r/CMakeLists.txt @@ -509,6 +509,8 @@ set(SLIC3R_SOURCES Arachne/WallToolPaths.hpp Arachne/WallToolPaths.cpp StaticMap.hpp + Utils/JsonUtils.hpp + Utils/JsonUtils.cpp ) add_library(libslic3r STATIC ${SLIC3R_SOURCES}) diff --git a/src/libslic3r/Utils/JsonUtils.cpp b/src/libslic3r/Utils/JsonUtils.cpp new file mode 100644 index 0000000000..e4f4710b7c --- /dev/null +++ b/src/libslic3r/Utils/JsonUtils.cpp @@ -0,0 +1,24 @@ +#include "JsonUtils.hpp" +#include +#include + +namespace Slic3r { + +namespace pt = boost::property_tree; + +std::string write_json_with_post_process(const pt::ptree& ptree) +{ + std::stringstream oss; + pt::write_json(oss, ptree); + + // fix json-out to show node values as a string just for string nodes + std::regex reg("\\\"([0-9]+\\.{0,1}[0-9]*)\\\""); // code is borrowed from https://stackoverflow.com/questions/2855741/why-does-boost-property-tree-write-json-save-everything-as-string-is-it-possibl + std::string result = std::regex_replace(oss.str(), reg, "$1"); + + boost::replace_all(result, "\"true\"", "true"); + boost::replace_all(result, "\"false\"", "false"); + + return result; +} + +} // namespace Slic3r diff --git a/src/libslic3r/Utils/JsonUtils.hpp b/src/libslic3r/Utils/JsonUtils.hpp new file mode 100644 index 0000000000..08745a620c --- /dev/null +++ b/src/libslic3r/Utils/JsonUtils.hpp @@ -0,0 +1,12 @@ +#ifndef slic3r_JsonUtils_hpp_ +#define slic3r_JsonUtils_hpp_ + +#include + +namespace Slic3r { + +std::string write_json_with_post_process(const boost::property_tree::ptree& ptree); + +} // namespace Slic3r + +#endif // slic3r_jsonUtils_hpp_