json_token.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright (C) 2012-2019 UCloud. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License").
  5. * You may not use this file except in compliance with the License.
  6. * A copy of the License is located at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * or in the "license" file accompanying this file. This file is distributed
  11. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. * express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. #include "json_parser.h"
  16. #include "lite-utils.h"
  17. #include "uiot_import.h"
  18. #include "uiot_defs.h"
  19. char *LITE_json_value_of(char *key, char *src)
  20. {
  21. char *value = NULL;
  22. int value_len = -1;
  23. char *ret = NULL;
  24. char *delim = NULL;
  25. char *key_iter;
  26. char *key_next;
  27. int key_len;
  28. char *src_iter;
  29. src_iter = src;
  30. key_iter = key;
  31. do {
  32. if ((delim = strchr(key_iter, '.')) != NULL) {
  33. key_len = delim - key_iter;
  34. key_next = HAL_Malloc(key_len + 1);
  35. strncpy(key_next, key_iter, key_len);
  36. key_next[key_len] = '\0';
  37. value = json_get_value_by_name(src_iter, strlen(src_iter), key_next, &value_len, 0);
  38. if (value == NULL) {
  39. HAL_Free(key_next);
  40. return NULL;
  41. }
  42. src_iter = value;
  43. key_iter = delim + 1;
  44. HAL_Free(key_next);
  45. }
  46. } while (delim);
  47. value = json_get_value_by_name(src_iter, strlen(src_iter), key_iter, &value_len, 0);
  48. if (NULL == value) {
  49. return NULL;
  50. }
  51. ret = HAL_Malloc((value_len + 1) * sizeof(char));
  52. if (NULL == ret) {
  53. return NULL;
  54. }
  55. HAL_Snprintf(ret, value_len + 1, "%s", value);
  56. return ret;
  57. }
  58. list_head_t *LITE_json_keys_of(char *src, char *prefix)
  59. {
  60. static LIST_HEAD(keylist);
  61. char *pos = 0, *key = 0, *val = 0;
  62. int klen = 0, vlen = 0, vtype = 0;
  63. if (src == NULL || prefix == NULL) {
  64. return NULL;
  65. }
  66. if (!strcmp("", prefix)) {
  67. INIT_LIST_HEAD(&keylist);
  68. }
  69. json_object_for_each_kv(src, pos, key, klen, val, vlen, vtype) {
  70. if (key && klen && val && vlen) {
  71. json_key_t *entry = NULL;
  72. entry = HAL_Malloc(sizeof(json_key_t));
  73. memset(entry, 0, sizeof(json_key_t));
  74. entry->key = LITE_format_string("%s%.*s", prefix, klen, key);
  75. list_add_tail(&entry->list, &keylist);
  76. if (JSOBJECT == vtype) {
  77. char *iter_val = LITE_format_string("%.*s", vlen, val);
  78. char *iter_pre = LITE_format_string("%s%.*s.", prefix, klen, key);
  79. LITE_json_keys_of(iter_val, iter_pre);
  80. HAL_Free(iter_val);
  81. HAL_Free(iter_pre);
  82. }
  83. }
  84. }
  85. if (!strcmp("", prefix)) {
  86. json_key_t *entry = NULL;
  87. entry = HAL_Malloc(sizeof(json_key_t));
  88. memset(entry, 0, sizeof(json_key_t));
  89. list_add_tail(&entry->list, &keylist);
  90. return &keylist;
  91. }
  92. return NULL;
  93. }
  94. void LITE_json_keys_release(list_head_t *keylist)
  95. {
  96. json_key_t *pos, *tmp;
  97. list_for_each_entry_safe(pos, tmp, keylist, list, json_key_t) {
  98. if (pos->key) {
  99. HAL_Free(pos->key);
  100. }
  101. list_del(&pos->list);
  102. HAL_Free(pos);
  103. }
  104. }
  105. int LITE_get_int32(int32_t *value, char *src) {
  106. return (sscanf(src, "%" SCNi32, value) == 1) ? SUCCESS_RET : FAILURE_RET;
  107. }
  108. int LITE_get_int16(int16_t *value, char *src) {
  109. return (sscanf(src, "%" SCNi16, value) == 1) ? SUCCESS_RET : FAILURE_RET;
  110. }
  111. /* NOTICE: scanning 8-bit types requires use of the hh specifier
  112. * which is only supported on newlib platforms that
  113. * are built with C99 I/O format support enabled. If the flag in
  114. * newlib.h hasn't been set during configuration to indicate this, the 8-bit
  115. * scanning format macros are disabled here as they result in undefined
  116. * behaviour which can include memory overwrite. Overriding the flag after the
  117. * library has been built is not recommended as it will expose the underlying
  118. * undefined behaviour.so we use int16 and uint16 transfer to int8 and uint8
  119. */
  120. int LITE_get_int8(int8_t *value, char *src) {
  121. int16_t temp = 0;
  122. if(1 != sscanf(src, "%" SCNi16, &temp))
  123. {
  124. return FAILURE_RET;
  125. }
  126. *value = (int8_t)temp;
  127. return SUCCESS_RET;
  128. }
  129. int LITE_get_uint32(uint32_t *value, char *src) {
  130. return (sscanf(src, "%" SCNu32, value) == 1) ? SUCCESS_RET : FAILURE_RET;
  131. }
  132. int LITE_get_uint16(uint16_t *value, char *src) {
  133. return (sscanf(src, "%" SCNu16, value) == 1) ? SUCCESS_RET : FAILURE_RET;
  134. }
  135. int LITE_get_uint8(uint8_t *value, char *src) {
  136. uint16_t temp = 0;
  137. if(1 != sscanf(src, "%" SCNu16, &temp))
  138. {
  139. return FAILURE_RET;
  140. }
  141. *value = (uint8_t)temp;
  142. return SUCCESS_RET;
  143. }
  144. int LITE_get_float(float *value, char *src) {
  145. return (sscanf(src, "%f", value) == 1) ? SUCCESS_RET : FAILURE_RET;
  146. }
  147. int LITE_get_double(double *value, char *src) {
  148. return (sscanf(src, "%lf", value) == 1) ? SUCCESS_RET : FAILURE_RET;
  149. }
  150. int LITE_get_boolean(bool *value, char *src) {
  151. if (!strcmp(src, "0")) {
  152. *value = false;
  153. }
  154. else {
  155. *value = true;
  156. }
  157. return SUCCESS_RET;
  158. }