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