9b79892d2e28830cff38066f6b92f4ae324b9d58
[jansson.git] / src / jansson.h
1 #ifndef JANSSON_H
2 #define JANSSON_H
3
4 #include <stdio.h>
5 #include <stdint.h>
6
7 /* types */
8
9 typedef enum {
10     JSON_OBJECT,
11     JSON_ARRAY,
12     JSON_STRING,
13     JSON_INTEGER,
14     JSON_REAL,
15     JSON_TRUE,
16     JSON_FALSE,
17     JSON_NULL
18 } json_type;
19
20 typedef struct {
21     json_type type;
22     unsigned long refcount;
23 } json_t;
24
25 #define json_typeof(json)      ((json)->type)
26 #define json_is_object(json)   (json && json_typeof(json) == JSON_OBJECT)
27 #define json_is_array(json)    (json && json_typeof(json) == JSON_ARRAY)
28 #define json_is_string(json)   (json && json_typeof(json) == JSON_STRING)
29 #define json_is_integer(json)  (json && json_typeof(json) == JSON_INTEGER)
30 #define json_is_real(json)     (json && json_typeof(json) == JSON_REAL)
31 #define json_is_number(json)   (json_is_integer(json) || json_is_real(json))
32 #define json_is_true(json)     (json && json_typeof(json) == JSON_TRUE)
33 #define json_is_false(json)    (json && json_typeof(json) == JSON_FALSE)
34 #define json_is_null(json)     (json && json_typeof(json) == JSON_NULL)
35
36 /* construction, destruction, reference counting */
37
38 json_t *json_object(void);
39 json_t *json_array(void);
40 json_t *json_string(const char *value);
41 json_t *json_integer(int value);
42 json_t *json_real(double value);
43 json_t *json_true(void);
44 json_t *json_false(void);
45 json_t *json_null(void);
46
47 json_t *json_clone(json_t *json);
48
49 static inline json_t *json_incref(json_t *json)
50 {
51     if(json)
52         ++json->refcount;
53     return json;
54 }
55
56 /* do not call json_delete directly */
57 void json_delete(json_t *json);
58
59 static inline void json_decref(json_t *json)
60 {
61     if(json && --json->refcount == 0)
62         json_delete(json);
63 }
64
65
66 /* getters, setters, manipulation */
67
68 json_t *json_object_get(const json_t *object, const char *key);
69 int json_object_set(json_t *object, const char *key, json_t *value);
70 int json_object_del(json_t *object, const char *key);
71 void *json_object_iter(json_t *object);
72 void *json_object_iter_next(json_t *object, void *iter);
73 const char *json_object_iter_key(void *iter);
74 json_t *json_object_iter_value(void *iter);
75
76 unsigned int json_array_size(const json_t *array);
77 json_t *json_array_get(const json_t *array, unsigned int index);
78 int json_array_set(json_t *array, unsigned int index, json_t *value);
79 int json_array_append(json_t *array, json_t *value);
80
81 const char *json_string_value(const json_t *json);
82 int json_integer_value(const json_t *json);
83 double json_real_value(const json_t *json);
84 double json_number_value(const json_t *json);
85
86
87 /* loading, printing */
88
89 #define JSON_ERROR_TEXT_LENGTH  160
90
91 typedef struct {
92     char text[JSON_ERROR_TEXT_LENGTH];
93     int line;
94 } json_error_t;
95
96 json_t *json_loads(const char *input, json_error_t *error);
97 json_t *json_loadf(FILE *input, json_error_t *error);
98 json_t *json_load_file(const char *path, json_error_t *error);
99
100 #define JSON_INDENT(n)   (n & 0xFF)
101
102 char *json_dumps(const json_t *json, uint32_t flags);
103 int json_dumpf(const json_t *json, FILE *output, uint32_t flags);
104 int json_dump_file(const json_t *json, const char *path, uint32_t flags);
105
106 #endif