json_token.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Copyright (c) 2017-2019 Tencent Group. All rights reserved.
  3. * License-Identifier: Apache-2.0
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  6. * not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. /**
  19. * Edit by shockcao@tencent.com 2018/3/15
  20. */
  21. #include "json_parser.h"
  22. #include "lite-utils.h"
  23. #include "qcloud_iot_export_error.h"
  24. #ifndef SCNi8
  25. #define SCNi8 "hhi"
  26. #endif
  27. #ifndef SCNu8
  28. #define SCNu8 "hhu"
  29. #endif
  30. char *LITE_json_value_of(char *key, char *src)
  31. {
  32. char *value = NULL;
  33. int value_len = -1;
  34. char *ret = NULL;
  35. char *delim = NULL;
  36. char *key_iter;
  37. char *key_next;
  38. int key_len;
  39. char *src_iter;
  40. src_iter = src;
  41. key_iter = key;
  42. do {
  43. if ((delim = strchr(key_iter, '.')) != NULL) {
  44. key_len = delim - key_iter;
  45. key_next = HAL_Malloc(key_len + 1);
  46. strncpy(key_next, key_iter, key_len);
  47. key_next[key_len] = '\0';
  48. value = json_get_value_by_name(src_iter, strlen(src_iter), key_next, &value_len, 0);
  49. if (value == NULL) {
  50. HAL_Free(key_next);
  51. return NULL;
  52. }
  53. src_iter = value;
  54. key_iter = delim + 1;
  55. HAL_Free(key_next);
  56. }
  57. } while (delim);
  58. value = json_get_value_by_name(src_iter, strlen(src_iter), key_iter, &value_len, 0);
  59. if (NULL == value) {
  60. return NULL;
  61. }
  62. ret = HAL_Malloc((value_len + 1) * sizeof(char));
  63. if (NULL == ret) {
  64. return NULL;
  65. }
  66. HAL_Snprintf(ret, value_len + 1, "%s", value);
  67. return ret;
  68. }
  69. list_head_t *LITE_json_keys_of(char *src, char *prefix)
  70. {
  71. static LIST_HEAD(keylist);
  72. char *pos = 0, *key = 0, *val = 0;
  73. int klen = 0, vlen = 0, vtype = 0;
  74. if (src == NULL || prefix == NULL) {
  75. return NULL;
  76. }
  77. if (!strcmp("", prefix)) {
  78. INIT_LIST_HEAD(&keylist);
  79. }
  80. json_object_for_each_kv(src, pos, key, klen, val, vlen, vtype)
  81. {
  82. if (key && klen && val && vlen) {
  83. json_key_t *entry = NULL;
  84. entry = HAL_Malloc(sizeof(json_key_t));
  85. memset(entry, 0, sizeof(json_key_t));
  86. entry->key = LITE_format_string("%s%.*s", prefix, klen, key);
  87. list_add_tail(&entry->list, &keylist);
  88. if (JSOBJECT == vtype) {
  89. char *iter_val = LITE_format_string("%.*s", vlen, val);
  90. char *iter_pre = LITE_format_string("%s%.*s.", prefix, klen, key);
  91. LITE_json_keys_of(iter_val, iter_pre);
  92. HAL_Free(iter_val);
  93. HAL_Free(iter_pre);
  94. }
  95. }
  96. }
  97. if (!strcmp("", prefix)) {
  98. json_key_t *entry = NULL;
  99. entry = HAL_Malloc(sizeof(json_key_t));
  100. memset(entry, 0, sizeof(json_key_t));
  101. list_add_tail(&entry->list, &keylist);
  102. return &keylist;
  103. }
  104. return NULL;
  105. }
  106. void LITE_json_keys_release(list_head_t *keylist)
  107. {
  108. json_key_t *pos, *tmp;
  109. list_for_each_entry_safe(pos, tmp, keylist, list, json_key_t)
  110. {
  111. if (pos->key) {
  112. HAL_Free(pos->key);
  113. }
  114. list_del(&pos->list);
  115. HAL_Free(pos);
  116. }
  117. }
  118. static void _strip_transfer(char *src)
  119. {
  120. char *end = src + strlen(src) + 1;
  121. while (*src != '\0') {
  122. if (*src == '\\') {
  123. memmove(src, src + 1, end - src);
  124. end--;
  125. }
  126. src++;
  127. }
  128. }
  129. char *LITE_json_string_value_strip_transfer(char *key, char *src)
  130. {
  131. char *str = LITE_json_value_of(key, src);
  132. if (NULL != str) {
  133. _strip_transfer(str);
  134. }
  135. return str;
  136. }
  137. int LITE_get_int32(int32_t *value, char *src)
  138. {
  139. return (sscanf(src, "%" SCNi32, value) == 1) ? QCLOUD_RET_SUCCESS : QCLOUD_ERR_FAILURE;
  140. }
  141. int LITE_get_int16(int16_t *value, char *src)
  142. {
  143. return (sscanf(src, "%" SCNi16, value) == 1) ? QCLOUD_RET_SUCCESS : QCLOUD_ERR_FAILURE;
  144. }
  145. int LITE_get_int8(int8_t *value, char *src)
  146. {
  147. return (sscanf(src, "%" SCNi8, value) == 1) ? QCLOUD_RET_SUCCESS : QCLOUD_ERR_FAILURE;
  148. }
  149. int LITE_get_uint32(uint32_t *value, char *src)
  150. {
  151. return (sscanf(src, "%" SCNu32, value) == 1) ? QCLOUD_RET_SUCCESS : QCLOUD_ERR_FAILURE;
  152. }
  153. int LITE_get_uint16(uint16_t *value, char *src)
  154. {
  155. return (sscanf(src, "%" SCNu16, value) == 1) ? QCLOUD_RET_SUCCESS : QCLOUD_ERR_FAILURE;
  156. }
  157. int LITE_get_uint8(uint8_t *value, char *src)
  158. {
  159. return (sscanf(src, "%" SCNu8, value) == 1) ? QCLOUD_RET_SUCCESS : QCLOUD_ERR_FAILURE;
  160. }
  161. int LITE_get_float(float *value, char *src)
  162. {
  163. return (sscanf(src, "%f", value) == 1) ? QCLOUD_RET_SUCCESS : QCLOUD_ERR_FAILURE;
  164. }
  165. int LITE_get_double(double *value, char *src)
  166. {
  167. return (sscanf(src, "%lf", value) == 1) ? QCLOUD_RET_SUCCESS : QCLOUD_ERR_FAILURE;
  168. }
  169. int LITE_get_boolean(bool *value, char *src)
  170. {
  171. if (!strcmp(src, "false")) {
  172. *value = false;
  173. } else {
  174. *value = true;
  175. }
  176. return QCLOUD_RET_SUCCESS;
  177. }
  178. int LITE_get_string(int8_t *value, char *src, uint16_t max_len)
  179. {
  180. int rc;
  181. if (NULL != strncpy((char *)value, src, max_len)) {
  182. value[Min(strlen(src), max_len)] = '\0';
  183. rc = QCLOUD_RET_SUCCESS;
  184. } else {
  185. rc = QCLOUD_ERR_FAILURE;
  186. }
  187. return rc;
  188. }