#serialization #syn #serde

syn-serde

Library to serialize and deserialize Syn syntax trees

9 releases

new 0.3.2 Feb 27, 2026
0.3.1 Oct 18, 2023
0.3.0 Jul 16, 2023
0.2.3 Apr 6, 2021
0.2.0 Sep 16, 2019

#305 in Procedural macros

Download history 127/week @ 2025-11-07 289/week @ 2025-11-14 196/week @ 2025-11-21 253/week @ 2025-11-28 448/week @ 2025-12-05 456/week @ 2025-12-12 294/week @ 2025-12-19 128/week @ 2025-12-26 299/week @ 2026-01-02 150/week @ 2026-01-09 337/week @ 2026-01-16 515/week @ 2026-01-23 449/week @ 2026-01-30 657/week @ 2026-02-06 304/week @ 2026-02-13 286/week @ 2026-02-20

1,766 downloads per month
Used in 33 crates (14 directly)

Apache-2.0 OR MIT

240KB
6.5K SLoC

syn-serde

crates.io docs.rs license msrv github actions

Library to serialize and deserialize Syn syntax trees.

Usage

Add this to your Cargo.toml:

[dependencies]
syn-serde = "0.3"

Examples

[dependencies]
syn-serde = { version = "0.3", features = ["json"] }
syn = { version = "2", features = ["full"] }
use syn_serde::json;

let syn_file: syn::File = syn::parse_quote! {
    fn main() {
        println!("Hello, world!");
    }
};

println!("{}", json::to_string_pretty(&syn_file));

This prints the following JSON:

{
  "items": [
    {
      "fn": {
        "ident": "main",
        "inputs": [],
        "output": null,
        "stmts": [
          {
            "semi": {
              "macro": {
                "path": {
                  "segments": [
                    {
                      "ident": "println"
                    }
                  ]
                },
                "delimiter": "paren",
                "tokens": [
                  {
                    "lit": "\"Hello, world!\""
                  }
                ]
              }
            }
          }
        ]
      }
    }
  ]
}

Rust source file -> JSON representation of the syntax tree

The rust2json example parse a Rust source file into a syn_serde::File and print out a JSON representation of the syntax tree.

JSON file -> Rust syntax tree

The json2rust example parse a JSON file into a syn_serde::File and print out a Rust syntax tree.

Optional features

  • json — Provides functions for JSON <-> Rust serializing and deserializing.

Relationship to Syn

syn-serde is a fork of Syn, and syn-serde provides a set of data structures similar but not identical to Syn. All data structures provided by syn-serde can be converted to the data structures of Syn and proc-macro2.

The data structures of syn-serde 0.3 is compatible with the data structures of Syn 2.x.

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~0.3–1MB
~22K SLoC