Skip to content

Add an as_json flag to dumps()/dump() for guaranteed RFC 8259-compliant output #158

Open

Description

@rahul05ranjan

Filing this as the follow-up that came out of #151, per the discussion there.

dumps() has several defaults that make its output legal JSON5 but not legal JSON:

  • identifier keys are emitted unquoted ({foo: 1})
  • trailing commas are added when indenting
  • NaN/Infinity literals are allowed (allow_nan=True by default)

The dump() docstring claim that quote_keys=True, trailing_commas=False "should produce exactly the same output as json.dump(obj, fp)" turned out to be misleading, because the json module also refuses non-finite floats where the JSON grammar has no spelling for them. #155 fixed the CLI side of this (--as-json now rejects non-finite numbers), but the underlying API still makes callers compose the guarantee themselves.

Proposal: an as_json flag on dumps()/dump() that bundles the JSON-compliance guarantees:

  • quote all object keys
  • no trailing commas
  • reject NaN/Infinity (allow_nan=False behavior)

with the explicit goal from #151 being: the output is legal JSON per RFC 8259 and evaluates to the same values — not byte-for-byte identity with the json module's output.

The CLI's --as-json mode could then route through dumps(as_json=True) so the library and the CLI share one definition of "JSON output" (today the CLI hand-sets the three kwargs and the flag's semantics live partly in tool.py).

Happy to take a crack at this if that's useful, though it looked like a largeish change touching the docstring compatibility claims too.

Activity

  1. dpranke commented on Nov 2, 2026

    @dpranke
    Owner

    Happy to take a crack at this if that's useful

    Sure! :).

    I would as part of this also update the doc strings and other places to reflect that the quote_keys/trailing_commas combination just matches JSON's general formatting, not the exact output.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions