musli_json/lib.rs
1//! [<img alt="github" src="https://img.shields.io/badge/github-udoprog/musli-8da0cb?style=for-the-badge&logo=github" height="20">](https://github.com/udoprog/musli)
2//! [<img alt="crates.io" src="https://img.shields.io/crates/v/musli-json.svg?style=for-the-badge&color=fc8d62&logo=rust" height="20">](https://crates.io/crates/musli-json)
3//! [<img alt="docs.rs" src="https://img.shields.io/badge/docs.rs-musli--json-66c2a5?style=for-the-badge&logoColor=white&logo=data:image/svg+xml;base64,PHN2ZyByb2xlPSJpbWciIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld0JveD0iMCAwIDUxMiA1MTIiPjxwYXRoIGZpbGw9IiNmNWY1ZjUiIGQ9Ik00ODguNiAyNTAuMkwzOTIgMjE0VjEwNS41YzAtMTUtOS4zLTI4LjQtMjMuNC0zMy43bC0xMDAtMzcuNWMtOC4xLTMuMS0xNy4xLTMuMS0yNS4zIDBsLTEwMCAzNy41Yy0xNC4xIDUuMy0yMy40IDE4LjctMjMuNCAzMy43VjIxNGwtOTYuNiAzNi4yQzkuMyAyNTUuNSAwIDI2OC45IDAgMjgzLjlWMzk0YzAgMTMuNiA3LjcgMjYuMSAxOS45IDMyLjJsMTAwIDUwYzEwLjEgNS4xIDIyLjEgNS4xIDMyLjIgMGwxMDMuOS01MiAxMDMuOSA1MmMxMC4xIDUuMSAyMi4xIDUuMSAzMi4yIDBsMTAwLTUwYzEyLjItNi4xIDE5LjktMTguNiAxOS45LTMyLjJWMjgzLjljMC0xNS05LjMtMjguNC0yMy40LTMzLjd6TTM1OCAyMTQuOGwtODUgMzEuOXYtNjguMmw4NS0zN3Y3My4zek0xNTQgMTA0LjFsMTAyLTM4LjIgMTAyIDM4LjJ2LjZsLTEwMiA0MS40LTEwMi00MS40di0uNnptODQgMjkxLjFsLTg1IDQyLjV2LTc5LjFsODUtMzguOHY3NS40em0wLTExMmwtMTAyIDQxLjQtMTAyLTQxLjR2LS42bDEwMi0zOC4yIDEwMiAzOC4ydi42em0yNDAgMTEybC04NSA0Mi41di03OS4xbDg1LTM4Ljh2NzUuNHptMC0xMTJsLTEwMiA0MS40LTEwMi00MS40di0uNmwxMDItMzguMiAxMDIgMzguMnYuNnoiPjwvcGF0aD48L3N2Zz4K" height="20">](https://docs.rs/musli-json)
4//!
5//! JSON support for [Müsli] suitable for network and usually browser
6//! communication.
7//!
8//! JSON encoding is fully upgrade stable:
9//!
10//! * ✔ Can tolerate missing fields if they are annotated with
11//! `#[musli(default)]`.
12//! * ✔ Can skip over unknown fields.
13//!
14//! ```
15//! use musli::{Encode, Decode};
16//!
17//! #[derive(Debug, PartialEq, Encode, Decode)]
18//! struct Version1 {
19//! name: String,
20//! }
21//!
22//! #[derive(Debug, PartialEq, Encode, Decode)]
23//! struct Version2 {
24//! name: String,
25//! #[musli(default)]
26//! age: Option<u32>,
27//! }
28//!
29//! let version2 = musli_json::to_vec(&Version2 {
30//! name: String::from("Aristotle"),
31//! age: Some(62),
32//! })?;
33//!
34//! let version1: Version1 = musli_json::from_slice(version2.as_slice())?;
35//!
36//! assert_eq!(version1, Version1 {
37//! name: String::from("Aristotle"),
38//! });
39//! # Ok::<_, musli_json::Error>(())
40//! ```
41//!
42//! [Müsli]: https://github.com/udoprog/musli
43
44#![deny(missing_docs)]
45#![no_std]
46#![cfg_attr(doc_cfg, feature(doc_cfg))]
47
48#[cfg(feature = "alloc")]
49extern crate alloc;
50
51#[cfg_attr(test, macro_use)]
52#[cfg(feature = "std")]
53extern crate std;
54
55mod de;
56mod en;
57pub mod encoding;
58mod error;
59mod parser;
60pub use self::parser::Parser;
61#[cfg(feature = "test")]
62#[cfg_attr(doc_cfg, doc(cfg(feature = "test")))]
63#[doc(hidden)]
64#[macro_use]
65pub mod test;
66
67/// Convenient result alias for use with `musli_json`.
68pub type Result<T, E = Error> = core::result::Result<T, E>;
69
70#[cfg(feature = "std")]
71#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
72#[doc(inline)]
73pub use self::encoding::to_writer;
74#[doc(inline)]
75pub use self::encoding::{decode, encode, from_slice, from_str, to_fixed_bytes, Encoding};
76#[cfg(feature = "alloc")]
77#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
78#[doc(inline)]
79pub use self::encoding::{to_string, to_vec};
80#[doc(inline)]
81pub use self::error::Error;
82
83musli_utils::simdutf8!();