pub struct LazyValue<'a> { /* private fields */ }Expand description
LazyValue wrappers a unparsed raw JSON text. It is borrowed from the origin JSON text.
LazyValue can be get, get_unchecked or
deserialize from a JSON text.
§Examples
use sonic_rs::{get, JsonValueTrait, LazyValue, Value};
// get a lazyvalue from a json, the "a"'s value will not be parsed
let input = r#"{
"a": "hello world",
"b": true,
"c": [0, 1, 2],
"d": {
"sonic": "rs"
}
}"#;
let lv_a: LazyValue = get(input, &["a"]).unwrap();
let lv_c: LazyValue = get(input, &["c"]).unwrap();
// use as_raw_xx to get the unparsed JSON text
assert_eq!(lv_a.as_raw_str(), "\"hello world\"");
assert_eq!(lv_c.as_raw_str(), "[0, 1, 2]");
// use as_xx to get the parsed value
assert_eq!(lv_a.as_str().unwrap(), "hello world");
assert_eq!(lv_c.as_str(), None);
assert!(lv_c.is_array());§Serde Examples
LazyValue<'a> can only be deserialized with borrowed.
If need to be owned, use OwnedLazyValue.
use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize, Serialize, PartialEq)]
struct TestLazyValue<'a> {
#[serde(borrow)]
borrowed_lv: LazyValue<'a>,
}
let input = r#"{ "borrowed_lv": "hello"}"#;
let data: TestLazyValue = sonic_rs::from_str(input).unwrap();
assert_eq!(data.borrowed_lv.as_raw_str(), "\"hello\"");Implementations§
source§impl<'a> LazyValue<'a>
impl<'a> LazyValue<'a>
sourcepub fn as_raw_str(&self) -> &str
pub fn as_raw_str(&self) -> &str
Export the raw JSON text as str.
§Examples
use sonic_rs::{get, LazyValue};
let lv: LazyValue = sonic_rs::get(r#"{"a": "hello world"}"#, &["a"]).unwrap();
assert_eq!(lv.as_raw_str(), "\"hello world\"");sourcepub fn as_raw_cow(&self) -> Cow<'a, str>
pub fn as_raw_cow(&self) -> Cow<'a, str>
Export the raw JSON text as Cow<'de, str>. The lifetime 'de is the origin JSON.
§Examples
use sonic_rs::{get, LazyValue};
let lv: LazyValue = sonic_rs::get(r#"{"a": "hello world"}"#, &["a"]).unwrap();
assert_eq!(lv.as_raw_cow(), "\"hello world\"");sourcepub fn as_raw_faststr(&self) -> FastStr
pub fn as_raw_faststr(&self) -> FastStr
Export the raw json text as faststr.
§Note
If the input json is not bytes or faststr, there will be a string copy.
§Examples
use faststr::FastStr;
use sonic_rs::LazyValue;
let lv: LazyValue = sonic_rs::get(r#"{"a": "hello world"}"#, &["a"]).unwrap();
// will copy the raw_str into a new faststr
assert_eq!(lv.as_raw_faststr(), "\"hello world\"");
let fs = FastStr::new(r#"{"a": "hello world"}"#);
let lv: LazyValue = sonic_rs::get(&fs, &["a"]).unwrap();
assert_eq!(lv.as_raw_faststr(), "\"hello world\""); // zero-copyTrait Implementations§
source§impl<'de: 'a, 'a> Deserialize<'de> for LazyValue<'a>
impl<'de: 'a, 'a> Deserialize<'de> for LazyValue<'a>
source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
source§impl<'de> From<LazyValue<'de>> for OwnedLazyValue
impl<'de> From<LazyValue<'de>> for OwnedLazyValue
source§impl<'a> JsonValueTrait for LazyValue<'a>
impl<'a> JsonValueTrait for LazyValue<'a>
type ValueType<'v> = LazyValue<'v> where Self: 'v
source§fn get_type(&self) -> JsonType
fn get_type(&self) -> JsonType
Gets the type of the value. Returns
JsonType::Null as default if self is Option::None
or Result::Err(_). Read moresource§fn get<I: Index>(&self, index: I) -> Option<LazyValue<'_>>
fn get<I: Index>(&self, index: I) -> Option<LazyValue<'_>>
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. Read more
source§fn pointer<P: IntoIterator>(&self, path: P) -> Option<LazyValue<'_>>where
P::Item: Index,
fn pointer<P: IntoIterator>(&self, path: P) -> Option<LazyValue<'_>>where
P::Item: Index,
Looks up a value by a path. Read more
source§fn is_boolean(&self) -> bool
fn is_boolean(&self) -> bool
Returns true if the value is a
bool. Read moresource§fn is_f64(&self) -> bool
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. Read moresource§impl<'a> Ord for LazyValue<'a>
impl<'a> Ord for LazyValue<'a>
source§impl PartialEq for LazyValue<'_>
impl PartialEq for LazyValue<'_>
source§impl PartialOrd for LazyValue<'_>
impl PartialOrd for LazyValue<'_>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for
self and other) and is used by the <=
operator. Read moresource§impl<'de> TryFrom<LazyValue<'de>> for Value
impl<'de> TryFrom<LazyValue<'de>> for Value
Try parse a LazyValue into a Value. LazyValue is always a valid JSON, at least it is
followed the JSON syntax.
However, in some cases, the parse will failed and return errors, such as the float number in JSON is inifity.
§Examples
use sonic_rs::{JsonValueTrait, LazyValue, Result, Value};
let lazy = sonic_rs::get(r#"{"a": 111e9999999, "b": 2}"#, &["a"]).unwrap();
let x1: Result<Value> = lazy.try_into();
assert!(x1
.unwrap_err()
.to_string()
.contains("Float number must be finite"));impl<'a> Eq for LazyValue<'a>
Auto Trait Implementations§
impl<'a> RefUnwindSafe for LazyValue<'a>
impl<'a> Send for LazyValue<'a>
impl<'a> Sync for LazyValue<'a>
impl<'a> Unpin for LazyValue<'a>
impl<'a> UnwindSafe for LazyValue<'a>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more