93e6d698770e8e5fd1f57f6cf01245d579ce0398
[staging/windowmanager.git] / src / json_helper.cpp
1 #include "json_helper.hpp"
2
3 #include <json.h>
4
5 json_object *to_json(genivi::surface_properties const &s) {
6    // auto j = json::object({
7    auto j = json_object_new_object();
8
9    //    {"id", s.id},
10    json_object_object_add(j, "id", json_object_new_int(s.id));
11
12    //    {"size", {{"width", s.size.w}, {"height", s.size.h}}},
13    auto jsize = json_object_new_object();
14    json_object_object_add(jsize, "width", json_object_new_int(s.size.w));
15    json_object_object_add(jsize, "height", json_object_new_int(s.size.h));
16    json_object_object_add(j, "size", jsize);
17
18    //    {"dst",
19    //     {{"width", s.dst_rect.w},
20    //      {"height", s.dst_rect.h},
21    //      {"x", s.dst_rect.x},
22    //      {"y", s.dst_rect.y}}},
23    auto jdst = json_object_new_object();
24    json_object_object_add(jdst, "width", json_object_new_int(s.dst_rect.w));
25    json_object_object_add(jdst, "height", json_object_new_int(s.dst_rect.h));
26    json_object_object_add(jdst, "x", json_object_new_int(s.dst_rect.x));
27    json_object_object_add(jdst, "y", json_object_new_int(s.dst_rect.y));
28    json_object_object_add(j, "dst", jdst);
29
30    //    {"src",
31    //     {{"width", s.src_rect.w},
32    //      {"height", s.src_rect.h},
33    //      {"x", s.src_rect.x},
34    //      {"y", s.src_rect.y}}},
35    auto jsrc = json_object_new_object();
36    json_object_object_add(jsrc, "width", json_object_new_int(s.src_rect.w));
37    json_object_object_add(jsrc, "height", json_object_new_int(s.src_rect.h));
38    json_object_object_add(jsrc, "x", json_object_new_int(s.src_rect.x));
39    json_object_object_add(jsrc, "y", json_object_new_int(s.src_rect.y));
40    json_object_object_add(j, "src", jsrc);
41
42    //    {"visibility", s.visibility},
43    json_object_object_add(j, "visibility", json_object_new_boolean(s.visibility == 1));
44
45    //    {"opacity", s.opacity},
46    json_object_object_add(j, "opacity", json_object_new_double(s.opacity));
47
48    //    {"orientation", s.orientation},
49    json_object_object_add(j, "orientation", json_object_new_int(s.orientation));
50
51    // });
52    return j;
53 }
54
55 json_object *to_json(genivi::screen const *s) {
56    auto o = json_object_new_object();
57    json_object_object_add(o, "id", json_object_new_int(s->id));
58    return o;
59 }
60
61 template <typename T>
62 json_object *to_json_(T const &s) {
63    auto a = json_object_new_array();
64
65    if (!s.empty()) {
66       for (auto const &i : s) {
67          json_object_array_add(a, to_json(i.second));
68       }
69    }
70
71    return a;
72 }
73
74 json_object *to_json(genivi::controller::props_map const &s) {
75    return to_json_(s);
76 }
77
78 json_object *to_json(std::vector<uint32_t> const &v) {
79    auto a = json_object_new_array();
80    for (const auto i : v) {
81       json_object_array_add(a, json_object_new_int(i));
82    }
83    return a;
84 }