예제 #1
0
json_object *
array_to_json(struct svalue *sp)
{
    int i = 0;
    json_object *ary = json_object_new_array();

    for (i = 0; i < sp->u.vec->size; i++)
    {
        json_object_array_add(ary, value_to_json(&sp->u.vec->item[i]));
    }

    return ary;
}
예제 #2
0
char *
val2json(struct svalue *sp)
{
    json_object *json;
    const char *ptr;
    char *str = NULL;

    json = value_to_json(sp);
    ptr = json_object_to_json_string(json);
    str = strdup(ptr);
    json_object_put(json);

    return str;
}
예제 #3
0
파일: tomltk.c 프로젝트: trws/flux-core
/* Convert TOML table to JSON.
 */
static int table_to_json (toml_table_t *tab, json_t **op)
{
    int i;
    int saved_errno;
    json_t *obj;

    if (!(obj = json_object ()))
        goto nomem;
    for (i = 0; ; i++) {
        const char *key;
        const char *raw;
        toml_table_t *subtab;
        toml_array_t *arr;
        json_t *val = NULL;

        if (!(key = toml_key_in (tab, i)))
            break;
        if ((raw = toml_raw_in (tab, key))) {
            if (value_to_json (raw, &val) < 0)
                goto error;
        }
        else if ((subtab = toml_table_in (tab, key))) {
            if (table_to_json (subtab, &val) < 0)
                goto error;
        }
        else if ((arr = toml_array_in (tab, key))) {
            if (array_to_json (arr, &val) < 0)
                goto error;
        }
        if (json_object_set_new (obj, key, val) < 0) {
            json_decref (val);
            goto nomem;
        }
    }
    *op = obj;
    return 0;
nomem:
    errno = ENOMEM;
error:
    saved_errno = errno;
    json_decref (obj);
    errno = saved_errno;
    return -1;
}
예제 #4
0
파일: tomltk.c 프로젝트: trws/flux-core
/* Convert TOML array to JSON.
 */
static int array_to_json (toml_array_t *arr, json_t **op)
{
    int i;
    int saved_errno;
    json_t *obj;

    if (!(obj = json_array ()))
        goto nomem;
    for (i = 0; ; i++) {
        const char *raw;
        json_t *val;
        toml_table_t *tab;
        toml_array_t *subarr;

        if ((raw = toml_raw_at (arr, i))) {
            if (value_to_json (raw, &val) < 0)
                goto error;
        }
        else if ((tab = toml_table_at (arr, i))) {
            if (table_to_json (tab, &val) < 0)
                goto error;
        }
        else if ((subarr = toml_array_at (arr, i))) {
            if (array_to_json (subarr, &val) < 0)
                goto error;
        }
        else
            break;
        if (json_array_append_new (obj, val) < 0) {
            json_decref (val);
            goto nomem;
        }
    }
    *op = obj;
    return 0;
nomem:
    errno = ENOMEM;
error:
    saved_errno = errno;
    json_decref (obj);
    errno = saved_errno;
    return -1;
}
예제 #5
0
json_object *
mapping_to_json(struct svalue *sp)
{
    int i;
    struct apair *p;
    struct mapping *m = sp->u.map;
    json_object *obj = json_object_new_object();

    for (i = 0; i < m->size; i++)
    {
        for(p = m->pairs[i]; p; p = p->next)
        {
            /* Only strings can be keys */
            if (p->arg.type != T_STRING)
                continue;
            json_object_object_add(obj, p->arg.u.string, value_to_json(&p->val));
        }
    }
    return obj;
}