#[repr(C)]
pub struct RawValue { /* fields omitted */ }
Reference to a range of bytes encompassing a single valid JSON value in the
input data.
A RawValue
can be used to defer parsing parts of a payload until later,
or to avoid parsing it at all in the case that part of the payload just
needs to be transferred verbatim into a different output object.
When serializing, a value of this type will retain its original formatting
and will not be minified or pretty-printed.
use serde::{Deserialize, Serialize};
use serde_json::{Result, value::RawValue};
#[derive(Deserialize)]
struct Input<'a> {
code: u32,
#[serde(borrow)]
payload: &'a RawValue,
}
#[derive(Serialize)]
struct Output<'a> {
info: (u32, &'a RawValue),
}
fn rearrange(input: &str) -> Result<String> {
let input: Input = serde_json::from_str(input)?;
let output = Output {
info: (input.code, input.payload),
};
serde_json::to_string(&output)
}
fn main() -> Result<()> {
let out = rearrange(r#" {"code": 200, "payload": {}} "#)?;
assert_eq!(out, r#"{"info":[200,{}]}"#);
Ok(())
}
The typical usage of RawValue
will be in the borrowed form:
#[derive(Deserialize)]
struct SomeStruct<'a> {
#[serde(borrow)]
raw_value: &'a RawValue,
}
The borrowed form is suitable when deserializing through
serde_json::from_str
and serde_json::from_slice
which support
borrowing from the input data without memory allocation.
When deserializing through serde_json::from_reader
you will need to use
the boxed form of RawValue
instead. This is almost as efficient but
involves buffering the raw value from the I/O stream into memory.
#[derive(Deserialize)]
struct SomeStruct {
raw_value: Box<RawValue>,
}
RawValue
is only available if serde_json is built with the "raw_value"
feature.
[dependencies]
serde_json = { version = "1.0", features = ["raw_value"] }
Convert an owned String
of JSON data to an owned RawValue
.
This function is equivalent to serde_json::from_str::<Box<RawValue>>
except that we avoid an allocation and memcpy if both of the following
are true:
- the input has no leading or trailing whitespace, and
- the input has capacity equal to its length.
Access the JSON text underlying a raw value.
use serde::Deserialize;
use serde_json::{Result, value::RawValue};
#[derive(Deserialize)]
struct Response<'a> {
code: u32,
#[serde(borrow)]
payload: &'a RawValue,
}
fn process(input: &str) -> Result<()> {
let response: Response = serde_json::from_str(input)?;
let payload = response.payload.get();
if payload.starts_with('{') {
} else {
}
Ok(())
}
fn main() -> Result<()> {
process(r#" {"code": 200, "payload": {}} "#)?;
Ok(())
}
Creates owned data from borrowed data, usually by cloning. Read more
🔬 This is a nightly-only experimental API. (toowned_clone_into
)
recently added
Uses borrowed data to replace owned data, usually by cloning. Read more
Formats the value using the given formatter. Read more
Formats the value using the given formatter. Read more
Serialize this value into the given Serde serializer. Read more
Deserialize this value from the given Serde deserializer. Read more
Converts the given value to a String
. Read more
Creates owned data from borrowed data, usually by cloning. Read more
🔬 This is a nightly-only experimental API. (toowned_clone_into
)
recently added
Uses borrowed data to replace owned data, usually by cloning. Read more
Immutably borrows from an owned value. Read more
🔬 This is a nightly-only experimental API. (get_type_id
)
this method will likely be replaced by an associated static
Mutably borrows from an owned value. Read more