[−][src]Crate jsonp
jsonp
Fast, zero copy Json pointers.
This library leverages serde and serde_json to provide fast, easy to use, on demand deserialization of Json.
Ever wanted to retrieve some deeply nested data without all the hassle of defining the required
Rust structures, or allocating multiple times into a serde_json::Value
? No problem:
{
"some": {
"deeply": [
{
"nested": {
"truth": "the cake is a lie"
}
}
]
}
}
fn deeply_nested(json: &str) -> Result { let p = Pointer::default(); let truth: &str = p.dotted(json, ".some.deeply.0.nested.truth")?; assert_eq!(truth, "the cake is a lie"); Ok(()) }
Leveraging serde's zero copy deserialization
we borrow the deeply nested truth
right out of the backing Json data.
Pointer
The core structure of this library is the jsonp::Pointer
. It provides several
methods of dereferencing into Json:
While Pointer::with_segments
provides the most control over exactly how each
Segment
is generated, the other two -- Pointer::with_pattern
and
Pointer::dotted
-- make some assumptions about how the pointer string is handled. These are:
- Passing in an empty pointer (or pattern) string will default to deserializing the entire backing Json.
- If the pointer and pattern are equal, same as above.
- A pointer starting with a pattern is equivalent to one that doesn't. For example,
dotted(".foo")
anddotted("foo")
both result infoo
being dereferenced.
Mode
jsonp::Mode
controls how jsonp
interprets pointer segments. It has two settings,
Late -- the default -- and Early.
Late
Lazily attempts to coerce pointer segments to map keys or array indexes during deserialization. This provides maximum flexibility and allows one to deserialize numeric map keys, i.e "42": "...", but also has the potential to improperly deserialize an array where a map was expected, or vice versa.
Early
Decides on initialization whether a given pointer segment is a numeric index or string key. Guarantees that backing Json object agrees with its expected layout, erroring out otherwise.
Helpers
This library also provides a few convenience wrapper structs around
jsonp::Pointer
. These provide pleasant interfaces if you're planning on using a
Pointer
to repeatedly dereference from a single backing Json structure, and reduce some of
the generics and lifetime noise in function signatures.
Structs
BackingBytes | Convenience wrapper around the library core functions for byte slices, removing some of the generic noise from function signatures. |
BackingJson | Convenience wrapper around the library core functions for raw Json, removing some of the generic noise from function signatures. |
BackingStr | Convenience wrapper around the library core functions for string slices, removing some of the generic noise from function signatures. |
Pointer | The heart of this library, this structure contains all of the base functionality to dereference into borrowed Json structures. |
Segment | Represents a segment of a pointer |
Enums
Mode | Set the mode for interpreting pointer segments. |
Traits
ToRaw | Glue trait allowing the crate's functions to abstract over the exact layout of the borrowed Json. |