aboutsummaryrefslogtreecommitdiff
path: root/test/loads_dumps.c
blob: d2a7240ba98b1d73c8f38918be08e8af3ee33fd4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
 * Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
 *
 * Jansson is free software; you can redistribute it and/or modify
 * it under the terms of the MIT license. See LICENSE for details.
 */

#include <stdio.h>
#include <stdlib.h>
#include <jansson.h>

#define BUFFER_SIZE (256 * 1024)

int main(int argc, char *argv[])
{
    json_t *json;
    json_error_t error;
    int count;
    char buffer[BUFFER_SIZE];
    char *result;

    if(argc != 1) {
        fprintf(stderr, "usage: %s\n", argv[0]);
        return 2;
    }

    count = fread(buffer, 1, BUFFER_SIZE, stdin);
    if(count < 0 || count >= BUFFER_SIZE) {
      fprintf(stderr, "unable to read input\n");
      return 1;
    }
    buffer[count] = '\0';

    json = json_loads(buffer, &error);
    if(!json) {
        fprintf(stderr, "%d\n%s\n", error.line, error.text);
        return 1;
    }

    result = json_dumps(json, 0);
    json_decref(json);

    puts(result);
    free(result);

    return 0;
}