Repository navigation
Write and read the JSON of what is held in Python by pydantic, where it is installed - #31
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Where pydantic is installed,
to_jsonandfrom_jsonwrite and read by pydantic-core what Python holds itself: so every class inpuremode. It is three to four times faster than the Python codec for a class, and up to ten for a value.Orderpure, Pythonpure, pydanticHow
_pydantic.py), one to write by and one to read by, not that of aTypeAdapter. So the format is JSON_FORMAT.md's, with aDiscriminatedUnionas{"t": ..., "v": ...}.Decimal,bytes, aFloat32, 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.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.
0.00001where Python writes1e-05."P1Y35D".NaNorInfinitywithin an entry which is ignored.Writing is checked
pydantic does not validate what it writes: it writes
Noneasnullin 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
test_json_in_python.pywith the Python alone.test_invalid_valuesfor what pydantic would have written, each asserting that no warning is given.TypeAdaptertests of bytes.Not decided