Skip to main content

JsonRef

Trait JsonRef 

Source
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 flat crate::tape::Tape.
  • Option<J> where J: JsonRef<'a> — transparent wrapper enabling chaining without intermediate ? or .and_then: root.get("a").get("b").as_str().

Required Associated Types§

Source

type Item: JsonRef<'a>

The concrete node type returned by get and index_at.

For concrete node types (TapeRef) this is Self. For Option<J> it is J::Item, keeping chains flat: opt.get("a") returns Option<J::Item>, not Option<Option<J>>.

Required Methods§

Source

fn is_array(self) -> bool

Returns true if this value is a JSON array.

Source

fn is_object(self) -> bool

Returns true if this value is a JSON object.

Source

fn as_null(self) -> Option<()>

Returns Some(()) if this value is null, otherwise None.

Source

fn as_bool(self) -> Option<bool>

Returns the boolean if this value is a boolean, otherwise None.

Source

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.

Source

fn as_str(self) -> Option<&'a str>

Returns the string content if this value is a JSON string, otherwise None.

Source

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.

Source

fn index_at(self, i: usize) -> Option<Self::Item>

Index into an array by zero-based position.

Returns None if this value is not an array or i is out of bounds.

Source

fn len(self) -> Option<usize>

Returns the number of elements (array) or key-value pairs (object).

Returns None if this value is neither an array nor an object.

Provided Methods§

Source

fn is_null(self) -> bool

Returns true if this value is JSON null.

Source

fn is_bool(self) -> bool

Returns true if this value is a JSON boolean.

Source

fn is_number(self) -> bool

Returns true if this value is a JSON number.

Source

fn is_string(self) -> bool

Returns true if this value is a JSON string.

Source

fn as_i64(self) -> Option<i64>

Parse the number as i64.

Returns None for non-numbers or when the value is out of range.

Source

fn as_u64(self) -> Option<u64>

Parse the number as u64.

Returns None for non-numbers or when the value is out of range.

Source

fn as_f64(self) -> Option<f64>

Parse the number as f64.

Returns None for non-numbers.

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.

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_*) return false.
  • as_* accessors return None.
  • get, index_at, len return None.

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

Chaining stays flat: Option<J> produces the same item type as J.

Source§

fn is_array(self) -> bool

Source§

fn is_object(self) -> bool

Source§

fn as_null(self) -> Option<()>

Source§

fn as_bool(self) -> Option<bool>

Source§

fn as_number_str(self) -> Option<&'a str>

Source§

fn as_str(self) -> Option<&'a str>

Source§

fn get(self, key: &str) -> Option<J::Item>

Source§

fn index_at(self, i: usize) -> Option<J::Item>

Source§

fn len(self) -> Option<usize>

Implementors§

Source§

impl<'t, 'src: 't> JsonRef<'t> for TapeRef<'t, 'src>

Source§

type Item = TapeRef<'t, 'src>