PageRenderTime 345ms CodeModel.GetById 181ms app.highlight 18ms RepoModel.GetById 143ms app.codeStats 0ms

/jansson/src/jansson.h

http://github.com/nicolasff/webdis
C++ Header | 222 lines | 157 code | 47 blank | 18 comment | 26 complexity | 35f8bd1f472f8706a18faabfa68f7db3 MD5 | raw file
  1/*
  2 * Copyright (c) 2009, 2010 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 <stdlib.h>  /* for size_t */
 13#include <jansson_config.h>
 14
 15#ifdef __cplusplus
 16extern "C" {
 17#endif
 18
 19/* version */
 20
 21#define JANSSON_MAJOR_VERSION  1
 22#define JANSSON_MINOR_VERSION  3
 23#define JANSSON_MICRO_VERSION  0
 24
 25/* Micro version is omitted if it's 0 */
 26#define JANSSON_VERSION  "1.3"
 27
 28/* Version as a 3-byte hex number, e.g. 0x010201 == 1.2.1. Use this
 29   for numeric comparisons, e.g. #if JANSSON_VERSION_HEX >= ... */
 30#define JANSSON_VERSION_HEX  ((JANSSON_MAJOR_VERSION << 16) |   \
 31                              (JANSSON_MINOR_VERSION << 8)  |   \
 32                              (JANSSON_MICRO_VERSION << 0)))
 33
 34
 35/* types */
 36
 37typedef enum {
 38    JSON_OBJECT,
 39    JSON_ARRAY,
 40    JSON_STRING,
 41    JSON_INTEGER,
 42    JSON_REAL,
 43    JSON_TRUE,
 44    JSON_FALSE,
 45    JSON_NULL
 46} json_type;
 47
 48typedef struct {
 49    json_type type;
 50    size_t refcount;
 51} json_t;
 52
 53#if JSON_INTEGER_IS_LONG_LONG
 54#define JSON_INTEGER_FORMAT "lld"
 55typedef long long json_int_t;
 56#else
 57#define JSON_INTEGER_FORMAT "ld"
 58typedef long json_int_t;
 59#endif /* JSON_INTEGER_IS_LONG_LONG */
 60
 61#define json_typeof(json)      ((json)->type)
 62#define json_is_object(json)   (json && json_typeof(json) == JSON_OBJECT)
 63#define json_is_array(json)    (json && json_typeof(json) == JSON_ARRAY)
 64#define json_is_string(json)   (json && json_typeof(json) == JSON_STRING)
 65#define json_is_integer(json)  (json && json_typeof(json) == JSON_INTEGER)
 66#define json_is_real(json)     (json && json_typeof(json) == JSON_REAL)
 67#define json_is_number(json)   (json_is_integer(json) || json_is_real(json))
 68#define json_is_true(json)     (json && json_typeof(json) == JSON_TRUE)
 69#define json_is_false(json)    (json && json_typeof(json) == JSON_FALSE)
 70#define json_is_boolean(json)  (json_is_true(json) || json_is_false(json))
 71#define json_is_null(json)     (json && json_typeof(json) == JSON_NULL)
 72
 73/* construction, destruction, reference counting */
 74
 75json_t *json_object(void);
 76json_t *json_array(void);
 77json_t *json_string(const char *value);
 78json_t *json_string_nocheck(const char *value);
 79json_t *json_integer(json_int_t value);
 80json_t *json_real(double value);
 81json_t *json_true(void);
 82json_t *json_false(void);
 83json_t *json_null(void);
 84
 85static JSON_INLINE
 86json_t *json_incref(json_t *json)
 87{
 88    if(json && json->refcount != (size_t)-1)
 89        ++json->refcount;
 90    return json;
 91}
 92
 93/* do not call json_delete directly */
 94void json_delete(json_t *json);
 95
 96static JSON_INLINE
 97void json_decref(json_t *json)
 98{
 99    if(json && json->refcount != (size_t)-1 && --json->refcount == 0)
100        json_delete(json);
101}
102
103
104/* error reporting */
105
106#define JSON_ERROR_TEXT_LENGTH    160
107#define JSON_ERROR_SOURCE_LENGTH   80
108
109typedef struct {
110    char text[JSON_ERROR_TEXT_LENGTH];
111    int line;
112    int column;
113    char source[JSON_ERROR_SOURCE_LENGTH];
114} json_error_t;
115
116
117/* getters, setters, manipulation */
118
119size_t json_object_size(const json_t *object);
120json_t *json_object_get(const json_t *object, const char *key);
121int json_object_set_new(json_t *object, const char *key, json_t *value);
122int json_object_set_new_nocheck(json_t *object, const char *key, json_t *value);
123int json_object_del(json_t *object, const char *key);
124int json_object_clear(json_t *object);
125int json_object_update(json_t *object, json_t *other);
126void *json_object_iter(json_t *object);
127void *json_object_iter_at(json_t *object, const char *key);
128void *json_object_iter_next(json_t *object, void *iter);
129const char *json_object_iter_key(void *iter);
130json_t *json_object_iter_value(void *iter);
131int json_object_iter_set_new(json_t *object, void *iter, json_t *value);
132
133static JSON_INLINE
134int json_object_set(json_t *object, const char *key, json_t *value)
135{
136    return json_object_set_new(object, key, json_incref(value));
137}
138
139static JSON_INLINE
140int json_object_set_nocheck(json_t *object, const char *key, json_t *value)
141{
142    return json_object_set_new_nocheck(object, key, json_incref(value));
143}
144
145static JSON_INLINE
146int json_object_iter_set(json_t *object, void *iter, json_t *value)
147{
148    return json_object_iter_set_new(object, iter, json_incref(value));
149}
150
151size_t json_array_size(const json_t *array);
152json_t *json_array_get(const json_t *array, size_t index);
153int json_array_set_new(json_t *array, size_t index, json_t *value);
154int json_array_append_new(json_t *array, json_t *value);
155int json_array_insert_new(json_t *array, size_t index, json_t *value);
156int json_array_remove(json_t *array, size_t index);
157int json_array_clear(json_t *array);
158int json_array_extend(json_t *array, json_t *other);
159
160static JSON_INLINE
161int json_array_set(json_t *array, size_t index, json_t *value)
162{
163    return json_array_set_new(array, index, json_incref(value));
164}
165
166static JSON_INLINE
167int json_array_append(json_t *array, json_t *value)
168{
169    return json_array_append_new(array, json_incref(value));
170}
171
172static JSON_INLINE
173int json_array_insert(json_t *array, size_t index, json_t *value)
174{
175    return json_array_insert_new(array, index, json_incref(value));
176}
177
178const char *json_string_value(const json_t *string);
179json_int_t json_integer_value(const json_t *integer);
180double json_real_value(const json_t *real);
181double json_number_value(const json_t *json);
182
183int json_string_set(json_t *string, const char *value);
184int json_string_set_nocheck(json_t *string, const char *value);
185int json_integer_set(json_t *integer, json_int_t value);
186int json_real_set(json_t *real, double value);
187
188json_t *json_pack(json_error_t *error, const char *fmt, ...);
189int json_unpack(json_t *root, json_error_t *error, const char *fmt, ...);
190
191/* equality */
192
193int json_equal(json_t *value1, json_t *value2);
194
195
196/* copying */
197
198json_t *json_copy(json_t *value);
199json_t *json_deep_copy(json_t *value);
200
201
202/* loading, printing */
203
204json_t *json_loads(const char *input, size_t flags, json_error_t *error);
205json_t *json_loadf(FILE *input, size_t flags, json_error_t *error);
206json_t *json_load_file(const char *path, size_t flags, json_error_t *error);
207
208#define JSON_INDENT(n)      (n & 0x1F)
209#define JSON_COMPACT        0x20
210#define JSON_ENSURE_ASCII   0x40
211#define JSON_SORT_KEYS      0x80
212#define JSON_PRESERVE_ORDER 0x100
213
214char *json_dumps(const json_t *json, size_t flags);
215int json_dumpf(const json_t *json, FILE *output, size_t flags);
216int json_dump_file(const json_t *json, const char *path, size_t flags);
217
218#ifdef __cplusplus
219}
220#endif
221
222#endif