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("wm:jh", "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("wm:jh", "Not found key \"%s\"", key);
69 return json_object_get_int(tmp);
72 json_bool getBoolFromJson(json_object *obj, const char *key)
75 if (!json_object_object_get_ex(obj, key, &tmp))
77 HMI_DEBUG("wm:jh", "Not found key \"%s\"", key);
81 return json_object_get_boolean(tmp);
84 int inputJsonFilie(const char* file, json_object** obj)
86 const int input_size = 128;
89 HMI_DEBUG("wm:jh", "Input file: %s", file);
92 FILE *fp = fopen(file, "rb");
95 HMI_ERROR("wm:jh", "Could not open file");
100 struct json_tokener *tokener = json_tokener_new();
101 enum json_tokener_error json_error;
102 char buffer[input_size];
106 size_t len = fread(buffer, sizeof(char), input_size, fp);
107 *obj = json_tokener_parse_ex(tokener, buffer, len);
110 HMI_DEBUG("wm:jh", "File input is success");
115 json_error = json_tokener_get_error(tokener);
116 if ((json_tokener_continue != json_error)
117 || (input_size > len))
119 HMI_ERROR("wm:jh", "Failed to parse file (byte:%d err:%s)",
120 (input_size * block_cnt), json_tokener_error_desc(json_error));
121 HMI_ERROR("wm:jh", "\n%s", buffer);
132 json_tokener_free(tokener);