Skip to content

Write and read the JSON of what is held in Python by pydantic, where it is installed - #31

Merged
nickyoung-github merged 1 commit into
masterfrom
feature/pure-json-by-pydantic
Nov 30, 2026
Merged

nickyoung-github merged 1 commit into
masterfrom
feature/pure-json-by-pydantic

Conversation

@nickyoung-github

Copy link
Copy Markdown
Owner

Where pydantic is installed, to_json and from_json write and read by pydantic-core what Python holds itself: so every class in pure mode. It is three to four times faster than the Python codec for a class, and up to ten for a value.

Microseconds per Order pure, Python pure, pydantic C++ Rust
Write JSON 10.9 3.5 2.3 2.0
Read JSON 13.6 3.6 3.2 2.7

How

  • Schema: pydantic-core is given a schema made from our description of the type (_pydantic.py), one to write by and one to read by, not that of a TypeAdapter. So the format is JSON_FORMAT.md's, with a DiscriminatedUnion as {"t": ..., "v": ...}.
  • Reference: the Python codec is still the reference. If pydantic refuses a value or data, the Python codec is given it, and raises as before or writes or reads what pydantic could not.
  • Within pydantic, by the Python codec's functions: a Decimal, bytes, a Float32, an array, a key which is not a string, a timedelta which is read, and a plain union of a float and what is written as an integer.
  • Left to the Python codec: a class bound to a native class, a pydantic dataclass, and a class with a field it is not made from.
  • Dependency: optional, dataclass-native[pydantic] (pydantic>=2.8), and used whenever it can be imported.

What pydantic does otherwise

In JSON_FORMAT.md. C++, Rust and the Python codec read what it writes.

  • A float as it writes one: 0.00001 where Python writes 1e-05.
  • A timedelta of a year or more with its years: "P1Y35D".
  • Of a key which is twice in an object, it reads the last, where the Python codec refuses the data.
  • It does not notice NaN or Infinity within an entry which is ignored.

Writing is checked

pydantic does not validate what it writes: it writes None as null in any field, a subclass as its base, and within a union a value no member takes as whatever it is, with only a warning. So each value is first checked to be of its type, by a function pydantic calls, and unions are not left to pydantic's own. That is why writing takes 3.5 µs where pydantic alone takes 1.7.

Tested

  • The JSON tests run twice: by pydantic, and by test_json_in_python.py with the Python alone.
  • New cases in test_invalid_values for what pydantic would have written, each asserting that no warning is given.
  • The JSON tests also pass with pydantic 2.11 and 2.14; with 2.8, all but the existing TypeAdapter tests of bytes.
  • Not in the suite: pydantic's path fuzzed against the Python codec, with some 230,000 corrupted texts and 230,000 values not of their types. They disagreed only on the two readings above.

Not decided

  • Whether there should be a way to turn it off: it is used whenever pydantic is installed.
  • Whether to keep the checks on writing, which cost about half of its time.

@nickyoung-github
nickyoung-github merged commit fa0fc0b into master Nov 30, 2026
6 checks passed
@nickyoung-github
nickyoung-github deleted the feature/pure-json-by-pydantic branch November 30, 2026 13:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant