POI: AGL LifeCycle Management
[apps/agl-service-windowmanager.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 json_bool getBoolFromJson(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 FALSE;
79     }
80
81     return json_object_get_boolean(tmp);
82 }
83
84 int inputJsonFilie(const char* file, json_object** obj)
85 {
86     const int input_size = 128;
87     int ret = -1;
88
89     HMI_DEBUG("Input file: %s", file);
90
91     // Open json file
92     FILE *fp = fopen(file, "rb");
93     if (nullptr == fp)
94     {
95         HMI_ERROR("Could not open file");
96         return ret;
97     }
98
99     // Parse file data
100     struct json_tokener *tokener = json_tokener_new();
101     enum json_tokener_error json_error;
102     char buffer[input_size];
103     int block_cnt = 1;
104     while (1)
105     {
106         size_t len = fread(buffer, sizeof(char), input_size, fp);
107         *obj = json_tokener_parse_ex(tokener, buffer, len);
108         if (nullptr != *obj)
109         {
110             HMI_DEBUG("File input is success");
111             ret = 0;
112             break;
113         }
114
115         json_error = json_tokener_get_error(tokener);
116         if ((json_tokener_continue != json_error)
117             || (input_size > len))
118         {
119             HMI_ERROR("Failed to parse file (byte:%d err:%s)",
120                       (input_size * block_cnt), json_tokener_error_desc(json_error));
121             HMI_ERROR("\n%s", buffer);
122             *obj = nullptr;
123             break;
124         }
125         block_cnt++;
126     }
127
128     // Close json file
129     fclose(fp);
130
131     // Free json_tokener
132     json_tokener_free(tokener);
133
134     return ret;
135 }
136
137 } // namespace jh