Skip to content

[deep-review][duplication] D7: Per-file JsonSerializerOptions singletons with drifted defaults #370

Closed

Description

@codemonkeychris

Summary

Five separate per-file JsonSerializerOptions singletons in the codebase, with subtly different defaults. The two most consequential drifts:

  • WindowsNodeClient sets DefaultIgnoreCondition = WhenWritingNull (deliberate; documented at WindowsNodeClient.cs:47 — keeps auth/error absent rather than null on the wire).
  • OpenClawGatewayClient does not set this — so its outbound payloads emit "error": null instead of omitting the key.

Other smaller drifts: PropertyNameCaseInsensitive, WriteIndented, presence of custom converters.

Where

Five JsonSerializerOptions declarations to consolidate:

  1. src/OpenClaw.Shared/WindowsNodeClient.cs:47 — uses WhenWritingNull.
  2. src/OpenClaw.Shared/OpenClawGatewayClient.cs — does not use WhenWritingNull.
  3. src/OpenClaw.Shared/Mcp/McpToolBridge.cs — JSON-RPC formatting.
  4. src/OpenClaw.Tray.WinUI/Services/... — at least one site for diagnostics or settings.
  5. src/OpenClaw.WinNode.Cli/Program.cs:213-310 — uses Utf8JsonWriter directly with its own conventions.

grep -rn "JsonSerializerOptions" src/ for the full list.

Why it matters

  1. Wire-shape drift. Operator and node messages can have different null semantics, even though they share an envelope shape and the same gateway parses both. A gateway-side change that becomes strict about null vs missing breaks one side without the other.
  2. 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.
  3. Adding a custom converter (e.g., a typed JsonValueConverter for A2UIValue) 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.

Activity

  1. github-actions commented on May 17, 2026

    @github-actions
    Contributor

    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
    
  2. shanselman commented on May 18, 2026

    @shanselman
    Collaborator

    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.

  3. locked as resolved and limited conversation to collaborators on May 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions