Struct abi_stable::external_types::serde_json::RawValueBox [−][src]
#[repr(transparent)]pub struct RawValueBox { /* fields omitted */ }
serde_json
only.Expand description
An ffi-safe equivalent of Box<serde_json::value::RawValue>
Example
This defines a function that serializes a struct,
and deserializes the json into another one with RawValueBox
fields.
use abi_stable::{
external_types::RawValueBox,
sabi_extern_fn,
std_types::{RBoxError, RErr, ROk, RResult, RStr, RString},
};
use serde::{Deserialize, Serialize};
const JSON: &'static str = r##"{"hello":"world"}"##;
let value = RawValueBox::try_from_string(JSON.to_string()).unwrap();
assert_eq!(serde_json::to_string(&value).unwrap().as_str(), JSON);
#[derive(Serialize)]
pub struct Pair {
pub first: u64,
pub second: RString,
}
#[derive(Debug, Deserialize)]
pub struct PairDeserialize {
pub first: RawValueBox,
pub second: RawValueBox,
}
#[sabi_extern_fn]
fn deserialize_data_structure(
input: RStr<'_>,
) -> RResult<PairDeserialize, RBoxError> {
match serde_json::from_str::<PairDeserialize>(input.into()) {
Ok(x) => ROk(x),
Err(x) => RErr(RBoxError::new(x)),
}
}
let json = serde_json::to_string(&Pair {
first: 99,
second: "How many apples?".into(),
})
.unwrap();
let pair = deserialize_data_structure(json.as_str().into()).unwrap();
assert_eq!(pair.first.get(), "99");
assert_eq!(pair.second.get(), r##""How many apples?"}"##);
Implementations
Converts a String
to an RawValueBox
without checking whether it is valid JSON.
Safety
input
must be valid JSON and contain no leading or trailing whitespace.
Example
use abi_stable::external_types::RawValueBox;
const JSON: &'static str = r##"{"huh":"that is interesting"}"##;
let value = unsafe { RawValueBox::from_string_unchecked(JSON.to_string()) };
assert_eq!(serde_json::to_string(&value).unwrap().as_str(), JSON);
Converts an RString
to an RawValueBox
without checking whether it is valid JSON.
Safety
input
must be valid JSON and contain no leading or trailing whitespace.
Example
use abi_stable::{external_types::RawValueBox, std_types::RString};
const JSON: &'static str = r##"{"huh":"that is interesting"}"##;
let json_rstring = RString::from(JSON);
let value = unsafe { RawValueBox::from_rstring_unchecked(json_rstring) };
assert_eq!(serde_json::to_string(&value).unwrap().as_str(), JSON);
Attempts to convert a String
into a RawValueBox
.
Fails in the same cases as converting a String into a Box<RawValue>
does.
Example
use abi_stable::{external_types::RawValueBox, std_types::RString};
const JSON: &'static str = r##"{"nope":"oof"}"##;
let raw = RawValueBox::try_from_string(JSON.to_string()).unwrap();
assert_eq!(raw.get(), JSON);
Gets the json being serialized,as a &str
.
Example
use abi_stable::external_types::RawValueBox;
const JSON: &'static str = r##"{"huh":1007}"##;
let raw = serde_json::from_str::<RawValueBox>(JSON).unwrap();
assert_eq!(raw.get(), JSON);
Gets the json being serialized,as a RStr<'a>
.
Example
use abi_stable::{external_types::RawValueBox, std_types::RStr};
const JSON: &'static str = r##"{"bugs":"life"}"##;
let raw = serde_json::from_str::<RawValueBox>(JSON).unwrap();
assert_eq!(raw.get_rstr(), RStr::from(JSON));
Gets a RawValueRef<’_> borrowing from this RawValueBox.
Example
use abi_stable::external_types::{RawValueBox, RawValueRef};
const JSON: &'static str = r##"{"bugs":"life"}"##;
let raw = serde_json::from_str::<RawValueBox>(JSON).unwrap();
assert_eq!(raw.get(), RawValueRef::try_from_str(JSON).unwrap().get());
Trait Implementations
Deserialize this value from the given Serde deserializer. Read more
type StaticEquivalent = _static_RawValueBox
type IsNonZeroType = <RString as __StableAbi>::IsNonZeroType
type IsNonZeroType = <RString as __StableAbi>::IsNonZeroType
Whether this type has a single invalid bit-pattern. Read more
The layout of the type provided by implementors.
const
-equivalents of the associated types.
Auto Trait Implementations
impl RefUnwindSafe for RawValueBox
impl Send for RawValueBox
impl Sync for RawValueBox
impl Unpin for RawValueBox
impl UnwindSafe for RawValueBox
Blanket Implementations
Mutably borrows from an owned value. Read more
type ROwned = T
type ROwned = T
The owned type, stored in RCow::Owned
type ForSelf = WithMetadata_<T, T>
type ForSelf = WithMetadata_<T, T>
This is always WithMetadata_<Self, Self>
Compares the address of self
with the address of other
. Read more
Emulates the pipeline operator, allowing method syntax in more places. Read more
The same as piped
except that the function takes &Self
Useful for functions that take &Self
instead of Self
. Read more
The same as piped
, except that the function takes &mut Self
.
Useful for functions that take &mut Self
instead of Self
. Read more
Mutates self using a closure taking self by mutable reference, passing it along the method chain. Read more
Observes the value of self, passing it along unmodified. Useful in long method chains. Read more
Performs a conversion with Into
.
using the turbofish .into_::<_>()
syntax. Read more
Performs a reference to reference conversion with AsRef
,
using the turbofish .as_ref_::<_>()
syntax. Read more
Performs a mutable reference to mutable reference conversion with AsMut
,
using the turbofish .as_mut_::<_>()
syntax. Read more
unsafe fn transmute_element<T>(
self
) -> <Self as CanTransmuteElement<T>>::TransmutedPtr where
Self: CanTransmuteElement<T>,
unsafe fn transmute_element<T>(
self
) -> <Self as CanTransmuteElement<T>>::TransmutedPtr where
Self: CanTransmuteElement<T>,
Transmutes the element type of this pointer.. Read more
type Type = T
type Type = T
This is always Self
.
Converts a value back to the original type.
Converts a mutable reference back to the original type.
alloc
only.Converts a box back to the original type.
Converts a value back to the original type.
Converts a reference back to the original type.
Converts a mutable reference back to the original type.
alloc
only.Converts a box back to the original type.
alloc
only.Converts an Arc back to the original type.
alloc
only.Converts an Rc back to the original type.