Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: NerfUSXbee PinDetect EthernetInterface JSON MFRC522 WebSocketClient mbed-rtos mbed
jsonParser.hpp
00001 #pragma once 00002 #include "ports.hpp" 00003 #include "Json.h" 00004 #include "ServerData.hpp" 00005 00006 bool get_value_from_json(char *jsonSource, const char *keyName, char *outValue) 00007 { 00008 Json json(jsonSource, strlen(jsonSource), 40); //Can add 3rd argument if json is too big, default: 32 tokens 00009 if (!json.isValidJson()) 00010 { 00011 toPc("Invalid JSON: %s", jsonSource); 00012 return false; 00013 } 00014 if (json.type(0) != JSMN_OBJECT) 00015 { 00016 toPc("Invalid JSON. ROOT element is not Object: %s", jsonSource); 00017 return false; 00018 } 00019 int keyIndex = json.findKeyIndexIn(keyName, 0); 00020 if (keyIndex == -1) 00021 { 00022 //toPc("\"%s\" key not found in json: %s", keyName, jsonSource); 00023 return false; 00024 } 00025 int valueIndex = json.findChildIndexOf(keyIndex, -1); 00026 if (valueIndex > 0) 00027 { 00028 const char *valueStart = json.tokenAddress(valueIndex); 00029 int valueLength = json.tokenLength(valueIndex); 00030 strncpy(outValue, valueStart, valueLength); 00031 outValue[valueLength] = 0; 00032 return true; 00033 } 00034 return false; 00035 } 00036 00037 void parse_server_event_from_json(char *jsonSource, ServerEvent *returnEvent) 00038 { 00039 char data[250]; //container for the json in "data" 00040 get_value_from_json(jsonSource, "event", returnEvent->event); 00041 00042 bool data_parse_successful = get_value_from_json(jsonSource, "data", data); 00043 if (data_parse_successful) 00044 { 00045 char tmp[32]; 00046 if (get_value_from_json(data, "game_id", tmp)) 00047 returnEvent->data.game_id = atoi(tmp); 00048 00049 if (get_value_from_json(data, "max_reflex_time", tmp)) 00050 returnEvent->data.max_reflex_time = atoi(tmp); 00051 00052 if (get_value_from_json(data, "number_of_target", tmp)) 00053 returnEvent->data.number_of_target = atoi(tmp); 00054 } 00055 }
Generated on Tue Jul 12 2022 15:36:49 by
1.7.2