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\"");
§Convert to serde_json::Value
LazyValue<'a>
can convert into serde_json::Value
from bytes slice.
use sonic_rs::{pointer, JsonValueTrait};
let json: &str = r#"{
"bool": true,
"int": -1,
"uint": 0,
"float": 1.1,
"string": "hello",
"string_escape": "\"hello\"",
"array": [1,2,3],
"object": {"a":"aaa"},
"strempty": "",
"objempty": {},
"arrempty": []
}"#;
let lazy_value = sonic_rs::get(json, pointer![].iter()).unwrap();
for (key, expect_value) in [
("bool", serde_json::json!(true)),
("int", serde_json::json!(-1)),
("uint", serde_json::json!(0)),
("float", serde_json::json!(1.1)),
("string", serde_json::json!("hello")),
("string_escape", serde_json::json!("\"hello\"")),
("array", serde_json::json!([1, 2, 3])),
("object", serde_json::json!({"a":"aaa"})),
("strempty", serde_json::json!("")),
("objempty", serde_json::json!({})),
("arrempty", serde_json::json!([])),
] {
let value = lazy_value.get(key);
let trans_value =
serde_json::from_slice::<serde_json::Value>(value.unwrap().as_raw_str().as_bytes())
.unwrap();
assert_eq!(trans_value, expect_value);
println!("checked {key} with {trans_value:?}");
}
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-copy
Trait 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>,
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 as_raw_number(&self) -> Option<RawNumber>
fn as_raw_number(&self) -> Option<RawNumber>
Source§fn get_type(&self) -> JsonType
fn get_type(&self) -> JsonType
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<'_>>
Source§fn pointer<P: IntoIterator>(&self, path: P) -> Option<LazyValue<'_>>
fn pointer<P: IntoIterator>(&self, path: P) -> Option<LazyValue<'_>>
Source§fn is_boolean(&self) -> bool
fn is_boolean(&self) -> bool
bool
. Read moreSource§fn is_f64(&self) -> bool
fn is_f64(&self) -> bool
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 PartialOrd for LazyValue<'_>
impl PartialOrd for LazyValue<'_>
Source§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.
impl<'a> Eq for LazyValue<'a>
Auto Trait Implementations§
impl<'a> !Freeze for LazyValue<'a>
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
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)
clone_to_uninit
)