#![doc(html_logo_url = "https://duper.dev.br/logos/duper-100-100.png")] //! # Serde Duper //! //! Duper is a format which aims to be a human-friendly extension of JSON, with //! quality-of-life improvements, extra types, and semantic identifiers. //! //! ```duper //! Product({ //! product_id: Uuid("1dd7b7aa-515e-405f-85a9-8ac812242609"), //! name: "Wireless Bluetooth Headphones", //! brand: "AudioTech", //! price: Decimal("129.99"), //! dimensions: (18.5, 15.2, 7.8), // In centimeters //! weight: Kilograms(0.285), //! in_stock: true, //! specifications: { //! battery_life: Duration("30h"), //! noise_cancellation: true, //! connectivity: ["Bluetooth 5.0", "3.5mm Jack"], //! }, //! image_thumbnail: Png(b64"iVBORw0KGgoAAAANSUhEUgAAAGQ="), //! tags: ["electronics", "audio", "wireless"], //! release_date: Date("2023-11-15"), //! /* Warranty is optional */ //! warranty_period: null, //! customer_ratings: { //! latest_review: r#"Absolutely ""astounding""!! 😎"#, //! average: 4.5, //! count: 127, //! }, //! created_at: Instant('2023-11-17T21:50:43+00:00'), //! }) //! ``` //! //! This crate allows you to convert between Duper's text representation and //! Rust's native data types, thanks to the `serde` framework. //! //! Serde provides a powerful way of mapping Duper data to and from Rust data //! structures largely automatically. //! //! ``` //! use serde::{Deserialize, Serialize}; //! use serde_duper::Result; //! //! #[derive(Serialize, Deserialize)] //! struct Person { //! name: String, //! age: u8, //! phones: Vec, //! } //! //! fn deserialize() -> Result { //! // Some Duper input data as a &str. Maybe this comes from the user. //! let data = r#" //! Person({ //! name: "John Doe", //! age: 43, //! phones: [ //! ResidentialPhone("+44 1234567"), //! CellPhone("+44 2345678"), //! ], //! })"#; //! //! // Parse the string of data into a Person object. //! let p: Person = serde_duper::from_string(data)?; //! //! // Do things just like with any other Rust data structure. //! println!("Please call {} at the number {}", p.name, p.phones[0]); //! //! Ok(p) //! } //! //! fn serialize(p: Person) -> Result<()> { //! // Serialize the person back into a Duper string. //! let d = serde_duper::to_string(&p)?; //! //! // Print, write to a file, or send to an HTTP server. //! println!("{}", d); //! //! Ok(()) //! } //! //! fn main() { //! let p = deserialize().unwrap(); //! serialize(p).unwrap(); //! } //! ``` //! //! Any type that implements Serde's `Deserialize` trait can be deserialized //! into a struct like this. This includes built-in Rust standard library type //! like `Vec` and `HashMap`, as well as any structs or enums annotated //! with `#[derive(Deserialize)]` in the Rust ecosystem. //! //! Conversely, any type that implements Serde's `Serialize` trait can be //! serialized into a string like this. This includes built-in Rust standard //! library types like `Vec` and `HashMap`, as well as any structs or //! enums annotated with `#[derive(Serialize)]` in the Rust ecosystem. //! //! # Handling bytes //! //! This crate re-exports [`serde_bytes`] wrapper types via the [`bytes`] //! module. Prefer using those over the following types if you'd like better //! byte support: //! //! - [`serde_bytes::ByteBuf`] over [`Vec`] //! - [`serde_bytes::ByteArray`] over `[u8; N]` //! - [`serde_bytes::Bytes`] over `[u8]` //! //! # Support for Temporal values //! //! For generic Temporal value support, use [`TemporalString`] as a field in //! your structs/enums. This will bring support for both serialization with the //! appropriate identifier, and deserialization with value parsing. //! //! ``` //! use serde::{Deserialize, Serialize}; //! use serde_duper::TemporalString; //! //! #[derive(Serialize, Deserialize)] //! struct SomeData<'a> { //! temporal: TemporalString<'a>, //! } //! ``` //! //! For [`jiff`] support, use the types provided by [`types::jiff`] with the //! `#[serde(with = "...")]` attribute. For example: //! //! ``` //! use jiff::{Zoned, Timestamp, Span, civil::{Date, Time, DateTime}}; //! use serde::{Deserialize, Serialize}; //! use serde_duper::types::jiff::{ //! DuperZoned, DuperTimestamp, DuperSpan, DuperOptionDate, DuperOptionTime, DuperOptionDateTime //! }; //! //! #[derive(Serialize, Deserialize)] //! struct JiffStuff { //! #[serde(with = "DuperZoned")] //! zoned: Zoned, //! #[serde(with = "DuperTimestamp")] //! ts: Timestamp, //! #[serde(with = "DuperSpan")] //! span: Span, //! #[serde(with = "DuperOptionDate")] //! date: Option, //! #[serde(with = "DuperOptionTime")] //! time: Option