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

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