1/* 2 * Copyright (c) 2009-2014 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 <stdarg.h> 14 15#include <jansson_config.h> 16 17#ifdef __cplusplus 18extern "C" { 19#endif 20 21/* version */ 22 23#define JANSSON_MAJOR_VERSION 2 24#define JANSSON_MINOR_VERSION 7 25#define JANSSON_MICRO_VERSION 0 26 27/* Micro version is omitted if it's 0 */ 28#define JANSSON_VERSION "2.7" 29 30/* Version as a 3-byte hex number, e.g. 0x010201 == 1.2.1. Use this 31 for numeric comparisons, e.g. #if JANSSON_VERSION_HEX >= ... */ 32#define JANSSON_VERSION_HEX ((JANSSON_MAJOR_VERSION << 16) | \ 33 (JANSSON_MINOR_VERSION << 8) | \ 34 (JANSSON_MICRO_VERSION << 0)) 35 36 37/* types */ 38 39typedef enum { 40 JSON_OBJECT, 41 JSON_ARRAY, 42 JSON_STRING, 43 JSON_INTEGER, 44 JSON_NATURAL, 45 JSON_REAL, 46 JSON_TRUE, 47 JSON_FALSE, 48 JSON_NULL 49} json_type; 50 51typedef struct json_t { 52 json_type type; 53 size_t refcount; 54} json_t; 55 56#ifndef JANSSON_USING_CMAKE /* disabled if using cmake */ 57#if JSON_INTEGER_IS_LONG_LONG 58#ifdef _WIN32 59#define JSON_INTEGER_FORMAT "I64d" 60#define JSON_NATURAL_FORMAT "I64u" 61#else 62#define JSON_INTEGER_FORMAT "lld" 63#define JSON_NATURAL_FORMAT "llu" 64#endif 65typedef long long json_int_t; 66#else 67#define JSON_INTEGER_FORMAT "ld" 68typedef long json_int_t; 69#define JSON_NATURAL_FORMAT "lu" 70#endif /* JSON_INTEGER_IS_LONG_LONG */ 71#endif 72 73 74#define json_typeof(json) ((json)->type) 75#define json_is_object(json) ((json) && json_typeof(json) == JSON_OBJECT) 76#define json_is_array(json) ((json) && json_typeof(json) == JSON_ARRAY) 77#define json_is_string(json) ((json) && json_typeof(json) == JSON_STRING) 78#define json_is_integer(json) ((json) && json_typeof(json) == JSON_INTEGER) 79#define json_is_natural(json) ((json) && json_typeof(json) == JSON_NATURAL) 80#define json_is_real(json) ((json) && json_typeof(json) == JSON_REAL) 81#define json_is_number(json) (json_is_integer(json) || json_is_real(json) || \ 82 json_is_natural(json)) 83#define json_is_true(json) ((json) && json_typeof(json) == JSON_TRUE) 84#define json_is_false(json) ((json) && json_typeof(json) == JSON_FALSE) 85#define json_boolean_value json_is_true 86#define json_is_boolean(json) (json_is_true(json) || json_is_false(json)) 87#define json_is_null(json) ((json) && json_typeof(json) == JSON_NULL) 88 89/* construction, destruction, reference counting */ 90 91json_t *json_object(void); 92json_t *json_array(void); 93json_t *json_string(const char *value); 94json_t *json_stringn(const char *value, size_t len); 95json_t *json_string_nocheck(const char *value); 96json_t *json_stringn_nocheck(const char *value, size_t len); 97json_t *json_integer(json_int_t value); 98json_t *json_real(double value); 99json_t *json_true(void); 100json_t *json_false(void); 101#define json_boolean(val) ((val) ? json_true() : json_false()) 102json_t *json_null(void); 103 104static JSON_INLINE 105json_t *json_incref(json_t *json) 106{ 107 if(json && json->refcount != (size_t)-1) 108 ++json->refcount; 109 return json; 110} 111 112/* do not call json_delete directly */ 113void json_delete(json_t *json); 114 115static JSON_INLINE 116void json_decref(json_t *json) 117{ 118 if(json && json->refcount != (size_t)-1 && --json->refcount == 0) 119 json_delete(json); 120} 121 122 123/* error reporting */ 124 125#define JSON_ERROR_TEXT_LENGTH 160 126#define JSON_ERROR_SOURCE_LENGTH 80 127 128typedef struct { 129 int line; 130 int column; 131 int position; 132 char source[JSON_ERROR_SOURCE_LENGTH]; 133 char text[JSON_ERROR_TEXT_LENGTH]; 134} json_error_t; 135 136 137/* getters, setters, manipulation */ 138 139void json_object_seed(size_t seed); 140size_t json_object_size(const json_t *object); 141json_t *json_object_get(const json_t *object, const char *key); 142int json_object_set_new(json_t *object, const char *key, json_t *value); 143int json_object_set_new_nocheck(json_t *object, const char *key, json_t *value); 144int json_object_del(json_t *object, const char *key); 145int json_object_clear(json_t *object); 146int json_object_update(json_t *object, json_t *other); 147int json_object_update_existing(json_t *object, json_t *other); 148int json_object_update_missing(json_t *object, json_t *other); 149void *json_object_iter(json_t *object); 150void *json_object_iter_at(json_t *object, const char *key); 151void *json_object_key_to_iter(const char *key); 152void *json_object_iter_next(json_t *object, void *iter); 153const char *json_object_iter_key(void *iter); 154json_t *json_object_iter_value(void *iter); 155int json_object_iter_set_new(json_t *object, void *iter, json_t *value); 156 157#define json_object_foreach(object, key, value) \ 158 for(key = json_object_iter_key(json_object_iter(object)); \ 159 key && (value = json_object_iter_value(json_object_key_to_iter(key))); \ 160 key = json_object_iter_key(json_object_iter_next(object, json_object_key_to_iter(key)))) 161 162#define json_array_foreach(array, index, value) \ 163 for(index = 0; \ 164 index < json_array_size(array) && (value = json_array_get(array, index)); \ 165 index++) 166 167static JSON_INLINE 168int json_object_set(json_t *object, const char *key, json_t *value) 169{ 170 return json_object_set_new(object, key, json_incref(value)); 171} 172 173static JSON_INLINE 174int json_object_set_nocheck(json_t *object, const char *key, json_t *value) 175{ 176 return json_object_set_new_nocheck(object, key, json_incref(value)); 177} 178 179static JSON_INLINE 180int json_object_iter_set(json_t *object, void *iter, json_t *value) 181{ 182 return json_object_iter_set_new(object, iter, json_incref(value)); 183} 184 185size_t json_array_size(const json_t *array); 186json_t *json_array_get(const json_t *array, size_t index); 187int json_array_set_new(json_t *array, size_t index, json_t *value); 188int json_array_append_new(json_t *array, json_t *value); 189int json_array_insert_new(json_t *array, size_t index, json_t *value); 190int json_array_remove(json_t *array, size_t index); 191int json_array_clear(json_t *array); 192int json_array_extend(json_t *array, json_t *other); 193 194static JSON_INLINE 195int json_array_set(json_t *array, size_t ind, json_t *value) 196{ 197 return json_array_set_new(array, ind, json_incref(value)); 198} 199 200static JSON_INLINE 201int json_array_append(json_t *array, json_t *value) 202{ 203 return json_array_append_new(array, json_incref(value)); 204} 205 206static JSON_INLINE 207int json_array_insert(json_t *array, size_t ind, json_t *value) 208{ 209 return json_array_insert_new(array, ind, json_incref(value)); 210} 211 212const char *json_string_value(const json_t *string); 213size_t json_string_length(const json_t *string); 214json_int_t json_integer_value(const json_t *integer); 215double json_real_value(const json_t *real); 216double json_number_value(const json_t *json); 217 218int json_string_set(json_t *string, const char *value); 219int json_string_setn(json_t *string, const char *value, size_t len); 220int json_string_set_nocheck(json_t *string, const char *value); 221int json_string_setn_nocheck(json_t *string, const char *value, size_t len); 222int json_integer_set(json_t *integer, json_int_t value); 223int json_real_set(json_t *real, double value); 224 225/* pack, unpack */ 226 227json_t *json_pack(const char *fmt, ...); 228json_t *json_pack_ex(json_error_t *error, size_t flags, const char *fmt, ...); 229json_t *json_vpack_ex(json_error_t *error, size_t flags, const char *fmt, va_list ap); 230 231#define JSON_VALIDATE_ONLY 0x1 232#define JSON_STRICT 0x2 233 234int json_unpack(json_t *root, const char *fmt, ...); 235int json_unpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, ...); 236int json_vunpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, va_list ap); 237 238 239/* equality */ 240 241int json_equal(json_t *value1, json_t *value2); 242 243 244/* copying */ 245 246json_t *json_copy(json_t *value); 247json_t *json_deep_copy(const json_t *value); 248 249 250/* decoding */ 251 252#define JSON_REJECT_DUPLICATES 0x1 253#define JSON_DISABLE_EOF_CHECK 0x2 254#define JSON_DECODE_ANY 0x4 255#define JSON_DECODE_INT_AS_REAL 0x8 256#define JSON_ALLOW_NUL 0x10 257 258typedef size_t (*json_load_callback_t)(void *buffer, size_t buflen, void *data); 259 260json_t *json_loads(const char *input, size_t flags, json_error_t *error); 261json_t *json_loadb(const char *buffer, size_t buflen, size_t flags, json_error_t *error); 262json_t *json_loadf(FILE *input, size_t flags, json_error_t *error); 263json_t *json_load_file(const char *path, size_t flags, json_error_t *error); 264json_t *json_load_callback(json_load_callback_t callback, void *data, size_t flags, json_error_t *error); 265 266 267/* encoding */ 268 269#define JSON_MAX_INDENT 0x1F 270#define JSON_INDENT(n) ((n) & JSON_MAX_INDENT) 271#define JSON_COMPACT 0x20 272#define JSON_ENSURE_ASCII 0x40 273#define JSON_SORT_KEYS 0x80 274#define JSON_PRESERVE_ORDER 0x100 275#define JSON_ENCODE_ANY 0x200 276#define JSON_ESCAPE_SLASH 0x400 277#define JSON_REAL_PRECISION(n) (((n) & 0x1F) << 11) 278 279typedef int (*json_dump_callback_t)(const char *buffer, size_t size, void *data); 280 281char *json_dumps(const json_t *json, size_t flags); 282int json_dumpf(const json_t *json, FILE *output, size_t flags); 283int json_dump_file(const json_t *json, const char *path, size_t flags); 284int json_dump_callback(const json_t *json, json_dump_callback_t callback, void *data, size_t flags); 285 286/* custom memory allocation */ 287 288typedef void *(*json_malloc_t)(size_t); 289typedef void (*json_free_t)(void *); 290 291void json_set_alloc_funcs(json_malloc_t malloc_fn, json_free_t free_fn); 292 293#ifdef __cplusplus 294} 295#endif 296 297#endif 298