Skip to main content

Crate deser_json5

Crate deser_json5 

Source
Expand description

Parse JSON5 compatible with deser.

JSON5 extends JSON with syntax from ECMAScript 5.1:

  • // and /* */ comments,
  • commas after the last element of sequences and maps,
  • map keys which are ECMAScript identifiers ({name: "api"}, with Unicode letters and \u escapes),
  • strings in single quotes, escaped line breaks within strings and the escapes \', \v, \0 and \xFF,
  • hexadecimal numbers (those that do not fit into 64 bits are 128 bit integers and floats beyond that), numbers with a leading + or a leading or trailing decimal point, Infinity and NaN,
  • more whitespace characters.

Otherwise this works like deser-json: strings and identifiers 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 and strings end a value. The parser passes the JSON5 test suite.

#[derive(deser::Deserialize)]
struct Config<'a> {
    name: &'a str,
    ports: Vec<u16>,
    timeout: f64,
}

let config: Config = deser_json5::from_str(r#"{
    // the name of the service
    name: 'api',
    ports: [0x50, 443,],
    timeout: .5,
}"#).unwrap();
assert_eq!(config.name, "api");
assert_eq!(config.ports, [80, 443]);
assert_eq!(config.timeout, 0.5);

JSON is valid JSON5, so values are serialized as JSON (with the same serializer as deser-json). Unlike JSON, JSON5 can represent NaN and infinite floats: to_string and to_writer write them as NaN, Infinity and -Infinity where deser-json writes null. A SerializerConfig needs set_non_finite_floats for this:

use deser_json5::{Indent, SerializerConfig};

let values = vec![1.5, f64::NAN, f64::NEG_INFINITY];
assert_eq!(deser_json5::to_string(&values).unwrap(), "[1.5,NaN,-Infinity]");

const PRETTY: SerializerConfig = SerializerConfig::builder()
    .pretty(Indent::Spaces(2))
    .non_finite_floats(true).build();
assert_eq!(
    PRETTY.to_string(&values).unwrap(),
    "[\n  1.5,\n  NaN,\n  -Infinity\n]"
);

§Raw Values

RawJson5 holds the JSON5 text of a value. Like deser_json::RawJson it keeps the text of values that are deserialized from JSON5 as it is (including comments), other values are encoded as JSON. The serializer of this crate writes the text of raw JSON5 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.
Json5
The JSON5 format of RawJson5 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 JSON5.
to_writer
Serializes a value as JSON5 to a writer.

Type Aliases§

RawJson5
JSON5 text of a value.