msgpack-schema is a schema language for describing data formats encoded in MessagePack.
It provides two derive macros Serialize
and Deserialize
that allow you to transcode MessagePack binary data to/from Rust data structures in a type-directed way.
```rust use msgpack_schema::{Deserialize, Serialize};
struct Human {
#[tag = 0]
name: String,
#[tag = 2]
#[optional]
age: Option
Option<T>
is roughly equal to declaring T | null
in TypeScript. Deserializer interprets null
as None
whatever T
is. So Option<Option<T>>
is the same as Option<T>
(unless used together with #[optional]
.)Structs with named fields will be serialized into a MsgPack map object whose keys are fixints specified by #[tag]
attributes.
schema | Rust | MessagePack |
---|---|---|
```rust struct S { #[tag = 0] foo: u32, #[tag = 1] bar: String, } ``` | ```rust S { foo: 42, bar: "hello".to_owned() } ``` | ```js { 0: 42, 1: "hello" } ``` |
Fields in named structs may be tagged with #[optional]
.
Option<T>
.None
.None
when the given MsgPack map object contains no corresponding key-value pair.Tuple structs with only one element are treated transparently.
schema | Rust | MessagePack |
---|---|---|
```rust struct S(u32) ``` | ```rust S(42) ``` | ```js 42 ``` |
Serialization and deserialization of unit structs and empty tuple structs are currently unsupported.
schema | Rust | MessagePack |
---|---|---|
```rust struct S ``` | ```rust S ``` | UNSUPPORTED |
```rust struct S() ``` | ```rust S() ``` | UNSUPPORTED |
Unit variants and empty tuple variants are serialized into a single fixint whose value is determined by the tag.
schema | Rust | MessagePack |
---|---|---|
```rust enum E { #[tag = 3] Foo } ``` | ```rust E::Foo ``` | ```js 3 ``` |
```rust enum E { #[tag = 3] Foo() } ``` | ```rust E::Foo() ``` | ```js 3 ``` |
Newtype variants (one-element tuple variants) are serialized into an array of the tag and the inner value.
schema | Rust | MessagePack |
---|---|---|
```rust enum E { #[tag = 3] Foo(u32) } ``` | ```rust E::Foo(42) ``` | ```js [ 3, 42 ] ``` |
Enums may be attached #[untagged]
when all variants are newtype variants.
Serializing untagged variants results in the same data layout as the inner type.
The deserializer deserializes into an untagged enum type by trying deserization one by one from the first variant to the last.
schema | Rust | MessagePack |
---|---|---|
```rust #[derive(Serialize, Deserialize)] #[untagged] enum Animal { Foo(String), Bar(u32), } ``` | ```rust E::Bar(42) ``` | ```js 42 ``` |
msgpack
macro#[required]
#[tag]
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 Serde by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.