JSON to Rust
Paste a JSON sample. Get Rust types, and the code that reads and writes the JSON. It runs in your browser, and your data never leaves it.
Open the tool with Rust chosen
An example
This JSON:
{
"id": 42,
"name": "Ada Lovelace",
"active": true,
"joined": "2020-01-02T03:04:05Z",
"tags": ["engineer", "writer"],
"address": { "city": "London", "postcode": null },
"scores": [9.5, 8, 10]
}
becomes this Rust:
// Example code that deserializes and serializes the model.
// extern crate serde;
// #[macro_use]
// extern crate serde_derive;
// extern crate serde_json;
//
// use generated_module::Root;
//
// fn main() {
// let json = r#"{"answer": 42}"#;
// let model: Root = serde_json::from_str(&json).unwrap();
// }
use serde::{Serialize, Deserialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Root {
pub id: i64,
pub name: String,
pub active: bool,
pub joined: String,
pub tags: Vec<String>,
pub address: Address,
pub scores: Vec<f64>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Address {
pub city: String,
pub postcode: Option<serde_json::Value>,
}
With the reader and the writer
The tool also writes the code that turns the text into those types, and back again. That code checks the shape as it reads, which is the part a type alone cannot do.
// Example code that deserializes and serializes the model.
// extern crate serde;
// #[macro_use]
// extern crate serde_derive;
// extern crate serde_json;
//
// use generated_module::Root;
//
// fn main() {
// let json = r#"{"answer": 42}"#;
// let model: Root = serde_json::from_str(&json).unwrap();
// }
use serde::{Serialize, Deserialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Root {
pub id: i64,
pub name: String,
pub active: bool,
pub joined: String,
pub tags: Vec<String>,
pub address: Address,
pub scores: Vec<f64>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Address {
pub city: String,
pub postcode: Option<serde_json::Value>,
}
What you need in your project
`serde` and `serde_json`.
Every struct carries `#[derive(Serialize, Deserialize)]`, so serde does the work. Add both crates to `Cargo.toml`.
A JSON key that is not a legal Rust name gets a `#[serde(rename = "...")]`, so the original name is kept.
A field that may be missing becomes `Option<T>`, which Rust forces you to handle. That is the language doing its job.
The trap to know about
A number above the range of `i64` becomes a float and loses digits. Keep such a value as a string and parse it yourself.
The options you can change
These belong to Rust, so the row of switches changes with the language.
| Option | What it does | Default |
|---|---|---|
| Density | Density | normal | dense |
| Visibility | Field visibility | private | crate | public |
| Integer type inference | Integer type inference | conservative | force-i32 | force-i64 |
| derive Debug | Derive Debug impl | true |
| derive Clone | Derive Clone impl | true |
| derive PartialEq | Derive PartialEq impl | false |
| Skip serializing empty Option fields | Skip serializing empty Option fields | false |
| Leading Comments | Leading Comments | true |
Questions
How do I turn JSON into Rust?
Paste your JSON into the box, choose Rust, and the code appears at once. Give the top type a name, and every other name follows from your keys. Then copy the code, or download it as a file.
Does my JSON go to a server?
No. The generator runs inside your browser, in a worker. Nothing is uploaded, nothing is stored, and the tool works with the network turned off.
What do I need to add to my Rust project?
`serde` and `serde_json`.
How does it know which fields are optional?
From your sample. A key that is missing, or holds null, becomes optional. One sample cannot show everything, so give more than one, or start from a JSON Schema.
Other languages
Try it on your own JSON. The generator runs in your browser. Nothing is uploaded, and nothing is stored on a server.
Open JSON to Code