dot-prop 0.1.0

Get, set, has, and delete a property from a nested JSON value using a dot path (a.b.0.c). A faithful port of the dot-prop npm package.
Documentation
  • Coverage
  • 100%
    15 out of 15 items documented6 out of 13 items with examples
  • Size
  • Source code size: 48.1 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 553.19 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 6s Average build duration of successful builds.
  • all releases: 6s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • trananhtung/dot-prop
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • trananhtung

dot-prop

All Contributors

crates.io docs.rs CI license

Get, set, check, and delete nested JSON values by dot path.

get_property(&value, "foo.bar.0"), set_property(&mut value, "a[1].b", json!(2)). A faithful Rust port of the widely-used dot-prop npm package, operating on [serde_json::Value].

Unlike json-pointer (RFC 6901, /-separated), this uses the JavaScript dot-prop dot-path syntax: . separates keys, [i] indexes arrays, and \ escapes a literal ., [, or \.

  • get_property, set_property, has_property, delete_property
  • parse_path / stringify_path / escape_path, plus deep_keys and unflatten
  • Prototype-pollution guard (__proto__, prototype, constructor are rejected)
  • Differential-tested against the reference dot-prop implementation (60k cases across all operations)

Install

[dependencies]
dot-prop = "0.1"
serde_json = "1"

Usage

use serde_json::json;
use dot_prop::{get_property, set_property, has_property, delete_property, deep_keys, unflatten};

let mut value = json!({ "foo": { "bar": [10, 20] } });

assert_eq!(get_property(&value, "foo.bar.1"), Some(&json!(20)));
assert_eq!(get_property(&value, "foo.bar[0]"), Some(&json!(10)));
assert!(has_property(&value, "foo.bar.0"));

set_property(&mut value, "foo.bar.2", json!(30));
assert_eq!(get_property(&value, "foo.bar.2"), Some(&json!(30)));

delete_property(&mut value, "foo.bar.0"); // leaves a null hole, like JS `delete`
assert_eq!(get_property(&value, "foo.bar.0"), Some(&json!(null)));

// Flatten/expand helpers:
assert_eq!(deep_keys(&json!({ "a": { "b": 1 } })), ["a.b"]);
assert_eq!(unflatten(&json!({ "a.b": 1 })), json!({ "a": { "b": 1 } }));

Notes

  • get_property returns the value itself when the input is not an object/array (matching the reference's default-value behavior).
  • This crate sets the correct keys and values; the serialization order of object keys is governed by serde_json (enable its preserve_order feature for insertion order). The JavaScript original reorders integer-like keys first — a runtime quirk that does not apply to Rust.

Contributors ✨

This project follows the all-contributors specification. Contributions of any kind are welcome — code, docs, bug reports, ideas, reviews! See the emoji key for how each contribution is recognized, and open a PR or issue to get involved.

Thanks goes to these wonderful people:

License

Licensed under either of MIT or Apache-2.0 at your option.