1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use crate::errors::*;
use crate::engine::ctx::State;
use crate::hlua::{self, AnyLuaValue};
use std::sync::Arc;
use crate::json;


pub fn json_decode(lua: &mut hlua::Lua, state: Arc<dyn State>) {
    lua.set("json_decode", hlua::function1(move |x: String| -> Result<AnyLuaValue> {
        json::decode(&x)
            .map_err(|err| state.set_error(err))
    }))
}

pub fn json_encode(lua: &mut hlua::Lua, state: Arc<dyn State>) {
    lua.set("json_encode", hlua::function1(move |x: AnyLuaValue| -> Result<String> {
        json::encode(x)
            .map_err(|err| state.set_error(err))
    }))
}

pub fn json_decode_stream(lua: &mut hlua::Lua, state: Arc<dyn State>) {
    lua.set("json_decode_stream", hlua::function1(move |x: String| -> Result<Vec<AnyLuaValue>> {
        json::decode_stream(&x)
            .map_err(|err| state.set_error(err))
    }))
}


#[cfg(test)]
mod tests {
    use crate::engine::ctx::Script;

    #[test]
    fn verify_json_encode() {
        let script = Script::load_unchecked(r#"
        function run()
            x = json_encode({1,2,3,4})
            if x ~= '[1,2,3,4]' then
                return 'encode failed: ' .. x
            end
        end
        "#).expect("Failed to load script");
        script.test().expect("Script failed");
    }

    #[test]
    fn verify_json_decode() {
        let script = Script::load_unchecked(r#"
        function run()
            x = json_decode('[1,2,3,4]')
            if not (x[1] == 1 and x[2] == 2 and x[3] == 3 and x[4] == 4) then
                return 'decode failed: ' .. json_encode(x)
            end
        end
        "#).expect("Failed to load script");
        script.test().expect("Script failed");
    }

    #[test]
    fn verify_json_decode_stream() {
        let script = Script::load_unchecked(r#"
        function run()
            x = json_decode_stream('[1,2][3,4]')
            if not (x[1][1] == 1 and x[1][2] == 2 and x[2][1] == 3 and x[2][2] == 4) then
                return 'decode_stream failed: ' .. json_encode(x)
            end
        end
        "#).expect("Failed to load script");
        script.test().expect("Script failed");
    }
}