You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_visit.c 4.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #ifdef NDEBUG
  2. #undef NDEBUG
  3. #endif
  4. #include <assert.h>
  5. #include <stddef.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include "json.h"
  10. #include "json_tokener.h"
  11. #include "json_visit.h"
  12. static json_c_visit_userfunc emit_object;
  13. static json_c_visit_userfunc skip_arrays;
  14. static json_c_visit_userfunc pop_and_stop;
  15. static json_c_visit_userfunc err_on_subobj2;
  16. static json_c_visit_userfunc pop_array;
  17. static json_c_visit_userfunc stop_array;
  18. static json_c_visit_userfunc err_return;
  19. int main(void)
  20. {
  21. MC_SET_DEBUG(1);
  22. const char *input = "{\
  23. \"obj1\": 123,\
  24. \"obj2\": {\
  25. \"subobj1\": \"aaa\",\
  26. \"subobj2\": \"bbb\",\
  27. \"subobj3\": [ \"elem1\", \"elem2\", true ],\
  28. },\
  29. \"obj3\": 1.234,\
  30. \"obj4\": [ true, false, null ]\
  31. }";
  32. json_object *jso = json_tokener_parse(input);
  33. printf("jso.to_string()=%s\n", json_object_to_json_string(jso));
  34. int rv;
  35. rv = json_c_visit(jso, 0, emit_object, NULL);
  36. printf("json_c_visit(emit_object)=%d\n", rv);
  37. printf("================================\n\n");
  38. rv = json_c_visit(jso, 0, skip_arrays, NULL);
  39. printf("json_c_visit(skip_arrays)=%d\n", rv);
  40. printf("================================\n\n");
  41. rv = json_c_visit(jso, 0, pop_and_stop, NULL);
  42. printf("json_c_visit(pop_and_stop)=%d\n", rv);
  43. printf("================================\n\n");
  44. rv = json_c_visit(jso, 0, err_on_subobj2, NULL);
  45. printf("json_c_visit(err_on_subobj2)=%d\n", rv);
  46. printf("================================\n\n");
  47. rv = json_c_visit(jso, 0, pop_array, NULL);
  48. printf("json_c_visit(pop_array)=%d\n", rv);
  49. printf("================================\n\n");
  50. rv = json_c_visit(jso, 0, stop_array, NULL);
  51. printf("json_c_visit(stop_array)=%d\n", rv);
  52. printf("================================\n\n");
  53. rv = json_c_visit(jso, 0, err_return, NULL);
  54. printf("json_c_visit(err_return)=%d\n", rv);
  55. printf("================================\n\n");
  56. json_object_put(jso);
  57. return 0;
  58. }
  59. static int emit_object(json_object *jso, int flags, json_object *parent_jso, const char *jso_key,
  60. size_t *jso_index, void *userarg)
  61. {
  62. printf("flags: 0x%x, key: %s, index: %ld, value: %s\n", flags,
  63. (jso_key ? jso_key : "(null)"), (jso_index ? (long)*jso_index : -1L),
  64. json_object_to_json_string(jso));
  65. return JSON_C_VISIT_RETURN_CONTINUE;
  66. }
  67. static int skip_arrays(json_object *jso, int flags, json_object *parent_jso, const char *jso_key,
  68. size_t *jso_index, void *userarg)
  69. {
  70. (void)emit_object(jso, flags, parent_jso, jso_key, jso_index, userarg);
  71. if (json_object_get_type(jso) == json_type_array)
  72. return JSON_C_VISIT_RETURN_SKIP;
  73. return JSON_C_VISIT_RETURN_CONTINUE;
  74. }
  75. static int pop_and_stop(json_object *jso, int flags, json_object *parent_jso, const char *jso_key,
  76. size_t *jso_index, void *userarg)
  77. {
  78. (void)emit_object(jso, flags, parent_jso, jso_key, jso_index, userarg);
  79. if (jso_key != NULL && strcmp(jso_key, "subobj1") == 0)
  80. {
  81. printf("POP after handling subobj1\n");
  82. return JSON_C_VISIT_RETURN_POP;
  83. }
  84. if (jso_key != NULL && strcmp(jso_key, "obj3") == 0)
  85. {
  86. printf("STOP after handling obj3\n");
  87. return JSON_C_VISIT_RETURN_STOP;
  88. }
  89. return JSON_C_VISIT_RETURN_CONTINUE;
  90. }
  91. static int err_on_subobj2(json_object *jso, int flags, json_object *parent_jso, const char *jso_key,
  92. size_t *jso_index, void *userarg)
  93. {
  94. (void)emit_object(jso, flags, parent_jso, jso_key, jso_index, userarg);
  95. if (jso_key != NULL && strcmp(jso_key, "subobj2") == 0)
  96. {
  97. printf("ERROR after handling subobj1\n");
  98. return JSON_C_VISIT_RETURN_ERROR;
  99. }
  100. return JSON_C_VISIT_RETURN_CONTINUE;
  101. }
  102. static int pop_array(json_object *jso, int flags, json_object *parent_jso, const char *jso_key,
  103. size_t *jso_index, void *userarg)
  104. {
  105. (void)emit_object(jso, flags, parent_jso, jso_key, jso_index, userarg);
  106. if (jso_index != NULL && (*jso_index == 0))
  107. {
  108. printf("POP after handling array[0]\n");
  109. return JSON_C_VISIT_RETURN_POP;
  110. }
  111. return JSON_C_VISIT_RETURN_CONTINUE;
  112. }
  113. static int stop_array(json_object *jso, int flags, json_object *parent_jso, const char *jso_key,
  114. size_t *jso_index, void *userarg)
  115. {
  116. (void)emit_object(jso, flags, parent_jso, jso_key, jso_index, userarg);
  117. if (jso_index != NULL && (*jso_index == 0))
  118. {
  119. printf("STOP after handling array[1]\n");
  120. return JSON_C_VISIT_RETURN_STOP;
  121. }
  122. return JSON_C_VISIT_RETURN_CONTINUE;
  123. }
  124. static int err_return(json_object *jso, int flags, json_object *parent_jso, const char *jso_key,
  125. size_t *jso_index, void *userarg)
  126. {
  127. printf("flags: 0x%x, key: %s, index: %ld, value: %s\n", flags,
  128. (jso_key ? jso_key : "(null)"), (jso_index ? (long)*jso_index : -1L),
  129. json_object_to_json_string(jso));
  130. return 100;
  131. }