Skip to main content

deser_jsonc/
lib.rs

1//! Parse JSONC (JSON with comments) compatible with deser.
2//!
3//! JSONC is JSON with `//` and `/* */` comments and commas after the last
4//! element of sequences and maps, as used by configuration files such as
5//! `tsconfig.json` or the settings of VS Code.  Otherwise this works like
6//! [`deser-json`](https://docs.rs/deser-json): strings without escape
7//! sequences are borrowed from the input and the positions of errors and
8//! values refer to the input.  In [JSON Lines](Trailing::Newline) only
9//! line breaks outside of comments end a value.
10//!
11//! ```rust
12//! #[derive(deser::Deserialize)]
13//! struct Config<'a> {
14//!     name: &'a str,
15//!     ports: Vec<u16>,
16//! }
17//!
18//! let config: Config = deser_jsonc::from_str(r#"{
19//!     // the name of the service
20//!     "name": "api",
21//!     /* the ports it listens on */
22//!     "ports": [80, 443,],
23//! }"#).unwrap();
24//! assert_eq!(config.name, "api");
25//! assert_eq!(config.ports, [80, 443]);
26//! ```
27//!
28//! JSON is valid JSONC, so values are serialized as JSON (with the same
29//! serializer as `deser-json`).
30//!
31//! # Raw Values
32//!
33//! [`RawJsonc`] holds the JSONC text of a value.  Like `deser_json::RawJson` it
34//! keeps the text of values that are deserialized from JSONC as it is
35//! (including comments), other values are encoded as JSON.  The serializer
36//! of this crate writes the text of raw JSONC values as it is.  See
37//! [`Raw`](deser_core::ext::Raw) for more information.
38//!
39//! # Features
40//!
41//! * `io` (enabled by default): reading and writing streams of the
42//!   standard library (with `DeserializerConfig::reader`).  Requires
43//!   `std`.
44//! * `speedups` (enabled by default): faster UTF-8 validation.
45//! * `std` (enabled by default): uses the standard library.  Without it
46//!   this crate only needs `alloc` (see [`no_std`](https://docs.rs/deser/latest/deser/#no_std)).
47#![doc(html_logo_url = "https://raw.githubusercontent.com/mitsuhiko/deser/main/artwork/logo.svg")]
48#![cfg_attr(not(any(feature = "std", test)), no_std)]
49
50extern crate alloc;
51
52// These are generated from `deser-template-json`.
53mod buf;
54mod copy;
55mod de;
56mod escape;
57mod parser;
58mod pretty;
59mod raw;
60mod scan;
61mod ser;
62mod stream;
63mod trailing;
64
65pub use self::de::{
66    Deserializer, DeserializerConfig, DeserializerConfigBuilder, Iter, from_slice, from_str,
67};
68pub use self::raw::{Jsonc, RawJsonc};
69#[cfg(feature = "io")]
70pub use self::ser::to_writer;
71pub use self::ser::{
72    Indent, InlinePolicy, Serializer, SerializerConfig, SerializerConfigBuilder, to_string,
73};
74pub use self::stream::StreamDeserializer;
75#[cfg(feature = "io")]
76pub use self::stream::from_reader;
77pub use self::trailing::Trailing;