Struct serde_json::value::RawValue
source · pub struct RawValue { /* private fields */ }
Expand description
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.
§Note
RawValue
is only available if serde_json is built with the "raw_value"
feature.
[dependencies]
serde_json = { version = "1.0", features = ["raw_value"] }
§Example
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),
}
// Efficiently rearrange JSON input containing separate "code" and "payload"
// keys into a single "info" key holding an array of code and payload.
//
// This could be done equivalently using serde_json::Value as the type for
// payload, but &RawValue will perform better because it does not require
// memory allocation. The correct range of bytes is borrowed from the input
// data and pasted verbatim into the output.
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(())
}
§Ownership
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>,
}
Implementations§
source§impl RawValue
impl RawValue
sourcepub fn from_string(json: String) -> Result<Box<Self>, Error>
pub fn from_string(json: String) -> Result<Box<Self>, Error>
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.
sourcepub fn get(&self) -> &str
pub fn get(&self) -> &str
Access the JSON text underlying a raw value.
§Example
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('{') {
// handle a payload which is a JSON map
} else {
// handle any other type
}
Ok(())
}
fn main() -> Result<()> {
process(r#" {"code": 200, "payload": {}} "#)?;
Ok(())
}
Trait Implementations§
source§impl<'de: 'a, 'a> Deserialize<'de> for &'a RawValue
impl<'de: 'a, 'a> Deserialize<'de> for &'a RawValue
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> Deserialize<'de> for Box<RawValue>
impl<'de> Deserialize<'de> for Box<RawValue>
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> Deserializer<'de> for &'de RawValue
impl<'de> Deserializer<'de> for &'de RawValue
source§fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor<'de>,
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor<'de>,
Deserializer
to figure out how to drive the visitor based
on what data type is in the input. Read moresource§fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor<'de>,
fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor<'de>,
Deserialize
type is expecting a bool
value.source§fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor<'de>,
fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor<'de>,
Deserialize
type is expecting an i8
value.source§fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor<'de>,
fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor<'de>,
Deserialize
type is expecting an i16
value.source§fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor<'de>,
fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor<'de>,
Deserialize
type is expecting an i32
value.source§fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor<'de>,
fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor<'de>,
Deserialize
type is expecting an i64
value.source§fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor<'de>,
fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor<'de>,
Deserialize
type is expecting a u8
value.source§fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor<'de>,
fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor<'de>,
Deserialize
type is expecting a u16
value.source§fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor<'de>,
fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor<'de>,
Deserialize
type is expecting a u32
value.source§fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor<'de>,
fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor<'de>,
Deserialize
type is expecting a u64
value.source§fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor<'de>,
fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor<'de>,
Deserialize
type is expecting a f32
value.source§fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor<'de>,
fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor<'de>,
Deserialize
type is expecting a f64
value.source§fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor<'de>,
fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor<'de>,
Deserialize
type is expecting a char
value.source§fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor<'de>,
fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor<'de>,
Deserialize
type is expecting a string value and does
not benefit from taking ownership of buffered data owned by the
Deserializer
. Read moresource§fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor<'de>,
fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor<'de>,
Deserialize
type is expecting a string value and would
benefit from taking ownership of buffered data owned by the
Deserializer
. Read moresource§fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor<'de>,
fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor<'de>,
Deserialize
type is expecting a byte array and does not
benefit from taking ownership of buffered data owned by the
Deserializer
. Read moresource§fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor<'de>,
fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor<'de>,
Deserialize
type is expecting a byte array and would
benefit from taking ownership of buffered data owned by the
Deserializer
. Read moresource§fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor<'de>,
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor<'de>,
Deserialize
type is expecting an optional value. Read moresource§fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor<'de>,
fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor<'de>,
Deserialize
type is expecting a unit value.source§fn deserialize_unit_struct<V>(
self,
name: &'static str,
visitor: V
) -> Result<V::Value, Error>where
V: Visitor<'de>,
fn deserialize_unit_struct<V>(
self,
name: &'static str,
visitor: V
) -> Result<V::Value, Error>where
V: Visitor<'de>,
Deserialize
type is expecting a unit struct with a
particular name.source§fn deserialize_newtype_struct<V>(
self,
name: &'static str,
visitor: V
) -> Result<V::Value, Error>where
V: Visitor<'de>,
fn deserialize_newtype_struct<V>(
self,
name: &'static str,
visitor: V
) -> Result<V::Value, Error>where
V: Visitor<'de>,
Deserialize
type is expecting a newtype struct with a
particular name.source§fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor<'de>,
fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor<'de>,
Deserialize
type is expecting a sequence of values.source§fn deserialize_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value, Error>where
V: Visitor<'de>,
fn deserialize_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value, Error>where
V: Visitor<'de>,
Deserialize
type is expecting a sequence of values and
knows how many values there are without looking at the serialized data.source§fn deserialize_tuple_struct<V>(
self,
name: &'static str,
len: usize,
visitor: V
) -> Result<V::Value, Error>where
V: Visitor<'de>,
fn deserialize_tuple_struct<V>(
self,
name: &'static str,
len: usize,
visitor: V
) -> Result<V::Value, Error>where
V: Visitor<'de>,
Deserialize
type is expecting a tuple struct with a
particular name and number of fields.source§fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor<'de>,
fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor<'de>,
Deserialize
type is expecting a map of key-value pairs.source§fn deserialize_struct<V>(
self,
name: &'static str,
fields: &'static [&'static str],
visitor: V
) -> Result<V::Value, Error>where
V: Visitor<'de>,
fn deserialize_struct<V>(
self,
name: &'static str,
fields: &'static [&'static str],
visitor: V
) -> Result<V::Value, Error>where
V: Visitor<'de>,
Deserialize
type is expecting a struct with a particular
name and fields.source§fn deserialize_enum<V>(
self,
name: &'static str,
variants: &'static [&'static str],
visitor: V
) -> Result<V::Value, Error>where
V: Visitor<'de>,
fn deserialize_enum<V>(
self,
name: &'static str,
variants: &'static [&'static str],
visitor: V
) -> Result<V::Value, Error>where
V: Visitor<'de>,
Deserialize
type is expecting an enum value with a
particular name and possible variants.source§fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor<'de>,
fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor<'de>,
Deserialize
type is expecting the name of a struct
field or the discriminant of an enum variant.source§fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor<'de>,
fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Error>where
V: Visitor<'de>,
Deserialize
type needs to deserialize a value whose type
doesn’t matter because it is ignored. Read moresource§fn is_human_readable(&self) -> bool
fn is_human_readable(&self) -> bool
Deserialize
implementations should expect to
deserialize their human-readable form. Read more