summaryrefslogtreecommitdiff
path: root/contrib/json-object.vala
blob: 993381d982e9e92d07ffbef7873674cc30b2d998 (plain)
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
using GLib;
using Json;

public class Sample : GLib.Object {
        public bool toggle { get; set; }
        public string name { get; set; }

        public Sample () {
                name = "Hello, world!";
                toggle = true;
        }

        public string to_json () {
                var obj = new Json.Object ();

                var node = new Json.Node (Json.NodeType.VALUE);
                node.set_string (name);
                obj.add_member ("name", node.copy ());

                node = new Json.Node (Json.NodeType.VALUE);
                node.set_boolean (toggle);
                obj.add_member ("toggle", node.copy ());
                
                var root = new Json.Node (Json.NodeType.OBJECT);
                root.set_object (obj);

                var generator = new Json.Generator ();
                generator.pretty = true;
                generator.root = root;

                return generator.to_data ();
        }

        static int main (string[] args) {
                var sample = new Sample ();
                
                stdout.printf ("[manual]    var sample = %s;\n",
                               sample.to_json ());

                var buf = Json.serialize_gobject (sample);
                stdout.printf ("[automatic] var sample = %s;\n",
                               buf);

                return 0;
        }
}