pub struct OwnedLazyValue { /* private fields */ }
Expand description
OwnedLazyValue wrappers a unparsed raw JSON text. It is owned.
It can be converted from LazyValue
. It can be used for serde.
Default value is a raw JSON text null
.
§Examples
use sonic_rs::{get, JsonValueTrait, OwnedLazyValue};
// 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 own_a = OwnedLazyValue::from(get(input, &["a"]).unwrap());
let own_c = OwnedLazyValue::from(get(input, &["c"]).unwrap());
// use as_raw_xx to get the unparsed JSON text
assert_eq!(own_a.as_raw_str(), "\"hello world\"");
assert_eq!(own_c.as_raw_str(), "[0, 1, 2]");
// use as_xx to get the parsed value
assert_eq!(own_a.as_str().unwrap(), "hello world");
assert_eq!(own_c.as_str(), None);
assert!(own_c.is_array());
§Serde Examples
use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize, Serialize, PartialEq)]
struct TestLazyValue<'a> {
#[serde(borrow)]
borrowed_lv: LazyValue<'a>,
owned_lv: OwnedLazyValue,
}
let input = r#"{ "borrowed_lv": "hello", "owned_lv": "world" }"#;
let data: TestLazyValue = sonic_rs::from_str(input).unwrap();
assert_eq!(data.borrowed_lv.as_raw_str(), "\"hello\"");
assert_eq!(data.owned_lv.as_raw_str(), "\"world\"");
Implementations§
Source§impl OwnedLazyValue
impl OwnedLazyValue
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_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-copy
Trait Implementations§
Source§impl Clone for OwnedLazyValue
impl Clone for OwnedLazyValue
Source§impl Debug for OwnedLazyValue
impl Debug for OwnedLazyValue
Source§impl Default for OwnedLazyValue
impl Default for OwnedLazyValue
Source§impl<'de> Deserialize<'de> for OwnedLazyValue
impl<'de> Deserialize<'de> for OwnedLazyValue
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 Display for OwnedLazyValue
impl Display for OwnedLazyValue
Source§impl<'de> From<LazyValue<'de>> for OwnedLazyValue
impl<'de> From<LazyValue<'de>> for OwnedLazyValue
Source§impl Hash for OwnedLazyValue
impl Hash for OwnedLazyValue
Source§impl JsonValueTrait for OwnedLazyValue
impl JsonValueTrait for OwnedLazyValue
type ValueType<'v> = OwnedLazyValue
Source§fn as_raw_number(&self) -> Option<RawNumber>
fn as_raw_number(&self) -> Option<RawNumber>
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<OwnedLazyValue>
fn get<I: Index>(&self, index: I) -> Option<OwnedLazyValue>
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<OwnedLazyValue>
fn pointer<P: IntoIterator>(&self, path: P) -> Option<OwnedLazyValue>
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 Ord for OwnedLazyValue
impl Ord for OwnedLazyValue
Source§impl PartialEq for OwnedLazyValue
impl PartialEq for OwnedLazyValue
Source§impl PartialOrd for OwnedLazyValue
impl PartialOrd for OwnedLazyValue
Source§impl Serialize for OwnedLazyValue
impl Serialize for OwnedLazyValue
impl Eq for OwnedLazyValue
Auto Trait Implementations§
impl !Freeze for OwnedLazyValue
impl RefUnwindSafe for OwnedLazyValue
impl Send for OwnedLazyValue
impl Sync for OwnedLazyValue
impl Unpin for OwnedLazyValue
impl UnwindSafe for OwnedLazyValue
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
🔬This is a nightly-only experimental API. (
clone_to_uninit
)