Struct RawValue
#[repr(transparent)]
pub struct RawValue { /* private fields */ }
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 ;
use ;
// 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.
Ownership
The typical usage of RawValue will be in the borrowed form:
# use Deserialize;
# use 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.
# use Deserialize;
# use RawValue;
#
Implementations
impl RawValue
const NULL: &'static RawValue = _;A constant RawValue with the JSON value
null.const TRUE: &'static RawValue = _;A constant RawValue with the JSON value
true.const FALSE: &'static RawValue = _;A constant RawValue with the JSON value
false.fn from_string(json: String) -> Result<Box<Self>, Error>Convert an owned
Stringof JSON data to an ownedRawValue.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.
unsafe fn from_string_unchecked(json: String) -> Box<Self>Convert an owned
Stringof JSON data to an ownedRawValuewithout checking that it contains valid JSON.This is the unchecked counterpart of
RawValue::from_string, for strings that are already known to be valid JSON, such as the output of another JSON serializer. Unlikefrom_string, it does not re-parse the string; the only cost isString::into_boxed_str.Safety
The string passed in must contain a single well-formed JSON value with no leading or trailing whitespace.
RawValueis written verbatim into JSON output wherever it is embedded, and other code (including unsafe code) is allowed to rely onRawValueupholding this invariant, in the same way that unsafe code may rely onstrcontaining valid UTF-8.In debug builds this contract is checked with a
debug_assert!that re-parses the input; the check is compiled out in release builds, so only release performance matches the "no re-parse" guarantee above.Example
use RawValue; let json = to_string?; // SAFETY: `json` was produced by serde_json's own serializer, so it is // a single well-formed JSON value without surrounding whitespace. let raw = unsafe ; assert_eq!; # Ok::fn get(&self) -> &strAccess the JSON text underlying a raw value.
Example
use Deserialize; use ;
Trait Implementations
impl Debug for RawValue
fn fmt(&self, formatter: &mut Formatter<'_>) -> Result
impl Display for RawValue
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl Serialize for RawValue
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer,
impl ToOwned for RawValue
type Owned = Box<RawValue>;fn to_owned(&self) -> Self::Owned
Auto Trait Implementations
impl !Sized for RawValue
impl Freeze for RawValue
impl RefUnwindSafe for RawValue
impl Send for RawValue
impl Sync for RawValue
impl Unpin for RawValue
impl UnsafeUnpin for RawValue
impl UnwindSafe for RawValue
Blanket Implementations
impl<T> Any for RawValue
where
T: 'static + ?Sized,
fn type_id(&self) -> TypeId
impl<T> Borrow<T> for RawValue
where
T: ?Sized,
fn borrow(&self) -> &T
impl<T> BorrowMut<T> for RawValue
where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
impl<T> ToString for RawValue
where
T: Display + ?Sized,
fn to_string(&self) -> String