#protobuf #json #serde-json

build pbjson-build

Generates Serialize and Deserialize implementations for prost message types

18 releases

Uses new Rust 2024

0.9.0 Dec 9, 2025
0.8.0 Jul 15, 2025
0.7.0 Jul 9, 2024
0.6.2 Sep 18, 2023
0.1.1 Dec 7, 2021

#1543 in Encoding

Download history 138937/week @ 2025-12-24 251946/week @ 2025-12-31 562399/week @ 2026-01-07 632899/week @ 2026-01-14 787196/week @ 2026-01-21 658568/week @ 2026-01-28 659673/week @ 2026-02-04 620253/week @ 2026-02-11 674775/week @ 2026-02-18 768814/week @ 2026-02-25 887963/week @ 2026-03-04 961563/week @ 2026-03-11 634321/week @ 2026-03-18 574087/week @ 2026-03-25 566237/week @ 2026-04-01 667175/week @ 2026-04-08

2,623,243 downloads per month
Used in 275 crates (38 directly)

MIT license

80KB
2K SLoC

pbjson-build consumes the descriptor output of prost-build and generates serde::Serialize and serde::Deserialize implementations that are compliant with the protobuf JSON mapping

Usage

It is recommended you first follow the example in prost-build to familiarise yourself with prost

Add prost-build, prost, pbjson, pbjson-build and pbjson-types to your Cargo.toml

[dependencies]
prost = <prost-version>
pbjson = <pbjson-version>
pbjson-types = <pbjson-version>

[build-dependencies]
prost-build = <prost-version>
pbjson-build = <pbjson-version>

Next create a build.rs containing the following

// This assumes protobuf files are under a directory called `protos`
// and in a protobuf package `mypackage`

let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("protos");
let proto_files = vec![root.join("myproto.proto")];

// Tell cargo to recompile if any of these proto files are changed
for proto_file in &proto_files {
    println!("cargo:rerun-if-changed={}", proto_file.display());
}

let descriptor_path = PathBuf::from(env::var("OUT_DIR").unwrap())
    .join("proto_descriptor.bin");

prost_build::Config::new()
    // Save descriptors to file
    .file_descriptor_set_path(&descriptor_path)
    // Override prost-types with pbjson-types
    .compile_well_known_types()
    .extern_path(".google.protobuf", "::pbjson_types")
    // Generate prost structs
    .compile_protos(&proto_files, &[root])?;

let descriptor_set = std::fs::read(descriptor_path)?;
pbjson_build::Builder::new()
    .register_descriptors(&descriptor_set)?
    .build(&[".mypackage"])?;

Finally within lib.rs

/// Generated by [`prost-build`]
include!(concat!(env!("OUT_DIR"), "/mypackage.rs"));
/// Generated by [`pbjson-build`]
include!(concat!(env!("OUT_DIR"), "/mypackage.serde.rs"));

The module will now contain the generated prost structs for your protobuf definition along with compliant implementations of serde::Serialize and serde::Deserialize


Pbjson

Pbjson is a set of crates to automatically generate serde Serialize and Deserialize implementations for auto-generated prost types.

See pbjson-build for usage instructions

Dependencies

~1–1.6MB
~30K SLoC