Crate jsonptr_lite

Crate jsonptr_lite 

Source
Expand description

Tiny JSON Pointer lookups for serde_json::Value.

Supports RFC 6901 escape rules for path tokens:

  • ~1 becomes /
  • ~0 becomes ~

This is intentionally minimal: one function, no allocations beyond token unescaping. Returns None for invalid pointers or missing paths.

§Examples

use serde_json::{json, Value};
use jsonptr_lite::ptr;

let v = json!({ "a": { "b": 3 }});
assert_eq!(ptr(&v, "/a/b").and_then(Value::as_i64), Some(3));

// array index
let v = json!({ "items": [10, 20, 30] });
assert_eq!(ptr(&v, "/items/1").and_then(Value::as_i64), Some(20));

// escaped slash in key name: "/"
let v = json!({ "a/b": 7 });
assert_eq!(ptr(&v, "/a~1b").and_then(Value::as_i64), Some(7));

// empty pointer returns the whole value
let v = json!(42);
assert_eq!(ptr(&v, "").and_then(Value::as_i64), Some(42));

change value via pointer use serde_json::{json, Value}; use jsonptr_lite::{ptr, ptr_mut}; let mut v = json!({“a”:{“b”:0}}); *ptr_mut(&mut v, “/a/b”).unwrap() = json!(42); assert_eq!(ptr(&v, “/a/b”).and_then(Value::as_i64), Some(42));

Functions§

ptr
Lookup a JSON value by JSON Pointer (RFC 6901).
ptr_mut