Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for converting text to Json #95

Merged
merged 1 commit into from
Jun 9, 2022
Merged

Add support for converting text to Json #95

merged 1 commit into from
Jun 9, 2022

Conversation

nickbabcock
Copy link
Contributor

The mid-level API now provides the excellent utility of converting the
plaintext Clausewitz format to JSON when the json feature is enabled.

use jomini::TextTape;

let tape = TextTape::from_slice(b"foo=bar")?;
let reader = tape.windows1252_reader();
let actual = reader.json().to_string()?;
assert_eq!(actual, r#"{"foo":"bar"}"#);

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);

It's debatable whether duplicate keys is valid JSON, so this allows one
to customize output depending how flexible a downstream client is at handling
JSON.

The options are either:

  • Group values into an array under a single field
  • Preserve the duplicate keys
  • Rewrite objects as an array of key value pairs

The mid-level API now provides the excellent utility of converting the
plaintext Clausewitz format to JSON when the `json` feature is enabled.

```rust
use jomini::TextTape;

let tape = TextTape::from_slice(b"foo=bar")?;
let reader = tape.windows1252_reader();
let actual = reader.json().to_string()?;
assert_eq!(actual, r#"{"foo":"bar"}"#);
```

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.

```rust
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);
```

It's debatable whether [duplicate keys is valid JSON][0], so this allows one
to customize output depending how flexible a downstream client is at handling
JSON.

The options are either:

- Group values into an array under a single field
- Preserve the duplicate keys
- Rewrite objects as an array of key value pairs

[0]: https://stackoverflow.com/q/21832701
@nickbabcock nickbabcock self-assigned this Jun 9, 2022
@nickbabcock nickbabcock merged commit 965c326 into master Jun 9, 2022
@nickbabcock nickbabcock deleted the json branch June 9, 2022 10:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant