Expand description
Parse Hjson compatible with deser.
Hjson is JSON for humans, a format for configuration files:
#,//and/* */comments,- commas between values are optional, commas after the last element of sequences and maps are allowed,
- map keys without quotes (
name: api), they end at whitespace and the punctuators{}[],:, - strings without quotes, which end at the end of the line (
note: runs until the end, #, and // too), - multiline strings in triple single quotes (
'''), the indentation up to the column of the opening quotes is removed, - strings in single quotes,
- the braces of a map at the root can be omitted.
Numbers, true, false and null without quotes are values only if
nothing but whitespace, a comma, the end of a container or a comment
follows them on the line: 5 # minutes is the number 5, 5 minutes
is a string. Their type is inferred from their text, so they are
passed on as Implicit values: an u16
receives 8080 as number, a String as "8080".
Otherwise this works like deser-json:
strings and keys without escape sequences are borrowed from the input and
the positions of errors and values refer to the input. In JSON
Lines every line break ends a value. The parser passes the Hjson test
suite.
#[derive(deser::Deserialize)]
struct Config<'a> {
name: &'a str,
version: String,
ports: Vec<u16>,
motd: String,
}
let config: Config = deser_hj::from_str(r#"
# the name of the service
name: api
version: 2
ports: [80, 443]
motd:
'''
Welcome!
Have a nice day.
'''
"#).unwrap();
assert_eq!(config.name, "api");
assert_eq!(config.version, "2");
assert_eq!(config.ports, [80, 443]);
assert_eq!(config.motd, "Welcome!\nHave a nice day.");JSON is valid Hjson, so values are serialized as JSON (with the same
serializer as deser-json).
§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.
- 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.