add source for ces2019
[apps/agl-service-windowmanager-2017.git] / src / json_helper.cpp
1 /*
2  * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
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 #include "util.hpp"
19
20 template <typename T>
21 json_object *to_json_(T const &s)
22 {
23     auto a = json_object_new_array();
24
25     if (!s.empty())
26     {
27         for (auto const &i : s)
28         {
29             json_object_array_add(a, to_json(i.second));
30         }
31     }
32
33     return a;
34 }
35
36 json_object *to_json(std::vector<uint32_t> const &v)
37 {
38     auto a = json_object_new_array();
39     for (const auto i : v)
40     {
41         json_object_array_add(a, json_object_new_int(i));
42     }
43     return a;
44 }
45
46 namespace jh {
47
48 const char* getStringFromJson(json_object* obj, const char* key)
49 {
50     json_object* tmp;
51     if (!json_object_object_get_ex(obj, key, &tmp))
52     {
53         HMI_DEBUG("Not found key \"%s\"", key);
54         return nullptr;
55     }
56
57     return json_object_get_string(tmp);
58 }
59
60 int getIntFromJson(json_object *obj, const char *key)
61 {
62     json_object *tmp;
63     if (!json_object_object_get_ex(obj, key, &tmp))
64     {
65         HMI_DEBUG("Not found key \"%s\"", key);
66         return 0;
67     }
68
69     return json_object_get_int(tmp);
70 }
71
72 double getDoubleFromJson(json_object *obj, const char *key)
73 {
74     json_object *tmp;
75     if (!json_object_object_get_ex(obj, key, &tmp))
76     {
77         HMI_DEBUG("Not found key \"%s\"", key);
78         return 0;
79     }
80
81     return json_object_get_double(tmp);
82 }
83
84 json_bool getBoolFromJson(json_object *obj, const char *key)
85 {
86     json_object *tmp;
87     if (!json_object_object_get_ex(obj, key, &tmp))
88     {
89         HMI_DEBUG("Not found key \"%s\"", key);
90         return FALSE;
91     }
92
93     return json_object_get_boolean(tmp);
94 }
95
96 int inputJsonFilie(const char* file, json_object** obj)
97 {
98     const int input_size = 128;
99     int ret = -1;
100
101     HMI_DEBUG("Input file: %s", file);
102
103     // Open json file
104     FILE *fp = fopen(file, "rb");
105     if (nullptr == fp)
106     {
107         HMI_ERROR("Could not open file");
108         return ret;
109     }
110
111     // Parse file data
112     struct json_tokener *tokener = json_tokener_new();
113     enum json_tokener_error json_error;
114     char buffer[input_size];
115     int block_cnt = 1;
116     while (1)
117     {
118         size_t len = fread(buffer, sizeof(char), input_size, fp);
119         *obj = json_tokener_parse_ex(tokener, buffer, len);
120         if (nullptr != *obj)
121         {
122             HMI_DEBUG("File input is success");
123             ret = 0;
124             break;
125         }
126
127         json_error = json_tokener_get_error(tokener);
128         if ((json_tokener_continue != json_error)
129             || (input_size > len))
130         {
131             HMI_ERROR("Failed to parse file (byte:%d err:%s)",
132                       (input_size * block_cnt), json_tokener_error_desc(json_error));
133             HMI_ERROR("\n%s", buffer);
134             *obj = nullptr;
135             break;
136         }
137         block_cnt++;
138     }
139
140     // Close json file
141     fclose(fp);
142
143     // Free json_tokener
144     json_tokener_free(tokener);
145
146     return ret;
147 }
148
149 } // namespace jh