doc: shuffled around some sections, fixes.
[staging/windowmanager.git] / src / json_helper.cpp
1 /*
2  * Copyright (C) 2017 Mentor Graphics Development (Deutschland) GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "json_helper.hpp"
18
19 #include <json.h>
20
21 json_object *to_json(genivi::surface_properties const &s) {
22    // auto j = json::object({
23    auto j = json_object_new_object();
24
25    //    {"id", s.id},
26    json_object_object_add(j, "id", json_object_new_int(s.id));
27
28    //    {"size", {{"width", s.size.w}, {"height", s.size.h}}},
29    auto jsize = json_object_new_object();
30    json_object_object_add(jsize, "width", json_object_new_int(s.size.w));
31    json_object_object_add(jsize, "height", json_object_new_int(s.size.h));
32    json_object_object_add(j, "size", jsize);
33
34    //    {"dst",
35    //     {{"width", s.dst_rect.w},
36    //      {"height", s.dst_rect.h},
37    //      {"x", s.dst_rect.x},
38    //      {"y", s.dst_rect.y}}},
39    auto jdst = json_object_new_object();
40    json_object_object_add(jdst, "width", json_object_new_int(s.dst_rect.w));
41    json_object_object_add(jdst, "height", json_object_new_int(s.dst_rect.h));
42    json_object_object_add(jdst, "x", json_object_new_int(s.dst_rect.x));
43    json_object_object_add(jdst, "y", json_object_new_int(s.dst_rect.y));
44    json_object_object_add(j, "dst", jdst);
45
46    //    {"src",
47    //     {{"width", s.src_rect.w},
48    //      {"height", s.src_rect.h},
49    //      {"x", s.src_rect.x},
50    //      {"y", s.src_rect.y}}},
51    auto jsrc = json_object_new_object();
52    json_object_object_add(jsrc, "width", json_object_new_int(s.src_rect.w));
53    json_object_object_add(jsrc, "height", json_object_new_int(s.src_rect.h));
54    json_object_object_add(jsrc, "x", json_object_new_int(s.src_rect.x));
55    json_object_object_add(jsrc, "y", json_object_new_int(s.src_rect.y));
56    json_object_object_add(j, "src", jsrc);
57
58    //    {"visibility", s.visibility},
59    json_object_object_add(
60       j, "visibility",
61       json_object_new_boolean(static_cast<json_bool>(s.visibility == 1)));
62
63    //    {"opacity", s.opacity},
64    json_object_object_add(j, "opacity", json_object_new_double(s.opacity));
65
66    //    {"orientation", s.orientation},
67    json_object_object_add(j, "orientation", json_object_new_int(s.orientation));
68
69    // });
70    return j;
71 }
72
73 json_object *to_json(genivi::screen const *s) {
74    auto o = json_object_new_object();
75    json_object_object_add(o, "id", json_object_new_int(s->id));
76    return o;
77 }
78
79 template <typename T>
80 json_object *to_json_(T const &s) {
81    auto a = json_object_new_array();
82
83    if (!s.empty()) {
84       for (auto const &i : s) {
85          json_object_array_add(a, to_json(i.second));
86       }
87    }
88
89    return a;
90 }
91
92 json_object *to_json(genivi::controller::props_map const &s) {
93    return to_json_(s);
94 }
95
96 json_object *to_json(std::vector<uint32_t> const &v) {
97    auto a = json_object_new_array();
98    for (const auto i : v) {
99       json_object_array_add(a, json_object_new_int(i));
100    }
101    return a;
102 }