aboutsummaryrefslogtreecommitdiff
path: root/src/value.c
blob: 6ab44a6367708bac145cc8888cfaaedd81037352 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#define _GNU_SOURCE
#include <stdlib.h>
#include <string.h>

#include <jansson.h>
#include "hashtable.h"

#define max(a, b)  ((a) > (b) ? (a) : (b))

#define container_of(ptr_, type_, member_)  \
    ((type_ *)((char *)ptr_ - (size_t)&((type_ *)0)->member_))

typedef struct {
    json_t json;
    hashtable_t *hashtable;
} json_object_t;

typedef struct {
    json_t json;
    unsigned int size;
    unsigned int entries;
    json_t **table;
} json_array_t;

typedef struct {
    json_t json;
    char *value;
} json_string_t;

typedef struct {
    json_t json;
    double value;
} json_number_t;

#define json_to_object(json_)  container_of(json_, json_object_t, json)
#define json_to_array(json_)   container_of(json_, json_array_t, json)
#define json_to_string(json_)  container_of(json_, json_string_t, json)
#define json_to_number(json_)  container_of(json_, json_number_t, json)

static inline void json_init(json_t *json, json_type type)
{
    json->type = type;
    json->refcount = 1;
}


/*** object ***/

static unsigned int hash_string(const void *key)
{
    const char *str = (const char *)key;
    unsigned int hash = 5381;
    unsigned int c;

    while((c = (unsigned int)*str))
    {
        hash = ((hash << 5) + hash) + c;
        str++;
    }

    return hash;
}

static int string_equal(const void *key1, const void *key2)
{
    return strcmp((const char *)key1, (const char *)key2) == 0;
}

static void value_decref(void *value)
{
    json_decref((json_t *)value);
}

json_t *json_object(void)
{
    json_object_t *object = malloc(sizeof(json_object_t));
    if(!object)
        return NULL;
    json_init(&object->json, JSON_OBJECT);

    object->hashtable =
      hashtable_new(hash_string, string_equal, free, value_decref);
    if(!object->hashtable)
    {
        free(object);
        return NULL;
    }
    return &object->json;
}

static void json_delete_object(json_object_t *object)
{
    hashtable_free(object->hashtable);
    free(object);
}

json_t *json_object_get(const json_t *json, const char *key)
{
    json_object_t *object;

    if(!json_is_object(json))
        return NULL;

    return hashtable_get(object->hashtable, key);
}

int json_object_del(json_t *json, const char *key)
{
    json_object_t *object;

    if(!json_is_object(json))
        return -1;

    object = json_to_object(json);
    return hashtable_del(object->hashtable, key);
}

int json_object_set(json_t *json, const char *key, json_t *value)
{
    json_object_t *object;

    if(!json_is_object(json))
        return -1;

    object = json_to_object(json);
    return hashtable_set(object->hashtable, strdup(key), json_incref(value));
}


/*** array ***/

json_t *json_array(void)
{
    json_array_t *array = malloc(sizeof(json_array_t));
    if(!array)
      return NULL;
    json_init(&array->json, JSON_ARRAY);

    array->entries = 0;
    array->size = 0;
    array->table = NULL;

    return &array->json;
}

static void json_delete_array(json_array_t *array)
{
    unsigned int i;

    for(i = 0; i < array->entries; i++)
        json_decref(array->table[i]);

    free(array->table);
    free(array);
}

unsigned int json_array_size(const json_t *json)
{
    if(!json_is_array(json))
        return 0;

    return json_to_array(json)->entries;
}

json_t *json_array_get(const json_t *json, unsigned int index)
{
    json_array_t *array;
    if(!json_is_array(json))
        return NULL;
    array = json_to_array(json);

    if(index >= array->size)
        return NULL;

    return array->table[index];
}

int json_array_set(json_t *json, unsigned int index, json_t *value)
{
    json_array_t *array;
    if(!json_is_array(json))
        return -1;
    array = json_to_array(json);

    if(index >= array->size)
        return -1;

    array->table[index] = json_incref(value);
    return 0;
}

int json_array_append(json_t *json, json_t *value)
{
    json_array_t *array;
    if(!json_is_array(json))
        return -1;
    array = json_to_array(json);

    if(array->entries == array->size) {
        array->size = max(8, array->size * 2);
        array->table = realloc(array->table, array->size * sizeof(json_t *));
        if(!array->table)
            return -1;
    }

    array->table[array->entries] = json_incref(value);
    array->entries++;

    return 0;
}


/*** string ***/

json_t *json_string(const char *value)
{
    json_string_t *string = malloc(sizeof(json_string_t));
    if(!string)
       return NULL;
    json_init(&string->json, JSON_STRING);

    string->value = strdup(value);
    return &string->json;
}

const char *json_string_value(const json_t *json)
{
    if(!json_is_string(json))
        return NULL;

    return json_to_string(json)->value;
}

static void json_delete_string(json_string_t *string)
{
    free(string->value);
    free(string);
}

json_t *json_number(double value)
{
    json_number_t *number = malloc(sizeof(json_number_t));
    if(!number)
       return NULL;
    json_init(&number->json, JSON_NUMBER);

    number->value = value;
    return &number->json;
}


/*** number ***/

double json_number_value(const json_t *json)
{
    if(!json_is_number(json))
        return 0.0;

    return json_to_number(json)->value;
}

static void json_delete_number(json_number_t *number)
{
    free(number);
}


/*** simple values ***/

json_t *json_true(void)
{
    static json_t the_true = {
        .type = JSON_TRUE,
        .refcount = 1
    };
    return json_incref(&the_true);
}


json_t *json_false(void)
{
    static json_t the_false = {
        .type = JSON_FALSE,
        .refcount = 1
    };
    return json_incref(&the_false);
}


json_t *json_null(void)
{
    static json_t the_null = {
        .type = JSON_NULL,
        .refcount = 1
    };
    return json_incref(&the_null);
}


/*** deletion ***/

void json_delete(json_t *json)
{
    if(json_is_object(json))
        json_delete_object(json_to_object(json));

    else if(json_is_array(json))
        json_delete_array(json_to_array(json));

    else if(json_is_string(json))
        json_delete_string(json_to_string(json));

    else if(json_is_number(json))
        json_delete_number(json_to_number(json));

    /* json_delete is not called for true, false or null */
}