Crates.io | serde_magnus |
lib.rs | serde_magnus |
version | 0.8.1 |
source | src |
created_at | 2023-01-20 01:49:50.059912 |
updated_at | 2024-01-26 17:28:10.601293 |
description | Serde integration for Magnus |
homepage | https://github.com/OneSignal/serde-magnus |
repository | https://github.com/OneSignal/serde-magnus |
max_upload_size | |
id | 763063 |
size | 70,525 |
serde_magnus
serde_magnus
converts between Rust and Ruby data structures using Serde and Magnus.
Quick links
The serde_magnus::serialize
function converts from a Rust type implementing the
serde::Serialize
trait into a Ruby equivalent.
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<String>
}
#[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
.
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
requires Rust 1.65+ and Ruby 3.0+.
serde_magnus
is released under the terms of the MIT License. See LICENSE
for details.