Refactor error reporting
[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_NUMBER,
14     JSON_TRUE,
15     JSON_FALSE,
16     JSON_NULL
17 } json_type;
18
19 typedef struct {
20     json_type type;
21     unsigned long refcount;
22 } json_t;
23
24 #define json_typeof(json)      ((json)->type)
25 #define json_is_object(json)   (json && json_typeof(json) == JSON_OBJECT)
26 #define json_is_array(json)    (json && json_typeof(json) == JSON_ARRAY)
27 #define json_is_string(json)   (json && json_typeof(json) == JSON_STRING)
28 #define json_is_number(json)   (json && json_typeof(json) == JSON_NUMBER)
29 #define json_is_true(json)     (json && json_typeof(json) == JSON_TRUE)
30 #define json_is_false(json)    (json && json_typeof(json) == JSON_FALSE)
31 #define json_is_null(json)     (json && json_typeof(json) == JSON_NULL)
32
33 /* construction, destruction, reference counting */
34
35 json_t *json_object(void);
36 json_t *json_array(void);
37 json_t *json_string(const char *value);
38 json_t *json_number(double value);
39 json_t *json_true(void);
40 json_t *json_false(void);
41 json_t *json_null(void);
42
43 json_t *json_clone(json_t *json);
44
45 static inline json_t *json_incref(json_t *json)
46 {
47     if(json)
48         ++json->refcount;
49     return json;
50 }
51
52 /* do not call json_delete directly */
53 void json_delete(json_t *json);
54
55 static inline void json_decref(json_t *json)
56 {
57     if(json && --json->refcount == 0)
58         json_delete(json);
59 }
60
61
62 /* getters, setters, manipulation */
63
64 json_t *json_object_get(const json_t *object, const char *key);
65 int json_object_set(json_t *object, const char *key, json_t *value);
66 int json_object_del(json_t *object, const char *key);
67 void *json_object_iter(json_t *object);
68 void *json_object_iter_next(json_t *object, void *iter);
69 const char *json_object_iter_key(void *iter);
70 json_t *json_object_iter_value(void *iter);
71
72 unsigned int json_array_size(const json_t *array);
73 json_t *json_array_get(const json_t *array, unsigned int index);
74 int json_array_set(json_t *array, unsigned int index, json_t *value);
75 int json_array_append(json_t *array, json_t *value);
76
77 const char *json_string_value(const json_t *json);
78 double json_number_value(const json_t *json);
79
80
81 /* loading, printing */
82
83 #define JSON_ERROR_TEXT_LENGTH  160
84
85 typedef struct {
86     char text[JSON_ERROR_TEXT_LENGTH];
87     int line;
88 } json_error_t;
89
90 json_t *json_load(const char *path, json_error_t *error);
91 json_t *json_loads(const char *input, json_error_t *error);
92 json_t *json_loadf(FILE *input, json_error_t *error);
93
94 #define JSON_INDENT(n)   (n & 0xFF)
95 #define JSON_SORT_KEYS   0x100
96
97 int json_dump(const json_t *json, const char *path, uint32_t flags);
98 char *json_dumps(const json_t *json, uint32_t flags);
99 int json_dumpf(const json_t *json, FILE *output, uint32_t flags);
100
101 #endif