pub struct RawValue<'a>(/* private fields */);Expand description
One complete JSON value kept as its exact source text: validated once, then stored, compared, forwarded, or read later.
This is serde_json’s RawValue. A JSON-RPC server keeps params as a
RawValue until the handler for the method knows what type to read it
as; a proxy forwards a result without decoding it.
A RawValue always holds exactly one value with no whitespace around
it. Every way of making one validates, or takes text this crate has
just validated or written: new and
from_string check the text,
Reader::read_raw_value and FromJson take it from a read that
checked it, and encode takes it from this crate’s
writer. So writing one (ToJson) copies the text with no second
check, and the output stays valid JSON. Under
Format::html_safe or
Format::ascii the characters those formats
escape are escaped inside the text’s strings as well (see
ValueWriter::raw); otherwise the text is
copied unchanged.
Valid means grammatical JSON. A RawValue taken under
DuplicateKeys::KeepAll, or made by
encode from a ToJson that writes a key twice, can
hold an object with a repeated key: decode (default
policy) refuses that text, parse keeps every member.
§Examples
use jsonx_lib::{DecodeError, FromJson, RawValue, Reader};
struct Request<'a> { method: &'a str, params: RawValue<'a> }
impl<'de> FromJson<'de> for Request<'de> {
fn from_json(reader: &mut Reader<'de>) -> Result<Self, DecodeError> {
let (mut method, mut params) = (None, None);
let mut o = reader.read_object()?;
while let Some(key) = o.next_key()? {
match &*key {
"method" => o.field(&mut method)?,
"params" => o.field(&mut params)?,
_ => {}
}
}
Ok(Request { method: o.required(method, "method")?, params: o.required(params, "params")? })
}
}
let req: Request = jsonx_lib::from_str(r#"{"method": "add", "params": [2, 3]}"#)?;
assert_eq!(req.params.as_str(), "[2, 3]"); // not decoded yet
let (a, b): (i32, i32) = req.params.decode()?; // now, once the method is known
assert_eq!(a + b, 5);Implementations§
Source§impl<'a> RawValue<'a>
impl<'a> RawValue<'a>
Sourcepub fn new(text: &'a str) -> Result<Self, Error>
pub fn new(text: &'a str) -> Result<Self, Error>
Validates text as one JSON value under the default Limits and
keeps it, minus surrounding whitespace, as a borrowed slice.
§Errors
The parse error of text that is not exactly one JSON value.
§Examples
use jsonx_lib::RawValue;
assert_eq!(RawValue::new(" {\"a\": 1} ")?.as_str(), "{\"a\": 1}");
assert!(RawValue::new("[1,").is_err());
assert!(RawValue::new("1 2").is_err());Sourcepub fn encode<T: ToJson + ?Sized>(value: &T) -> RawValue<'static>
pub fn encode<T: ToJson + ?Sized>(value: &T) -> RawValue<'static>
Writes value as compact JSON and keeps the text.
§Examples
let raw = jsonx_lib::RawValue::encode(&[1, 2]);
assert_eq!(raw.as_str(), "[1,2]");Sourcepub fn into_owned(self) -> RawValue<'static>
pub fn into_owned(self) -> RawValue<'static>
Copies borrowed text so the value no longer borrows from the input.
§Examples
let owned = jsonx_lib::RawValue::new(&String::from("1"))?.into_owned();
assert_eq!(owned.as_str(), "1");Sourcepub fn decode<'s, T: FromJson<'s>>(&'s self) -> Result<T, DecodeError>
pub fn decode<'s, T: FromJson<'s>>(&'s self) -> Result<T, DecodeError>
Reads the text as a T (FromJson), with the default Limits
(which bound a recursive type’s depth) and duplicate keys refused.
§Errors
The DecodeError of T; offsets count from the start of the raw
text.
§Examples
let raw = jsonx_lib::RawValue::new("[1, 2, 3]")?;
assert_eq!(raw.decode::<Vec<u8>>()?, [1, 2, 3]);