#lua #serde #serialization #de #mlua #value

mlua_serde

Serde (De)serializer implementation for mlua Value

3 unstable releases

Uses old Rust 2015

0.6.0 Apr 18, 2021
0.5.1 Jul 6, 2020
0.5.0 Jul 6, 2020

#1088 in Encoding

MIT license

30KB
848 lines

mlua_serde

Implementation of serde Serializer/Deserializer for mlua::Value

License: MIT Crates.io Documentation

More information about this crate can be found in the crate documentation.

Usage

To use mlua_serde, first add this to your Cargo.toml:

[dependencies]
mlua_serde = { version = "0.6", features = ["lua53"] }

You must choose between Lua 5.1, 5.2, 5.3 and 5.4. To use Lua 5.4, change lua53 for lua54 like this:

[dependencies]
mlua_serde = { version = "0.6", features = ["lua54"] }

If you don't have lua installed on your system for some reason, use the vendored feature:

[dependencies]
mlua_serde = { version = "0.6", features = ["lua53", "vendored"] }

Next, you can use to_value/from_value functions to serialize/deserialize:

#[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();
}

Dependencies

~1.3–3MB
~49K SLoC