json5/lib.rs
1//! [JSON5][] is a superset of [JSON][] with an expanded syntax including some productions from
2//! [ECMAScript 5.1][]. It aims to be easier to write and maintain by hand (e.g. for config files).
3//! It is not intended to be used for machine-to-machine communication, for which you'd be better
4//! served by [serde-rs/json][].
5//!
6//! In particular, JSON5 allows comments, trailing commas, object keys without quotes, single quoted
7//! strings, hexadecimal numbers, multi-line strings...
8//!
9//! ```json5
10//! {
11//! // comments
12//! unquoted: 'and you can quote me on that',
13//! singleQuotes: 'I can use "double quotes" here',
14//! lineBreaks: "Look, Mom! \
15//! No \\n's!",
16//! hexadecimal: 0xdecaf,
17//! leadingDecimalPoint: .8675309, andTrailing: 8675309.,
18//! positiveSign: +1,
19//! trailingComma: 'in objects', andIn: ['arrays',],
20//! "backwardsCompatible": "with JSON",
21//! }
22//! ```
23//!
24//! This crate provides functions for deserializing JSON5 text into a Rust datatype and for
25//! serializing a Rust datatype as JSON5 text, both via the [Serde framework][].
26//!
27//! # Deserialization
28//!
29//! Implementing [`serde::Deserialize`] on your type will allow you to parse JSON5 text into a value
30//! of that type with [`from_str`].
31//!
32//! ```
33//! use serde_derive::Deserialize;
34//!
35//! #[derive(Debug, PartialEq, Deserialize)]
36//! struct Config<'a> {
37//! foo: u32,
38//! bar: &'a str,
39//! }
40//!
41//! let config: Config = json5::from_str("
42//! {
43//! // Note unquoted keys, comments, and trailing commas.
44//! foo: 42,
45//! bar: 'baz',
46//! }
47//! ")?;
48//!
49//! assert_eq!(config, Config{ foo: 42, bar: "baz" });
50//! # Ok::<(), json5::Error>(())
51//! ```
52//!
53//! There are many ways to customize the deserialization (e.g. deserializing `camelCase` field names
54//! into a struct with `snake_case` fields). See the Serde docs, especially the [Attributes][],
55//! [Custom serialization][], and [Examples][] sections.
56//!
57//! # Serialization
58//!
59//! Similarly, implementing [`serde::Serialize`] on a Rust type allows you to produce a JSON5
60//! serialization of values of that type with [`to_string`] or [`to_writer`]. The serializer will
61//! omit quotes around object keys where possible and will indent nested objects and arrays, but is
62//! otherwise fairly basic.
63//!
64//! ```
65//! use serde_derive::Serialize;
66//!
67//! #[derive(Serialize)]
68//! struct Config<'a> {
69//! foo: u32,
70//! bar: &'a str,
71//! }
72//!
73//! let config = Config {
74//! foo: 42,
75//! bar: "baz",
76//! };
77//!
78//! assert_eq!(&json5::to_string(&config)?, "{
79//! foo: 42,
80//! bar: \"baz\",
81//! }");
82//! # Ok::<(), json5::Error>(())
83//! ```
84//!
85//! There are many ways to customize the serialization (e.g. serializing `snake_case` struct fields
86//! as `camelCase`). See the Serde docs, especially the [Attributes][], [Custom serialization][] and
87//! [Examples][] sections.
88//!
89//! # Byte arrays
90//!
91//! All the types of the [Serde data model][] are supported. Byte arrays are encoded as hex strings.
92//! e.g.
93//!
94//! ```
95//! use serde_bytes::{Bytes, ByteBuf};
96//!
97//! let s = json5::to_string(&Bytes::new(b"JSON5"))?;
98//! assert_eq!(&s, "\"4a534f4e35\"");
99//! assert_eq!(json5::from_str::<ByteBuf>(&s)?, ByteBuf::from("JSON5"));
100//! # Ok::<(), json5::Error>(())
101//! ```
102//!
103//! [Attributes]: https://serde.rs/attributes.html
104//! [Custom serialization]: https://serde.rs/custom-serialization.html
105//! [ECMAScript 5.1]: https://www.ecma-international.org/ecma-262/5.1/
106//! [Examples]: https://serde.rs/examples.html
107//! [JSON]: https://www.json.org/json-en.html
108//! [JSON5]: https://json5.org/
109//! [Serde data model]: https://serde.rs/data-model.html#types
110//! [Serde framework]: https://serde.rs/
111//! [serde-rs/json]: https://github.com/serde-rs/json
112
113#![warn(clippy::pedantic)]
114
115#[macro_use]
116mod de;
117pub mod char;
118mod error;
119mod ser;
120
121#[allow(clippy::all, clippy::pedantic, dead_code)]
122mod unicode;
123
124pub use de::{Deserializer, from_str};
125pub use error::{Error, ErrorCode, Position};
126pub use ser::{Serializer, to_string, to_writer};