sonic_rs::value

Trait JsonValueTrait

Source
pub trait JsonValueTrait {
    type ValueType<'v>
       where Self: 'v;

Show 21 methods // Required methods fn get_type(&self) -> JsonType; fn as_number(&self) -> Option<Number>; fn as_raw_number(&self) -> Option<RawNumber>; fn as_str(&self) -> Option<&str>; fn as_bool(&self) -> Option<bool>; fn get<I: Index>(&self, index: I) -> Option<Self::ValueType<'_>>; fn pointer<P: IntoIterator>(&self, path: P) -> Option<Self::ValueType<'_>> where P::Item: Index; // Provided methods fn is_boolean(&self) -> bool { ... } fn is_true(&self) -> bool { ... } fn is_false(&self) -> bool { ... } fn is_null(&self) -> bool { ... } fn is_number(&self) -> bool { ... } fn is_str(&self) -> bool { ... } fn is_array(&self) -> bool { ... } fn is_object(&self) -> bool { ... } fn is_f64(&self) -> bool { ... } fn is_i64(&self) -> bool { ... } fn is_u64(&self) -> bool { ... } fn as_i64(&self) -> Option<i64> { ... } fn as_u64(&self) -> Option<u64> { ... } fn as_f64(&self) -> Option<f64> { ... }
}
Expand description

A trait for all JSON values. Used by Value and LazyValue.

The Option<V: JsonValueTrait> and Result<V: JsonValueTrait, E> also implement this trait. The Option::None or Result::Err(_) will be viewed as a null value.

Required Associated Types§

Source

type ValueType<'v> where Self: 'v

Required Methods§

Source

fn get_type(&self) -> JsonType

Gets the type of the value. Returns JsonType::Null as default if self is Option::None or Result::Err(_).

§Examples
use sonic_rs::{
    value::{JsonType, JsonValueTrait, Value},
    Result,
};

