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
73
74
75
76
#[cfg(test)]
mod tests {
    use serde_json::{self, json, Value};
    use std::io::{BufWriter, Write};
    use swipl::prelude::*;

    #[test]
    fn deserialize_into_json() {
        let engine = Engine::new();
        let activation = engine.activate();
        let context: Context<_> = activation.into();

        let term = context
            .term_from_string("_{foo: bar, baz: _{elts: [42, _{wow:moo}, 50]}}")
            .unwrap();
        let result: Value = context.deserialize_from_term(&term).unwrap();
        let expected = json!({"foo": "bar",
                              "baz": {"elts": [42,
                                               {"wow":"moo"},
                                               50]}});

        eprintln!("{:?}", result);
        assert_eq!(expected, result);
    }

    #[test]
    fn transcode_into_json() {
        let engine = Engine::new();
        let activation = engine.activate();
        let context: Context<_> = activation.into();

        let term = context
            .term_from_string("_{foo: bar, baz: _{elts: [42, _{wow:moo}, 50]}}")
            .unwrap();

        let buf = BufWriter::new(Vec::new());
        let deserializer = swipl::term::de::Deserializer::new(&context, term.clone());
        let mut serializer = serde_json::Serializer::new(buf);

        serde_transcode::transcode(deserializer, &mut serializer).unwrap();
        let mut buf = serializer.into_inner();
        buf.flush().unwrap();

        let vec = buf.into_inner().unwrap();
        let s = std::str::from_utf8(&vec).unwrap();

        assert_eq!(r#"{"foo":"bar","baz":{"elts":[42,{"wow":"moo"},50]}}"#, s);
    }

    #[test]
    fn deserialize_atom_from_json() {
        let engine = Engine::new();
        let activation = engine.activate();
        let _context: Context<_> = activation.into();

        let json = json!("hello");

        let atom: Atom = serde_json::from_value(json).unwrap();

        assert_eq!(atom!("hello"), atom);
    }

    #[test]
    fn serialize_atom_to_json() {
        let engine = Engine::new();
        let activation = engine.activate();
        let _context: Context<_> = activation.into();

        let atom = atom!("hello");
        let expected = json!("hello");

        let json = serde_json::to_value(atom).unwrap();

        assert_eq!(expected, json);
    }
}