json_parser.h 4.7 KB

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