2 * Copyright (c) 2009, 2010 Petri Lehtinen <petri@digip.org>
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.
12 #include <stdlib.h> /* for size_t */
13 #include <jansson_config.h>
37 #define json_typeof(json) ((json)->type)
38 #define json_is_object(json) (json && json_typeof(json) == JSON_OBJECT)
39 #define json_is_array(json) (json && json_typeof(json) == JSON_ARRAY)
40 #define json_is_string(json) (json && json_typeof(json) == JSON_STRING)
41 #define json_is_integer(json) (json && json_typeof(json) == JSON_INTEGER)
42 #define json_is_real(json) (json && json_typeof(json) == JSON_REAL)
43 #define json_is_number(json) (json_is_integer(json) || json_is_real(json))
44 #define json_is_true(json) (json && json_typeof(json) == JSON_TRUE)
45 #define json_is_false(json) (json && json_typeof(json) == JSON_FALSE)
46 #define json_is_boolean(json) (json_is_true(json) || json_is_false(json))
47 #define json_is_null(json) (json && json_typeof(json) == JSON_NULL)
49 /* construction, destruction, reference counting */
51 json_t *json_object(void);
52 json_t *json_array(void);
53 json_t *json_string(const char *value);
54 json_t *json_string_nocheck(const char *value);
55 json_t *json_integer(long value);
56 json_t *json_real(double value);
57 json_t *json_true(void);
58 json_t *json_false(void);
59 json_t *json_null(void);
62 json_t *json_incref(json_t *json)
64 if(json && json->refcount != (size_t)-1)
69 /* do not call json_delete directly */
70 void json_delete(json_t *json);
73 void json_decref(json_t *json)
75 if(json && json->refcount != (size_t)-1 && --json->refcount == 0)
80 /* getters, setters, manipulation */
82 size_t json_object_size(const json_t *object);
83 json_t *json_object_get(const json_t *object, const char *key);
84 int json_object_set_new(json_t *object, const char *key, json_t *value);
85 int json_object_set_new_nocheck(json_t *object, const char *key, json_t *value);
86 int json_object_del(json_t *object, const char *key);
87 int json_object_clear(json_t *object);
88 int json_object_update(json_t *object, json_t *other);
89 void *json_object_iter(json_t *object);
90 void *json_object_iter_at(json_t *object, const char *key);
91 void *json_object_iter_next(json_t *object, void *iter);
92 const char *json_object_iter_key(void *iter);
93 json_t *json_object_iter_value(void *iter);
94 int json_object_iter_set_new(json_t *object, void *iter, json_t *value);
97 int json_object_set(json_t *object, const char *key, json_t *value)
99 return json_object_set_new(object, key, json_incref(value));
103 int json_object_set_nocheck(json_t *object, const char *key, json_t *value)
105 return json_object_set_new_nocheck(object, key, json_incref(value));
109 int json_object_iter_set(json_t *object, void *iter, json_t *value)
111 return json_object_iter_set_new(object, iter, json_incref(value));
114 size_t json_array_size(const json_t *array);
115 json_t *json_array_get(const json_t *array, size_t index);
116 int json_array_set_new(json_t *array, size_t index, json_t *value);
117 int json_array_append_new(json_t *array, json_t *value);
118 int json_array_insert_new(json_t *array, size_t index, json_t *value);
119 int json_array_remove(json_t *array, size_t index);
120 int json_array_clear(json_t *array);
121 int json_array_extend(json_t *array, json_t *other);
124 int json_array_set(json_t *array, size_t index, json_t *value)
126 return json_array_set_new(array, index, json_incref(value));
130 int json_array_append(json_t *array, json_t *value)
132 return json_array_append_new(array, json_incref(value));
136 int json_array_insert(json_t *array, size_t index, json_t *value)
138 return json_array_insert_new(array, index, json_incref(value));
141 const char *json_string_value(const json_t *string);
142 long json_integer_value(const json_t *integer);
143 double json_real_value(const json_t *real);
144 double json_number_value(const json_t *json);
146 int json_string_set(json_t *string, const char *value);
147 int json_string_set_nocheck(json_t *string, const char *value);
148 int json_integer_set(json_t *integer, long value);
149 int json_real_set(json_t *real, double value);
154 int json_equal(json_t *value1, json_t *value2);
159 json_t *json_copy(json_t *value);
160 json_t *json_deep_copy(json_t *value);
163 /* loading, printing */
165 #define JSON_ERROR_TEXT_LENGTH 160
168 char text[JSON_ERROR_TEXT_LENGTH];
172 json_t *json_loads(const char *input, json_error_t *error);
173 json_t *json_loadf(FILE *input, json_error_t *error);
174 json_t *json_load_file(const char *path, json_error_t *error);
176 #define JSON_INDENT(n) (n & 0xFF)
177 #define JSON_COMPACT 0x100
178 #define JSON_ENSURE_ASCII 0x200
179 #define JSON_SORT_KEYS 0x400
180 #define JSON_PRESERVE_ORDER 0x800
182 char *json_dumps(const json_t *json, size_t flags);
183 int json_dumpf(const json_t *json, FILE *output, size_t flags);
184 int json_dump_file(const json_t *json, const char *path, size_t flags);