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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #include <assert.h>
  2. #include <stddef.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.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, json_object *parent_jso, const char *jso_key,
  57. size_t *jso_index, void *userarg)
  58. {
  59. printf("flags: 0x%x, key: %s, index: %ld, value: %s\n", flags,
  60. (jso_key ? jso_key : "(null)"), (jso_index ? (long)*jso_index : -1L),
  61. json_object_to_json_string(jso));
  62. return JSON_C_VISIT_RETURN_CONTINUE;
  63. }
  64. static int skip_arrays(json_object *jso, int flags, json_object *parent_jso, const char *jso_key,
  65. size_t *jso_index, void *userarg)
  66. {
  67. (void)emit_object(jso, flags, parent_jso, jso_key, jso_index, userarg);
  68. if (json_object_get_type(jso) == json_type_array)
  69. return JSON_C_VISIT_RETURN_SKIP;
  70. return JSON_C_VISIT_RETURN_CONTINUE;
  71. }
  72. static int pop_and_stop(json_object *jso, int flags, json_object *parent_jso, const char *jso_key,
  73. size_t *jso_index, void *userarg)
  74. {
  75. (void)emit_object(jso, flags, parent_jso, jso_key, jso_index, userarg);
  76. if (jso_key != NULL && strcmp(jso_key, "subobj1") == 0)
  77. {
  78. printf("POP after handling subobj1\n");
  79. return JSON_C_VISIT_RETURN_POP;
  80. }
  81. if (jso_key != NULL && strcmp(jso_key, "obj3") == 0)
  82. {
  83. printf("STOP after handling obj3\n");
  84. return JSON_C_VISIT_RETURN_STOP;
  85. }
  86. return JSON_C_VISIT_RETURN_CONTINUE;
  87. }
  88. static int err_on_subobj2(json_object *jso, int flags, json_object *parent_jso, const char *jso_key,
  89. size_t *jso_index, void *userarg)
  90. {
  91. (void)emit_object(jso, flags, parent_jso, jso_key, jso_index, userarg);
  92. if (jso_key != NULL && strcmp(jso_key, "subobj2") == 0)
  93. {
  94. printf("ERROR after handling subobj1\n");
  95. return JSON_C_VISIT_RETURN_ERROR;
  96. }
  97. return JSON_C_VISIT_RETURN_CONTINUE;
  98. }
  99. static int pop_array(json_object *jso, int flags, json_object *parent_jso, const char *jso_key,
  100. size_t *jso_index, void *userarg)
  101. {
  102. (void)emit_object(jso, flags, parent_jso, jso_key, jso_index, userarg);
  103. if (jso_index != NULL && (*jso_index == 0))
  104. {
  105. printf("POP after handling array[0]\n");
  106. return JSON_C_VISIT_RETURN_POP;
  107. }
  108. return JSON_C_VISIT_RETURN_CONTINUE;
  109. }
  110. static int stop_array(json_object *jso, int flags, json_object *parent_jso, const char *jso_key,
  111. size_t *jso_index, void *userarg)
  112. {
  113. (void)emit_object(jso, flags, parent_jso, jso_key, jso_index, userarg);
  114. if (jso_index != NULL && (*jso_index == 0))
  115. {
  116. printf("STOP after handling array[1]\n");
  117. return JSON_C_VISIT_RETURN_STOP;
  118. }
  119. return JSON_C_VISIT_RETURN_CONTINUE;
  120. }
  121. static int err_return(json_object *jso, int flags, json_object *parent_jso, const char *jso_key,
  122. size_t *jso_index, void *userarg)
  123. {
  124. printf("flags: 0x%x, key: %s, index: %ld, value: %s\n", flags,
  125. (jso_key ? jso_key : "(null)"), (jso_index ? (long)*jso_index : -1L),
  126. json_object_to_json_string(jso));
  127. return 100;
  128. }