pub trait Assign {
type Value;
type Error;
// Required method
fn assign<V>(
&mut self,
ptr: &Pointer,
value: V,
) -> Result<Option<Self::Value>, Self::Error>
where V: Into<Self::Value>;
}
Expand description
Implemented by types which can internally assign a
(Value
) at a path represented by a JSON Pointer
.
§Expansion
For provided implementations ("json"
, and "toml"
) path will
automatically be expanded the if the Pointer
is not fully exhausted
before reaching a non-existent key in the case of objects, index in the case
of arrays, or a scalar value (including null
) based upon a best-guess
effort on the meaning of each Token
:
- If the
Token
is equal to"0"
or"-"
, the token will be considered an index of an array. - All tokens not equal to
"0"
or"-"
will be considered keys of an object.
§Examples
§Successful assignment with replacement
This example demonstrates a successful assignment with replacement.
use jsonptr::{Pointer, assign::Assign};
use serde_json::{json, Value};
let mut data = json!({"foo": "bar"});
let ptr = Pointer::from_static("/foo");
let replaced = data.assign(&ptr, "baz").unwrap();
assert_eq!(replaced, Some(json!("bar")));
assert_eq!(data, json!({"foo": "baz"}));
§Successful assignment with path expansion
This example demonstrates path expansion, including an array index ("0"
)
let ptr = Pointer::from_static("/foo/bar/0/baz");
let mut data = serde_json::json!({"foo": "bar"});
let replaced = data.assign(ptr, json!("qux")).unwrap();
assert_eq!(&data, &json!({"foo": {"bar": [{"baz": "qux"}]}}));
assert_eq!(replaced, Some(json!("bar")));
§Successful assignment with "-"
token
This example performs path expansion using the special "-"
token (per RFC
6901) to represent the next element in an array.
let ptr = Pointer::from_static("/foo/bar/-/baz");
let mut data = json!({"foo": "bar"});
let replaced = data.assign(ptr, json!("qux")).unwrap();
assert_eq!(&data, &json!({"foo": {"bar": [{"baz": "qux"}]}}));
assert_eq!(replaced, Some(json!("bar")));
Required Associated Types§
Required Methods§
Object Safety§
This trait is not object safe.