JSON Library - modified from jsmn library

Committer:
nguyenmanhthao996tn
Date:
Sat Sep 30 09:11:57 2017 +0000
Revision:
0:3c44784c513d
Worked version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nguyenmanhthao996tn 0:3c44784c513d 1 #include "JSONObject.h"
nguyenmanhthao996tn 0:3c44784c513d 2
nguyenmanhthao996tn 0:3c44784c513d 3 int __jsoneq(const char *json, jsmntok_t *tok, const char *s)
nguyenmanhthao996tn 0:3c44784c513d 4 {
nguyenmanhthao996tn 0:3c44784c513d 5 if (tok->type == JSMN_STRING && (int)strlen(s) == tok->end - tok->start &&
nguyenmanhthao996tn 0:3c44784c513d 6 strncmp(json + tok->start, s, tok->end - tok->start) == 0)
nguyenmanhthao996tn 0:3c44784c513d 7 {
nguyenmanhthao996tn 0:3c44784c513d 8 return 0;
nguyenmanhthao996tn 0:3c44784c513d 9 }
nguyenmanhthao996tn 0:3c44784c513d 10 return -1;
nguyenmanhthao996tn 0:3c44784c513d 11 }
nguyenmanhthao996tn 0:3c44784c513d 12
nguyenmanhthao996tn 0:3c44784c513d 13 JSONObject *JSONObject_CreateObject(void)
nguyenmanhthao996tn 0:3c44784c513d 14 {
nguyenmanhthao996tn 0:3c44784c513d 15 JSONObject *returnObject = (JSONObject *)malloc(sizeof(JSONObject));
nguyenmanhthao996tn 0:3c44784c513d 16
nguyenmanhthao996tn 0:3c44784c513d 17 returnObject->tokens_counter = 0;
nguyenmanhthao996tn 0:3c44784c513d 18 jsmn_init(&(returnObject->parser));
nguyenmanhthao996tn 0:3c44784c513d 19
nguyenmanhthao996tn 0:3c44784c513d 20 return returnObject;
nguyenmanhthao996tn 0:3c44784c513d 21 }
nguyenmanhthao996tn 0:3c44784c513d 22
nguyenmanhthao996tn 0:3c44784c513d 23 JSONObject_OperationResultCode JSONObject_Parse(JSONObject *json_object, const char *json_string)
nguyenmanhthao996tn 0:3c44784c513d 24 {
nguyenmanhthao996tn 0:3c44784c513d 25 json_object->json_string = json_string;
nguyenmanhthao996tn 0:3c44784c513d 26 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]));
nguyenmanhthao996tn 0:3c44784c513d 27
nguyenmanhthao996tn 0:3c44784c513d 28 if (json_object->tokens_counter < 0)
nguyenmanhthao996tn 0:3c44784c513d 29 {
nguyenmanhthao996tn 0:3c44784c513d 30 return OPERATION_FAIL;
nguyenmanhthao996tn 0:3c44784c513d 31 }
nguyenmanhthao996tn 0:3c44784c513d 32 else if (json_object->tokens_counter < 1 || (json_object->tokens)[0].type != JSMN_OBJECT)
nguyenmanhthao996tn 0:3c44784c513d 33 {
nguyenmanhthao996tn 0:3c44784c513d 34 return OPERATION_FAIL;
nguyenmanhthao996tn 0:3c44784c513d 35 }
nguyenmanhthao996tn 0:3c44784c513d 36 else
nguyenmanhthao996tn 0:3c44784c513d 37 {
nguyenmanhthao996tn 0:3c44784c513d 38 return OPERATION_SUCCESS;
nguyenmanhthao996tn 0:3c44784c513d 39 }
nguyenmanhthao996tn 0:3c44784c513d 40 }
nguyenmanhthao996tn 0:3c44784c513d 41
nguyenmanhthao996tn 0:3c44784c513d 42 const char *JSONObject_GetOperationResultName(JSONObject_OperationResultCode code)
nguyenmanhthao996tn 0:3c44784c513d 43 {
nguyenmanhthao996tn 0:3c44784c513d 44 static const char *JSONObject_OperationResultCodeName[] = {
nguyenmanhthao996tn 0:3c44784c513d 45 "OPERATION_FAIL",
nguyenmanhthao996tn 0:3c44784c513d 46 "OPERATION_SUCCESS"};
nguyenmanhthao996tn 0:3c44784c513d 47
nguyenmanhthao996tn 0:3c44784c513d 48 return JSONObject_OperationResultCodeName[code];
nguyenmanhthao996tn 0:3c44784c513d 49 }
nguyenmanhthao996tn 0:3c44784c513d 50
nguyenmanhthao996tn 0:3c44784c513d 51 JSONObject_OperationResultCode JSONObject_GetString(JSONObject *json_object, const char *key, char **returnValue)
nguyenmanhthao996tn 0:3c44784c513d 52 {
nguyenmanhthao996tn 0:3c44784c513d 53 int i = 1;
nguyenmanhthao996tn 0:3c44784c513d 54 *returnValue = (char *)malloc(sizeof(char) * NUM_OF_MAX_STRING_BUFFER);
nguyenmanhthao996tn 0:3c44784c513d 55
nguyenmanhthao996tn 0:3c44784c513d 56 for (i = 1; i < json_object->tokens_counter; i++)
nguyenmanhthao996tn 0:3c44784c513d 57 {
nguyenmanhthao996tn 0:3c44784c513d 58 if (__jsoneq(json_object->json_string, &(json_object->tokens[i]), key) == 0)
nguyenmanhthao996tn 0:3c44784c513d 59 {
nguyenmanhthao996tn 0:3c44784c513d 60 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);
nguyenmanhthao996tn 0:3c44784c513d 61 break;
nguyenmanhthao996tn 0:3c44784c513d 62 }
nguyenmanhthao996tn 0:3c44784c513d 63 }
nguyenmanhthao996tn 0:3c44784c513d 64
nguyenmanhthao996tn 0:3c44784c513d 65 if (i == json_object->tokens_counter)
nguyenmanhthao996tn 0:3c44784c513d 66 {
nguyenmanhthao996tn 0:3c44784c513d 67 return OPERATION_FAIL;
nguyenmanhthao996tn 0:3c44784c513d 68 }
nguyenmanhthao996tn 0:3c44784c513d 69 else
nguyenmanhthao996tn 0:3c44784c513d 70 {
nguyenmanhthao996tn 0:3c44784c513d 71 return OPERATION_SUCCESS;
nguyenmanhthao996tn 0:3c44784c513d 72 }
nguyenmanhthao996tn 0:3c44784c513d 73 }
nguyenmanhthao996tn 0:3c44784c513d 74
nguyenmanhthao996tn 0:3c44784c513d 75 JSONObject_OperationResultCode JSONObject_GetFloat(JSONObject *json_object, const char *key, float *returnValue)
nguyenmanhthao996tn 0:3c44784c513d 76 {
nguyenmanhthao996tn 0:3c44784c513d 77 char *strValue;
nguyenmanhthao996tn 0:3c44784c513d 78 JSONObject_OperationResultCode res = JSONObject_GetString(json_object, key, &strValue);
nguyenmanhthao996tn 0:3c44784c513d 79
nguyenmanhthao996tn 0:3c44784c513d 80 if (res == OPERATION_SUCCESS)
nguyenmanhthao996tn 0:3c44784c513d 81 {
nguyenmanhthao996tn 0:3c44784c513d 82 *returnValue = atof(strValue);
nguyenmanhthao996tn 0:3c44784c513d 83 return OPERATION_SUCCESS;
nguyenmanhthao996tn 0:3c44784c513d 84 }
nguyenmanhthao996tn 0:3c44784c513d 85 else
nguyenmanhthao996tn 0:3c44784c513d 86 {
nguyenmanhthao996tn 0:3c44784c513d 87 return OPERATION_FAIL;
nguyenmanhthao996tn 0:3c44784c513d 88 }
nguyenmanhthao996tn 0:3c44784c513d 89 }
nguyenmanhthao996tn 0:3c44784c513d 90
nguyenmanhthao996tn 0:3c44784c513d 91 JSONObject_OperationResultCode JSONObject_GetInt(JSONObject *json_object, const char *key, int *returnValue)
nguyenmanhthao996tn 0:3c44784c513d 92 {
nguyenmanhthao996tn 0:3c44784c513d 93 char *strValue;
nguyenmanhthao996tn 0:3c44784c513d 94 JSONObject_OperationResultCode res = JSONObject_GetString(json_object, key, &strValue);
nguyenmanhthao996tn 0:3c44784c513d 95
nguyenmanhthao996tn 0:3c44784c513d 96 if (res == OPERATION_SUCCESS)
nguyenmanhthao996tn 0:3c44784c513d 97 {
nguyenmanhthao996tn 0:3c44784c513d 98 *returnValue = atoi(strValue);
nguyenmanhthao996tn 0:3c44784c513d 99 return OPERATION_SUCCESS;
nguyenmanhthao996tn 0:3c44784c513d 100 }
nguyenmanhthao996tn 0:3c44784c513d 101 else
nguyenmanhthao996tn 0:3c44784c513d 102 {
nguyenmanhthao996tn 0:3c44784c513d 103 return OPERATION_FAIL;
nguyenmanhthao996tn 0:3c44784c513d 104 }
nguyenmanhthao996tn 0:3c44784c513d 105 }
nguyenmanhthao996tn 0:3c44784c513d 106
nguyenmanhthao996tn 0:3c44784c513d 107 JSONObject_OperationResultCode JSONObject_GetLong(JSONObject *json_object, const char *key, long *returnValue)
nguyenmanhthao996tn 0:3c44784c513d 108 {
nguyenmanhthao996tn 0:3c44784c513d 109 char *strValue;
nguyenmanhthao996tn 0:3c44784c513d 110 JSONObject_OperationResultCode res = JSONObject_GetString(json_object, key, &strValue);
nguyenmanhthao996tn 0:3c44784c513d 111
nguyenmanhthao996tn 0:3c44784c513d 112 if (res == OPERATION_SUCCESS)
nguyenmanhthao996tn 0:3c44784c513d 113 {
nguyenmanhthao996tn 0:3c44784c513d 114 *returnValue = atol(strValue);
nguyenmanhthao996tn 0:3c44784c513d 115 return OPERATION_SUCCESS;
nguyenmanhthao996tn 0:3c44784c513d 116 }
nguyenmanhthao996tn 0:3c44784c513d 117 else
nguyenmanhthao996tn 0:3c44784c513d 118 {
nguyenmanhthao996tn 0:3c44784c513d 119 return OPERATION_FAIL;
nguyenmanhthao996tn 0:3c44784c513d 120 }
nguyenmanhthao996tn 0:3c44784c513d 121 }
nguyenmanhthao996tn 0:3c44784c513d 122
nguyenmanhthao996tn 0:3c44784c513d 123 JSONObject_OperationResultCode JSONObject_GetBool(JSONObject *json_object, const char *key, __boolType *returnValue)
nguyenmanhthao996tn 0:3c44784c513d 124 {
nguyenmanhthao996tn 0:3c44784c513d 125 char *strValue;
nguyenmanhthao996tn 0:3c44784c513d 126 JSONObject_OperationResultCode res = JSONObject_GetString(json_object, key, &strValue);
nguyenmanhthao996tn 0:3c44784c513d 127
nguyenmanhthao996tn 0:3c44784c513d 128 if (res == OPERATION_SUCCESS)
nguyenmanhthao996tn 0:3c44784c513d 129 {
nguyenmanhthao996tn 0:3c44784c513d 130 if (strcmp(strValue, "true") == 0)
nguyenmanhthao996tn 0:3c44784c513d 131 {
nguyenmanhthao996tn 0:3c44784c513d 132 *returnValue = __boolTrueValue;
nguyenmanhthao996tn 0:3c44784c513d 133 }
nguyenmanhthao996tn 0:3c44784c513d 134 else if (strcmp(strValue, "false") == 0)
nguyenmanhthao996tn 0:3c44784c513d 135 {
nguyenmanhthao996tn 0:3c44784c513d 136 *returnValue = __boolFalseValue;
nguyenmanhthao996tn 0:3c44784c513d 137 }
nguyenmanhthao996tn 0:3c44784c513d 138 else
nguyenmanhthao996tn 0:3c44784c513d 139 {
nguyenmanhthao996tn 0:3c44784c513d 140 return OPERATION_FAIL;
nguyenmanhthao996tn 0:3c44784c513d 141 }
nguyenmanhthao996tn 0:3c44784c513d 142
nguyenmanhthao996tn 0:3c44784c513d 143 return OPERATION_SUCCESS;
nguyenmanhthao996tn 0:3c44784c513d 144 }
nguyenmanhthao996tn 0:3c44784c513d 145 else
nguyenmanhthao996tn 0:3c44784c513d 146 {
nguyenmanhthao996tn 0:3c44784c513d 147 return OPERATION_FAIL;
nguyenmanhthao996tn 0:3c44784c513d 148 }
nguyenmanhthao996tn 0:3c44784c513d 149 }