gron/lib.rs
1//! # gron — make JSON greppable
2//!
3//! Flatten a JSON document into discrete, greppable assignment lines, and
4//! reconstruct it back — a Rust port of [gron](https://github.com/tomnomnom/gron),
5//! available both as a **library** and a `gron` **CLI**.
6//!
7//! ```
8//! use serde_json::json;
9//!
10//! let v = json!({ "user": { "name": "Tom", "tags": ["a", "b"] } });
11//! let lines = gron::gron(&v);
12//! assert_eq!(lines, "\
13//! json = {};
14//! json.user = {};
15//! json.user.name = \"Tom\";
16//! json.user.tags = [];
17//! json.user.tags[0] = \"a\";
18//! json.user.tags[1] = \"b\";
19//! ");
20//!
21//! // …and back again.
22//! assert_eq!(gron::ungron(&lines).unwrap(), v);
23//! ```
24//!
25//! Flat assignment lines are perfect for `grep`, `diff`, and `sed` over JSON.
26
27#![doc(html_root_url = "https://docs.rs/gron-rs/0.1.0")]
28
29use serde_json::Value;
30
31mod decode;
32mod encode;
33mod ident;
34
35pub use decode::{ungron, UngronError};
36
37/// Flatten `value` into gron assignment lines using the default root `json`.
38///
39/// ```
40/// assert_eq!(gron::gron(&serde_json::json!(42)), "json = 42;\n");
41/// ```
42#[must_use]
43pub fn gron(value: &Value) -> String {
44 gron_with_root(value, "json")
45}
46
47/// Flatten `value` into gron assignment lines using a custom root identifier.
48#[must_use]
49pub fn gron_with_root(value: &Value, root: &str) -> String {
50 encode::gron(value, root)
51}