loader: emit traffic body as nested JSON, not escaped string

Body field in traffic.ndjson / battle-traffic.ndjson is now a real nested
JSON object instead of an escaped JSON string. Parse-on-write is wrapped in
ParseBodyOrKeep with a string-fallback so a future non-JSON caller doesn't
crash the line write.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
gamer147
2026-05-26 17:42:55 -04:00
parent 34a17a2b6b
commit 76b7e59489

View File

@@ -91,7 +91,7 @@ internal static class CaptureWriter
{ "direction", direction },
{ "url", url },
{ "encrypted", encrypted },
{ "body", body },
{ "body", ParseBodyOrKeep(body) },
});
}
@@ -102,7 +102,7 @@ internal static class CaptureWriter
{ "ts", DateTime.UtcNow.ToString("o") },
{ "direction", direction },
{ "uri", uri },
{ "body", body },
{ "body", ParseBodyOrKeep(body) },
});
}
@@ -278,6 +278,17 @@ internal static class CaptureWriter
if (classes.Count > 0) dump["classes"] = classes;
}
// Parse body JSON so it serializes nested in the NDJSON line rather than as an
// escaped string. Falls back to the original string on parse failure — no current
// caller produces non-JSON, but a future caller passing raw text shouldn't crash
// the line write.
private static object ParseBodyOrKeep(string body)
{
if (string.IsNullOrEmpty(body)) return body;
try { return JsonMapper.ToObject(body); }
catch { return body; }
}
private static void AppendNdjson(string path, Dictionary<string, object> entry)
{
string line = JsonMapper.ToJson(entry);