ab8db1a63a0e331ce9a784ab75c0542919282f16
[jansson.git] / test / loads_dumps.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <jansson.h>
4
5 #define BUFFER_SIZE (256 * 1024)
6
7 int main(int argc, char *argv[])
8 {
9     json_t *json;
10     json_error_t error;
11     int count;
12     char buffer[BUFFER_SIZE];
13     char *result;
14
15     if(argc != 1) {
16         fprintf(stderr, "usage: %s\n", argv[0]);
17         return 2;
18     }
19
20     count = fread(buffer, 1, BUFFER_SIZE, stdin);
21     if(count < 0 || count >= BUFFER_SIZE) {
22       fprintf(stderr, "unable to read input\n");
23       return 1;
24     }
25     buffer[count] = '\0';
26
27     json = json_loads(buffer, &error);
28     if(!json) {
29         fprintf(stderr, "%d\n%s\n", error.line, error.text);
30         return 1;
31     }
32
33     result = json_dumps(json, 0);
34     json_decref(json);
35
36     puts(result);
37     free(result);
38
39     return 0;
40 }