JSON to Rust Struct
Convierte objetos JSON a definiciones de struct Rust con macros derive serde.
Acerca de esta herramienta
JSON to Rust Struct es una herramienta de generación de código que convierte automáticamente datos JSON en definiciones de struct de Rust con tipos seguros. Dado que Rust requiere declaraciones explícitas de tipos, escribir manualmente structs a partir de JSON puede ser tedioso y propenso a errores, especialmente con objetos grandes u objetos anidados profundamente. Esta herramienta elimina ese trabajo repetitivo analizando la estructura JSON y generando código struct con macros derivadas serde para serialización y deserialización sin inconvenientes.
Simplemente pegue sus datos JSON en el campo de entrada y la herramienta generará instantáneamente código struct de Rust listo para usar en su proyecto. Los structs generados incluyen anotaciones serde estándar, lo que los hace compatibles con bibliotecas populares de Rust como serde_json. Esto es particularmente útil al trabajar con API externas, archivos de configuración o cualquier dato basado en JSON que deba analizarse en Rust.
La herramienta maneja campos opcionales y objetos anidados de forma inteligente, reduciendo los ajustes manuales necesarios antes de que el código se compile. Ya sea que esté creando un servicio web, una herramienta CLI o integrando con API REST, este convertidor ahorra tiempo y ayuda a mantener la seguridad de tipos en toda su base de código Rust.
Preguntas Frecuentes
Implementación de Código
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.