Skip to content
This repository was archived by the owner on Jan 26, 2023. It is now read-only.

meilisearch/jayson

Jayson


⚠️ This repository is archived, please check out deserr instead.


Introduction

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())?;

Example

Implementing deserialize for a custom type

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]))
            }
        }
    }
}

Using macros

#[derive(DeserializeFromValue)]
#[jayson(error = MyError)]
struct User {
	name: Name,
}

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
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.

About

No description, website, or topics provided.

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages