simple_parse.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Simple example of parsing and printing JSON using jansson.
  3. *
  4. * SYNOPSIS:
  5. * $ examples/simple_parse
  6. * Type some JSON > [true, false, null, 1, 0.0, -0.0, "", {"name": "barney"}]
  7. * JSON Array of 8 elements:
  8. * JSON True
  9. * JSON False
  10. * JSON Null
  11. * JSON Integer: "1"
  12. * JSON Real: 0.000000
  13. * JSON Real: -0.000000
  14. * JSON String: ""
  15. * JSON Object of 1 pair:
  16. * JSON Key: "name"
  17. * JSON String: "barney"
  18. *
  19. * Copyright (c) 2014 Robert Poor <[email protected]>
  20. *
  21. * Jansson is free software; you can redistribute it and/or modify
  22. * it under the terms of the MIT license. See LICENSE for details.
  23. */
  24. #include <jansson.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. /* forward refs */
  28. void print_json(json_t *root);
  29. void print_json_aux(json_t *element, int indent);
  30. void print_json_indent(int indent);
  31. const char *json_plural(size_t count);
  32. void print_json_object(json_t *element, int indent);
  33. void print_json_array(json_t *element, int indent);
  34. void print_json_string(json_t *element, int indent);
  35. void print_json_integer(json_t *element, int indent);
  36. void print_json_real(json_t *element, int indent);
  37. void print_json_true(json_t *element, int indent);
  38. void print_json_false(json_t *element, int indent);
  39. void print_json_null(json_t *element, int indent);
  40. void print_json(json_t *root) { print_json_aux(root, 0); }
  41. void print_json_aux(json_t *element, int indent) {
  42. switch (json_typeof(element)) {
  43. case JSON_OBJECT:
  44. print_json_object(element, indent);
  45. break;
  46. case JSON_ARRAY:
  47. print_json_array(element, indent);
  48. break;
  49. case JSON_STRING:
  50. print_json_string(element, indent);
  51. break;
  52. case JSON_INTEGER:
  53. print_json_integer(element, indent);
  54. break;
  55. case JSON_REAL:
  56. print_json_real(element, indent);
  57. break;
  58. case JSON_TRUE:
  59. print_json_true(element, indent);
  60. break;
  61. case JSON_FALSE:
  62. print_json_false(element, indent);
  63. break;
  64. case JSON_NULL:
  65. print_json_null(element, indent);
  66. break;
  67. default:
  68. fprintf(stderr, "unrecognized JSON type %d\n", json_typeof(element));
  69. }
  70. }
  71. void print_json_indent(int indent) {
  72. int i;
  73. for (i = 0; i < indent; i++) {
  74. putchar(' ');
  75. }
  76. }
  77. const char *json_plural(size_t count) { return count == 1 ? "" : "s"; }
  78. void print_json_object(json_t *element, int indent) {
  79. size_t size;
  80. const char *key;
  81. json_t *value;
  82. print_json_indent(indent);
  83. size = json_object_size(element);
  84. printf("JSON Object of %lld pair%s:\n", (long long)size, json_plural(size));
  85. json_object_foreach(element, key, value) {
  86. print_json_indent(indent + 2);
  87. printf("JSON Key: \"%s\"\n", key);
  88. print_json_aux(value, indent + 2);
  89. }
  90. }
  91. void print_json_array(json_t *element, int indent) {
  92. size_t i;
  93. size_t size = json_array_size(element);
  94. print_json_indent(indent);
  95. printf("JSON Array of %lld element%s:\n", (long long)size, json_plural(size));
  96. for (i = 0; i < size; i++) {
  97. print_json_aux(json_array_get(element, i), indent + 2);
  98. }
  99. }
  100. void print_json_string(json_t *element, int indent) {
  101. print_json_indent(indent);
  102. printf("JSON String: \"%s\"\n", json_string_value(element));
  103. }
  104. void print_json_integer(json_t *element, int indent) {
  105. print_json_indent(indent);
  106. printf("JSON Integer: \"%" JSON_INTEGER_FORMAT "\"\n", json_integer_value(element));
  107. }
  108. void print_json_real(json_t *element, int indent) {
  109. print_json_indent(indent);
  110. printf("JSON Real: %f\n", json_real_value(element));
  111. }
  112. void print_json_true(json_t *element, int indent) {
  113. (void)element;
  114. print_json_indent(indent);
  115. printf("JSON True\n");
  116. }
  117. void print_json_false(json_t *element, int indent) {
  118. (void)element;
  119. print_json_indent(indent);
  120. printf("JSON False\n");
  121. }
  122. void print_json_null(json_t *element, int indent) {
  123. (void)element;
  124. print_json_indent(indent);
  125. printf("JSON Null\n");
  126. }
  127. /*
  128. * Parse text into a JSON object. If text is valid JSON, returns a
  129. * json_t structure, otherwise prints and error and returns null.
  130. */
  131. json_t *load_json(const char *text) {
  132. json_t *root;
  133. json_error_t error;
  134. root = json_loads(text, 0, &error);
  135. if (root) {
  136. return root;
  137. } else {
  138. fprintf(stderr, "json error on line %d: %s\n", error.line, error.text);
  139. return (json_t *)0;
  140. }
  141. }
  142. /*
  143. * Print a prompt and return (by reference) a null-terminated line of
  144. * text. Returns NULL on eof or some error.
  145. */
  146. char *read_line(char *line, int max_chars) {
  147. printf("Type some JSON > ");
  148. fflush(stdout);
  149. return fgets(line, max_chars, stdin);
  150. }
  151. /* ================================================================
  152. * main
  153. */
  154. #define MAX_CHARS 4096
  155. int main(int argc, char *argv[]) {
  156. char line[MAX_CHARS];
  157. if (argc != 1) {
  158. fprintf(stderr, "Usage: %s\n", argv[0]);
  159. exit(-1);
  160. }
  161. while (read_line(line, MAX_CHARS) != (char *)NULL) {
  162. /* parse text into JSON structure */
  163. json_t *root = load_json(line);
  164. if (root) {
  165. /* print and release the JSON structure */
  166. print_json(root);
  167. json_decref(root);
  168. }
  169. }
  170. return 0;
  171. }