Expand description

This module holds the two dom implementations we use. We distinguish between owned and borrowed. The difference being is that the borrowed value will use &str as its string type, referencing the input, while owned will allocate a new String for each value.

Note that since json strings allow for for escape sequences the borrowed value does not implement zero copy parsing, it does however not allocate new memory for strings.

This differs notably from serde’s zero copy implementation as, unlike serde, we do not require prior knowledge about string content to to take advantage of it.

Usage

The value trait is meant to simplify interacting with DOM values, for both creation as well as mutation and inspection.

Objects can be treated as hashmap’s for the most part

use simd_json::{OwnedValue as Value, prelude::*};
let mut v = Value::object();
v.insert("key", 42);
assert_eq!(v.get("key").unwrap(), &42);
assert_eq!(v["key"], &42);
assert_eq!(v.remove("key").unwrap().unwrap(), 42);
assert_eq!(v.get("key"), None);

Arrays can be treated as vectors for the most part

use simd_json::{OwnedValue as Value, prelude::*};
let mut v = Value::array();
v.push("zero");
v.push(1);
assert_eq!(v[0], &"zero");
assert_eq!(v.get_idx(1).unwrap(), &1);
assert_eq!(v.pop().unwrap().unwrap(), 1);
assert_eq!(v.pop().unwrap().unwrap(), "zero");
assert_eq!(v.pop().unwrap(), None);

Nested changes are also possible:

use simd_json::{OwnedValue as Value, prelude::*};
let mut o = Value::object();
o.insert("key", Value::array());
o["key"].push(Value::object());
o["key"][0].insert("other", "value");
assert_eq!(o.encode(), r#"{"key":[{"other":"value"}]}"#);

Borrowed values, using Cow’s for strings using in situ parsing strategies wherever possible

Enums

Borrowed JSON-DOM Value, consider using the ValueTrait to access its content

Functions

Parses a slice of bytes into a Value dom. This function will rewrite the slice to de-escape strings. As we reference parts of the input slice the resulting dom has the same lifetime as the slice it was created from.

Parses a slice of bytes into a Value dom. This function will rewrite the slice to de-escape strings. As we reference parts of the input slice the resulting dom has the same lifetime as the slice it was created from.

Type Definitions

Representation of a JSON object