[Awesome Rust] jsonapi : Serialize, deserialize and work with JSON-API data
jsonapi
jsonapi
is the Rust library for serialization, deserialization and working with JSON-API data
This is an implementation of the JSON-API v1 specification at jsonapi.org - http://jsonapi.org/.
Installation
Add this crate to your Cargo.toml
file,
1 | # Cargo.toml |
Or use the master branch directly from github,
1 | # Cargo.toml |
Examples of most serialization and deserialization cases can be found in the tests/ - https://github.com/michiel/jsonapi-rust/tree/master/tests directory or the documentation - https://docs.rs/jsonapi.
Development
Examples
Basic Usage with Macro
Using the jsonapi_model!
macro a struct can be converted into a JsonApiDocument
or Resource
. It is required that the struct have an id property whose type is String. The second argument in the jsonapi_model!
marco defines the type member as required by the JSON:API specification
1 | extern crate serde_derive; |
Deserializing a JSON:API Document
Deserialize a JSON:API
document using serde by explicitly declaring the variable type in Result
1 | let serialized = r#" |
Or parse the String directly using the Resource::from_str
trait implementation
1 | let data = Resource::from_str(&serialized); |
JsonApiDocument
implements PartialEq which allows two documents to be compared for equality. If two documents possess the same contents the ordering of the attributes and fields within the JSON:API
document are irrelevant and their equality will be true.
References
[2] jsonapi - Rust - https://docs.rs/jsonapi/latest/jsonapi/
[3] jsonapi - crates.io: Rust Package Registry - https://crates.io/crates/jsonapi
[4] tests/ - https://github.com/michiel/jsonapi-rust/tree/master/tests