simple_parse.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 <rdpoor@gmail.com>
  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 <stdio.h>
  25. #include <stdlib.h>
  26. #include <jansson.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(int 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) {
  41. print_json_aux(root, 0);
  42. }
  43. void print_json_aux(json_t *element, int indent) {
  44. switch (json_typeof(element)) {
  45. case JSON_OBJECT:
  46. print_json_object(element, indent);
  47. break;
  48. case JSON_ARRAY:
  49. print_json_array(element, indent);
  50. break;
  51. case JSON_STRING:
  52. print_json_string(element, indent);
  53. break;
  54. case JSON_INTEGER:
  55. print_json_integer(element, indent);
  56. break;
  57. case JSON_REAL:
  58. print_json_real(element, indent);
  59. break;
  60. case JSON_TRUE:
  61. print_json_true(element, indent);
  62. break;
  63. case JSON_FALSE:
  64. print_json_false(element, indent);
  65. break;
  66. case JSON_NULL:
  67. print_json_null(element, indent);
  68. break;
  69. default:
  70. fprintf(stderr, "unrecognized JSON type %d\n", json_typeof(element));
  71. }
  72. }
  73. void print_json_indent(int indent) {
  74. int i;
  75. for (i = 0; i < indent; i++) { putchar(' '); }
  76. }
  77. const char *json_plural(int count) {
  78. return count == 1 ? "" : "s";
  79. }
  80. void print_json_object(json_t *element, int indent) {
  81. size_t size;
  82. const char *key;
  83. json_t *value;
  84. print_json_indent(indent);
  85. size = json_object_size(element);
  86. printf("JSON Object of %ld pair%s:\n", size, json_plural(size));
  87. json_object_foreach(element, key, value) {
  88. print_json_indent(indent + 2);
  89. printf("JSON Key: \"%s\"\n", key);
  90. print_json_aux(value, indent + 2);
  91. }
  92. }
  93. void print_json_array(json_t *element, int indent) {
  94. size_t i;
  95. size_t size = json_array_size(element);
  96. print_json_indent(indent);
  97. printf("JSON Array of %ld element%s:\n", size, json_plural(size));
  98. for (i = 0; i < size; i++) {
  99. print_json_aux(json_array_get(element, i), indent + 2);
  100. }
  101. }
  102. void print_json_string(json_t *element, int indent) {
  103. print_json_indent(indent);
  104. printf("JSON String: \"%s\"\n", json_string_value(element));
  105. }
  106. void print_json_integer(json_t *element, int indent) {
  107. print_json_indent(indent);
  108. printf("JSON Integer: \"%" JSON_INTEGER_FORMAT "\"\n", json_integer_value(element));
  109. }
  110. void print_json_real(json_t *element, int indent) {
  111. print_json_indent(indent);
  112. printf("JSON Real: %f\n", json_real_value(element));
  113. }
  114. void print_json_true(json_t *element, int indent) {
  115. (void)element;
  116. print_json_indent(indent);
  117. printf("JSON True\n");
  118. }
  119. void print_json_false(json_t *element, int indent) {
  120. (void)element;
  121. print_json_indent(indent);
  122. printf("JSON False\n");
  123. }
  124. void print_json_null(json_t *element, int indent) {
  125. (void)element;
  126. print_json_indent(indent);
  127. printf("JSON Null\n");
  128. }
  129. /*
  130. * Parse text into a JSON object. If text is valid JSON, returns a
  131. * json_t structure, otherwise prints and error and returns null.
  132. */
  133. json_t *load_json(const char *text) {
  134. json_t *root;
  135. json_error_t error;
  136. root = json_loads(text, 0, &error);
  137. if (root) {
  138. return root;
  139. } else {
  140. fprintf(stderr, "json error on line %d: %s\n", error.line, error.text);
  141. return (json_t *)0;
  142. }
  143. }
  144. /*
  145. * Print a prompt and return (by reference) a null-terminated line of
  146. * text. Returns NULL on eof or some error.
  147. */
  148. char *read_line(char *line, int max_chars) {
  149. printf("Type some JSON > ");
  150. fflush(stdout);
  151. return fgets(line, max_chars, stdin);
  152. }
  153. /* ================================================================
  154. * main
  155. */
  156. #define MAX_CHARS 4096
  157. int main(int argc, char *argv[]) {
  158. char line[MAX_CHARS];
  159. if (argc != 1) {
  160. fprintf(stderr, "Usage: %s\n", argv[0]);
  161. exit(-1);
  162. }
  163. while (read_line(line, MAX_CHARS) != (char *)NULL) {
  164. /* parse text into JSON structure */
  165. json_t *root = load_json(line);
  166. if (root) {
  167. /* print and release the JSON structure */
  168. print_json(root);
  169. json_decref(root);
  170. }
  171. }
  172. return 0;
  173. }