🛠️ToolsShed

JSON to Rust Struct

Convert JSON objects to Rust struct definitions with serde derive macros.

Frequently Asked Questions

Code Implementation

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.