Update the vendored kernel to the JSON writer fix
build / windows (push) Has been cancelled
build / release (push) Has been cancelled

This commit is contained in:
TheM14 committed 2026-10-09 00:12:41 +08:00
1 parent 49f3a0f040
commit ce3de51f8f
4 files changed
+105 -72

No files matched your search

+46
View File
@@ -7,6 +7,8 @@ namespace joc::json {
namespace { namespace {
const char kHexDigits[] = "0123456789abcdef";
void skip_space(const std::string& text, std::size_t* index) { void skip_space(const std::string& text, std::size_t* index) {
while (*index < text.size() && while (*index < text.size() &&
(text[*index] == ' ' || text[*index] == '\t' || text[*index] == '\n' || (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 } // 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<unsigned char>(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<Field>& 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<Member>* out, std::string* error) { bool parse_object(const std::string& text, std::vector<Member>* out, std::string* error) {
out->clear(); out->clear();
std::size_t index = 0; std::size_t index = 0;
+18
View File
@@ -16,6 +16,24 @@ struct Member {
// Parses a top-level JSON object. Rejects non-objects and duplicate keys. // Parses a top-level JSON object. Rejects non-objects and duplicate keys.
bool parse_object(const std::string& text, std::vector<Member>* out, std::string* error); bool parse_object(const std::string& text, std::vector<Member>* 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<Field>& fields);
const Member* find(const std::vector<Member>& members, const std::string& key); const Member* find(const std::vector<Member>& members, const std::string& key);
bool as_string(const Member& member, std::string* out); bool as_string(const Member& member, std::string* out);
+5 -26
View File
@@ -13,6 +13,7 @@
#include <vector> #include <vector>
#include "foundation/fft.h" #include "foundation/fft.h"
#include "foundation/mini_json.h"
#include "foundation/sha256.h" #include "foundation/sha256.h"
#include "hrtf/public_filterbank.h" #include "hrtf/public_filterbank.h"
#include "io/npy_writer.h" #include "io/npy_writer.h"
@@ -651,32 +652,10 @@ Status convert_coordinates(const std::vector<double>& values, std::size_t rows,
// ------------------------------------------------------------- JSON --------- // ------------------------------------------------------------- JSON ---------
std::string json_escape(const std::string& text) { // The cache metadata is the compact form the reference writes
std::string out = "\""; // (json.dumps(sort_keys=True, separators=(",", ":"))), so only the escaping is
for (const char character : text) { // shared with the indented report and metadata documents.
const unsigned char value = static_cast<unsigned char>(character); std::string json_escape(const std::string& text) { return json::quote(text); }
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;
}
std::string json_number(double value) { return io::python_float_repr(value); } std::string json_number(double value) { return io::python_float_repr(value); }
+36 -46
View File
@@ -30,6 +30,7 @@
#include "binaural/binaural_runtime.h" #include "binaural/binaural_runtime.h"
#include "eac3_transport/eac3_reader.h" #include "eac3_transport/eac3_reader.h"
#include "emdf/emdf_parser.h" #include "emdf/emdf_parser.h"
#include "foundation/mini_json.h"
#include "foundation/sha256.h" #include "foundation/sha256.h"
#include "hrtf/jochrtf.h" #include "hrtf/jochrtf.h"
#include "hrtf/rosella_model.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"); 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<unsigned char>(character) < 0x20u) {
out += fmt("\\u%04x", static_cast<unsigned>(character));
} else {
out.push_back(character);
}
}
}
return out;
}
// Parses the bitstream without decoding or rendering: --metadata-only and the // Parses the bitstream without decoding or rendering: --metadata-only and the
// diagnostic metadata reports. // diagnostic metadata reports.
Status report_metadata(const Settings& cfg, telemetry::EventBus* bus, const std::string& eac3_path, 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<unsigned long long>(oamd_payloads)); static_cast<unsigned long long>(oamd_payloads));
} }
if (!cfg.metadata_json_path.empty()) { if (!cfg.metadata_json_path.empty()) {
std::string json = fmt( const std::string document = json::pretty_object({
"{\n \"input\": \"%s\",\n \"frames\": %llu,\n \"objects_min\": %u,\n" {"input", json::quote(cfg.input_path)},
" \"objects_mean\": %.6f,\n \"objects_max\": %u,\n \"clipgain_min\": %.9g,\n" {"frames", std::to_string(frames)},
" \"clipgain_max\": %.9g,\n \"oamd_payloads\": %llu\n}\n", {"objects_min", std::to_string(objects_min == 0xFFFFFFFFu ? 0u : objects_min)},
json_escape(cfg.input_path).c_str(), static_cast<unsigned long long>(frames), {"objects_mean", fmt("%.6f", mean_objects)},
objects_min == 0xFFFFFFFFu ? 0u : objects_min, mean_objects, objects_max, {"objects_max", std::to_string(objects_max)},
clipgain_min, clipgain_max, static_cast<unsigned long long>(oamd_payloads)); {"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); std::ofstream output = fs_utf8::open_output(cfg.metadata_json_path);
if (!output) { if (!output) {
return Status::fail(JOC_ERR_OUTPUT_OPEN, stage::kOutput, return Status::fail(JOC_ERR_OUTPUT_OPEN, stage::kOutput,
"cannot write " + cfg.metadata_json_path); "cannot write " + cfg.metadata_json_path);
} }
output << json; output << document << "\n";
} }
bus->emit(JOC_EV_METADATA_INDEXED, JOC_STAGE_METADATA, JOC_LOG_INFO, bus->emit(JOC_EV_METADATA_INDEXED, JOC_STAGE_METADATA, JOC_LOG_INFO,
fmt("metadata: %llu frames, oamd payloads %llu", 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) { if (out == nullptr) {
return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kOutput, "null output"); return Status::fail(JOC_ERR_INVALID_ARGUMENT, stage::kOutput, "null output");
} }
*out = fmt( // The string fields are escaped, so a Windows path or a quote in an error
"{\"status\":%u,\"error_code\":%u,\"error_stage\":\"%s\",\"error_message\":\"%s\"," // message still leaves the document valid JSON.
"\"input_frames\":%llu,\"output_samples\":%llu,\"duration_sec\":%.6f," *out = json::pretty_object({
"\"output_format_actual\":%u,\"output_peak\":%.9g,\"output_over_unity_values\":%llu," {"status", std::to_string(result.status)},
"\"output_file_bytes\":%llu,\"output_sha256\":\"%s\",\"oamd_payloads\":%llu," {"error_code", std::to_string(result.error_code)},
"\"oamd_transitions\":%llu,\"t_decode_bed\":%.6f,\"t_render\":%.6f,\"t_write\":%.6f," {"error_stage", json::quote(result.error_stage)},
"\"t_render_dsp\":%.6f,\"t_write_file\":%.6f,\"t_total\":%.6f}", {"error_message", json::quote(result.error_message)},
result.status, result.error_code, result.error_stage, result.error_message, {"input_frames", std::to_string(result.input_frames)},
static_cast<unsigned long long>(result.input_frames), {"output_samples", std::to_string(result.output_samples)},
static_cast<unsigned long long>(result.output_samples), result.duration_sec, {"duration_sec", fmt("%.6f", result.duration_sec)},
result.output_format_actual, result.output_peak, {"output_format_actual", std::to_string(result.output_format_actual)},
static_cast<unsigned long long>(result.output_over_unity_values), {"output_peak", fmt("%.9g", result.output_peak)},
static_cast<unsigned long long>(result.output_file_bytes), result.output_sha256, {"output_over_unity_values", std::to_string(result.output_over_unity_values)},
static_cast<unsigned long long>(result.oamd_payloads), {"output_file_bytes", std::to_string(result.output_file_bytes)},
static_cast<unsigned long long>(result.oamd_transitions), result.t_decode_bed, {"output_sha256", json::quote(result.output_sha256)},
result.t_render, result.t_write, result.t_render_dsp, result.t_write_file, {"oamd_payloads", std::to_string(result.oamd_payloads)},
result.t_total); {"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(); return Status::success();
} }