Skip to main content

RawJson

Struct RawJson 

Source
pub struct RawJson { /* private fields */ }
Expand description

An owned piece of JSON text that travels through the SDK unchanged.

It holds whatever value it was built from - an object, an array or a scalar - as the text the wire carried, so a response field of a shape this version does not know survives a decode and can be read later with decode. Two values are equal when their text is equal, which means equality is textual: {"a":1} and { "a": 1 } are different values.

§Invariant

The text is always exactly one complete JSON value whose structure is valid. Nothing builds a RawJson without the codec having established that, because the text is written into a request body unread: a second value in it would become a field of the enclosing object. Escapes inside strings are passed through unchecked: a \u escape with bad hex digits, or a lone surrogate half, is kept as it was written, and a strict server refuses the request that carries it. String boundaries are found the same way either way, so such an escape cannot end a string early.

§Serialization

Inside the SDK the text is spliced into the request body byte for byte: key order, spacing and the exact spelling of every number are what the caller or the server wrote. Through any other serializer - serde_json, for instance - the value is written out as ordinary JSON data instead, so the data survives while its spelling may not: numbers are re-rendered by that serializer and insignificant whitespace is dropped.

Which of the two happens is decided by whether the SDK’s own encoder is running on this thread, not by the type of the serializer, because serde offers no way to ask a serializer what it is. A RawJson handed to a foreign serializer from inside a caller’s own Serialize implementation while the SDK is encoding a request is therefore spliced rather than transcoded; use as_str there.

Implementations§

Source§

impl RawJson

Source

pub fn from_value<T>(value: &T) -> Result<Self, EncodeError>
where T: Serialize + ?Sized,

Encodes value and keeps the resulting JSON text.

§Errors

Returns EncodeError when the value cannot be represented as JSON.

Source

pub fn as_str(&self) -> &str

The JSON text, exactly as it was received or encoded.

Source

pub fn decode<'de, T>(&'de self) -> Result<T, DecodeError>
where T: Deserialize<'de>,

Decodes the held text into T.

It is decode rather than deserialize so that it does not shadow Deserialize::deserialize, which a caller reaches for when they read a RawJson out of a document with a codec of their own.

§Errors

Returns DecodeError when the text does not have the shape T expects, or when it is nested deeper than the decoder allows - which a value built by from_value can be, since encoding is not depth limited.

Trait Implementations§

Source§

impl Clone for RawJson

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for RawJson

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result

Prints the JSON text itself, with control characters and the format characters that reorder or hide text written as Rust escapes: JSON allows those raw inside a string, the text usually came from a server, and a {:?} usually ends up in a log line. Backslashes are kept as they are, since they are the JSON’s own escapes. Display, as_str and serialization give the text byte for byte. The default derive would wrap the text in a struct with one field and quote it a second time.

Source§

impl<'de> Deserialize<'de> for RawJson

Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for RawJson

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for RawJson

Source§

impl Hash for RawJson

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for RawJson

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Serialize for RawJson

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for RawJson

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToCompactString for T
where T: Display,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more