[deep-review][duplication] D7: Per-file JsonSerializerOptions singletons with drifted defaults
Summary
Five separate per-file JsonSerializerOptions singletons in the codebase, with subtly different defaults. The two most consequential drifts:
WindowsNodeClientsetsDefaultIgnoreCondition = WhenWritingNull(deliberate; documented atWindowsNodeClient.cs:47— keepsauth/errorabsent rather than null on the wire).OpenClawGatewayClientdoes not set this — so its outbound payloads emit"error": nullinstead of omitting the key.
Other smaller drifts: PropertyNameCaseInsensitive, WriteIndented, presence of custom converters.
Where
Five JsonSerializerOptions declarations to consolidate:
src/OpenClaw.Shared/WindowsNodeClient.cs:47— usesWhenWritingNull.src/OpenClaw.Shared/OpenClawGatewayClient.cs— does not useWhenWritingNull.src/OpenClaw.Shared/Mcp/McpToolBridge.cs— JSON-RPC formatting.src/OpenClaw.Tray.WinUI/Services/...— at least one site for diagnostics or settings.src/OpenClaw.WinNode.Cli/Program.cs:213-310— usesUtf8JsonWriterdirectly with its own conventions.
grep -rn "JsonSerializerOptions" src/ for the full list.
Why it matters
- Wire-shape drift. Operator and node messages can have different
nullsemantics, even though they share an envelope shape and the same gateway parses both. A gateway-side change that becomes strict aboutnullvs missing breaks one side without the other. - Per-file caching is fine; per-file defaults are not. The pattern is correct in principle (cache the options once, reuse), but each cache snapshot has subtly different rules.
- Adding a custom converter (e.g., a typed
JsonValueConverterforA2UIValue) means hunting down all five sites and updating them — easy to miss one.
Proposed fix
src/OpenClaw.Shared/Json/JsonOptions.cs:
public static class JsonOptions
{
public static JsonSerializerOptions ForWireDefault { get; } = Create(...);
public static JsonSerializerOptions ForLogging { get; } = Create(indented: true, ...);
// ...as needed
}
Migrate all five sites to read from JsonOptions. Decide once (probably yes) whether WhenWritingNull is the wire default.
Severity
Low — wire-shape drift; subtle but real. Becomes Medium next time the gateway-side schema becomes stricter.
Related
- A5 (overlapping JSON helpers) — same general area, complementary fix.
[!NOTE] The pull request was not created — a fallback review issue was created instead due to protected file changes: #431
🤖 This is an automated response from Repo Assist.
The JsonOptions central-config approach from the issue is the right fix. A few implementation notes to smooth the migration:
The key decision: WhenWritingNull as the wire default
The issue flags the WindowsNodeClient vs OpenClawGatewayClient drift. Looking at the wire shape: the gateway parses both operator and node envelopes, and the protocol treats absent fields and null values differently (e.g. "error": null vs omitting "error"). WhenWritingNull should be the default for outbound wire serialization — absent is cleaner than explicit null for optional envelope fields.
Suggested JsonOptions.cs structure:
// src/OpenClaw.Shared/Json/JsonOptions.cs
public static class JsonOptions
{
/// Standard outbound wire serialization: null fields omitted, camelCase.
public static JsonSerializerOptions Wire { get; } = new(JsonSerializerDefaults.Web)
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
};
/// Human-readable indented variant for logs and diagnostics.
public static JsonSerializerOptions Indented { get; } = new(Wire) { WriteIndented = true };
}
Using JsonSerializerDefaults.Web sets PropertyNameCaseInsensitive = true and PropertyNamingPolicy = CamelCase, which matches the current WindowsNodeClient options.
Migration order: start with WindowsNodeClient and OpenClawGatewayClient (sites 1 and 2 from the issue). The McpToolBridge JSON-RPC site (site 3) should be validated separately — MCP JSON-RPC has its own schema requirements and may need a distinct options instance. Don't assume Wire is a drop-in there without checking what the current options look like.
Site 5 (WinNode.Cli/Program.cs): Utf8JsonWriter bypasses JsonSerializerOptions entirely — it's a direct writer. Leave that site for a separate pass once the JsonSerializer sites are consolidated.
Generated by 🌈 Repo Assist, see workflow run. Learn more.
To install this agentic workflow, run
gh aw add githubnext/agentics/workflows/repo-assist.md@97143ac59cb3a13ef2a77581f929f06719c7402a
Closing during the post-refactor deep-review cleanup. JsonSerializerOptions drift is consolidated into #372 for a fresh bounded audit. This keeps the signal while reducing stale one-off issue noise.