Module jomini::json

source ·
Expand description

Handles conversion of plaintext clausewitz format to JSON

use jomini::{TextTape, json::{JsonOptions, DuplicateKeyMode}};

let tape = TextTape::from_slice(b"core=a core=b")?;
let reader = tape.windows1252_reader();

let options = JsonOptions::new()
    .with_prettyprint(false)
    .with_duplicate_keys(DuplicateKeyMode::Preserve);

// These are the default options
assert_eq!(options, JsonOptions::default());

let actual = reader.json()
    .with_options(options)
    .to_string();
assert_eq!(actual, r#"{"core":"a","core":"b"}"#);

The scope of the JSON can be narrowed to an inner value. This comes in handy when the parsed document is large but only a small subset of it needs to be exposed with JSON.

use jomini::{TextTape};

let tape = TextTape::from_slice(b"nums={1 2 3 4}")?;
let reader = tape.windows1252_reader();
let mut fields = reader.fields();
let (_key, _op, value) = fields.next().unwrap();
let array = value.read_array()?;
let actual = array.json().to_string();
let expected = r#"[1,2,3,4]"#;
assert_eq!(&actual, expected);

Structs§

Enums§

  • Controls JSON structure when duplicate keys are encountered
  • Controls when a value is attempted to be narrowed to a more specific type