#protobuf #serialization #json

macro prost-reflect-derive

A derive macro for prost-reflect to generate ReflectMessage implementations

11 releases (6 breaking)

0.9.0 Jul 30, 2022
0.8.0 May 9, 2022
0.7.0 Apr 3, 2022
0.6.1 Feb 27, 2022
0.3.3 Dec 30, 2021

#549 in Encoding

Download history 555/week @ 2022-08-10 786/week @ 2022-08-17 570/week @ 2022-08-24 542/week @ 2022-08-31 483/week @ 2022-09-07 541/week @ 2022-09-14 485/week @ 2022-09-21 474/week @ 2022-09-28 459/week @ 2022-10-05 548/week @ 2022-10-12 547/week @ 2022-10-19 627/week @ 2022-10-26 614/week @ 2022-11-02 561/week @ 2022-11-09 562/week @ 2022-11-16 295/week @ 2022-11-23

2,160 downloads per month
Used in 2 crates (via prost-reflect)


119 lines

crates.io docs.rs deps.rs MSRV Continuous integration codecov.io Apache 2.0 OR MIT licensed


A protobuf library extending prost with reflection support and dynamic messages.


This crate provides support for dynamic protobuf messages. These are useful when the protobuf type definition is not known ahead of time.

The main entry points into the API of this crate are:

Example - decoding

DynamicMessage does not implement Default since it needs a message descriptor to function. To decode a protobuf byte stream into an instance of this type, use DynamicMessage::decode to create a default value for the MessageDescriptor instance and merge into it:

use prost::Message;
use prost_types::FileDescriptorSet;
use prost_reflect::{DynamicMessage, DescriptorPool, Value};

let pool = DescriptorPool::decode(include_bytes!("file_descriptor_set.bin").as_ref()).unwrap();
let message_descriptor = pool.get_message_by_name("package.MyMessage").unwrap();

let dynamic_message = DynamicMessage::decode(message_descriptor, b"\x08\x96\x01".as_ref()).unwrap();

assert_eq!(dynamic_message.get_field_by_name("foo").unwrap().as_ref(), &Value::I32(150));

Example - JSON mapping

When the serde feature is enabled, DynamicMessage can be deserialized to and from the canonical JSON mapping defined for protobuf messages.

use prost::Message;
use prost_reflect::{DynamicMessage, DescriptorPool, Value};
use serde_json::de::Deserializer;

let pool = DescriptorPool::decode(include_bytes!("file_descriptor_set.bin").as_ref()).unwrap();
let message_descriptor = pool.get_message_by_name("package.MyMessage").unwrap();

let json = r#"{ "foo": 150 }"#;
let mut deserializer = Deserializer::from_str(json);
let dynamic_message = DynamicMessage::deserialize(message_descriptor, &mut deserializer).unwrap();

assert_eq!(dynamic_message.get_field_by_name("foo").unwrap().as_ref(), &Value::I32(150));

Example - implementing ReflectMessage

The ReflectMessage trait provides a .descriptor() method to get type information for a message. By default it is just implemented for DynamicMessage.

When the reflect-well-known-types feature is enabled, it is implemented for the well-known-types provided by prost-types.

When the derive feature is enabled, it can be derived for Message implementations. The derive macro takes the following parameters:

Name Value
descriptor_pool An expression that resolves to a DescriptorPool containing the message type. The descriptor should be cached to avoid re-building it.
message_name The full name of the message, used to look it up within DescriptorPool.
use prost::Message;
use prost_reflect::{DescriptorPool, ReflectMessage};
use once_cell::sync::Lazy;

static DESCRIPTOR_POOL: Lazy<DescriptorPool>
    = Lazy::new(|| DescriptorPool::decode(include_bytes!("file_descriptor_set.bin").as_ref()).unwrap());

#[derive(Message, ReflectMessage)]
#[prost_reflect(descriptor_pool = "DESCRIPTOR_POOL", message_name = "package.MyMessage")]
pub struct MyMessage {}

let message = MyMessage {};
assert_eq!(message.descriptor().full_name(), "package.MyMessage");

If you are using prost-build, the prost-reflect-build crate provides helpers to generate ReflectMessage implementations:

    .compile_protos(&["src/package.proto"], &["src"])

Minimum Supported Rust Version

Rust 1.56 or higher.

The minimum supported Rust version may be changed in the future, but it will be done with a minor version bump.


Licensed under either of

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.


~15K SLoC