| 1 |
//! # Serde |
| 2 |
//! |
| 3 |
//! Serde is a framework for ***ser***ializing and ***de***serializing Rust data |
| 4 |
//! structures efficiently and generically. |
| 5 |
//! |
| 6 |
//! The Serde ecosystem consists of data structures that know how to serialize |
| 7 |
//! and deserialize themselves along with data formats that know how to |
| 8 |
//! serialize and deserialize other things. Serde provides the layer by which |
| 9 |
//! these two groups interact with each other, allowing any supported data |
| 10 |
//! structure to be serialized and deserialized using any supported data format. |
| 11 |
//! |
| 12 |
//! See the Serde website <https://serde.rs> for additional documentation and |
| 13 |
//! usage examples. |
| 14 |
//! |
| 15 |
//! ## Design |
| 16 |
//! |
| 17 |
//! Where many other languages rely on runtime reflection for serializing data, |
| 18 |
//! Serde is instead built on Rust's powerful trait system. A data structure |
| 19 |
//! that knows how to serialize and deserialize itself is one that implements |
| 20 |
//! Serde's `Serialize` and `Deserialize` traits (or uses Serde's derive |
| 21 |
//! attribute to automatically generate implementations at compile time). This |
| 22 |
//! avoids any overhead of reflection or runtime type information. In fact in |
| 23 |
//! many situations the interaction between data structure and data format can |
| 24 |
//! be completely optimized away by the Rust compiler, leaving Serde |
| 25 |
//! serialization to perform the same speed as a handwritten serializer for the |
| 26 |
//! specific selection of data structure and data format. |
| 27 |
//! |
| 28 |
//! ## Data formats |
| 29 |
//! |
| 30 |
//! The following is a partial list of data formats that have been implemented |
| 31 |
//! for Serde by the community. |
| 32 |
//! |
| 33 |
//! - [JSON], the ubiquitous JavaScript Object Notation used by many HTTP APIs. |
| 34 |
//! - [Postcard], a no\_std and embedded-systems friendly compact binary format. |
| 35 |
//! - [CBOR], a Concise Binary Object Representation designed for small message |
| 36 |
//! size without the need for version negotiation. |
| 37 |
//! - [YAML], a self-proclaimed human-friendly configuration language that ain't |
| 38 |
//! markup language. |
| 39 |
//! - [MessagePack], an efficient binary format that resembles a compact JSON. |
| 40 |
//! - [TOML], a minimal configuration format used by [Cargo]. |
| 41 |
//! - [Pickle], a format common in the Python world. |
| 42 |
//! - [RON], a Rusty Object Notation. |
| 43 |
//! - [BSON], the data storage and network transfer format used by MongoDB. |
| 44 |
//! - [Avro], a binary format used within Apache Hadoop, with support for schema |
| 45 |
//! definition. |
| 46 |
//! - [JSON5], a superset of JSON including some productions from ES5. |
| 47 |
//! - [URL] query strings, in the x-www-form-urlencoded format. |
| 48 |
//! - [Starlark], the format used for describing build targets by the Bazel and |
| 49 |
//! Buck build systems. *(serialization only)* |
| 50 |
//! - [Envy], a way to deserialize environment variables into Rust structs. |
| 51 |
//! *(deserialization only)* |
| 52 |
//! - [Envy Store], a way to deserialize [AWS Parameter Store] parameters into |
| 53 |
//! Rust structs. *(deserialization only)* |
| 54 |
//! - [S-expressions], the textual representation of code and data used by the |
| 55 |
//! Lisp language family. |
| 56 |
//! - [D-Bus]'s binary wire format. |
| 57 |
//! - [FlexBuffers], the schemaless cousin of Google's FlatBuffers zero-copy |
| 58 |
//! serialization format. |
| 59 |
//! - [Bencode], a simple binary format used in the BitTorrent protocol. |
| 60 |
//! - [Token streams], for processing Rust procedural macro input. |
| 61 |
//! *(deserialization only)* |
| 62 |
//! - [DynamoDB Items], the format used by [rusoto_dynamodb] to transfer data to |
| 63 |
//! and from DynamoDB. |
| 64 |
//! - [Hjson], a syntax extension to JSON designed around human reading and |
| 65 |
//! editing. *(deserialization only)* |
| 66 |
//! - [CSV], Comma-separated values is a tabular text file format. |
| 67 |
//! |
| 68 |
//! [JSON]: https://github.com/serde-rs/json |
| 69 |
//! [Postcard]: https://github.com/jamesmunns/postcard |
| 70 |
//! [CBOR]: https://github.com/enarx/ciborium |
| 71 |
//! [YAML]: https://github.com/dtolnay/serde-yaml |
| 72 |
//! [MessagePack]: https://github.com/3Hren/msgpack-rust |
| 73 |
//! [TOML]: https://docs.rs/toml |
| 74 |
//! [Pickle]: https://github.com/birkenfeld/serde-pickle |
| 75 |
//! [RON]: https://github.com/ron-rs/ron |
| 76 |
//! [BSON]: https://github.com/mongodb/bson-rust |
| 77 |
//! [Avro]: https://docs.rs/apache-avro |
| 78 |
//! [JSON5]: https://github.com/callum-oakley/json5-rs |
| 79 |
//! [URL]: https://docs.rs/serde_qs |
| 80 |
//! [Starlark]: https://github.com/dtolnay/serde-starlark |
| 81 |
//! [Envy]: https://github.com/softprops/envy |
| 82 |
//! [Envy Store]: https://github.com/softprops/envy-store |
| 83 |
//! [Cargo]: https://doc.rust-lang.org/cargo/reference/manifest.html |
| 84 |
//! [AWS Parameter Store]: https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-parameter-store.html |
| 85 |
//! [S-expressions]: https://github.com/rotty/lexpr-rs |
| 86 |
//! [D-Bus]: https://docs.rs/zvariant |
| 87 |
//! [FlexBuffers]: https://github.com/google/flatbuffers/tree/master/rust/flexbuffers |
| 88 |
//! [Bencode]: https://github.com/P3KI/bendy |
| 89 |
//! [Token streams]: https://github.com/oxidecomputer/serde_tokenstream |
| 90 |
//! [DynamoDB Items]: https://docs.rs/serde_dynamo |
| 91 |
//! [rusoto_dynamodb]: https://docs.rs/rusoto_dynamodb |
| 92 |
//! [Hjson]: https://github.com/Canop/deser-hjson |
| 93 |
//! [CSV]: https://docs.rs/csv |
| 94 |
|
| 95 |
//////////////////////////////////////////////////////////////////////////////// |
| 96 |
|
| 97 |
// Serde types in rustdoc of other crates get linked to here. |
| 98 |
#![doc(html_root_url = "https://docs.rs/serde/1.0.229")] |
| 99 |
// Support using Serde without the standard library! |
| 100 |
#![cfg_attr(not(feature = "std"), no_std)] |
| 101 |
// Show which crate feature enables conditionally compiled APIs in documentation. |
| 102 |
#![cfg_attr(docsrs, feature(doc_cfg, rustdoc_internals))] |
| 103 |
#![cfg_attr(docsrs, allow(internal_features))] |
| 104 |
// Unstable functionality only if the user asks for it. For tracking and |
| 105 |
// discussion of these features please refer to this issue: |
| 106 |
// |
| 107 |
// https://github.com/serde-rs/serde/issues/812 |
| 108 |
#![cfg_attr(all(feature = "unstable", docsrs), feature(never_type))] |
| 109 |
#![allow( |
| 110 |
unknown_lints, |
| 111 |
bare_trait_objects, |
| 112 |
deprecated, |
| 113 |
mismatched_lifetime_syntaxes |
| 114 |
)] |
| 115 |
// Ignored clippy and clippy_pedantic lints |
| 116 |
#![allow( |
| 117 |
// clippy bug: https://github.com/rust-lang/rust-clippy/issues/5704 |
| 118 |
clippy::unnested_or_patterns, |
| 119 |
// clippy bug: https://github.com/rust-lang/rust-clippy/issues/7768 |
| 120 |
clippy::semicolon_if_nothing_returned, |
| 121 |
// not available in our oldest supported compiler |
| 122 |
clippy::empty_enums, |
| 123 |
clippy::type_repetition_in_bounds, // https://github.com/rust-lang/rust-clippy/issues/8772 |
| 124 |
// integer and float ser/de requires these sorts of casts |
| 125 |
clippy::cast_possible_truncation, |
| 126 |
clippy::cast_possible_wrap, |
| 127 |
clippy::cast_precision_loss, |
| 128 |
clippy::cast_sign_loss, |
| 129 |
// things are often more readable this way |
| 130 |
clippy::cast_lossless, |
| 131 |
clippy::module_name_repetitions, |
| 132 |
clippy::single_match_else, |
| 133 |
clippy::type_complexity, |
| 134 |
clippy::use_self, |
| 135 |
clippy::zero_prefixed_literal, |
| 136 |
// correctly used |
| 137 |
clippy::derive_partial_eq_without_eq, |
| 138 |
clippy::enum_glob_use, |
| 139 |
clippy::explicit_auto_deref, |
| 140 |
clippy::incompatible_msrv, |
| 141 |
clippy::let_underscore_untyped, |
| 142 |
clippy::map_err_ignore, |
| 143 |
clippy::new_without_default, |
| 144 |
clippy::result_unit_err, |
| 145 |
clippy::wildcard_imports, |
| 146 |
// not practical |
| 147 |
clippy::needless_pass_by_value, |
| 148 |
clippy::similar_names, |
| 149 |
clippy::too_many_lines, |
| 150 |
// preference |
| 151 |
clippy::doc_markdown, |
| 152 |
clippy::elidable_lifetime_names, |
| 153 |
clippy::needless_lifetimes, |
| 154 |
clippy::unseparated_literal_suffix, |
| 155 |
// false positive |
| 156 |
clippy::needless_doctest_main, |
| 157 |
// noisy |
| 158 |
clippy::missing_errors_doc, |
| 159 |
clippy::must_use_candidate, |
| 160 |
)] |
| 161 |
// Restrictions |
| 162 |
#![deny(clippy::question_mark_used)] |
| 163 |
// Rustc lints. |
| 164 |
#![deny(missing_docs, unused_imports)] |
| 165 |
|
| 166 |
//////////////////////////////////////////////////////////////////////////////// |
| 167 |
|
| 168 |
#[cfg(feature = "alloc")] |
| 169 |
extern crate alloc; |
| 170 |
|
| 171 |
// Rustdoc has a lot of shortcomings related to cross-crate re-exports that make |
| 172 |
// the rendered documentation of serde_core traits in serde more challenging to |
| 173 |
// understand than the equivalent documentation of the same items in serde_core. |
| 174 |
// https://github.com/rust-lang/rust/labels/A-cross-crate-reexports |
| 175 |
// So, just for the purpose of docs.rs documentation, we inline the contents of |
| 176 |
// serde_core into serde. This sidesteps all the cross-crate rustdoc bugs. |
| 177 |
#[cfg(docsrs)] |
| 178 |
#[macro_use] |
| 179 |
#[path = "core/crate_root.rs"] |
| 180 |
mod crate_root; |
| 181 |
|
| 182 |
#[cfg(docsrs)] |
| 183 |
#[macro_use] |
| 184 |
#[path = "core/macros.rs"] |
| 185 |
mod macros; |
| 186 |
|
| 187 |
#[cfg(not(docsrs))] |
| 188 |
macro_rules! crate_root { |
| 189 |
() => { |
| 190 |
/// A facade around all the types we need from the `std`, `core`, and `alloc` |
| 191 |
/// crates. This avoids elaborate import wrangling having to happen in every |
| 192 |
/// module. |
| 193 |
mod lib { |
| 194 |
mod core { |
| 195 |
#[cfg(not(feature = "std"))] |
| 196 |
pub use core::*; |
| 197 |
#[cfg(feature = "std")] |
| 198 |
pub use std::*; |
| 199 |
} |
| 200 |
|
| 201 |
pub use self::core::{f32, f64}; |
| 202 |
pub use self::core::{ptr, str}; |
| 203 |
|
| 204 |
#[cfg(any(feature = "std", feature = "alloc"))] |
| 205 |
pub use self::core::slice; |
| 206 |
|
| 207 |
pub use self::core::clone; |
| 208 |
pub use self::core::convert; |
| 209 |
pub use self::core::default; |
| 210 |
pub use self::core::fmt::{self, Debug, Display, Write as FmtWrite}; |
| 211 |
pub use self::core::marker::{self, PhantomData}; |
| 212 |
pub use self::core::option; |
| 213 |
pub use self::core::result; |
| 214 |
|
| 215 |
#[cfg(all(feature = "alloc", not(feature = "std")))] |
| 216 |
pub use alloc::borrow::{Cow, ToOwned}; |
| 217 |
#[cfg(feature = "std")] |
| 218 |
pub use std::borrow::{Cow, ToOwned}; |
| 219 |
|
| 220 |
#[cfg(all(feature = "alloc", not(feature = "std")))] |
| 221 |
pub use alloc::string::{String, ToString}; |
| 222 |
#[cfg(feature = "std")] |
| 223 |
pub use std::string::{String, ToString}; |
| 224 |
|
| 225 |
#[cfg(all(feature = "alloc", not(feature = "std")))] |
| 226 |
pub use alloc::vec::Vec; |
| 227 |
#[cfg(feature = "std")] |
| 228 |
pub use std::vec::Vec; |
| 229 |
|
| 230 |
#[cfg(all(feature = "alloc", not(feature = "std")))] |
| 231 |
pub use alloc::boxed::Box; |
| 232 |
#[cfg(feature = "std")] |
| 233 |
pub use std::boxed::Box; |
| 234 |
} |
| 235 |
|
| 236 |
// None of this crate's error handling needs the `From::from` error conversion |
| 237 |
// performed implicitly by the `?` operator or the standard library's `try!` |
| 238 |
// macro. This simplified macro gives a 5.5% improvement in compile time |
| 239 |
// compared to standard `try!`, and 9% improvement compared to `?`. |
| 240 |
#[cfg(not(no_serde_derive))] |
| 241 |
macro_rules! tri { |
| 242 |
($expr:expr) => { |
| 243 |
match $expr { |
| 244 |
Ok(val) => val, |
| 245 |
Err(err) => return Err(err), |
| 246 |
} |
| 247 |
}; |
| 248 |
} |
| 249 |
|
| 250 |
//////////////////////////////////////////////////////////////////////////////// |
| 251 |
|
| 252 |
pub use serde_core::{ |
| 253 |
de, forward_to_deserialize_any, ser, Deserialize, Deserializer, Serialize, Serializer, |
| 254 |
}; |
| 255 |
|
| 256 |
// Used by generated code and doc tests. Not public API. |
| 257 |
#[doc(hidden)] |
| 258 |
mod private; |
| 259 |
|
| 260 |
include!(concat!(env!("OUT_DIR"), "/private.rs")); |
| 261 |
}; |
| 262 |
} |
| 263 |
|
| 264 |
crate_root!(); |
| 265 |
|
| 266 |
mod integer128; |
| 267 |
|
| 268 |
// Re-export #[derive(Serialize, Deserialize)]. |
| 269 |
// |
| 270 |
// The reason re-exporting is not enabled by default is that disabling it would |
| 271 |
// be annoying for crates that provide handwritten impls or data formats. They |
| 272 |
// would need to disable default features and then explicitly re-enable std. |
| 273 |
#[cfg(feature = "serde_derive")] |
| 274 |
extern crate serde_derive; |
| 275 |
|
| 276 |
/// Derive macro available if serde is built with `features = ["derive"]`. |
| 277 |
#[cfg(feature = "serde_derive")] |
| 278 |
#[cfg_attr(docsrs, doc(cfg(feature = "derive")))] |
| 279 |
pub use serde_derive::{Deserialize, Serialize}; |
| 280 |
|
| 281 |
#[macro_export] |
| 282 |
#[doc(hidden)] |
| 283 |
macro_rules! __require_serde_not_serde_core { |
| 284 |
() => {}; |
| 285 |
} |