Skip to main content

Module json_tools

Module json_tools 

Source
Expand description

JSON-specific helpers: json:concat, json:append, and json:frag.

[concat] merges many JSON files into one object keyed by filename stem. Each input becomes <stem> (without .json); collisions get _2, _3. Values are preserved verbatim: objects stay objects, arrays stay arrays, scalars stay scalars.

append inserts many JSON files into an existing document, in place (or to -o). Each file is added under its filename stem. If a key already exists, it is overwritten. If the target does not exist, a new object is created.

frag descends level levels from the root and writes one JSON file per child found at that depth. Level 0 fragments the root’s direct children. When multiple paths reach the same depth, filenames are prefixed with the full key path joined by _ to avoid collisions. Keys are sanitized the same way xlsx:frag does (NFD, lowercase, non-alphanumeric collapsed to _).

§Examples

use sheeter_engine::json_tools;
use std::fs;

let dir = tempfile::tempdir().unwrap();
fs::write(dir.path().join("a.json"), r#"{"x":1}"#).unwrap();
fs::write(dir.path().join("b.json"), r#"{"y":2}"#).unwrap();
let out = dir.path().join("out.json");
json_tools::concat(
    &[
        dir.path().join("a.json").to_str().unwrap().to_string(),
        dir.path().join("b.json").to_str().unwrap().to_string(),
    ],
    out.to_str().unwrap(),
)
.unwrap();
let parsed: serde_json::Value =
    serde_json::from_str(&fs::read_to_string(out).unwrap()).unwrap();
assert_eq!(parsed["a"]["x"], 1);
assert_eq!(parsed["b"]["y"], 2);

Functions§

append
Inserts many JSON files into target under their filename stems.
concat
Merges many JSON files into a single object keyed by filename stem.
frag
Descends level levels and writes one JSON file per child found there.