pub trait JsonRef<'a>: Sized + Copy {
type Item: JsonRef<'a>;
Show 16 methods
// Required methods
fn is_array(self) -> bool;
fn is_object(self) -> bool;
fn as_null(self) -> Option<()>;
fn as_bool(self) -> Option<bool>;
fn as_number_str(self) -> Option<&'a str>;
fn as_str(self) -> Option<&'a str>;
fn get(self, key: &str) -> Option<Self::Item>;
fn index_at(self, i: usize) -> Option<Self::Item>;
fn len(self) -> Option<usize>;
// Provided methods
fn is_null(self) -> bool { ... }
fn is_bool(self) -> bool { ... }
fn is_number(self) -> bool { ... }
fn is_string(self) -> bool { ... }
fn as_i64(self) -> Option<i64> { ... }
fn as_u64(self) -> Option<u64> { ... }
fn as_f64(self) -> Option<f64> { ... }
}Expand description
Read-only, tree-like access to a parsed JSON value.
Modelled on serde_json::Value’s accessor methods. The lifetime 'a
is the string-access lifetime: returned &'a str slices are valid for at
least 'a.
Implemented by:
TapeRef<'a, _>— lightweight cursor into a flatcrate::tape::Tape.Option<J>whereJ: JsonRef<'a>— transparent wrapper enabling chaining without intermediate?or.and_then:root.get("a").get("b").as_str().
Required Associated Types§
Required Methods§
Sourcefn as_bool(self) -> Option<bool>
fn as_bool(self) -> Option<bool>
Returns the boolean if this value is a boolean, otherwise None.
Sourcefn as_number_str(self) -> Option<&'a str>
fn as_number_str(self) -> Option<&'a str>
Returns the raw number token if this value is a number, otherwise None.
The slice always contains valid JSON number syntax.
Sourcefn as_str(self) -> Option<&'a str>
fn as_str(self) -> Option<&'a str>
Returns the string content if this value is a JSON string, otherwise None.
Sourcefn get(self, key: &str) -> Option<Self::Item>
fn get(self, key: &str) -> Option<Self::Item>
Look up an object member by key.
Returns None if this value is not an object or the key is absent.
Provided Methods§
Sourcefn as_i64(self) -> Option<i64>
fn as_i64(self) -> Option<i64>
Parse the number as i64.
Returns None for non-numbers or when the value is out of range.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementations on Foreign Types§
Source§impl<'a, J: JsonRef<'a>> JsonRef<'a> for Option<J>
Blanket implementation of JsonRef for Option<J> where J: JsonRef.
impl<'a, J: JsonRef<'a>> JsonRef<'a> for Option<J>
Blanket implementation of JsonRef for Option<J> where J: JsonRef.
Every accessor returns the same result as calling it on the inner value,
or the appropriate “absent” answer when self is None:
- Boolean tests (
is_*) returnfalse. as_*accessors returnNone.get,index_at,lenreturnNone.
This allows chaining without intermediate ? or .and_then:
let city = root.get("address").get("city").as_str();
let first = root.get("items").index_at(0).as_i64();Source§type Item = <J as JsonRef<'a>>::Item
type Item = <J as JsonRef<'a>>::Item
Chaining stays flat: Option<J> produces the same item type as J.