2 * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "json_helper.hpp"
21 json_object *to_json_(T const &s)
23 auto a = json_object_new_array();
27 for (auto const &i : s)
29 json_object_array_add(a, to_json(i.second));
36 json_object *to_json(std::vector<uint32_t> const &v)
38 auto a = json_object_new_array();
39 for (const auto i : v)
41 json_object_array_add(a, json_object_new_int(i));
48 const char* getStringFromJson(json_object* obj, const char* key)
51 if (!json_object_object_get_ex(obj, key, &tmp))
53 HMI_DEBUG("Not found key \"%s\"", key);
57 return json_object_get_string(tmp);
60 int getIntFromJson(json_object *obj, const char *key)
63 if (!json_object_object_get_ex(obj, key, &tmp))
65 HMI_DEBUG("Not found key \"%s\"", key);
69 return json_object_get_int(tmp);
72 double getDoubleFromJson(json_object *obj, const char *key)
75 if (!json_object_object_get_ex(obj, key, &tmp))
77 HMI_DEBUG("Not found key \"%s\"", key);
81 return json_object_get_double(tmp);
84 json_bool getBoolFromJson(json_object *obj, const char *key)
87 if (!json_object_object_get_ex(obj, key, &tmp))
89 HMI_DEBUG("Not found key \"%s\"", key);
93 return json_object_get_boolean(tmp);
96 int inputJsonFilie(const char* file, json_object** obj)
98 const int input_size = 128;
101 HMI_DEBUG("Input file: %s", file);
104 FILE *fp = fopen(file, "rb");
107 HMI_ERROR("Could not open file");
112 struct json_tokener *tokener = json_tokener_new();
113 enum json_tokener_error json_error;
114 char buffer[input_size];
118 size_t len = fread(buffer, sizeof(char), input_size, fp);
119 *obj = json_tokener_parse_ex(tokener, buffer, len);
122 HMI_DEBUG("File input is success");
127 json_error = json_tokener_get_error(tokener);
128 if ((json_tokener_continue != json_error)
129 || (input_size > len))
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);
144 json_tokener_free(tokener);