JSON in Rust
Nessun contenuto
Strumento online gratuito JSON in Rust. Incolla JSON di risposta API per generare automaticamente struct con derive Serde, oggetti annidati suddivisi indipendentemente, Vec e Option dedotti intelligentemente, conversione locale pura nel browser, nessuna registrazione richiesta.
Raccomandazioni correlate
Informazioni su JSON in Rust: Convertire dati JSON in struct Rust compilabili
JSON in Rust è il processo di conversione dei dati in formato JSON in definizioni di tipo struct Rust.
Questo strumento viene eseguito localmente nel browser basato su quicktype-core.
L'inferenza dei tipi è il cuore di JSON in Rust.
Tutti i calcoli avvengono nel browser, nessun dato viene inviato a server.
Il codice generato viene tipicamente utilizzato in progetti Cargo.
Il codice generato automaticamente è un punto di partenza, non un punto di arrivo.
Casi d'uso
- Integrazione REST API
- Sviluppo backend Actix-web
- Sviluppo backend axum
- Definizione interfaccia microservizi
- Sviluppo strumenti CLI
- Progetti WebAssembly
- App desktop Tauri
- Parsing dati web scraper
- Sviluppo giochi
- IoT e embedded
- Migrazione database
- Blockchain/Web3
- Costruzione dati di test
- Parsing log strutturati
- Migrazione centro di configurazione
- Collaborazione cross-language
- Manutenzione SDK open source
- Esempi didattici Rust
- Conversione nomi campo
Come utilizzare
- Incolla JSON nell'editor a sinistra o carica un file
- Attendi 400ms per la conversione automatica
- Se JSON è malformato clicca «Ripara JSON»
- Clicca «Copia» o «Scarica» per salvare il file .rs
- Aggiungi dipendenze serde e serde_json a Cargo.toml
- (Opzionale) Regola manualmente i tipi di campo
Funzionalità
- Conversione locale nel browser: Parsing JSON e generazione codice Rust nel browser tramite quicktype-core Web Worker
- Output pronto per Serde: Genera automaticamente use serde::{Serialize, Deserialize}; e #[derive(...)]
- Inferenza intelligente dei tipi: String→String, intero→i64, float→f64, bool→bool, array→Vec<T>, oggetto annidato→struct indipendente, null→Option<serde_json::Value>
- Suddivisione automatica oggetti annidati
- Gestione automatica Vec e Option
- Pronto per Actix-web/axum/reqwest
- Auto-conversione con debounce 400ms
- Correzione errori JSON in un clic
- Copia e download
- Cronologia localStorage
- Pannelli reattivi
- Dati di esempio + caricamento file
Esempi di codice
Rust: Handler Actix-web che riceve body richiesta JSON
rustUsa Actix-web 4.x con la struct generata come parametro web::Json<T>.
// Cargo.toml
// [dependencies]
// actix-web = "4"
// serde = { version = "1", features = ["derive"] }
// serde_json = "1"
use actix_web::{web, App, HttpServer, HttpResponse, Responder};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
pub struct CreateUserRequest {
name: String,
email: String,
age: i64,
tags: Vec<String>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct User {
id: i64,
name: String,
email: String,
age: i64,
tags: Vec<String>,
}
async fn create_user(payload: web::Json<CreateUserRequest>) -> impl Responder {
let req = payload.into_inner();
let user = User { id: 1, name: req.name, email: req.email, age: req.age, tags: req.tags };
HttpResponse::Ok().json(user)
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| { App::new().route("/users", web::post().to(create_user)) })
.bind("127.0.0.1:8080")?.run().await
}Rust: Route axum + parsing risposta client reqwest
rustServer axum usa estrattore Json<T>, client reqwest usa .json::<T>().
// Cargo.toml
// axum = "0.7"
// tokio = { version = "1", features = ["full"] }
// serde = { version = "1", features = ["derive"] }
// reqwest = { version = "0.12", features = ["json"] }
use axum::{routing::get, Json, Router};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Address { city: String, zip: String }
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct User { id: i64, name: String, email: String, address: Address, tags: Vec<String> }
async fn get_user() -> Json<User> {
Json(User { id:1, name:"Alice".into(), email:"a@b.com".into(), address: Address{city:"Beijing".into(),zip:"100000".into()}, tags:vec!["rust".into(),"axum".into()] })
}
#[tokio::main]
async fn main() {
let app = Router::new().route("/user", get(get_user));
let listener = tokio::net::TcpListener::bind("127.0.0.1:8080").await.unwrap();
axum::serve(listener, app).await.unwrap();
}Domande frequenti
Come converto JSON in una struct Rust?
Incolla JSON nell'editor a sinistra, lo strumento genera automaticamente il codice Rust.
Il codice Rust generato include annotazioni serde?
Sì, include #[derive(Serialize, Deserialize)] e le istruzioni use.
Quali strutture JSON sono supportate?
Tutte le strutture JSON valide: tipi primitivi, array, oggetti annidati.
Come vengono mappati i tipi JSON ai tipi Rust?
Stringhe→String, interi→i64, float→f64, booleani→bool, array→Vec<T>, oggetti annidati→struct indipendente, null→Option<serde_json::Value>.
Che tipo generano i valori null?
I valori null generano Option<serde_json::Value>.
Gli array vengono convertiti in Vec?
Sì, gli array JSON diventano Vec<T>.
Come vengono gestiti gli oggetti annidati?
Ogni oggetto annidato genera una struct indipendente.
Posso personalizzare il nome della struct?
Sì, modifica manualmente i nomi dopo la generazione.
Il codice generato può essere usato direttamente in un progetto Cargo?
Sì, aggiungi serde e serde_json a Cargo.toml.
Può essere usato per Actix-web/axum?
Sì, direttamente come parametro web::Json<T> o Json<T>.
Supporta parametri di comando Tauri?
Sì, usa come tipo parametro #[tauri::command].
Supporta tipi precisi come chrono::DateTime?
Sì, modifica manualmente i tipi dopo la generazione.
I dati vengono caricati sui server?
No, tutto viene eseguito localmente nel browser.
È richiesta la registrazione?
No, completamente gratuito senza registrazione.
Risoluzione dei problemi
Mostra «Inserisci dati JSON» o lato destro vuoto
Mostra errore parsing JSON
Tipi di campo non sufficientemente precisi
Errore compilazione crate serde/serde_json mancante
Glossario
- struct
- Parola chiave in Rust per definire tipi di dati composti.
- Serde
- Il framework di serializzazione/deserializzazione più popolare nell'ecosistema Rust.
- Vec<T>
- Tipo array dinamico nella libreria standard Rust.
- Option<T>
- Tipo valore opzionale nella libreria standard Rust.
- Cargo
- Sistema di build e gestore pacchetti ufficiale di Rust.
- serde_json
- Crate del framework Serde specifico per l'elaborazione JSON.
- Serialize
- Trait di serializzazione di Serde.
- Deserialize
- Trait di deserializzazione di Serde.
- quicktype-core
- Libreria di inferenza tipi e generazione codice multi-linguaggio.
- chrono
- Libreria data/ora più popolare nell'ecosistema Rust.
- no_std
- Modalità di compilazione Rust che disabilita la libreria standard.
- tokio
- Runtime asincrono più popolare nell'ecosistema Rust.
Tabella di riferimento tipi JSON → tipi Rust
Lo strumento deduce automaticamente i tipi Rust corrispondenti:
| Esempio JSON | Tipo Rust generato | Descrizione |
|---|---|---|
null | Option<serde_json::Value> | Valore null, Option come fallback |
true / false | bool | Booleani JSON → bool |
42 | i64 | Interi JSON → i64 |
3.14 | f64 | Float JSON → f64 |
"hello" | String | Stringhe JSON → String |
["a","b"] | Vec<String> | Array di stringhe → Vec<String> |
[1,2,3] | Vec<i64> | Array di interi → Vec<i64> |
[{...},{...}] | Vec<Item> | Array di oggetti → Vec<Item> |
[] | Vec<serde_json::Value> | Array vuoto |
{...} oggetto annidato | struct indipendente | Oggetti annidati generano struct indipendenti |
Privacy & Security
Tutte le operazioni di questo strumento JSON in Rust avvengono completamente localmente nel browser: parsing JSON, generazione codice Rust e download file sono eseguiti tutti lato client tramite il modulo WebAssembly quicktype-core; nessun contenuto JSON, file caricati o codice generato viene inviato in rete a server.
Authoritative References
- Compressione JSON
- CSV in JSON
- JSON to CSV
- JSON Diff
- JSON Escape / Unescape
- Appiattimento JSON
- Formattatore JSON
- Generatore JSON
- Query JSONPath
- Unire JSON
- Riparare JSON
- Validatore di Schema JSON
- Ordinare JSON
- JSON Stringify
- JSON in HTML
- JSON in Java
- JSON to Markdown
- JSON in SQL
- JSON in TOML
- JSON in TypeScript
- XML in JSON
- JSON in XML
- YAML in JSON
- Da JSON a YAML
- JSON in Python
- JSON in Go
- JSON in Rust
- JSON in Swift
- JSON a C#
- JSON a C++
- JSON to PHP