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 5.1 kB

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