serde_json5/
lib.rs

1//! JSON5 is a superset of [JSON][] with an expanded syntax including some productions from
2//! [ECMAScript 5.1][].
3//!
4//! In particular, JSON5 allows comments, trailing commas, object keys without quotes, single
5//! quoted strings and more. See the [JSON5 project page][] for full details.
6//!
7//! ```json5,ignore
8//! {
9//!   // comments
10//!   unquoted: 'and you can quote me on that',
11//!   singleQuotes: 'I can use "double quotes" here',
12//!   lineBreaks: "Look, Mom! \
13//! No \\n's!",
14//!   hexadecimal: 0xdecaf,
15//!   leadingDecimalPoint: .8675309, andTrailing: 8675309.,
16//!   positiveSign: +1,
17//!   trailingComma: 'in objects', andIn: ['arrays',],
18//!   "backwardsCompatible": "with JSON",
19//! }
20//! ```
21//!
22//! This crate provides functions for deserializing JSON5 text into a Rust datatype and for
23//! serializing a Rust datatype as JSON5 text, both via the [Serde framework][].
24//!
25//! # Deserialization
26//!
27//! Implementing Serde’s [`Deserialize`][] trait on your type will allow you to parse JSON5
28//! text into a value of that type with [`from_str`][].
29//!
30//! ```rust
31//! use serde_derive::Deserialize;
32//!
33//! #[derive(Deserialize, Debug, PartialEq)]
34//! struct Config {
35//!     message: String,
36//!     n: i32,
37//! }
38//!
39//! let config = "
40//!     {
41//!       // A traditional message.
42//!       message: 'hello world',
43//!
44//!       // A number for some reason.
45//!       n: 42,
46//!     }
47//! ";
48//!
49//! assert_eq!(
50//!     serde_json5::from_str(config),
51//!     Ok(Config {
52//!         message: "hello world".to_string(),
53//!         n: 42,
54//!     }),
55//! );
56//! ```
57//!
58//! There are many ways to customise the deserialization (e.g. deserializing `camelCase` field
59//! names into a struct with `snake_case` fields). See the Serde docs, especially the
60//! [Attributes][], [Custom serialization][] and [Examples][] sections.
61//!
62//! # Serialization
63//!
64//! Similarly, implementing [`Serialize`][] on a Rust type allows you to produce a JSON5
65//! serialization of values of that type with [`to_string`][]. At present the serializer will just
66//! produce JSON (since it's a valid subset of JSON5), but future work will allow specifying the
67//! output style (single over double quotes, trailing commas, indentation etc.).
68//!
69//! ```rust
70//! use serde_derive::Serialize;
71//!
72//! #[derive(Serialize, PartialEq, Debug)]
73//! #[serde(untagged)]
74//! enum Val {
75//!     Number(f64),
76//!     Bool(bool),
77//!     String(String),
78//! }
79//!
80//! assert_eq!(
81//!     serde_json5::to_string(&vec![
82//!         Val::Number(42.),
83//!         Val::Bool(true),
84//!         Val::String("hello".to_owned()),
85//!     ]),
86//!     Ok("[42,true,\"hello\"]".to_owned()),
87//! )
88//! ```
89//!
90//! There are many ways to customise the serialization (e.g. serializing `snake_case` struct fields
91//! as `camelCase`). See the Serde docs, especially the [Attributes][], [Custom serialization][]
92//! and [Examples][] sections.
93//!
94//! # Limitations
95//!
96//! At the time of writing the following is unsupported:
97//!
98//! - deserializing into borrowed types (e.g. fields of type `&str`)
99//!
100//! - serializing or deserializing [byte arrays][]
101//!
102//! - specifying the style of JSON5 output from the serializer (single over double quotes, trailing
103//!   commas, indentation etc.)
104//!
105//! [JSON]: https://tools.ietf.org/html/rfc7159
106//! [ECMAScript 5.1]: https://www.ecma-international.org/ecma-262/5.1/
107//! [JSON5 project page]: https://json5.org/
108//! [Serde framework]: https://serde.rs/
109//! [`Deserialize`]: https://docs.serde.rs/serde/de/trait.Deserialize.html
110//! [`from_str`]: fn.from_str.html
111//! [Attributes]: https://serde.rs/attributes.html
112//! [Custom serialization]: https://serde.rs/custom-serialization.html
113//! [Examples]: https://serde.rs/examples.html
114//! [`Serialize`]: https://docs.serde.rs/serde/ser/trait.Serialize.html
115//! [`to_string`]: fn.to_string.html
116//! [byte arrays]: https://serde.rs/data-model.html#types
117
118#![warn(missing_docs)]
119#![warn(rust_2018_idioms)]
120
121mod de;
122mod error;
123mod ser;
124
125pub use crate::de::{from_reader, from_slice, from_str, Deserializer};
126pub use crate::error::{Error, Location, Result};
127pub use crate::ser::{to_string, to_writer};