Skip to main content

Crate deser_jsonc

Crate deser_jsonc 

Source
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 (with DeserializerConfig::reader). Requires std.
  • speedups (enabled by default): faster UTF-8 validation.
  • std (enabled by default): uses the standard library. Without it this crate only needs alloc (see no_std).

Structs§

Deserializer
Deserializes a serializable from JSON.
DeserializerConfig
Configures how JSON is deserialized.
DeserializerConfigBuilder
Builds a DeserializerConfig.
Iter
An iterator over the values of a JSON stream.
Jsonc
The JSONC format of RawJsonc values.
Serializer
Serializes values into JSON.
SerializerConfig
Configures how values are serialized to JSON.
SerializerConfigBuilder
Builds a SerializerConfig.
StreamDeserializer
Reads a stream of JSON values (see deser::stream).

Enums§

Indent
How the output is indented.
InlinePolicy
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.