diff --git a/src/clp_ffi_js/ir/StructuredIrStreamReader.cpp b/src/clp_ffi_js/ir/StructuredIrStreamReader.cpp index f12b66b4..67370067 100644 --- a/src/clp_ffi_js/ir/StructuredIrStreamReader.cpp +++ b/src/clp_ffi_js/ir/StructuredIrStreamReader.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include @@ -26,6 +27,20 @@ namespace { constexpr std::string_view cEmptyJsonStr{"{}"}; constexpr std::string_view cReaderOptionsLogLevelKey{"logLevelKey"}; constexpr std::string_view cReaderOptionsTimestampKey{"timestampKey"}; + +/** + * @see nlohmann::basic_json::dump + * Serializes a JSON value into a string with invalid UTF-8 sequences replaced rather than + * throwing an exception. + * @param json + * @return Serialized JSON. + */ +auto dump_json_with_replace(nlohmann::json const& json) -> std::string; + +auto dump_json_with_replace(nlohmann::json const& json) -> std::string { + return json.dump(-1, ' ', false, nlohmann::json::error_handler_t::replace); +} + } // namespace auto StructuredIrStreamReader::create( @@ -132,7 +147,7 @@ auto StructuredIrStreamReader::decode_range(size_t begin_idx, size_t end_idx, bo ); json_str = std::string(cEmptyJsonStr); } else { - json_str = json_result.value().dump(); + json_str = dump_json_with_replace(json_result.value()); } return json_str; };