Skip to content

Commit

Permalink
fix: Handle invalid UTF-8 sequences gracefully when converting struct…
Browse files Browse the repository at this point in the history
…ured log events to JSON. (#52)
  • Loading branch information
davemarco authored Jan 20, 2025
1 parent cda40a5 commit 4fc9565
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/clp_ffi_js/ir/StructuredIrStreamReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <clp/ffi/ir_stream/Deserializer.hpp>
#include <clp/TraceableException.hpp>
#include <emscripten/val.h>
#include <json/single_include/nlohmann/json.hpp>
#include <spdlog/spdlog.h>

#include <clp_ffi_js/ClpFfiJsException.hpp>
Expand All @@ -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(
Expand Down Expand Up @@ -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;
};
Expand Down

0 comments on commit 4fc9565

Please sign in to comment.