Example #1
0
json_t* extract(json_t* json, char* key)
{
    int i, s;
    json_t* temp;
    switch (json_typeof(json))
    {
        case JSON_OBJECT:
            temp = json_object_get(json, key);
            if (temp == NULL)
                {break;}
            return temp;
        case JSON_ARRAY:
            s = json_array_size(json);
            if (s == 0)
                {json_err("index out of bounds", json); break;}
            i = estrtol(key);
            if ((i < -s) || (i >= s))
                {json_err("index out of bounds", json);}
            // stupid fix for a stupid modulus operation
            while (i<0)
                {i+=s;}
            return json_array_get(json, i % s);
        case JSON_STRING:
        case JSON_INTEGER:
        case JSON_REAL:
        case JSON_TRUE:
        case JSON_FALSE:
        case JSON_NULL:
        default:
            break;
    }
    json_err("has no elements to extract", json);
    return json_null();
}
Example #2
0
void keys(json_t* json)
// shoddy, prints directly
{
    void* iter;
    const char** keys;
    size_t i, n;

    if (!json_is_object(json))
        {json_err("has no keys", json); return;}
    if (!((keys = malloc(sizeof(char*) * json_object_size(json)))))
        {hard_err("internal error: out of memory");}

    iter = json_object_iter(json);
    n = 0;
    while (iter)
    {
        keys[n++] = json_object_iter_key(iter);
        iter = json_object_iter_next(json, iter);
    }

    if (dumps_flags & JSON_SORT_KEYS)
        {qsort(keys, n, sizeof(char*), compare_strcmp);}

    for (i = 0; i < n; ++i)
        {printf("%s\n", keys[i]);}

    free(keys);
}
Example #3
0
json_t* extract(json_t* json, char* key)
{
    int i, s;
    json_t* temp;
    switch (json_typeof(json))
    {
        case JSON_OBJECT:
            temp = json_object_get(json, key);
            if (temp == NULL)
                {break;}
            return temp;
        case JSON_ARRAY:
            s = json_array_size(json);
            if (s == 0)
                {break;}
            i = estrtol(key);
            return json_array_get(json, i % s);
        case JSON_STRING:
        case JSON_INTEGER:
        case JSON_REAL:
        case JSON_TRUE:
        case JSON_FALSE:
        case JSON_NULL:
        default:
            break;
    }
    json_err("has no elements to extract", json);
    return json_null();
}
Example #4
0
const char* unstring(json_t* json)
{
    switch (json_typeof(json))
    {
        case JSON_STRING:
            return json_string_value(json);
        case JSON_INTEGER:
        case JSON_REAL:
        case JSON_TRUE:
        case JSON_FALSE:
        case JSON_NULL:
            return smart_dumps(json, 0);
        case JSON_OBJECT:
        case JSON_ARRAY:
        default:
            json_err("is not simple/printable", json);
            return "";
    }
}
Example #5
0
int length(json_t* json)
{
    switch (json_typeof(json))
    {
        case JSON_OBJECT:
            return json_object_size(json);
        case JSON_ARRAY:
            return json_array_size(json);
        case JSON_STRING:
            return strlen(json_string_value(json));
        case JSON_INTEGER:
        case JSON_REAL:
        case JSON_TRUE:
        case JSON_FALSE:
        case JSON_NULL:
        default:
            json_err("has no length", json);
            return 0;
    }
}