Extend array API
[jansson.git] / src / jansson.h
1 /*
2  * Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
3  *
4  * Jansson is free software; you can redistribute it and/or modify
5  * it under the terms of the MIT license. See LICENSE for details.
6  */
7
8 #ifndef JANSSON_H
9 #define JANSSON_H
10
11 #include <stdio.h>
12 #include <stdint.h>
13
14 /* types */
15
16 typedef enum {
17     JSON_OBJECT,
18     JSON_ARRAY,
19     JSON_STRING,
20     JSON_INTEGER,
21     JSON_REAL,
22     JSON_TRUE,
23     JSON_FALSE,
24     JSON_NULL
25 } json_type;
26
27 typedef struct {
28     json_type type;
29     unsigned long refcount;
30 } json_t;
31
32 #define json_typeof(json)      ((json)->type)
33 #define json_is_object(json)   (json && json_typeof(json) == JSON_OBJECT)
34 #define json_is_array(json)    (json && json_typeof(json) == JSON_ARRAY)
35 #define json_is_string(json)   (json && json_typeof(json) == JSON_STRING)
36 #define json_is_integer(json)  (json && json_typeof(json) == JSON_INTEGER)
37 #define json_is_real(json)     (json && json_typeof(json) == JSON_REAL)
38 #define json_is_number(json)   (json_is_integer(json) || json_is_real(json))
39 #define json_is_true(json)     (json && json_typeof(json) == JSON_TRUE)
40 #define json_is_false(json)    (json && json_typeof(json) == JSON_FALSE)
41 #define json_is_boolean(json)  (json_is_true(json) || json_is_false(json))
42 #define json_is_null(json)     (json && json_typeof(json) == JSON_NULL)
43
44 /* construction, destruction, reference counting */
45
46 json_t *json_object(void);
47 json_t *json_array(void);
48 json_t *json_string(const char *value);
49 json_t *json_integer(int value);
50 json_t *json_real(double value);
51 json_t *json_true(void);
52 json_t *json_false(void);
53 json_t *json_null(void);
54
55 static inline json_t *json_incref(json_t *json)
56 {
57     if(json && json->refcount != (unsigned int)-1)
58         ++json->refcount;
59     return json;
60 }
61
62 /* do not call json_delete directly */
63 void json_delete(json_t *json);
64
65 static inline void json_decref(json_t *json)
66 {
67     if(json && json->refcount != (unsigned int)-1 && --json->refcount == 0)
68         json_delete(json);
69 }
70
71
72 /* getters, setters, manipulation */
73
74 json_t *json_object_get(const json_t *object, const char *key);
75 int json_object_set_new(json_t *object, const char *key, json_t *value);
76 int json_object_del(json_t *object, const char *key);
77 void *json_object_iter(json_t *object);
78 void *json_object_iter_next(json_t *object, void *iter);
79 const char *json_object_iter_key(void *iter);
80 json_t *json_object_iter_value(void *iter);
81
82 static inline
83 int json_object_set(json_t *object, const char *key, json_t *value)
84 {
85     return json_object_set_new(object, key, json_incref(value));
86 }
87
88 unsigned int json_array_size(const json_t *array);
89 json_t *json_array_get(const json_t *array, unsigned int index);
90 int json_array_set_new(json_t *array, unsigned int index, json_t *value);
91 int json_array_append_new(json_t *array, json_t *value);
92 int json_array_insert_new(json_t *array, unsigned int index, json_t *value);
93 int json_array_remove(json_t *array, unsigned int index);
94 int json_array_clear(json_t *array);
95 int json_array_extend(json_t *array, json_t *other);
96
97 static inline
98 int json_array_set(json_t *array, unsigned int index, json_t *value)
99 {
100     return json_array_set_new(array, index, json_incref(value));
101 }
102
103 static inline
104 int json_array_append(json_t *array, json_t *value)
105 {
106     return json_array_append_new(array, json_incref(value));
107 }
108
109 static inline
110 int json_array_insert(json_t *array, unsigned int index, json_t *value)
111 {
112     return json_array_insert_new(array, index, json_incref(value));
113 }
114
115 const char *json_string_value(const json_t *json);
116 int json_integer_value(const json_t *json);
117 double json_real_value(const json_t *json);
118 double json_number_value(const json_t *json);
119
120
121 /* loading, printing */
122
123 #define JSON_ERROR_TEXT_LENGTH  160
124
125 typedef struct {
126     char text[JSON_ERROR_TEXT_LENGTH];
127     int line;
128 } json_error_t;
129
130 json_t *json_loads(const char *input, json_error_t *error);
131 json_t *json_loadf(FILE *input, json_error_t *error);
132 json_t *json_load_file(const char *path, json_error_t *error);
133
134 #define JSON_INDENT(n)   (n & 0xFF)
135
136 char *json_dumps(const json_t *json, unsigned long flags);
137 int json_dumpf(const json_t *json, FILE *output, unsigned long flags);
138 int json_dump_file(const json_t *json, const char *path, unsigned long flags);
139
140 #endif