summaryrefslogtreecommitdiff
path: root/src/instance_info.c
blob: 6dab3519c9a5adc909a7daed43c88db1344da50e (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
#define _POSIX_C_SOURCE 200809L
#include <string.h>
#include <jansson.h>
#include "instance_info.h"

struct instance_info *instance_info_from_json(char *json_data)
{
    struct instance_info *info;
    info = malloc(sizeof(struct instance_info));

    json_t *root;
    json_error_t error;

    root = json_loads(json_data, 0, &error);

    if (!root) {
        fprintf(stderr, "error: on line %d: %s\n", error.line, error.text);
        return NULL;
    }

    if (!json_is_object(root)) {
        fprintf(stderr, "root is not object");
        json_decref(root);
        return NULL;
    }

    json_t *title = json_object_get(root, "title");
    if (!json_is_string(title)) {
        fprintf(stderr, "title is not string");
        return NULL;
    }
    info->title = strdup(json_string_value(title));

    json_decref(root);
    return info;
}