#data-structures #serialization #serde


Library to generate (de)serialization code in multiple languages

58 releases

0.25.1 Mar 28, 2023
0.24.0 Aug 8, 2022
0.23.0 Jul 5, 2022
0.20.6 Dec 3, 2021
0.8.0 Jul 28, 2020

#95 in Encoding

Download history 692/week @ 2023-02-11 748/week @ 2023-02-18 682/week @ 2023-02-25 438/week @ 2023-03-04 575/week @ 2023-03-11 642/week @ 2023-03-18 434/week @ 2023-03-25 481/week @ 2023-04-01 532/week @ 2023-04-08 458/week @ 2023-04-15 351/week @ 2023-04-22 445/week @ 2023-04-29 569/week @ 2023-05-06 401/week @ 2023-05-13 346/week @ 2023-05-20 391/week @ 2023-05-27

1,776 downloads per month
Used in 5 crates (4 directly)


17K SLoC

Rust 10K SLoC // 0.0% comments OCaml 1.5K SLoC // 0.0% comments Go 1K SLoC // 0.0% comments Python 1K SLoC // 0.0% comments Java 890 SLoC // 0.1% comments C# 745 SLoC // 0.1% comments Swift 716 SLoC // 0.1% comments Dart 669 SLoC // 0.1% comments TypeScript 400 SLoC // 0.1% comments


serde-generate on crates.io Documentation (latest release) License License

This crate aims to compile the data formats extracted from Rust by serde-reflection into type definitions and (de)serialization methods for other programming languages.

It can be used as a library or as a command-line tool (see serdegen below).

Supported Languages

The following programming languages are fully supported as target languages:

  • C++ 17
  • Java 8
  • Python 3 (requires numpy >= 1.20.1)
  • Rust 2018
  • Go >= 1.14
  • C# (NetCoreApp >= 2.1)
  • Swift 5.3
  • OCaml

The following languages are partially supported and still considered under development:

Supported Encodings

Type definitions in a target language are meant to be used together with a runtime library that provides (de)serialization in a particular Serde encoding format.

This crate provides easy-to-deploy runtime libraries for the following binary formats, in all supported languages:

  • Bincode (default configuration only),
  • BCS (short for Binary Canonical Serialization, the main format used in the Diem blockchain).

Quick Start with Python and Bincode

In the following example, we transfer a Test value from Rust to Python using bincode.

use serde::{Deserialize, Serialize};
use serde_reflection::{Registry, Tracer, TracerConfig};
use std::io::Write;

#[derive(Serialize, Deserialize)]
struct Test {
    a: Vec<u64>,
    b: (u32, u32),

// Obtain the Serde format of `Test`. (In practice, formats are more likely read from a file.)
let mut tracer = Tracer::new(TracerConfig::default());
let registry = tracer.registry().unwrap();

// Create Python class definitions.
let mut source = Vec::new();
let config = serde_generate::CodeGeneratorConfig::new("testing".to_string())
let generator = serde_generate::python3::CodeGenerator::new(&config);
generator.output(&mut source, &registry)?;

class Test:
    a: typing.Sequence[st.uint64]
    b: typing.Tuple[st.uint32, st.uint32]

// Append some test code to demonstrate Bincode deserialization
// using the runtime in `serde_generate/runtime/python/bincode`.
value = Test.bincode_deserialize(bytes({:?}))
assert value == Test(a=[4, 6], b=(3, 5))
    bincode::serialize(&Test { a: vec![4, 6], b: (3, 5) }).unwrap(),

// Execute the Python code.
let mut child = std::process::Command::new("python3")
    .env("PYTHONPATH", std::env::var("PYTHONPATH").unwrap_or_default() + ":runtime/python")
let output = child.wait_with_output()?;

Binary Tool

In addition to a Rust library, this crate provides a binary tool serdegen to process Serde formats saved on disk.

The tool serdegen assumes that a Rust value of type serde_reflection::Registry has been serialized into a YAML file. The recommended way to generate such a value is to use the library serde-reflection to introspect Rust definitions (see also the example above).

For a quick test, one may create a test file like this:

cat >test.yaml <<EOF
      B: UNIT

Then, the following command will generate Python class definitions and write them into test.py:

cargo run -p serde-generate-bin -- --language python3 test.yaml > test.py

To create a python module test and install the bincode runtime in a directory $DEST, you may run:

cargo run -p serde-generate-bin -- --language python3 --with-runtimes serde bincode --module-name test --target-source-dir "$DEST" test.yaml

See the help message of the tool with --help for more options.

Note: Outside of this repository, you may install the tool with cargo install serde-generate-bin then use $HOME/.cargo/bin/serdegen.


See the CONTRIBUTING file for how to help out.


This project is available under the terms of either the Apache 2.0 license or the MIT license.


~43K SLoC