Skip to content

Write and read JSON in Rust, by serde - #26

Merged
nickyoung-github merged 1 commit into
masterfrom
feature/rust-json
Nov 29, 2026
Merged

nickyoung-github merged 1 commit into
masterfrom
feature/rust-json

Conversation

@nickyoung-github

Copy link
Copy Markdown
Owner

The generated Rust structs are written as JSON, and read from it, by serde: as JSON_FORMAT.md has it, which Python and C++ write and read.

  • With the crate's feature json, each struct, enum and union is Serialize and Deserialize: so they are also what a Rust program gives any other format of serde's. dataclass_native::json::{to_json, from_json} write and read JSON.
  • The crate still depends on nothing without the feature: the generated code of serde is within the crate's macro json!, which is of nothing without it. The feature python has it.
  • The generated code implements the crate's Form, and serde's traits by that, not by serde's derive: as what the format has of a float which is not finite, or of bytes, it has wherever one is, as within a tuple. A plain union is read by the kind of value which is next, and of a string by which of its members reads it.
  • serde_json is given a formatter which writes a float as Python does, so what is written is the text which Python and C++ write: though it need not be.
  • The classes of an extension have __json__ and __from_json__, as those of C++ have, so JSON of what is bound to Rust no longer goes by way of Python.
  • What is rejected is said as serde says it, within "The data is not a Money, as written" or "The data is not JSON": two tests which had what Python and C++ say now take either.

Also, as asked: WIRE_FORMAT.md and JSON_FORMAT.md say that what is asked of the implementations is that each reads what any of them writes, not that they write the same.

On an Apple M1, of extensions built for release, in microseconds for each Order of the README's Performance section:

pure C++ Rust, before Rust, now
Write JSON 10.7 2.3 17.1 2.1
Read JSON 13.6 3.1 20.5 2.8

Rust reads otherwise than Python and C++ only what none of them writes: -0 is not an integer to serde, and is the float -0.0; and a key which is a number may have no spaces about it. Five cases of the test that each reads as the other does are of these, and are not run with the Rust extension. JSON_FORMAT.md says so.

Tested by the tests of JSON which are for every mode, run with the extension; and by a program of Rust alone, which cargo now runs both without the feature and with it. Also checked, though not kept as a test: of 21 objects, Rust writes the bytes which the Python reference does; and of some 7,600 corruptions of them, Rust and C++ each took or rejected the same.

Arrow is still done by Python for what Rust holds, and is to follow.

…lize and Deserialize, as JSON_FORMAT.md has it
@nickyoung-github
nickyoung-github merged commit 28e0e08 into master Nov 29, 2026
6 checks passed
@nickyoung-github
nickyoung-github deleted the feature/rust-json branch November 29, 2026 22:31
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