Crate prost_reflect
source ·Expand description
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:
DescriptorPool
wraps aFileDescriptorSet
output by the protobuf compiler to provide an API for inspecting type definitions.DynamicMessage
provides encoding, decoding and reflection of an arbitrary protobuf message definition described by aMessageDescriptor
.
§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();
deserializer.end().unwrap();
assert_eq!(dynamic_message.get_field_by_name("foo").unwrap().as_ref(), &Value::I32(150));
Re-exports§
pub use prost;
pub use prost::bytes;
pub use prost_types;
Structs§
- An error that may occur while creating a
DescriptorPool
. - A
DescriptorPool
is a collection of related descriptors. Typically it will be created from aFileDescriptorSet
output by the protobuf compiler (seeDescriptorPool::from_file_descriptor_set
) but it may also be built up by adding files individually. - Options to control deserialization of messages.
DynamicMessage
provides encoding, decoding and reflection of a protobuf message.- A protobuf enum type.
- A value in a protobuf enum type.
- A protobuf extension field definition.
- A protobuf message definition.
- A single source file containing protobuf messages and services.
- A protobuf message definition.
- A method definition for a
ServiceDescriptor
. - A oneof field in a protobuf message.
- Options to control serialization of messages.
- A protobuf service definition.
- An unknown field found when deserializing a protobuf message.
Enums§
- Cardinality determines whether a field is optional, required, or repeated.
- The type of a protobuf message field.
- A dynamically-typed key for a protobuf map.
- Error type returned by
DynamicMessage::try_set_field()
. - The syntax of a proto file.
- A dynamically-typed protobuf value.
Traits§
- Trait for message types that support reflection.