let json: Value = sonic_rs::from_str(r#"{"a": 1, "b": true}"#).unwrap();

assert_eq!(json.get_type(), JsonType::Object);

let v: Option<&Value> = json.get("c");
assert!(v.is_none());
assert_eq!(v.get_type(), JsonType::Null);

let v: Result<Value> = sonic_rs::from_str("invalid json");
assert!(v.is_err());
assert_eq!(v.get_type(), JsonType::Null);
Source

fn as_number(&self) -> Option<Number>

Returns the Number if self is a Number.

§Examples
use sonic_rs::{json, JsonValueTrait, Number};

assert_eq!(json!(123).as_number(), Some(Number::from(123)));
Source

fn as_raw_number(&self) -> Option<RawNumber>

Returns the RawNumber without precision loss if self is a Number.

Source

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

Returns the str if self is a string.

§Examples
use sonic_rs::{json, JsonValueTrait};
assert_eq!(json!("foo").as_str(), Some("foo"));
Source

fn as_bool(&self) -> Option<bool>

Returns the bool if self is a boolean.

§Examples
use sonic_rs::{json, JsonValueTrait};
assert_eq!(json!(true).as_bool(), Some(true));
Source

fn get<I: Index>(&self, index: I) -> Option<Self::ValueType<'_>>

Index into a JSON array or map. A string-like index can be used to access a value in a map, and a usize index can be used to access an element of an array.

Returns None if the type of self does not match the type of the index, for example if the index is a string and self is an array or a number. Also returns None if the given key does not exist in the map or the given index is not within the bounds of the array.

§Examples
use sonic_rs::value::{JsonType, JsonValueTrait, Value};

let json: Value = sonic_rs::from_str(r#"{"a": 1, "b": true}"#).unwrap();

assert!(json.get("a").is_number());
assert!(json.get("unknown").is_none());
Source

fn pointer<P: IntoIterator>(&self, path: P) -> Option<Self::ValueType<'_>>
where P::Item: Index,

Looks up a value by a path.

The path is an iterator of multiple keys or indexes. It can be a &[&str], &[usize] or a JsonPointer.

§Examples
use sonic_rs::{json, pointer, JsonValueTrait};

let data = json!({
    "x": {
        "y": ["z", "zz"]
    }
});

assert_eq!(data.pointer(&["x", "y"] ).unwrap(), &json!(["z", "zz"]));
assert_eq!(data.pointer(&pointer!["x", "y", 1] ).unwrap(), &json!("zz"));
assert_eq!(data.pointer(&["a", "b"]), None);

Provided Methods§

Source

fn is_boolean(&self) -> bool

Returns true if the value is a bool.

§Examples
use sonic_rs::{json, JsonValueTrait};

let val = json!(true);
assert!(val.is_boolean());
Source

fn is_true(&self) -> bool

Returns true if the value is true.

§Examples
use sonic_rs::{json, JsonValueTrait};

let val = json!(true);
assert!(val.is_true());
Source

fn is_false(&self) -> bool

Returns true if the value is false.

§Examples
use sonic_rs::{json, JsonValueTrait};

let val = json!(false);
assert!(val.is_false());
Source

fn is_null(&self) -> bool

Returns true if the self value is null.

§Notes

It will Returns true if self is Option::None or Result::Err(_).

§Examples
use sonic_rs::{json, JsonValueTrait, Result, Value};

let val = json!(null);
assert!(val.is_null());

let val: Option<&Value> = val.get("unknown");
assert!(val.is_none());
assert!(val.is_null());

let val: Result<Value> = sonic_rs::from_str("invalid json");
assert!(val.is_err());
assert!(val.is_null());
Source

fn is_number(&self) -> bool

Returns true if the value is a number.

§Examples
use sonic_rs::{json, JsonValueTrait};

assert!(json!(1).is_number());
assert!(Option::Some(json!(1.23)).is_number());
Source

fn is_str(&self) -> bool

Returns true if the value is a string.

§Examples
use sonic_rs::{json, JsonValueTrait};
assert!(json!("foo").is_str());
Source

fn is_array(&self) -> bool

Returns true if the value is an array.

§Examples
use sonic_rs::{json, JsonValueTrait};
assert!(json!([]).is_array());
Source

fn is_object(&self) -> bool

Returns true if the value is an object.

§Examples
use sonic_rs::{json, JsonValueTrait};
assert!(json!({}).is_object());
Source

fn is_f64(&self) -> bool

Returns true if the value is a number and it is an f64. It will returns false if the value is a u64 or i64.

§Examples
use sonic_rs::{json, JsonValueTrait};
assert!(!json!(123).is_f64()); // false
assert!(!json!(-123).is_f64()); // false

assert!(json!(-1.23).is_f64());
Source

fn is_i64(&self) -> bool

Returns true if the value is a integer number and it between i64::MIN and i64::MAX

§Examples
use sonic_rs::{json, JsonValueTrait};

assert!(json!(-123).is_i64());
assert!(json!(0).is_i64());
assert!(json!(123).is_i64());
assert!(json!(i64::MIN).is_i64());
assert!(json!(i64::MAX).is_i64());

assert!(!json!(u64::MAX).is_i64()); // overflow for i64
assert!(!json!(-1.23).is_i64()); // false
Source

fn is_u64(&self) -> bool

Returns true if the value is a integer number and it between 0 and i64::MAX

§Examples
use sonic_rs::{json, JsonValueTrait};

assert!(json!(123).is_u64());
assert!(json!(0).is_u64());
assert!(json!(u64::MAX).is_u64());

assert!(!json!(-123).is_u64());
assert!(!json!(1.23).is_u64());
Source

fn as_i64(&self) -> Option<i64>

If self meets is_i64, represent it as i64 if possible. Returns None otherwise.

§Examples
use sonic_rs::{json, JsonValueTrait};

assert_eq!(json!(123).as_i64(), Some(123));
assert_eq!(json!(-123).as_i64(), Some(-123));
assert_eq!(json!(i64::MAX).as_i64(), Some(i64::MAX));

assert_eq!(json!(u64::MAX).as_i64(), None);
Source

fn as_u64(&self) -> Option<u64>

If self meets is_i64, represent it as u64 if possible. Returns None otherwise.

§Examples
use sonic_rs::{json, JsonValueTrait};

assert_eq!(json!(123).as_u64(), Some(123));
assert_eq!(json!(-123).as_u64(), None);
assert_eq!(json!(i64::MAX).as_u64(), Some(i64::MAX as u64));

assert_eq!(json!(u64::MAX).as_u64(), Some(u64::MAX));
Source

fn as_f64(&self) -> Option<f64>

If self is a number, represent it as f64 if possible. Returns None otherwise. The integer number will be converted to f64.

§Examples
use sonic_rs::{json, JsonValueTrait};

assert_eq!(json!(123).as_f64(), Some(123 as f64));
assert_eq!(json!(-123).as_f64(), Some(-123 as f64));
assert_eq!(json!(0.123).as_f64(), Some(0.123));

assert_eq!(json!("hello").as_f64(), None);

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<V: JsonValueTrait> JsonValueTrait for Option<V>

Source§

type ValueType<'v> = <V as JsonValueTrait>::ValueType<'v> where V: 'v, Self: 'v

Source§

fn as_bool(&self) -> Option<bool>

Source§

fn as_f64(&self) -> Option<f64>

Source§

fn as_i64(&self) -> Option<i64>

Source§

fn as_u64(&self) -> Option<u64>

Source§

fn as_number(&self) -> Option<Number>

Source§

fn as_raw_number(&self) -> Option<RawNumber>

Source§

fn get_type(&self) -> JsonType

Source§

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

Source§

fn get<I: Index>(&self, index: I) -> Option<Self::ValueType<'_>>

Source§

fn pointer<P: IntoIterator>(&self, path: P) -> Option<Self::ValueType<'_>>
where P::Item: Index,

Source§

impl<V: JsonValueTrait> JsonValueTrait for &V

Source§

type ValueType<'v> = <V as JsonValueTrait>::ValueType<'v> where V: 'v, Self: 'v

Source§

fn as_bool(&self) -> Option<bool>

Source§

fn as_f64(&self) -> Option<f64>

Source§

fn as_i64(&self) -> Option<i64>

Source§

fn as_u64(&self) -> Option<u64>

Source§

fn as_number(&self) -> Option<Number>

Source§

fn as_raw_number(&self) -> Option<RawNumber>

Source§

fn get_type(&self) -> JsonType

Source§

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

Source§

fn get<I: Index>(&self, index: I) -> Option<Self::ValueType<'_>>

Source§

fn pointer<P: IntoIterator>(&self, path: P) -> Option<Self::ValueType<'_>>
where P::Item: Index,

Source§

impl<V: JsonValueTrait, E> JsonValueTrait for Result<V, E>

Source§

type ValueType<'v> = <V as JsonValueTrait>::ValueType<'v> where V: 'v, Self: 'v

Source§

fn as_bool(&self) -> Option<bool>

Source§

fn as_f64(&self) -> Option<f64>

Source§

fn as_i64(&self) -> Option<i64>

Source§

fn as_u64(&self) -> Option<u64>

Source§

fn as_number(&self) -> Option<Number>

Source§

fn as_raw_number(&self) -> Option<RawNumber>

Source§

fn get_type(&self) -> JsonType

Source§

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

Source§

fn get<I: Index>(&self, index: I) -> Option<Self::ValueType<'_>>

Source§

fn pointer<P: IntoIterator>(&self, path: P) -> Option<Self::ValueType<'_>>
where P::Item: Index,

Implementors§