Fix the result JSON escaping and indent both JSON outputs
This commit is contained in:
1 parent
9b21f8861c
commit
9b71940722
5 files changed
+160
-72
No files matched your search
@@ -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<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) {
|
||||
out->clear();
|
||||
std::size_t index = 0;
|
||||
|
||||
@@ -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<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);
|
||||
|
||||
bool as_string(const Member& member, std::string* out);
|
||||
|
||||
+5
-26
@@ -13,6 +13,7 @@
|
||||
#include <vector>
|
||||
|
||||
#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<double>& 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<unsigned char>(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); }
|
||||
|
||||
|
||||
+36
-46
@@ -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<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
|
||||
// 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<unsigned long long>(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<unsigned long long>(frames),
|
||||
objects_min == 0xFFFFFFFFu ? 0u : objects_min, mean_objects, objects_max,
|
||||
clipgain_min, clipgain_max, static_cast<unsigned long long>(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<unsigned long long>(result.input_frames),
|
||||
static_cast<unsigned long long>(result.output_samples), result.duration_sec,
|
||||
result.output_format_actual, result.output_peak,
|
||||
static_cast<unsigned long long>(result.output_over_unity_values),
|
||||
static_cast<unsigned long long>(result.output_file_bytes), result.output_sha256,
|
||||
static_cast<unsigned long long>(result.oamd_payloads),
|
||||
static_cast<unsigned long long>(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();
|
||||
}
|
||||
|
||||
|
||||
@@ -32,6 +32,7 @@
|
||||
#include "io/zip_reader.h"
|
||||
#include "oamd/oamd_parser.h"
|
||||
#include "stream/stream.h"
|
||||
#include "task/task.h"
|
||||
|
||||
namespace {
|
||||
|
||||
@@ -173,6 +174,58 @@ void test_mini_json() {
|
||||
CHECK(!joc::json::parse_object("[1,2]", &duplicate, &error));
|
||||
}
|
||||
|
||||
void test_json_text() {
|
||||
// Escaping matches json.dumps(..., ensure_ascii=False): the named escapes,
|
||||
// \u00XX for the other control characters, UTF-8 bytes passed through.
|
||||
CHECK(joc::json::quote("") == "\"\"");
|
||||
CHECK(joc::json::quote("a\"b\\c") == "\"a\\\"b\\\\c\"");
|
||||
CHECK(joc::json::quote("x\ny\tz") == "\"x\\ny\\tz\"");
|
||||
CHECK(joc::json::quote(std::string("\b\f", 2u)) == "\"\\b\\f\"");
|
||||
CHECK(joc::json::quote("\x01") == "\"\\u0001\"");
|
||||
CHECK(joc::json::quote("月と私") == "\"月と私\"");
|
||||
|
||||
// Two-space indentation, one member per line, no trailing newline.
|
||||
CHECK(joc::json::pretty_object({}) == "{}");
|
||||
const std::string document = joc::json::pretty_object({
|
||||
{"status", "0"},
|
||||
{"message", joc::json::quote("cannot open C:\\out\\a.wav")},
|
||||
});
|
||||
CHECK(document == "{\n \"status\": 0,\n \"message\": "
|
||||
"\"cannot open C:\\\\out\\\\a.wav\"\n}");
|
||||
std::vector<joc::json::Member> members;
|
||||
std::string error;
|
||||
CHECK(joc::json::parse_object(document, &members, &error));
|
||||
std::string message;
|
||||
CHECK(joc::json::as_string(*joc::json::find(members, "message"), &message) &&
|
||||
message == "cannot open C:\\out\\a.wav");
|
||||
}
|
||||
|
||||
void test_task_result_json() {
|
||||
joc_task_result result{};
|
||||
result.struct_size = sizeof(result);
|
||||
result.status = JOC_TASK_FAILED;
|
||||
result.error_code = JOC_ERR_OUTPUT_OPEN;
|
||||
std::snprintf(result.error_stage, sizeof(result.error_stage), "%s", "output");
|
||||
// A Windows path and a quote in the message: the document has to stay valid.
|
||||
std::snprintf(result.error_message, sizeof(result.error_message), "%s",
|
||||
"cannot write \"C:\\out\\a.wav\"");
|
||||
result.output_peak = 1.5;
|
||||
result.t_total = 6.355818;
|
||||
std::string text;
|
||||
CHECK(joc::task::result_to_json(result, &text).ok());
|
||||
CHECK(text.find("\n \"error_message\": ") != std::string::npos);
|
||||
std::vector<joc::json::Member> members;
|
||||
std::string error;
|
||||
CHECK(joc::json::parse_object(text, &members, &error));
|
||||
std::string message;
|
||||
CHECK(joc::json::as_string(*joc::json::find(members, "error_message"), &message) &&
|
||||
message == "cannot write \"C:\\out\\a.wav\"");
|
||||
double number = 0.0;
|
||||
CHECK(joc::json::as_number(*joc::json::find(members, "output_peak"), &number) && number == 1.5);
|
||||
CHECK(joc::json::as_number(*joc::json::find(members, "t_total"), &number) &&
|
||||
number == 6.355818);
|
||||
}
|
||||
|
||||
void test_npy() {
|
||||
// Build a minimal <f8 (2,) image in memory, padded the way NumPy pads it.
|
||||
std::string header = "{'descr': '<f8', 'fortran_order': False, 'shape': (2,), }";
|
||||
@@ -795,6 +848,8 @@ int main() {
|
||||
test_variable_bits();
|
||||
test_eac3_frame_bytes();
|
||||
test_mini_json();
|
||||
test_json_text();
|
||||
test_task_result_json();
|
||||
test_npy();
|
||||
test_oamd_and_adm_helpers();
|
||||
test_int24_packing();
|
||||
|
||||
Reference in new issue
Block a user