Crates.io | json_ops |
lib.rs | json_ops |
version | 0.1.0 |
source | src |
created_at | 2023-04-19 03:25:01.208391 |
updated_at | 2023-04-19 03:25:01.208391 |
description | Implement json pointer following the json pointer syntax, with type Option<&toml::Value>. Overload / as path operator to point into a node in json tree, as well as some other meaningfull operator overload. |
homepage | https://github.com/lymslive/rustspace |
repository | https://github.com/lymslive/rustspace |
max_upload_size | |
id | 843163 |
size | 76,097 |
Though it is wonderful to use serde
crate to serialize and deserialize json
between native rust type, there is still case to use untyped json tree
directely, may because the json tree is too freely or too loose to map to some
strong type well, for example the json schema structure, or any other reason.
This crate focus to operate on a single node in json tree, based on json pointer, mainly followed the standard json pointer syntax, and with operator overload to make it more convenient and intuitive.
path()
or path_mut()
method to create an json pointer or mutable
one, we need new type to support operator overlaod./
to point to further deeper sub node.|
to read scalar primitive value from pointed node, may
finallize the /
operator chian, which would read as get_or
.<<
to put a new scalar value to node, or push more item to
array or object node.*
and so the pointer can implicitly used as
Opion<&Value>
.serde_json
, the json pointer is pointing to serde_json::Value
.toml
, if enable feature toml
, as the data structure of toml is very
similar to json.The json or toml pointer is actually concrete type of more generic Value
pointer. So it is possible to implement pointer and operator overload for
other json-like data structure, or common value node origanized in a tree,
provided implement the following trait:
ValuePath
, for opertor /
;ValueReader
, for opertor |
;ValueWriter
, for opertor <<
.If you implemnt some Value
struct from begining youself, then it is further
possible to overload operator for &Value
directely, no need to use path()
method to begin a /
operator chain.
let v: Value = "{...}".parse().unwrap(); # got a Value tree from anywhere
let node = v.path() / "sub" / 0 / "subdeep";
let node = v.path() / "sub/0/subdeep";
let node = v.pathto("sub/0/subdeep");
let scalar = node | 0;
let scalar = v.path() / "path" / "to" / "string-node" | "";
let mut v: Value = ...;
let _ = v.path_mut() / "path" / "to" / "leaf" << "some new value";
let _ = v.path_mut() / "path" / "to" / "object" << ("key", "val") << ("k2", "v2");
let _ = v.path_mut() / "path" / "to" / "array" << ("val",) << ["v2"];
Please refer to documatation for more detailed and runable examples and tests.