# flatted (Rust)

A super light and fast circular JSON parser.

## Usage

The package is published as `webreflection-flatted`; its library target is
still `flatted`, so Rust imports remain `use flatted::...`.

```toml
[dependencies]
webreflection-flatted = { path = "../rust" } # or git dependency
```

```rust
use std::cell::RefCell;
use std::rc::Rc;

use flatted::{parse_simple, stringify_simple, Value};

fn main() {
    // const a = [{}]; a[0].a = a; a.push(a);
    let a = Value::Array(Rc::new(RefCell::new(vec![Value::empty_object()])));
    let first = a.as_array().unwrap().borrow()[0].clone();
    first
        .as_object()
        .unwrap()
        .borrow_mut()
        .insert("a".into(), a.clone());
    a.as_array().unwrap().borrow_mut().push(a.clone());

    let s = stringify_simple(&a).unwrap();
    assert_eq!(s, r#"[["1","0"],{"a":"0"}]"#);

    let back = parse_simple(&s).unwrap();
    let again = back.as_array().unwrap().borrow()[0].clone();
    let self_ref = again.as_object().unwrap().borrow().get("a").cloned().unwrap();
    assert!(back.ptr_eq(&self_ref));
}
```

Arrays and objects use `Rc<RefCell<_>>` so circular and shared references keep
their identity after parsing (Rust has no built-in reference-typed JSON value).

## API

| Function | Role |
|----------|------|
| `stringify(value, replacer, space)` | Flatten to flatted JSON text |
| `parse(text, reviver)` | Restore a recursive value graph |
| `to_json(value)` | Flatten to a plain `serde_json::Value` array |
| `from_json(value)` | Restore recursion from that array |
| `stringify_simple` / `parse_simple` | Same without replacer/reviver/space |

## CLI

```bash
cargo build --release --features cli
echo '{"a":"b"}' | ./target/release/flatted
echo '[{"a":"1"},"b"]' | ./target/release/flatted -d
```

## Test

```bash
cargo test
```

## Bench (vs JS)

Same 1s wall-clock methodology as `test/bench.js`:

```bash
# from repo root — compares ESM/JS vs Rust release
npm run bench:js-rs

# or individually
node test/bench-flatted.mjs
cargo run --example bench --release --manifest-path rust/Cargo.toml
```

## Note on crates.io

The crate is published as `webreflection-flatted` because the name `flatted`
is already taken by an unofficial port. The library target remains `flatted`,
so source code and imports do not change.
