json_parser.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. #ifndef __JSON_PARSER_H__
  22. #define __JSON_PARSER_H__
  23. #include "lite-utils.h"
  24. /**
  25. The descriptions of the json value node type
  26. **/
  27. enum JSONTYPE { JSNONE = -1, JSSTRING = 0, JSOBJECT, JSARRAY, JSNUMBER, JSBOOLEAN, JSNULL, JSTYPEMAX };
  28. /**
  29. The error codes produced by the JSON parsers
  30. **/
  31. enum JSON_PARSE_CODE { JSON_PARSE_ERR, JSON_PARSE_OK, JSON_PARSE_FINISH };
  32. /**
  33. The return codes produced by the JSON parsers
  34. **/
  35. enum JSON_PARSE_RESULT { JSON_RESULT_ERR = -1, JSON_RESULT_OK };
  36. typedef int (*json_parse_cb)(char *p_cName, int iNameLen, char *p_cValue, int iValueLen, int iValueType,
  37. void *p_Result);
  38. /**
  39. * @brief Parse the JSON string, and iterate through all keys and values,
  40. * then handle the keys and values by callback function.
  41. *
  42. * @param[in] p_cJsonStr @n The JSON string
  43. * @param[in] iStrLen @n The JSON string length
  44. * @param[in] pfnCB @n Callback function
  45. * @param[out] p_CBData @n User data
  46. * @return JSON_RESULT_OK success, JSON_RESULT_ERR failed
  47. * @see None.
  48. * @note None.
  49. **/
  50. int json_parse_name_value(char *p_cJsonStr, int iStrLen, json_parse_cb pfnCB, void *p_CBData);
  51. /**
  52. * @brief Get the value by a specified key from a json string
  53. *
  54. * @param[in] p_cJsonStr @n the JSON string
  55. * @param[in] iStrLen @n the JSON string length
  56. * @param[in] p_cName @n the specified key string
  57. * @param[out] p_iValueLen @n the value length
  58. * @param[out] p_iValueType @n the value type
  59. * @return A pointer to the value
  60. * @see None.
  61. * @note None.
  62. **/
  63. char *json_get_value_by_name(char *p_cJsonStr, int iStrLen, char *p_cName, int *p_iValueLen, int *p_iValueType);
  64. /**
  65. * @brief Get the JSON object point associate with a given type.
  66. *
  67. * @param[in] type @n The object type
  68. * @param[in] str @n The JSON string
  69. * @returns The json object point with the given field type.
  70. * @see None.
  71. * @note None.
  72. */
  73. char *json_get_object(int type, char *str);
  74. char *json_get_next_object(int type, char *str, char **key, int *key_len, char **val, int *val_len, int *val_type);
  75. /**
  76. * @brief retrieve each key&value pair from the json string
  77. *
  78. * @param[in] str @n Json string to revolve
  79. * @param[in] pos @n cursor
  80. * @param[out] key @n pointer to the next Key object
  81. * @param[out] klen @n Key object length
  82. * @param[out] val @n pointer to the next Value object
  83. * @param[out] vlen @n Value object length
  84. * @param[out] vtype @n Value object type(digital, string, object, array)
  85. * @see None.
  86. * @note None.
  87. */
  88. #define json_object_for_each_kv(str, pos, key, klen, val, vlen, vtype) \
  89. for (pos = json_get_object(JSOBJECT, str); \
  90. pos != 0 && *pos != 0 && (pos = json_get_next_object(JSOBJECT, pos, &key, &klen, &val, &vlen, &vtype)) != 0;)
  91. /**
  92. * @brief retrieve each entry from the json array
  93. *
  94. * @param[in] str @n Json array to revolve
  95. * @param[in] pos @n cursor
  96. * @param[out] entry @n pointer to the next entry from the array
  97. * @param[out] len @n entry length
  98. * @param[out] type @n entry type(digital, string, object, array)
  99. * @see None.
  100. * @note None.
  101. */
  102. #define json_array_for_each_entry(str, pos, entry, len, type) \
  103. for (pos = json_get_object(JSARRAY, str); \
  104. pos != 0 && *pos != 0 && (pos = json_get_next_object(JSARRAY, ++pos, 0, 0, &entry, &len, &type)) != 0;)
  105. /**
  106. * @brief backup the last character to register parameters,
  107. * and set the end character with '\0'
  108. *
  109. * @param[in] json_str @n json string
  110. * @param[in] str_len @n json string lenth
  111. * @param[out] register @n used to backup the last character
  112. * @see None.
  113. * @note None.
  114. */
  115. #define backup_json_str_last_char(json_str, str_len, register) \
  116. { \
  117. register = *((char *)json_str + str_len); \
  118. *((char *)json_str + str_len) = '\0'; \
  119. }
  120. /**
  121. * @brief restore the last character from register parameters
  122. *
  123. * @param[in] json_str @n json string
  124. * @param[in] str_len @n json string lenth
  125. * @param[in] register @n used to restore the last character
  126. * @see None.
  127. * @note None.
  128. */
  129. #define restore_json_str_last_char(json_str, str_len, register) \
  130. { \
  131. *((char *)json_str + str_len) = register; \
  132. }
  133. #endif /* __JSON_PARSER_H__ */