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.
JSONObject.cpp
00001 #include "JSONObject.h" 00002 00003 int __jsoneq(const char *json, jsmntok_t *tok, const char *s) 00004 { 00005 if (tok->type == JSMN_STRING && (int)strlen(s) == tok->end - tok->start && 00006 strncmp(json + tok->start, s, tok->end - tok->start) == 0) 00007 { 00008 return 0; 00009 } 00010 return -1; 00011 } 00012 00013 JSONObject *JSONObject_CreateObject(void) 00014 { 00015 JSONObject *returnObject = (JSONObject *)malloc(sizeof(JSONObject)); 00016 00017 returnObject->tokens_counter = 0; 00018 jsmn_init(&(returnObject->parser)); 00019 00020 return returnObject; 00021 } 00022 00023 JSONObject_OperationResultCode JSONObject_Parse(JSONObject *json_object, const char *json_string) 00024 { 00025 json_object->json_string = json_string; 00026 json_object->tokens_counter = jsmn_parse(&(json_object->parser), json_string, strlen(json_string), (json_object->tokens), sizeof(json_object->tokens) / sizeof((json_object->tokens)[0])); 00027 00028 if (json_object->tokens_counter < 0) 00029 { 00030 return OPERATION_FAIL; 00031 } 00032 else if (json_object->tokens_counter < 1 || (json_object->tokens)[0].type != JSMN_OBJECT) 00033 { 00034 return OPERATION_FAIL; 00035 } 00036 else 00037 { 00038 return OPERATION_SUCCESS; 00039 } 00040 } 00041 00042 const char *JSONObject_GetOperationResultName(JSONObject_OperationResultCode code) 00043 { 00044 static const char *JSONObject_OperationResultCodeName[] = { 00045 "OPERATION_FAIL", 00046 "OPERATION_SUCCESS"}; 00047 00048 return JSONObject_OperationResultCodeName[code]; 00049 } 00050 00051 JSONObject_OperationResultCode JSONObject_GetString(JSONObject *json_object, const char *key, char **returnValue) 00052 { 00053 int i = 1; 00054 *returnValue = (char *)malloc(sizeof(char) * NUM_OF_MAX_STRING_BUFFER); 00055 00056 for (i = 1; i < json_object->tokens_counter; i++) 00057 { 00058 if (__jsoneq(json_object->json_string, &(json_object->tokens[i]), key) == 0) 00059 { 00060 sprintf(*returnValue, "%.*s", json_object->tokens[i + 1].end - json_object->tokens[i + 1].start, json_object->json_string + json_object->tokens[i + 1].start); 00061 break; 00062 } 00063 } 00064 00065 if (i == json_object->tokens_counter) 00066 { 00067 return OPERATION_FAIL; 00068 } 00069 else 00070 { 00071 return OPERATION_SUCCESS; 00072 } 00073 } 00074 00075 JSONObject_OperationResultCode JSONObject_GetFloat(JSONObject *json_object, const char *key, float *returnValue) 00076 { 00077 char *strValue; 00078 JSONObject_OperationResultCode res = JSONObject_GetString(json_object, key, &strValue); 00079 00080 if (res == OPERATION_SUCCESS) 00081 { 00082 *returnValue = atof(strValue); 00083 return OPERATION_SUCCESS; 00084 } 00085 else 00086 { 00087 return OPERATION_FAIL; 00088 } 00089 } 00090 00091 JSONObject_OperationResultCode JSONObject_GetInt(JSONObject *json_object, const char *key, int *returnValue) 00092 { 00093 char *strValue; 00094 JSONObject_OperationResultCode res = JSONObject_GetString(json_object, key, &strValue); 00095 00096 if (res == OPERATION_SUCCESS) 00097 { 00098 *returnValue = atoi(strValue); 00099 return OPERATION_SUCCESS; 00100 } 00101 else 00102 { 00103 return OPERATION_FAIL; 00104 } 00105 } 00106 00107 JSONObject_OperationResultCode JSONObject_GetLong(JSONObject *json_object, const char *key, long *returnValue) 00108 { 00109 char *strValue; 00110 JSONObject_OperationResultCode res = JSONObject_GetString(json_object, key, &strValue); 00111 00112 if (res == OPERATION_SUCCESS) 00113 { 00114 *returnValue = atol(strValue); 00115 return OPERATION_SUCCESS; 00116 } 00117 else 00118 { 00119 return OPERATION_FAIL; 00120 } 00121 } 00122 00123 JSONObject_OperationResultCode JSONObject_GetBool(JSONObject *json_object, const char *key, __boolType *returnValue) 00124 { 00125 char *strValue; 00126 JSONObject_OperationResultCode res = JSONObject_GetString(json_object, key, &strValue); 00127 00128 if (res == OPERATION_SUCCESS) 00129 { 00130 if (strcmp(strValue, "true") == 0) 00131 { 00132 *returnValue = __boolTrueValue; 00133 } 00134 else if (strcmp(strValue, "false") == 0) 00135 { 00136 *returnValue = __boolFalseValue; 00137 } 00138 else 00139 { 00140 return OPERATION_FAIL; 00141 } 00142 00143 return OPERATION_SUCCESS; 00144 } 00145 else 00146 { 00147 return OPERATION_FAIL; 00148 } 00149 }
Generated on Fri Jul 15 2022 22:07:25 by
1.7.2