Merge branch '1.1'
[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
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16
17 /* types */
18
19 typedef enum {
20     JSON_OBJECT,
21     JSON_ARRAY,
22     JSON_STRING,
23     JSON_INTEGER,
24     JSON_REAL,
25     JSON_TRUE,
26     JSON_FALSE,
27     JSON_NULL
28 } json_type;
29
30 typedef struct {
31     json_type type;
32     unsigned long refcount;
33 } json_t;
34
35 #define json_typeof(json)      ((json)->type)
36 #define json_is_object(json)   (json && json_typeof(json) == JSON_OBJECT)
37 #define json_is_array(json)    (json && json_typeof(json) == JSON_ARRAY)
38 #define json_is_string(json)   (json && json_typeof(json) == JSON_STRING)
39 #define json_is_integer(json)  (json && json_typeof(json) == JSON_INTEGER)
40 #define json_is_real(json)     (json && json_typeof(json) == JSON_REAL)
41 #define json_is_number(json)   (json_is_integer(json) || json_is_real(json))
42 #define json_is_true(json)     (json && json_typeof(json) == JSON_TRUE)
43 #define json_is_false(json)    (json && json_typeof(json) == JSON_FALSE)
44 #define json_is_boolean(json)  (json_is_true(json) || json_is_false(json))
45 #define json_is_null(json)     (json && json_typeof(json) == JSON_NULL)
46
47 /* construction, destruction, reference counting */
48
49 json_t *json_object(void);
50 json_t *json_array(void);
51 json_t *json_string(const char *value);
52 json_t *json_integer(int value);
53 json_t *json_real(double value);
54 json_t *json_true(void);
55 json_t *json_false(void);
56 json_t *json_null(void);
57
58 static inline json_t *json_incref(json_t *json)
59 {
60     if(json && json->refcount != (unsigned int)-1)
61         ++json->refcount;
62     return json;
63 }
64
65 /* do not call json_delete directly */
66 void json_delete(json_t *json);
67
68 static inline void json_decref(json_t *json)
69 {
70     if(json && json->refcount != (unsigned int)-1 && --json->refcount == 0)
71         json_delete(json);
72 }
73
74
75 /* getters, setters, manipulation */
76
77 unsigned int json_object_size(const json_t *object);
78 json_t *json_object_get(const json_t *object, const char *key);
79 int json_object_set_new(json_t *object, const char *key, json_t *value);
80 int json_object_del(json_t *object, const char *key);
81 int json_object_clear(json_t *object);
82 int json_object_update(json_t *object, json_t *other);
83 void *json_object_iter(json_t *object);
84 void *json_object_iter_next(json_t *object, void *iter);
85 const char *json_object_iter_key(void *iter);
86 json_t *json_object_iter_value(void *iter);
87
88 static inline
89 int json_object_set(json_t *object, const char *key, json_t *value)
90 {
91     return json_object_set_new(object, key, json_incref(value));
92 }
93
94 unsigned int json_array_size(const json_t *array);
95 json_t *json_array_get(const json_t *array, unsigned int index);
96 int json_array_set_new(json_t *array, unsigned int index, json_t *value);
97 int json_array_append_new(json_t *array, json_t *value);
98 int json_array_insert_new(json_t *array, unsigned int index, json_t *value);
99 int json_array_remove(json_t *array, unsigned int index);
100 int json_array_clear(json_t *array);
101 int json_array_extend(json_t *array, json_t *other);
102
103 static inline
104 int json_array_set(json_t *array, unsigned int index, json_t *value)
105 {
106     return json_array_set_new(array, index, json_incref(value));
107 }
108
109 static inline
110 int json_array_append(json_t *array, json_t *value)
111 {
112     return json_array_append_new(array, json_incref(value));
113 }
114
115 static inline
116 int json_array_insert(json_t *array, unsigned int index, json_t *value)
117 {
118     return json_array_insert_new(array, index, json_incref(value));
119 }
120
121 const char *json_string_value(const json_t *string);
122 int json_integer_value(const json_t *integer);
123 double json_real_value(const json_t *real);
124 double json_number_value(const json_t *json);
125
126 int json_string_set(json_t *string, const char *value);
127 int json_integer_set(json_t *integer, int value);
128 int json_real_set(json_t *real, double value);
129
130
131 /* loading, printing */
132
133 #define JSON_ERROR_TEXT_LENGTH  160
134
135 typedef struct {
136     char text[JSON_ERROR_TEXT_LENGTH];
137     int line;
138 } json_error_t;
139
140 json_t *json_loads(const char *input, json_error_t *error);
141 json_t *json_loadf(FILE *input, json_error_t *error);
142 json_t *json_load_file(const char *path, json_error_t *error);
143
144 #define JSON_INDENT(n)      (n & 0xFF)
145 #define JSON_COMPACT        0x100
146 #define JSON_ENSURE_ASCII   0x200
147 #define JSON_SORT_KEYS      0x400
148
149 char *json_dumps(const json_t *json, unsigned long flags);
150 int json_dumpf(const json_t *json, FILE *output, unsigned long flags);
151 int json_dump_file(const json_t *json, const char *path, unsigned long flags);
152
153 #ifdef __cplusplus
154 }
155 #endif
156
157 #endif