Expand description
Parse JSONC (JSON with comments) compatible with deser.
JSONC is JSON with // and /* */ comments and commas after the last
element of sequences and maps, as used by configuration files such as
tsconfig.json or the settings of VS Code. Otherwise this works like
deser-json: strings without escape
sequences are borrowed from the input and the positions of errors and
values refer to the input. In JSON Lines only
line breaks outside of comments end a value.
#[derive(deser::Deserialize)]
struct Config<'a> {
name: &'a str,
ports: Vec<u16>,
}
let config: Config = deser_jsonc::from_str(r#"{
// the name of the service
"name": "api",
/* the ports it listens on */
"ports": [80, 443,],
}"#).unwrap();
assert_eq!(config.name, "api");
assert_eq!(config.ports, [80, 443]);JSON is valid JSONC, so values are serialized as JSON (with the same
serializer as deser-json).
§Raw Values
RawJsonc holds the JSONC text of a value. Like deser_json::RawJson it
keeps the text of values that are deserialized from JSONC as it is
(including comments), other values are encoded as JSON. The serializer
of this crate writes the text of raw JSONC values as it is. See
Raw for more information.
§Features
io(enabled by default): reading and writing streams of the standard library (withDeserializerConfig::reader). Requiresstd.speedups(enabled by default): faster UTF-8 validation.std(enabled by default): uses the standard library. Without it this crate only needsalloc(seeno_std).
Structs§
- Deserializer
- Deserializes a serializable from JSON.
- Deserializer
Config - Configures how JSON is deserialized.
- Deserializer
Config Builder - Builds a
DeserializerConfig. - Iter
- An iterator over the values of a JSON stream.
- Jsonc
- The JSONC format of
RawJsoncvalues. - Serializer
- Serializes values into JSON.
- Serializer
Config - Configures how values are serialized to JSON.
- Serializer
Config Builder - Builds a
SerializerConfig. - Stream
Deserializer - Reads a stream of JSON values (see
deser::stream).
Enums§
- Indent
- How the output is indented.
- Inline
Policy - When maps and sequences are written on a single line in indented output.
- Trailing
- Controls what may follow a value.
Functions§
- from_
reader - Deserializes a value from a reader.
- from_
slice - Deserializes JSON from the given bytes.
- from_
str - Deserializes JSON from the given string.
- to_
string - Serializes a value to JSON.
- to_
writer - Serializes a value to a writer.
Type Aliases§
- RawJsonc
- JSONC text of a value.