lwjson_debug.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /**
  2. * \file lwjson_debug.c
  3. * \brief Debug and print function for tokens
  4. */
  5. /*
  6. * Copyright (c) 2024 Tilen MAJERLE
  7. *
  8. * Permission is hereby granted, free of charge, to any person
  9. * obtaining a copy of this software and associated documentation
  10. * files (the "Software"), to deal in the Software without restriction,
  11. * including without limitation the rights to use, copy, modify, merge,
  12. * publish, distribute, sublicense, and/or sell copies of the Software,
  13. * and to permit persons to whom the Software is furnished to do so,
  14. * subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be
  17. * included in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  21. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
  22. * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  23. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  24. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  25. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  26. * OTHER DEALINGS IN THE SOFTWARE.
  27. *
  28. * This file is part of LwJSON - Lightweight JSON format parser.
  29. *
  30. * Author: Tilen MAJERLE <tilen@majerle.eu>
  31. * Version: v1.8.1
  32. */
  33. #include <stdio.h>
  34. #include <string.h>
  35. #include "lwjson.h"
  36. /**
  37. * \brief Token print instance
  38. */
  39. typedef struct {
  40. size_t indent; /*!< Indent level for token print */
  41. } lwjson_token_print_t;
  42. /**
  43. * \brief Print token value
  44. * \param[in] prt: Token print instance
  45. * \param[in] token: Token to print
  46. */
  47. static void
  48. prv_print_token(lwjson_token_print_t* prt, const lwjson_token_t* token) {
  49. #define print_indent() printf("%.*s", (int)((prt->indent)), "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t");
  50. if (token == NULL) {
  51. return;
  52. }
  53. /* Check if token has a name */
  54. print_indent();
  55. if (token->token_name != NULL) {
  56. printf("\"%.*s\":", (int)token->token_name_len, token->token_name);
  57. }
  58. /* Print different types */
  59. switch (token->type) {
  60. case LWJSON_TYPE_OBJECT:
  61. case LWJSON_TYPE_ARRAY: {
  62. printf("%c", token->type == LWJSON_TYPE_OBJECT ? '{' : '[');
  63. if (token->u.first_child != NULL) {
  64. printf("\n");
  65. ++prt->indent;
  66. for (const lwjson_token_t* t = lwjson_get_first_child(token); t != NULL; t = t->next) {
  67. prv_print_token(prt, t);
  68. }
  69. --prt->indent;
  70. print_indent();
  71. }
  72. printf("%c", token->type == LWJSON_TYPE_OBJECT ? '}' : ']');
  73. break;
  74. }
  75. case LWJSON_TYPE_STRING: {
  76. printf("\"%.*s\"", (int)lwjson_get_val_string_length(token), lwjson_get_val_string(token, NULL));
  77. break;
  78. }
  79. case LWJSON_TYPE_NUM_INT: {
  80. printf("%lld", (long long)lwjson_get_val_int(token));
  81. break;
  82. }
  83. case LWJSON_TYPE_NUM_REAL: {
  84. printf("%f", (double)lwjson_get_val_real(token));
  85. break;
  86. }
  87. case LWJSON_TYPE_TRUE: {
  88. printf("true");
  89. break;
  90. }
  91. case LWJSON_TYPE_FALSE: {
  92. printf("false");
  93. break;
  94. }
  95. case LWJSON_TYPE_NULL: {
  96. printf("NULL");
  97. break;
  98. }
  99. default: break;
  100. }
  101. if (token->next != NULL) {
  102. printf(",");
  103. }
  104. printf("\n");
  105. }
  106. /**
  107. * \brief Prints and outputs token data to the stream output
  108. * \note This function is not re-entrant
  109. * \param[in] token: Token to print
  110. */
  111. void
  112. lwjson_print_token(const lwjson_token_t* token) {
  113. lwjson_token_print_t prt = {0};
  114. prv_print_token(&prt, token);
  115. }
  116. /**
  117. * \brief Prints and outputs full parsed LwJSON instance
  118. * \note This function is not re-entrant
  119. * \param[in] lwobj: LwJSON instance to print
  120. */
  121. void
  122. lwjson_print_json(const lwjson_t* lwobj) {
  123. lwjson_token_print_t prt = {0};
  124. prv_print_token(&prt, lwjson_get_first_token(lwobj));
  125. }