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

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