json_ops

Crates.iojson_ops
lib.rsjson_ops
version0.1.0
sourcesrc
created_at2023-04-19 03:25:01.208391
updated_at2023-04-19 03:25:01.208391
descriptionImplement 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.
homepagehttps://github.com/lymslive/rustspace
repositoryhttps://github.com/lymslive/rustspace
max_upload_size
id843163
size76,097
lymslive (lymslive)

documentation

https://docs.rs/json_ops

README

Json Pointer and Operator Overload

Overview

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.

  • Use path() or path_mut() method to create an json pointer or mutable one, we need new type to support operator overlaod.
  • Use path opeator / to point to further deeper sub node.
  • Use pipe operator | to read scalar primitive value from pointed node, may finallize the / operator chian, which would read as get_or.
  • Use operator << to put a new scalar value to node, or push more item to array or object node.
  • Also overralod operator * and so the pointer can implicitly used as Opion<&Value>.

Dependency

  • 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.

Extension

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.

Example View

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.

Commit count: 1

cargo fmt