test/.gitignore: Add testprogs/test_simple
[jansson.git] / test / loads_dumps.c
1 /*
2  * Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
3  *
4  * Jansson is free software; you can redistribute it and/or modify
5  * it under the terms of the MIT license. See LICENSE for details.
6  */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <jansson.h>
11
12 #define BUFFER_SIZE (256 * 1024)
13
14 int main(int argc, char *argv[])
15 {
16     json_t *json;
17     json_error_t error;
18     int count;
19     char buffer[BUFFER_SIZE];
20     char *result;
21
22     if(argc != 1) {
23         fprintf(stderr, "usage: %s\n", argv[0]);
24         return 2;
25     }
26
27     count = fread(buffer, 1, BUFFER_SIZE, stdin);
28     if(count < 0 || count >= BUFFER_SIZE) {
29       fprintf(stderr, "unable to read input\n");
30       return 1;
31     }
32     buffer[count] = '\0';
33
34     json = json_loads(buffer, &error);
35     if(!json) {
36         fprintf(stderr, "%d\n%s\n", error.line, error.text);
37         return 1;
38     }
39
40     result = json_dumps(json, 0);
41     json_decref(json);
42
43     puts(result);
44     free(result);
45
46     return 0;
47 }