JSON to Rust Struct
Converti gli oggetti JSON in definizioni di strutture Rust con macro derive serde.
Informazioni sullo strumento
JSON to Rust Struct è uno strumento di generazione del codice che converte automaticamente i dati JSON in definizioni di struct Rust fortemente tipizzate. Poiché Rust richiede dichiarazioni di tipo esplicite, scrivere manualmente struct da JSON può essere noioso e soggetto a errori, specialmente per oggetti grandi o profondamente annidati. Questo strumento elimina tale lavoro ripetitivo analizzando la struttura JSON e generando codice struct con macro derivate serde per la serializzazione e deserializzazione senza problemi.
Basta incollare i dati JSON nel campo di input e lo strumento genererà istantaneamente codice struct Rust pronto all'uso nel vostro progetto. Gli struct generati includono annotazioni serde standard, rendendoli compatibili con librerie Rust popolari come serde_json. Questo è particolarmente utile quando si lavora con API esterne, file di configurazione o qualsiasi dato basato su JSON che deve essere analizzato in Rust.
Lo strumento gestisce in modo intelligente i campi facoltativi e gli oggetti annidati, riducendo gli adattamenti manuali necessari prima della compilazione del codice. Sia che stiate creando un servizio web, uno strumento CLI o integrando API REST, questo convertitore fa risparmiare tempo e aiuta a mantenere la sicurezza dei tipi in tutta la vostra base di codice Rust.
Domande Frequenti
Implementazione del Codice
import json
from typing import Any
def to_pascal_case(s: str) -> str:
return ''.join(word.capitalize() for word in s.replace('-', '_').split('_'))
def to_snake_case(s: str) -> str:
import re
s = re.sub(r'([A-Z]+)([A-Z][a-z])', r'\1_\2', s)
s = re.sub(r'([a-z\d])([A-Z])', r'\1_\2', s)
return s.lower().replace('-', '_')
def infer_type(value: Any, key: str, structs: dict) -> str:
if value is None:
return 'Option<serde_json::Value>'
elif isinstance(value, bool):
return 'bool'
elif isinstance(value, int):
return 'i64'
elif isinstance(value, float):
return 'f64'
elif isinstance(value, str):
return 'String'
elif isinstance(value, list):
if not value:
return 'Vec<serde_json::Value>'
return f'Vec<{infer_type(value[0], key, structs)}>'
elif isinstance(value, dict):
struct_name = to_pascal_case(key)
collect_structs(value, struct_name, structs)
return struct_name
return 'serde_json::Value'
def collect_structs(obj: dict, name: str, structs: dict):
fields = []
for k, v in obj.items():
snake = to_snake_case(k)
rust_type = infer_type(v, k, structs)
if snake != k:
fields.append(f' #[serde(rename = "{k}")]')
fields.append(f' pub {snake}: {rust_type},')
structs[name] = fields
def json_to_rust(json_str: str, root_name: str = 'Root') -> str:
data = json.loads(json_str)
structs = {}
collect_structs(data, root_name, structs)
lines = []
for name, fields in structs.items():
lines.append('#[derive(Debug, Serialize, Deserialize)]')
lines.append(f'pub struct {name} {{')
lines.extend(fields)
lines.append('}')
lines.append('')
return '\n'.join(lines)
json_input = '{"name": "Alice", "age": 30, "active": true}'
print(json_to_rust(json_input))Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.