From ce3de51f8f50e41439a2aa2352b7879b2973bca0 Mon Sep 17 00:00:00 2001 From: TheM14 Date: Fri, 9 Oct 2026 00:12:41 +0800 Subject: [PATCH] Update the vendored kernel to the JSON writer fix --- kernel/src/foundation/mini_json.cpp | 46 ++++++++++++++++ kernel/src/foundation/mini_json.h | 18 +++++++ kernel/src/hrtf/sofa_field.cpp | 31 ++--------- kernel/src/task/task.cpp | 82 +++++++++++++---------------- 4 files changed, 105 insertions(+), 72 deletions(-) diff --git a/kernel/src/foundation/mini_json.cpp b/kernel/src/foundation/mini_json.cpp index e139661..662283b 100644 --- a/kernel/src/foundation/mini_json.cpp +++ b/kernel/src/foundation/mini_json.cpp @@ -7,6 +7,8 @@ namespace joc::json { namespace { +const char kHexDigits[] = "0123456789abcdef"; + void skip_space(const std::string& text, std::size_t* index) { while (*index < text.size() && (text[*index] == ' ' || text[*index] == '\t' || text[*index] == '\n' || @@ -109,6 +111,50 @@ bool read_compound(const std::string& text, std::size_t* index, std::string* out } // namespace +std::string quote(const std::string& text) { + std::string out; + out.reserve(text.size() + 2u); + out.push_back('"'); + for (const char character : text) { + const unsigned char value = static_cast(character); + switch (character) { + case '"': out += "\\\""; break; + case '\\': out += "\\\\"; break; + case '\b': out += "\\b"; break; + case '\f': out += "\\f"; break; + case '\n': out += "\\n"; break; + case '\r': out += "\\r"; break; + case '\t': out += "\\t"; break; + default: + if (value < 0x20u) { + out += "\\u00"; + out.push_back(kHexDigits[value >> 4u]); + out.push_back(kHexDigits[value & 0x0Fu]); + } else { + out.push_back(character); + } + break; + } + } + out.push_back('"'); + return out; +} + +std::string pretty_object(const std::vector& fields) { + if (fields.empty()) { + return "{}"; + } + std::string out = "{"; + for (std::size_t index = 0u; index < fields.size(); ++index) { + out += index == 0u ? "\n " : ",\n "; + out += quote(fields[index].key); + out += ": "; + out += fields[index].value; + } + out += "\n}"; + return out; +} + bool parse_object(const std::string& text, std::vector* out, std::string* error) { out->clear(); std::size_t index = 0; diff --git a/kernel/src/foundation/mini_json.h b/kernel/src/foundation/mini_json.h index 13f3cb8..f7cc360 100644 --- a/kernel/src/foundation/mini_json.h +++ b/kernel/src/foundation/mini_json.h @@ -16,6 +16,24 @@ struct Member { // Parses a top-level JSON object. Rejects non-objects and duplicate keys. bool parse_object(const std::string& text, std::vector* out, std::string* error); +// One member of an object being written: `key` is escaped here, `value` is +// already JSON text (a quoted string, a number, a literal). +struct Field { + std::string key; + std::string value; +}; + +// Escapes `text` as a JSON string literal, quotes included. Matches +// json.dumps(..., ensure_ascii=False): the named escapes for `"` and `\` and +// for U+0008/U+0009/U+000A/U+000C/U+000D, `\u00XX` for the other control +// characters, and every other byte - UTF-8 included - passed through. +std::string quote(const std::string& text); + +// Renders a flat object the way json.dumps(members, ensure_ascii=False, +// indent=2) does: two-space indentation, one member per line, the members in +// the order given, and no trailing newline. +std::string pretty_object(const std::vector& fields); + const Member* find(const std::vector& members, const std::string& key); bool as_string(const Member& member, std::string* out); diff --git a/kernel/src/hrtf/sofa_field.cpp b/kernel/src/hrtf/sofa_field.cpp index d22523b..70909cd 100644 --- a/kernel/src/hrtf/sofa_field.cpp +++ b/kernel/src/hrtf/sofa_field.cpp @@ -13,6 +13,7 @@ #include #include "foundation/fft.h" +#include "foundation/mini_json.h" #include "foundation/sha256.h" #include "hrtf/public_filterbank.h" #include "io/npy_writer.h" @@ -651,32 +652,10 @@ Status convert_coordinates(const std::vector& values, std::size_t rows, // ------------------------------------------------------------- JSON --------- -std::string json_escape(const std::string& text) { - std::string out = "\""; - for (const char character : text) { - const unsigned char value = static_cast(character); - switch (character) { - case '"': out += "\\\""; break; - case '\\': out += "\\\\"; break; - case '\n': out += "\\n"; break; - case '\r': out += "\\r"; break; - case '\t': out += "\\t"; break; - case '\b': out += "\\b"; break; - case '\f': out += "\\f"; break; - default: - if (value < 0x20u) { - char buffer[8]; - std::snprintf(buffer, sizeof(buffer), "\\u%04x", value); - out += buffer; - } else { - out.push_back(character); - } - break; - } - } - out += "\""; - return out; -} +// The cache metadata is the compact form the reference writes +// (json.dumps(sort_keys=True, separators=(",", ":"))), so only the escaping is +// shared with the indented report and metadata documents. +std::string json_escape(const std::string& text) { return json::quote(text); } std::string json_number(double value) { return io::python_float_repr(value); } diff --git a/kernel/src/task/task.cpp b/kernel/src/task/task.cpp index 040fb65..83fcd09 100644 --- a/kernel/src/task/task.cpp +++ b/kernel/src/task/task.cpp @@ -30,6 +30,7 @@ #include "binaural/binaural_runtime.h" #include "eac3_transport/eac3_reader.h" #include "emdf/emdf_parser.h" +#include "foundation/mini_json.h" #include "foundation/sha256.h" #include "hrtf/jochrtf.h" #include "hrtf/rosella_model.h" @@ -440,27 +441,6 @@ Status decide_output_format(const Settings& cfg, telemetry::EventBus* bus, doubl return Status::fail(JOC_ERR_INVALID_CONFIG, stage::kOutput, "unknown clip action"); } -std::string json_escape(const std::string& text) { - std::string out; - out.reserve(text.size() + 8u); - for (const char character : text) { - switch (character) { - case '"': out += "\\\""; break; - case '\\': out += "\\\\"; break; - case '\n': out += "\\n"; break; - case '\r': out += "\\r"; break; - case '\t': out += "\\t"; break; - default: - if (static_cast(character) < 0x20u) { - out += fmt("\\u%04x", static_cast(character)); - } else { - out.push_back(character); - } - } - } - return out; -} - // Parses the bitstream without decoding or rendering: --metadata-only and the // diagnostic metadata reports. Status report_metadata(const Settings& cfg, telemetry::EventBus* bus, const std::string& eac3_path, @@ -570,19 +550,22 @@ Status report_metadata(const Settings& cfg, telemetry::EventBus* bus, const std: static_cast(oamd_payloads)); } if (!cfg.metadata_json_path.empty()) { - std::string json = fmt( - "{\n \"input\": \"%s\",\n \"frames\": %llu,\n \"objects_min\": %u,\n" - " \"objects_mean\": %.6f,\n \"objects_max\": %u,\n \"clipgain_min\": %.9g,\n" - " \"clipgain_max\": %.9g,\n \"oamd_payloads\": %llu\n}\n", - json_escape(cfg.input_path).c_str(), static_cast(frames), - objects_min == 0xFFFFFFFFu ? 0u : objects_min, mean_objects, objects_max, - clipgain_min, clipgain_max, static_cast(oamd_payloads)); + const std::string document = json::pretty_object({ + {"input", json::quote(cfg.input_path)}, + {"frames", std::to_string(frames)}, + {"objects_min", std::to_string(objects_min == 0xFFFFFFFFu ? 0u : objects_min)}, + {"objects_mean", fmt("%.6f", mean_objects)}, + {"objects_max", std::to_string(objects_max)}, + {"clipgain_min", fmt("%.9g", clipgain_min)}, + {"clipgain_max", fmt("%.9g", clipgain_max)}, + {"oamd_payloads", std::to_string(oamd_payloads)}, + }); std::ofstream output = fs_utf8::open_output(cfg.metadata_json_path); if (!output) { return Status::fail(JOC_ERR_OUTPUT_OPEN, stage::kOutput, "cannot write " + cfg.metadata_json_path); } - output << json; + output << document << "\n"; } bus->emit(JOC_EV_METADATA_INDEXED, JOC_STAGE_METADATA, JOC_LOG_INFO, fmt("metadata: %llu frames, oamd payloads %llu", @@ -1919,23 +1902,30 @@ Status result_to_json(const joc_task_result& result, std::string* out) { if (out == nullptr) { return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kOutput, "null output"); } - *out = fmt( - "{\"status\":%u,\"error_code\":%u,\"error_stage\":\"%s\",\"error_message\":\"%s\"," - "\"input_frames\":%llu,\"output_samples\":%llu,\"duration_sec\":%.6f," - "\"output_format_actual\":%u,\"output_peak\":%.9g,\"output_over_unity_values\":%llu," - "\"output_file_bytes\":%llu,\"output_sha256\":\"%s\",\"oamd_payloads\":%llu," - "\"oamd_transitions\":%llu,\"t_decode_bed\":%.6f,\"t_render\":%.6f,\"t_write\":%.6f," - "\"t_render_dsp\":%.6f,\"t_write_file\":%.6f,\"t_total\":%.6f}", - result.status, result.error_code, result.error_stage, result.error_message, - static_cast(result.input_frames), - static_cast(result.output_samples), result.duration_sec, - result.output_format_actual, result.output_peak, - static_cast(result.output_over_unity_values), - static_cast(result.output_file_bytes), result.output_sha256, - static_cast(result.oamd_payloads), - static_cast(result.oamd_transitions), result.t_decode_bed, - result.t_render, result.t_write, result.t_render_dsp, result.t_write_file, - result.t_total); + // The string fields are escaped, so a Windows path or a quote in an error + // message still leaves the document valid JSON. + *out = json::pretty_object({ + {"status", std::to_string(result.status)}, + {"error_code", std::to_string(result.error_code)}, + {"error_stage", json::quote(result.error_stage)}, + {"error_message", json::quote(result.error_message)}, + {"input_frames", std::to_string(result.input_frames)}, + {"output_samples", std::to_string(result.output_samples)}, + {"duration_sec", fmt("%.6f", result.duration_sec)}, + {"output_format_actual", std::to_string(result.output_format_actual)}, + {"output_peak", fmt("%.9g", result.output_peak)}, + {"output_over_unity_values", std::to_string(result.output_over_unity_values)}, + {"output_file_bytes", std::to_string(result.output_file_bytes)}, + {"output_sha256", json::quote(result.output_sha256)}, + {"oamd_payloads", std::to_string(result.oamd_payloads)}, + {"oamd_transitions", std::to_string(result.oamd_transitions)}, + {"t_decode_bed", fmt("%.6f", result.t_decode_bed)}, + {"t_render", fmt("%.6f", result.t_render)}, + {"t_write", fmt("%.6f", result.t_write)}, + {"t_render_dsp", fmt("%.6f", result.t_render_dsp)}, + {"t_write_file", fmt("%.6f", result.t_write_file)}, + {"t_total", fmt("%.6f", result.t_total)}, + }); return Status::success(); }