app: when loading config, handle returned errors
[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(
44       j, "visibility",
45       json_object_new_boolean(static_cast<json_bool>(s.visibility == 1)));
46
47    //    {"opacity", s.opacity},
48    json_object_object_add(j, "opacity", json_object_new_double(s.opacity));
49
50    //    {"orientation", s.orientation},
51    json_object_object_add(j, "orientation", json_object_new_int(s.orientation));
52
53    // });
54    return j;
55 }
56
57 json_object *to_json(genivi::screen const *s) {
58    auto o = json_object_new_object();
59    json_object_object_add(o, "id", json_object_new_int(s->id));
60    return o;
61 }
62
63 template <typename T>
64 json_object *to_json_(T const &s) {
65    auto a = json_object_new_array();
66
67    if (!s.empty()) {
68       for (auto const &i : s) {
69          json_object_array_add(a, to_json(i.second));
70       }
71    }
72
73    return a;
74 }
75
76 json_object *to_json(genivi::controller::props_map const &s) {
77    return to_json_(s);
78 }
79
80 json_object *to_json(std::vector<uint32_t> const &v) {
81    auto a = json_object_new_array();
82    for (const auto i : v) {
83       json_object_array_add(a, json_object_new_int(i));
84    }
85    return a;
86 }