1 // Copyright 2017 AW SOFTWARE CO.,LTD
2 // Copyright 2017 AISIN AW CO.,LTD
6 #include "JsonResponseAnalyzer.h"
9 * @brief Response analysis of navicore_getallroutes
10 * @param res_json JSON string of response
11 * @return Map information for the key sent in the request
13 std::map< int32_t, naviapi::variant > JsonResponseAnalyzer::AnalyzeResponseGetPosition( std::string& res_json )
15 std::map< int32_t, naviapi::variant > ret;
17 TRACE_DEBUG("AnalyzeResponseGetPosition json_obj:\n%s\n", json_object_to_json_string(json_obj));
19 // convert to Json Object
20 struct json_object *json_obj = json_tokener_parse( res_json.c_str() );
23 struct json_object *json_map_ary = NULL;
24 if( json_object_object_get_ex(json_obj, "response", &json_map_ary) )
26 // Check if the response is array information
27 if( json_object_is_type(json_map_ary, json_type_array) )
29 for (int i = 0; i < json_object_array_length(json_map_ary); ++i)
31 struct json_object* j_elem = json_object_array_get_idx(json_map_ary, i);
33 if( json_object_is_type( j_elem, json_type_object) )
36 struct json_object* key = NULL;
37 struct json_object* value = NULL;
38 if( json_object_object_get_ex(j_elem, "key", &key)
39 && json_object_object_get_ex(j_elem, "value", &value) )
41 if( json_object_is_type(key, json_type_int) )
43 uint32_t req_key = (uint32_t)json_object_get_int(key);
47 case naviapi::NAVICORE_LATITUDE:
48 ret[req_key]._double = json_object_get_double(value);
51 case naviapi::NAVICORE_LONGITUDE:
52 ret[req_key]._double = json_object_get_double(value);
55 case naviapi::NAVICORE_TIMESTAMP:
56 ret[req_key]._uint32_t = (uint32_t)json_object_get_int(value);
59 case naviapi::NAVICORE_HEADING:
60 ret[req_key]._uint32_t = (uint32_t)json_object_get_int(value);
63 case naviapi::NAVICORE_SPEED:
64 ret[req_key]._int32_t = json_object_get_int(value);
67 case naviapi::NAVICORE_SIMULATION_MODE:
68 ret[req_key]._bool = json_object_get_boolean(value);
72 TRACE_WARN("unknown key type.\n");
80 TRACE_WARN("element type is not object.\n");
87 TRACE_WARN("response type is not array.\n");
91 json_object_put(json_obj);
96 * @brief Response analysis of navicore_getallroutes
97 * @param res_json JSON string of response
98 * @return Route handle array
100 std::vector< uint32_t > JsonResponseAnalyzer::AnalyzeResponseGetAllRoutes( std::string& res_json )
102 std::vector< uint32_t > routeList;
104 TRACE_DEBUG("AnalyzeResponseGetAllRoutes json_obj:\n%s\n", json_object_to_json_string(json_obj));
106 // convert to Json Object
107 struct json_object *json_obj = json_tokener_parse( res_json.c_str() );
110 struct json_object *json_route_ary = NULL;
111 if( json_object_object_get_ex(json_obj, "response", &json_route_ary) )
113 // Check if the response is array information
114 if( json_object_is_type(json_route_ary, json_type_array) )
116 for (int i = 0; i < json_object_array_length(json_route_ary); ++i)
118 struct json_object* j_elem = json_object_array_get_idx(json_route_ary, i);
120 // Check that it is an element
121 struct json_object *json_route_obj = NULL;
122 if( json_object_object_get_ex(j_elem, "route", &json_route_obj) )
124 // Check if it is an int value
125 if( json_object_is_type(json_route_obj, json_type_int) )
127 uint32_t routeHandle = (uint32_t)json_object_get_int(json_route_obj);
128 routeList.push_back( routeHandle );
132 TRACE_WARN("route value is not integer.\n");
138 TRACE_WARN("key route is not found.\n");
145 TRACE_WARN("response type is not array.\n");
149 json_object_put(json_obj);
154 * @brief Response analysis of navicore_createroute
155 * @param res_json JSON string of response
156 * @return Route handle
158 uint32_t JsonResponseAnalyzer::AnalyzeResponseCreateRoute( std::string& res_json )
160 uint32_t routeHandle = 0;
162 TRACE_DEBUG("AnalyzeResponseCreateRoute json_obj:\n%s\n", json_object_to_json_string(json_obj));
164 // convert to Json Object
165 struct json_object *json_obj = json_tokener_parse( res_json.c_str() );
168 struct json_object *json_root_obj = NULL;
169 struct json_object *json_route_obj = NULL;
170 if( json_object_object_get_ex(json_obj, "response", &json_root_obj)
171 && json_object_object_get_ex(json_root_obj, "route", &json_route_obj) )
173 // Check if the response is array information
174 if( json_object_is_type(json_route_obj, json_type_int) )
177 routeHandle = (uint32_t)json_object_get_int(json_route_obj);
181 TRACE_WARN("response type is not integer.\n");
185 json_object_put(json_obj);
190 * @brief Response analysis of navicore_getallsessions
191 * @param res_json JSON string of response
192 * @return Map of session information
194 std::map<uint32_t, std::string> JsonResponseAnalyzer::AnalyzeResponseGetAllSessions( std::string& res_json )
196 std::map<uint32_t, std::string> session_map;
198 TRACE_DEBUG("AnalyzeResponseGetAllSessions json_obj:\n%s\n", json_object_to_json_string(json_obj));
200 // convert to Json Object
201 struct json_object *json_obj = json_tokener_parse( res_json.c_str() );
204 struct json_object *json_map_ary = NULL;
205 if( json_object_object_get_ex(json_obj, "response", &json_map_ary) )
207 // Check if the response is array information
208 if( json_object_is_type(json_map_ary, json_type_array) )
210 for (int i = 0; i < json_object_array_length(json_map_ary); ++i)
212 struct json_object* j_elem = json_object_array_get_idx(json_map_ary, i);
214 if( json_object_is_type( j_elem, json_type_object) )
217 struct json_object* handle = NULL;
218 struct json_object* client = NULL;
219 if( json_object_object_get_ex(j_elem, "sessionHandle", &handle)
220 && json_object_object_get_ex(j_elem, "client", &client) )
222 if( json_object_is_type(handle, json_type_int)
223 && json_object_is_type(client, json_type_string) )
225 uint32_t sessionHandle = (uint32_t)json_object_get_int(handle);
226 std::string clientName = std::string( json_object_get_string(client) );
229 session_map[sessionHandle] = clientName;
233 TRACE_WARN("invalid response.\n");
240 TRACE_WARN("element type is not object.\n");
247 TRACE_WARN("response type is not array.\n");
251 json_object_put(json_obj);