test_visit.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. }
  43. static int emit_object(json_object *jso, int flags,
  44. json_object *parent_jso,
  45. const char *jso_key,
  46. size_t *jso_index, void *userarg)
  47. {
  48. printf("flags: 0x%x, key: %s, index: %ld, value: %s\n",
  49. flags,
  50. (jso_key ? jso_key : "(null)"),
  51. (jso_index ? (long)*jso_index : -1L),
  52. json_object_to_json_string(jso));
  53. return JSON_C_VISIT_RETURN_CONTINUE;
  54. }
  55. static int skip_arrays(json_object *jso, int flags,
  56. json_object *parent_jso,
  57. const char *jso_key,
  58. size_t *jso_index, void *userarg)
  59. {
  60. (void)emit_object(jso, flags, parent_jso, jso_key, jso_index, userarg);
  61. if (json_object_get_type(jso) == json_type_array)
  62. return JSON_C_VISIT_RETURN_SKIP;
  63. return JSON_C_VISIT_RETURN_CONTINUE;
  64. }
  65. static int pop_and_stop(json_object *jso, int flags,
  66. json_object *parent_jso,
  67. 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 (jso_key != NULL && strcmp(jso_key, "subobj1") == 0)
  72. {
  73. printf("POP after handling subobj1\n");
  74. return JSON_C_VISIT_RETURN_POP;
  75. }
  76. if (jso_key != NULL && strcmp(jso_key, "obj3") == 0)
  77. {
  78. printf("STOP after handling obj3\n");
  79. return JSON_C_VISIT_RETURN_STOP;
  80. }
  81. return JSON_C_VISIT_RETURN_CONTINUE;
  82. }
  83. static int err_on_subobj2(json_object *jso, int flags,
  84. json_object *parent_jso,
  85. const char *jso_key,
  86. size_t *jso_index, void *userarg)
  87. {
  88. (void)emit_object(jso, flags, parent_jso, jso_key, jso_index, userarg);
  89. if (jso_key != NULL && strcmp(jso_key, "subobj2") == 0)
  90. {
  91. printf("ERROR after handling subobj1\n");
  92. return JSON_C_VISIT_RETURN_ERROR;
  93. }
  94. return JSON_C_VISIT_RETURN_CONTINUE;
  95. }