Jayson is a crate for deserializing data, with the ability to return custom, type-specific errors upon failure.
Unlike serde, Jayson does not parse the data in its serialization format itself, but offload the work to other crates. Instead, it deserializes the already-parsed serialized data into the final type. For example:
// bytes of the serialized value
let s: &str = .. ;
// parse serialized data using another crate, such as serde_json
let json: serde_json::Value = serde_json::from_str(s)?;
// finally deserialize with Jayson
let data = T::deserialize_from_value(json.into_value())?;
use jayson::{DeserializeError, DeserializeFromValue, Error};
enum MyError {
ForbiddenName,
Other(Error)
}
impl DeserializeError for MyError {
fn unexpected(s: &str) -> Self {
Self::Other(Error::unexpected(s))
}
fn missing_field(field: &str) -> Self {
Self::Other(Error::missing_field(field))
}
fn incorrect_value_kind(accepted: &[ValueKind]) -> Self {
Self::Other(Error::incorrect_value_kind(accepted))
}
}
struct Name(String);
impl DeserializeFromValue<MyError> for Name {
fn deserialize_from_value<V: IntoValue>(value: Value<V>) -> Result<Self, MyError> {
match value {
Value::String(s) => {
if s == "Robert '); DROP TABLE Students; --" {
Err(MyError::ForbiddenName)
} else {
Ok(Name(s))
}
}
_ => {
Err(MyError::incorrect_value_kind(&[ValueKind::String]))
}
}
}
}
#[derive(DeserializeFromValue)]
#[jayson(error = MyError)]
struct User {
name: Name,
}
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.