jansson/examples/simple_parse.c

201 lines
5.3 KiB
C
Raw Permalink Normal View History

2014-12-27 02:49:15 +08:00
/*
* Simple example of parsing and printing JSON using jansson.
*
* SYNOPSIS:
* $ examples/simple_parse
* Type some JSON > [true, false, null, 1, 0.0, -0.0, "", {"name": "barney"}]
* JSON Array of 8 elements:
* JSON True
* JSON False
* JSON Null
* JSON Integer: "1"
* JSON Real: 0.000000
* JSON Real: -0.000000
* JSON String: ""
* JSON Object of 1 pair:
* JSON Key: "name"
* JSON String: "barney"
*
2014-12-27 02:49:15 +08:00
* Copyright (c) 2014 Robert Poor <rdpoor@gmail.com>
*
* Jansson is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
2019-10-17 14:08:51 +08:00
#include <jansson.h>
2014-12-27 02:49:15 +08:00
#include <stdio.h>
#include <stdlib.h>
/* forward refs */
void print_json(json_t *root);
void print_json_aux(json_t *element, int indent);
void print_json_indent(int indent);
const char *json_plural(size_t count);
2014-12-27 02:49:15 +08:00
void print_json_object(json_t *element, int indent);
void print_json_array(json_t *element, int indent);
void print_json_string(json_t *element, int indent);
void print_json_integer(json_t *element, int indent);
void print_json_real(json_t *element, int indent);
void print_json_true(json_t *element, int indent);
void print_json_false(json_t *element, int indent);
void print_json_null(json_t *element, int indent);
2019-10-17 14:08:51 +08:00
void print_json(json_t *root) { print_json_aux(root, 0); }
2014-12-27 02:49:15 +08:00
void print_json_aux(json_t *element, int indent) {
switch (json_typeof(element)) {
case JSON_OBJECT:
print_json_object(element, indent);
break;
case JSON_ARRAY:
print_json_array(element, indent);
break;
case JSON_STRING:
print_json_string(element, indent);
break;
case JSON_INTEGER:
print_json_integer(element, indent);
break;
case JSON_REAL:
print_json_real(element, indent);
break;
case JSON_TRUE:
print_json_true(element, indent);
break;
case JSON_FALSE:
print_json_false(element, indent);
break;
case JSON_NULL:
print_json_null(element, indent);
break;
2019-10-17 14:08:51 +08:00
default:
fprintf(stderr, "unrecognized JSON type %d\n", json_typeof(element));
2014-12-27 02:49:15 +08:00
}
}
void print_json_indent(int indent) {
int i;
2019-10-17 14:08:51 +08:00
for (i = 0; i < indent; i++) {
putchar(' ');
}
2014-12-27 02:49:15 +08:00
}
const char *json_plural(size_t count) { return count == 1 ? "" : "s"; }
2014-12-27 02:49:15 +08:00
void print_json_object(json_t *element, int indent) {
size_t size;
const char *key;
json_t *value;
print_json_indent(indent);
size = json_object_size(element);
printf("JSON Object of %lld pair%s:\n", (long long)size, json_plural(size));
json_object_foreach(element, key, value) {
print_json_indent(indent + 2);
printf("JSON Key: \"%s\"\n", key);
print_json_aux(value, indent + 2);
}
2014-12-27 02:49:15 +08:00
}
void print_json_array(json_t *element, int indent) {
size_t i;
size_t size = json_array_size(element);
print_json_indent(indent);
2014-12-27 02:49:15 +08:00
printf("JSON Array of %lld element%s:\n", (long long)size, json_plural(size));
for (i = 0; i < size; i++) {
print_json_aux(json_array_get(element, i), indent + 2);
}
2014-12-27 02:49:15 +08:00
}
void print_json_string(json_t *element, int indent) {
print_json_indent(indent);
printf("JSON String: \"%s\"\n", json_string_value(element));
2014-12-27 02:49:15 +08:00
}
void print_json_integer(json_t *element, int indent) {
print_json_indent(indent);
printf("JSON Integer: \"%" JSON_INTEGER_FORMAT "\"\n", json_integer_value(element));
2014-12-27 02:49:15 +08:00
}
void print_json_real(json_t *element, int indent) {
print_json_indent(indent);
printf("JSON Real: %f\n", json_real_value(element));
2014-12-27 02:49:15 +08:00
}
void print_json_true(json_t *element, int indent) {
(void)element;
print_json_indent(indent);
printf("JSON True\n");
2014-12-27 02:49:15 +08:00
}
void print_json_false(json_t *element, int indent) {
(void)element;
print_json_indent(indent);
printf("JSON False\n");
2014-12-27 02:49:15 +08:00
}
void print_json_null(json_t *element, int indent) {
(void)element;
print_json_indent(indent);
printf("JSON Null\n");
2014-12-27 02:49:15 +08:00
}
/*
2014-12-27 02:49:15 +08:00
* Parse text into a JSON object. If text is valid JSON, returns a
* json_t structure, otherwise prints and error and returns null.
*/
json_t *load_json(const char *text) {
json_t *root;
json_error_t error;
2014-12-27 02:49:15 +08:00
root = json_loads(text, 0, &error);
2014-12-27 02:49:15 +08:00
if (root) {
return root;
} else {
fprintf(stderr, "json error on line %d: %s\n", error.line, error.text);
return (json_t *)0;
}
2014-12-27 02:49:15 +08:00
}
/*
* Print a prompt and return (by reference) a null-terminated line of
2014-12-29 22:22:19 +08:00
* text. Returns NULL on eof or some error.
2014-12-27 02:49:15 +08:00
*/
2014-12-29 22:22:19 +08:00
char *read_line(char *line, int max_chars) {
printf("Type some JSON > ");
fflush(stdout);
return fgets(line, max_chars, stdin);
2014-12-27 02:49:15 +08:00
}
/* ================================================================
* main
*/
2014-12-29 22:22:19 +08:00
#define MAX_CHARS 4096
2014-12-27 02:49:15 +08:00
int main(int argc, char *argv[]) {
char line[MAX_CHARS];
if (argc != 1) {
fprintf(stderr, "Usage: %s\n", argv[0]);
exit(-1);
}
2014-12-27 02:49:15 +08:00
while (read_line(line, MAX_CHARS) != (char *)NULL) {
2014-12-27 02:49:15 +08:00
/* parse text into JSON structure */
json_t *root = load_json(line);
2014-12-27 02:49:15 +08:00
if (root) {
/* print and release the JSON structure */
print_json(root);
json_decref(root);
}
2014-12-27 02:49:15 +08:00
}
return 0;
2014-12-27 02:49:15 +08:00
}