deser_msgpack/lib.rs
1//! Parse and serialize [MessagePack](https://msgpack.org/) compatible with
2//! deser.
3//!
4//! ```rust
5//! let bytes = deser_msgpack::to_vec(&vec![1u64, 2, 3, 4]).unwrap();
6//! assert_eq!(bytes, [0x94, 0x01, 0x02, 0x03, 0x04]);
7//! let vec: Vec<u64> = deser_msgpack::from_slice(&bytes).unwrap();
8//! assert_eq!(vec, [1, 2, 3, 4]);
9//! ```
10//!
11//! # Data Model
12//!
13//! The deser data model maps onto MessagePack as follows:
14//!
15//! | deser | MessagePack |
16//! |-------------------------|-----------------------------------------------|
17//! | `Null` | nil |
18//! | `Bool` | false / true |
19//! | `U64`, `I64` | integers |
20//! | `u128`, `i128` | integers (if they fit into 64 bits) |
21//! | [`Timestamp`] | the timestamp extension (type `-1`) |
22//! | `F32` | float 32 |
23//! | `F64` | float 64 |
24//! | `Str`, `Char` | str |
25//! | `Bytes` | bin |
26//! | maps and sequences | maps and arrays |
27//! | [`Ext`] | other extensions |
28//!
29//! Integers and lengths are written in their shortest form. Floats keep
30//! their precision. Other extension values (such as
31//! [`Datetime`](deser_core::ext::Datetime) or [`Uuid`](deser_core::ext::Uuid))
32//! are written as their fallback, which usually is a string. Integers
33//! which do not fit into 64 bits cannot be written. With
34//! [`SerializerConfig::canonical`] map entries are sorted to produce a
35//! deterministic encoding.
36//!
37//! Deserialization accepts all well-formed MessagePack. Map keys can be of
38//! any type. Signed integers that are not negative are passed on as
39//! `U64`, negative integers as `I64`. Extensions are passed on as
40//! extension atoms: timestamps as [`Timestamp`], all others as [`Ext`] whose
41//! fallback is the binary data. Strings are validated as UTF-8.
42//!
43//! [`Timestamp`]: deser_core::ext::Timestamp
44//!
45//! # Features
46//!
47//! * `io` (enabled by default): reading and writing streams of the
48//! standard library, see [streams](#streams). Requires `std`.
49//! * `speedups`: validates UTF-8 with [`simdutf8`](https://docs.rs/simdutf8).
50//! * `std` (enabled by default): uses the standard library. Without it
51//! this crate only needs `alloc` (see [`no_std`](https://docs.rs/deser/latest/deser/#no_std)).
52//!
53//! # Streams
54//!
55//! Items are read from a [`Read`](std::io::Read) with [`from_reader`] and
56//! written to a [`Write`](std::io::Write) with [`to_writer`]. To read or
57//! write items that follow each other (for instance on a socket) the
58//! configurations create readers and writers of
59//! [`deser::io`](deser_core::io) ([`DeserializerConfig::reader`] and
60//! [`SerializerConfig::writer`]). The reader only buffers until an item is
61//! complete:
62//!
63//! ```rust
64//! # #[cfg(feature = "io")] {
65//! use deser_msgpack::{DeserializerConfig, SerializerConfig};
66//!
67//! let mut writer = SerializerConfig::new().writer(Vec::new());
68//! writer.write(&vec![1u32, 2]).unwrap();
69//! writer.write(&"three").unwrap();
70//! let bytes = writer.into_inner();
71//!
72//! let mut reader = DeserializerConfig::new().reader(&bytes[..]);
73//! assert_eq!(reader.read::<Vec<u32>>().unwrap(), Some(vec![1, 2]));
74//! assert_eq!(reader.read::<String>().unwrap().as_deref(), Some("three"));
75//! assert_eq!(reader.read::<String>().unwrap(), None);
76//! # }
77//! ```
78//!
79//! The stream serializer ([`Serializer`]) and the stream deserializer
80//! ([`StreamDeserializer`]) do not do IO themselves (see
81//! [`deser::stream`](deser_core::stream)), they also work with other kinds
82//! of IO (for instance async runtimes with `deser-tokio`) and without the
83//! standard library.
84#![doc(html_logo_url = "https://raw.githubusercontent.com/mitsuhiko/deser/main/artwork/logo.svg")]
85#![cfg_attr(not(any(feature = "std", test)), no_std)]
86
87extern crate alloc;
88
89mod de;
90mod ext;
91mod head;
92mod parser;
93mod ser;
94mod stream;
95
96pub use self::de::{Deserializer, DeserializerConfig, Iter, from_slice};
97pub use self::ext::Ext;
98#[cfg(feature = "io")]
99pub use self::ser::to_writer;
100pub use self::ser::{Serializer, SerializerConfig, to_vec};
101pub use self::stream::StreamDeserializer;
102#[cfg(feature = "io")]
103pub use self::stream::from_reader;