https://github.com/onesignal/serde-magnus
Serde bindings for Magnus Ruby crate
https://github.com/onesignal/serde-magnus
Last synced: about 1 year ago
JSON representation
Serde bindings for Magnus Ruby crate
- Host: GitHub
- URL: https://github.com/onesignal/serde-magnus
- Owner: OneSignal
- License: mit
- Created: 2023-10-18T17:49:11.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-07-30T16:26:08.000Z (almost 2 years ago)
- Last Synced: 2024-08-09T21:13:40.018Z (almost 2 years ago)
- Language: Rust
- Size: 88.9 KB
- Stars: 3
- Watchers: 1
- Forks: 6
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# `serde_magnus`
`serde_magnus` converts between Rust and Ruby data structures using [Serde] and [Magnus].
[Serde]: https://github.com/serde-rs/serde
[Magnus]: https://github.com/matsadler/magnus
**Quick links**
* [API documentation](https://docs.rs/serde_magnus)
* [Releases/changelog](https://github.com/OneSignal/serde-magnus/releases)
* [`serde_magnus` on crates.io](https://crates.io/crates/serde_magnus)
## Usage
The [`serde_magnus::serialize`] function converts from a Rust type implementing the
[`serde::Serialize`] trait into a Ruby equivalent.
```rust
use serde::{Serialize, Deserialize};
use magnus::{eval, Value};
use serde_magnus::serialize;
#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct Post {
title: String,
content: String,
author: Author,
tags: Vec
}
#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct Author {
name: String,
email_address: String
}
let post = Post {
title: "Spring carnival planning update".into(),
content: "Here's what's new.".into(),
author: Author {
name: "Martha".into(),
email_address: "martha@example.com".into()
},
tags: vec![
"carnival".into(),
"update".into()
]
};
let post: Value = serialize(&post)?;
assert!(eval!(
r#"
post == {
title: "Spring carnival planning update",
content: "Here's what's new.",
author: {
name: "Martha",
email_address: "martha@example.com"
},
tags: ["carnival", "update"]
}
"#,
post
)?);
```
[`serde_magnus::deserialize`] converts from a Ruby value to a Rust type implementing
[`serde::Deserialize`].
```rust
use magnus::RHash;
use serde_magnus::deserialize;
let post: RHash = eval!(r#"
{
title: "Spring carnival planning update",
content: "Here's what's new.",
author: {
name: "Martha",
email_address: "martha@example.com"
},
tags: ["carnival", "update"]
}
"#)?;
let post: Post = deserialize(post)?;
assert_eq!(
Post {
title: "Spring carnival planning update".into(),
content: "Here's what's new.".into(),
author: Author {
name: "Martha".into(),
email_address: "martha@example.com".into(),
},
tags: vec![
"carnival".into(),
"update".into()
]
},
post
);
```
[`serde_magnus::serialize`]: https://docs.rs/serde_magnus/latest/serde_magnus/fn.serialize.html
[`serde::Serialize`]: https://docs.rs/serde/latest/serde/trait.Serialize.html
[`serde_magnus::deserialize`]: https://docs.rs/serde_magnus/latest/serde_magnus/fn.deserialize.html
[`serde::Deserialize`]: https://docs.rs/serde/latest/serde/trait.Deserialize.html
## Requirements
`serde_magnus` requires Rust 1.65+ and Ruby 3.0+.
## License
`serde_magnus` is released under the terms of the MIT License. See `LICENSE` for details.