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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. int main(void)
  14. {
  15. MC_SET_DEBUG(1);
  16. const char *input = "{\
  17. \"obj1\": 123,\
  18. \"obj2\": {\
  19. \"subobj1\": \"aaa\",\
  20. \"subobj2\": \"bbb\",\
  21. \"subobj3\": [ \"elem1\", \"elem2\", true ],\
  22. },\
  23. \"obj3\": 1.234,\
  24. \"obj4\": [ true, false, null ]\
  25. }";
  26. json_object *jso = json_tokener_parse(input);
  27. printf("jso.to_string()=%s\n", json_object_to_json_string(jso));
  28. int rv;
  29. rv = json_c_visit(jso, 0, emit_object, NULL);
  30. printf("json_c_visit(emit_object)=%d\n", rv);
  31. printf("================================\n\n");
  32. rv = json_c_visit(jso, 0, skip_arrays, NULL);
  33. printf("json_c_visit(skip_arrays)=%d\n", rv);
  34. printf("================================\n\n");
  35. rv = json_c_visit(jso, 0, pop_and_stop, NULL);
  36. printf("json_c_visit(pop_and_stop)=%d\n", rv);
  37. printf("================================\n\n");
  38. rv = json_c_visit(jso, 0, err_on_subobj2, NULL);
  39. printf("json_c_visit(err_on_subobj2)=%d\n", rv);
  40. printf("================================\n\n");
  41. json_object_put(jso);
  42. return 0;
  43. }
  44. static int emit_object(json_object *jso, int flags,
  45. json_object *parent_jso,
  46. const char *jso_key,
  47. size_t *jso_index, void *userarg)
  48. {
  49. printf("flags: 0x%x, key: %s, index: %ld, value: %s\n",
  50. flags,
  51. (jso_key ? jso_key : "(null)"),
  52. (jso_index ? (long)*jso_index : -1L),
  53. json_object_to_json_string(jso));
  54. return JSON_C_VISIT_RETURN_CONTINUE;
  55. }
  56. static int skip_arrays(json_object *jso, int flags,
  57. json_object *parent_jso,
  58. const char *jso_key,
  59. size_t *jso_index, void *userarg)
  60. {
  61. (void)emit_object(jso, flags, parent_jso, jso_key, jso_index, userarg);
  62. if (json_object_get_type(jso) == json_type_array)
  63. return JSON_C_VISIT_RETURN_SKIP;
  64. return JSON_C_VISIT_RETURN_CONTINUE;
  65. }
  66. static int pop_and_stop(json_object *jso, int flags,
  67. json_object *parent_jso,
  68. const char *jso_key,
  69. size_t *jso_index, void *userarg)
  70. {
  71. (void)emit_object(jso, flags, parent_jso, jso_key, jso_index, userarg);
  72. if (jso_key != NULL && strcmp(jso_key, "subobj1") == 0)
  73. {
  74. printf("POP after handling subobj1\n");
  75. return JSON_C_VISIT_RETURN_POP;
  76. }
  77. if (jso_key != NULL && strcmp(jso_key, "obj3") == 0)
  78. {
  79. printf("STOP after handling obj3\n");
  80. return JSON_C_VISIT_RETURN_STOP;
  81. }
  82. return JSON_C_VISIT_RETURN_CONTINUE;
  83. }
  84. static int err_on_subobj2(json_object *jso, int flags,
  85. json_object *parent_jso,
  86. const char *jso_key,
  87. size_t *jso_index, void *userarg)
  88. {
  89. (void)emit_object(jso, flags, parent_jso, jso_key, jso_index, userarg);
  90. if (jso_key != NULL && strcmp(jso_key, "subobj2") == 0)
  91. {
  92. printf("ERROR after handling subobj1\n");
  93. return JSON_C_VISIT_RETURN_ERROR;
  94. }
  95. return JSON_C_VISIT_RETURN_CONTINUE;
  96. }