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.

json_parse.c 4.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #include <assert.h>
  2. #include <errno.h>
  3. #include <fcntl.h>
  4. #include <getopt.h>
  5. #include <stddef.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10. #include "apps_config.h"
  11. /* XXX for a regular program, these should be <json-c/foo.h>
  12. * but that's inconvenient when building in the json-c source tree.
  13. */
  14. #include "json_object.h"
  15. #include "json_tokener.h"
  16. #include "json_util.h"
  17. #ifdef HAVE_SYS_RESOURCE_H
  18. #include <sys/resource.h>
  19. #include <sys/time.h>
  20. #endif
  21. static int formatted_output = 0;
  22. static int show_output = 1;
  23. static int strict_mode = 0;
  24. static const char *fname = NULL;
  25. #ifndef HAVE_JSON_TOKENER_GET_PARSE_END
  26. #define json_tokener_get_parse_end(tok) ((tok)->char_offset)
  27. #endif
  28. static void showmem(void);
  29. static int parseit(int fd, int (*callback)(struct json_object *));
  30. static int showobj(struct json_object *new_obj);
  31. static void showmem(void)
  32. {
  33. #ifdef HAVE_GETRUSAGE
  34. struct rusage rusage;
  35. memset(&rusage, 0, sizeof(rusage));
  36. getrusage(RUSAGE_SELF, &rusage);
  37. printf("maxrss: %d KB\n", rusage.ru_maxrss);
  38. #endif
  39. }
  40. static int parseit(int fd, int (*callback)(struct json_object *))
  41. {
  42. struct json_object *obj;
  43. char buf[32768];
  44. int ret;
  45. int depth = JSON_TOKENER_DEFAULT_DEPTH;
  46. json_tokener *tok;
  47. tok = json_tokener_new_ex(depth);
  48. if (!tok)
  49. {
  50. fprintf(stderr, "unable to allocate json_tokener: %s\n", strerror(errno));
  51. return 1;
  52. }
  53. json_tokener_set_flags(tok, JSON_TOKENER_STRICT
  54. #ifdef JSON_TOKENER_ALLOW_TRAILING_CHARS
  55. | JSON_TOKENER_ALLOW_TRAILING_CHARS
  56. #endif
  57. );
  58. // XXX push this into some kind of json_tokener_parse_fd API?
  59. // json_object_from_fd isn't flexible enough, and mirroring
  60. // everything you can do with a tokener into json_util.c seems
  61. // like the wrong approach.
  62. size_t total_read = 0;
  63. while ((ret = read(fd, buf, sizeof(buf))) > 0)
  64. {
  65. total_read += ret;
  66. int start_pos = 0;
  67. while (start_pos != ret)
  68. {
  69. obj = json_tokener_parse_ex(tok, &buf[start_pos], ret - start_pos);
  70. enum json_tokener_error jerr = json_tokener_get_error(tok);
  71. int parse_end = json_tokener_get_parse_end(tok);
  72. if (obj == NULL && jerr != json_tokener_continue)
  73. {
  74. char *aterr = &buf[start_pos + parse_end];
  75. fflush(stdout);
  76. int fail_offset = total_read - ret + start_pos + parse_end;
  77. fprintf(stderr, "Failed at offset %d: %s %c\n", fail_offset,
  78. json_tokener_error_desc(jerr), aterr[0]);
  79. json_tokener_free(tok);
  80. return 1;
  81. }
  82. if (obj != NULL)
  83. {
  84. int cb_ret = callback(obj);
  85. json_object_put(obj);
  86. if (cb_ret != 0)
  87. {
  88. json_tokener_free(tok);
  89. return 1;
  90. }
  91. }
  92. start_pos += json_tokener_get_parse_end(tok);
  93. assert(start_pos <= ret);
  94. }
  95. }
  96. if (ret < 0)
  97. {
  98. fprintf(stderr, "error reading fd %d: %s\n", fd, strerror(errno));
  99. }
  100. json_tokener_free(tok);
  101. return 0;
  102. }
  103. static int showobj(struct json_object *new_obj)
  104. {
  105. if (new_obj == NULL)
  106. {
  107. fprintf(stderr, "%s: Failed to parse\n", fname);
  108. return 1;
  109. }
  110. printf("Successfully parsed object from %s\n", fname);
  111. if (show_output)
  112. {
  113. const char *output;
  114. if (formatted_output)
  115. output = json_object_to_json_string(new_obj);
  116. else
  117. output = json_object_to_json_string_ext(new_obj, JSON_C_TO_STRING_PRETTY);
  118. printf("%s\n", output);
  119. }
  120. showmem();
  121. return 0;
  122. }
  123. static void usage(const char *argv0, int exitval, const char *errmsg)
  124. {
  125. FILE *fp = stdout;
  126. if (exitval != 0)
  127. fp = stderr;
  128. if (errmsg != NULL)
  129. fprintf(fp, "ERROR: %s\n\n", errmsg);
  130. fprintf(fp, "Usage: %s [-f] [-n] [-s]\n", argv0);
  131. fprintf(fp, " -f - Format the output with JSON_C_TO_STRING_PRETTY\n");
  132. fprintf(fp, " -n - No output\n");
  133. fprintf(fp, " -s - Parse in strict mode, flags:\n");
  134. fprintf(fp, " JSON_TOKENER_STRICT|JSON_TOKENER_ALLOW_TRAILING_CHARS\n");
  135. fprintf(fp, "\nWARNING WARNING WARNING\n");
  136. fprintf(fp, "This is a prototype, it may change or be removed at any time!\n");
  137. exit(exitval);
  138. }
  139. int main(int argc, char **argv)
  140. {
  141. json_object *new_obj;
  142. int opt;
  143. while ((opt = getopt(argc, argv, "fns")) != -1)
  144. {
  145. switch (opt)
  146. {
  147. case 'f': formatted_output = 1; break;
  148. case 'n': show_output = 0; break;
  149. case 's': strict_mode = 1; break;
  150. default: /* '?' */ usage(argv[0], EXIT_FAILURE, NULL);
  151. }
  152. }
  153. if (optind >= argc)
  154. {
  155. usage(argv[0], EXIT_FAILURE, "Expected argument after options");
  156. }
  157. fname = argv[optind];
  158. int fd = open(argv[optind], O_RDONLY, 0);
  159. showmem();
  160. if (parseit(fd, showobj) != 0)
  161. exit(EXIT_FAILURE);
  162. showmem();
  163. exit(EXIT_SUCCESS);
  164. }