[][src]Struct abi_stable::external_types::serde_json::RawValueRef

#[repr(transparent)]pub struct RawValueRef<'a> { /* fields omitted */ }

An ffi-safe equivalent of &serde_json::value::RawValue

Example

This defines a function that serializes a struct, and deserializes the json into another one with RawValueRef fields.

use abi_stable::{
    external_types::RawValueRef,
    std_types::{RStr,RResult,ROk,RErr,RString,RBoxError},
    sabi_extern_fn,
};

use serde::{Serialize,Deserialize};

use std::collections::HashMap;

const JSON:&'static str=r##"{"hello":"world"}"##;

let value=RawValueRef::try_from_str(JSON).unwrap();

assert_eq!(
    serde_json::to_string(&value).unwrap().as_str(),
    JSON
);


#[derive(Serialize)]
pub struct Pair{
    pub first:Vec<u32>,
    pub second:HashMap<RString,RString>,
}

#[derive(Debug,Deserialize)]
pub struct PairDeserialize<'a>{
    #[serde(borrow)]
    pub first:RawValueRef<'a>,

    #[serde(borrow)]
    pub second:RawValueRef<'a>,
}

#[sabi_extern_fn]
fn deserialize_data_structure<'de>(
    input:RStr<'de>,
)->RResult<PairDeserialize<'de>,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:vec![0,1,2],
        second:vec![ (RString::from("hello"),"world".into()) ].into_iter().collect(),
    }).unwrap();

let pair=deserialize_data_structure(json.as_str().into()).unwrap();

assert_eq!(pair.first.get(),"[0,1,2]");
assert_eq!(pair.second.get(),r##"{"hello":"world"}"##);


Implementations

impl<'a> RawValueRef<'a>[src]

pub unsafe fn from_str_unchecked(input: &'a str) -> RawValueRef<'a>[src]

Converts a &str to a RawValueRef<'a> 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::RawValueRef;

const JSON:&'static str=r##"{"huh":"that is interesting"}"##;

let value=unsafe{ RawValueRef::from_str_unchecked(JSON) };

assert_eq!(
    serde_json::to_string(&value).unwrap().as_str(),
    JSON
);

pub unsafe fn from_rstr_unchecked(input: RStr<'a>) -> RawValueRef<'a>[src]

Converts a RStr<'a> to a RawValueRef<'a> 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::RawValueRef,
    std_types::RStr,
};

const JSON:&'static str=r##"{"huh":"that is interesting"}"##;

let json_rstr=RStr::from(JSON);
let value=unsafe{ RawValueRef::from_rstr_unchecked(json_rstr) };

assert_eq!(
    serde_json::to_string(&value).unwrap().as_str(),
    JSON
);

pub fn try_from_str(input: &'a str) -> Result<Self, JsonError>[src]

Attempts to convert a &'a str into a RawValueRef<'a>.

Fails in the same cases as parsing a &'a RawValue from a string does.

Example

use abi_stable::{
    external_types::RawValueRef,
    std_types::RStr,
};

const JSON:&'static str=r##"{"nope":"oof"}"##;

let raw=RawValueRef::try_from_str(JSON).unwrap();

assert_eq!( raw.get(), JSON );

pub fn get(&self) -> &'a str[src]

Gets the json being serialized,as a &str.

Example

use abi_stable::external_types::RawValueRef;

const JSON:&'static str=r##"{"huh":1007}"##;

let raw=serde_json::from_str::<RawValueRef<'static>>(JSON).unwrap();

assert_eq!( raw.get(), JSON );

pub fn get_rstr(&self) -> RStr<'a>[src]

Gets the json being serialized,as a RStr<'a>.

Example

use abi_stable::{
    external_types::RawValueRef,
    std_types::RStr,
};

const JSON:&'static str=r##"{"bugs":"life"}"##;

let raw=serde_json::from_str::<RawValueRef<'static>>(JSON).unwrap();

assert_eq!( raw.get_rstr(), RStr::from(JSON) );

Trait Implementations

impl<'a> Clone for RawValueRef<'a>[src]

impl<'a> Copy for RawValueRef<'a>[src]

impl<'a> Debug for RawValueRef<'a>[src]

impl<'de: 'a, 'a> Deserialize<'de> for RawValueRef<'a>[src]

impl<'a> Display for RawValueRef<'a>[src]

impl<'a> From<&'a RawValue> for RawValueRef<'a>[src]

impl<'a> GetStaticEquivalent_ for RawValueRef<'a>[src]

type StaticEquivalent = _static_RawValueRef<'static>

impl<'a> Serialize for RawValueRef<'a>[src]

impl<'a> SharedStableAbi for RawValueRef<'a>[src]

type IsNonZeroType = <RStr<'a> as __SharedStableAbi>::IsNonZeroType

Whether this type has a single invalid bit-pattern. Read more

type Kind = __ValueKind

The kind of abi stability of this type,there are 2: Read more

impl<'a> TryFrom<&'a str> for RawValueRef<'a>[src]

type Error = JsonError

The type returned in the event of a conversion error.

Auto Trait Implementations

impl<'a> RefUnwindSafe for RawValueRef<'a>

impl<'a> Send for RawValueRef<'a>

impl<'a> Sync for RawValueRef<'a>

impl<'a> Unpin for RawValueRef<'a>

impl<'a> UnwindSafe for RawValueRef<'a>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<'a, T> BorrowOwned<'a> for T where
    T: 'a + Clone
[src]

type ROwned = T

type RBorrowed = &'a T

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

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> SelfOps for T where
    T: ?Sized
[src]

impl<This> StableAbi for This where
    This: SharedStableAbi<Kind = ValueKind>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<This> TransmuteElement for This where
    This: ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The error type returned when the conversion fails.

impl<T> TypeIdentity for T where
    T: ?Sized
[src]

type Type = T

The same type as Self. Read more