mlua_serde/README.md

47 lines
1.2 KiB
Markdown

# rlue_serde
Implementation of [serde](https://serde.rs/) Serializer/Deserializer for [mlua::Value](https://docs.rs/mlua/0.12/mlua/enum.Value.html)
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
[![Crates.io](https://img.shields.io/crates/v/mlua_serde.svg)](https://crates.io/crates/mlua_serde)
[![Documentation](https://docs.rs/mlua_serde/badge.svg)][dox]
More information about this crate can be found in the [crate documentation][dox].
[dox]: https://docs.rs/mlua_serde/*/mlua_serde/
## Usage
To use `mlua_serde`, first add this to your `Cargo.toml`:
```toml
[dependencies]
mlua_serde = "0.4"
```
Next, you can use `to_value`/`from_value` functions to serialize/deserialize:
```rust
#[derive(Serialize, Deserialize)]
struct Foo {
bar: u32,
baz: Vec<String>,
}
fn main() {
let lua = mlua::Lua::new();
let foo = Foo {
bar: 42,
baz: vec![String::from("fizz"), String::from("buzz")],
};
let value = mlua_serde::to_value(lua, &foo).unwrap();
lua.globals().set("value", value).unwrap();
lua.load(
r#"
assert(value["bar"] == 42)
assert(value["baz"][2] == "buzz")
"#).exec().unwrap();
}
```