Path: blob/master/compat/jansson/jansson.h
1302 views
/*1* Copyright (c) 2009-2013 Petri Lehtinen <[email protected]>2*3* Jansson is free software; you can redistribute it and/or modify4* it under the terms of the MIT license. See LICENSE for details.5*/67#ifndef JANSSON_H8#define JANSSON_H910#include <stdio.h>11#include <stdlib.h> /* for size_t */12#include <stdarg.h>1314#include <jansson_config.h>1516#ifdef __cplusplus17extern "C" {18#endif1920/* version */2122#define JANSSON_MAJOR_VERSION 223#define JANSSON_MINOR_VERSION 624#define JANSSON_MICRO_VERSION 02526/* Micro version is omitted if it's 0 */27#define JANSSON_VERSION "2.6"2829/* Version as a 3-byte hex number, e.g. 0x010201 == 1.2.1. Use this30for numeric comparisons, e.g. #if JANSSON_VERSION_HEX >= ... */31#define JANSSON_VERSION_HEX ((JANSSON_MAJOR_VERSION << 16) | \32(JANSSON_MINOR_VERSION << 8) | \33(JANSSON_MICRO_VERSION << 0))343536/* types */3738typedef enum {39JSON_OBJECT,40JSON_ARRAY,41JSON_STRING,42JSON_INTEGER,43JSON_REAL,44JSON_TRUE,45JSON_FALSE,46JSON_NULL47} json_type;4849typedef struct json_t {50json_type type;51size_t refcount;52} json_t;5354#ifndef JANSSON_USING_CMAKE /* disabled if using cmake */55#if JSON_INTEGER_IS_LONG_LONG56#ifdef _WIN3257#define JSON_INTEGER_FORMAT "I64d"58#else59#define JSON_INTEGER_FORMAT "lld"60#endif61typedef long long json_int_t;62#else63#define JSON_INTEGER_FORMAT "ld"64typedef long json_int_t;65#endif /* JSON_INTEGER_IS_LONG_LONG */66#endif6768#define json_typeof(json) ((json)->type)69#define json_is_object(json) (json && json_typeof(json) == JSON_OBJECT)70#define json_is_array(json) (json && json_typeof(json) == JSON_ARRAY)71#define json_is_string(json) (json && json_typeof(json) == JSON_STRING)72#define json_is_integer(json) (json && json_typeof(json) == JSON_INTEGER)73#define json_is_real(json) (json && json_typeof(json) == JSON_REAL)74#define json_is_number(json) (json_is_integer(json) || json_is_real(json))75#define json_is_true(json) (json && json_typeof(json) == JSON_TRUE)76#define json_is_false(json) (json && json_typeof(json) == JSON_FALSE)77#define json_is_boolean(json) (json_is_true(json) || json_is_false(json))78#define json_is_null(json) (json && json_typeof(json) == JSON_NULL)7980/* construction, destruction, reference counting */8182json_t *json_object(void);83json_t *json_array(void);84json_t *json_string(const char *value);85json_t *json_string_nocheck(const char *value);86json_t *json_integer(json_int_t value);87json_t *json_real(double value);88json_t *json_true(void);89json_t *json_false(void);90#define json_boolean(val) ((val) ? json_true() : json_false())91json_t *json_null(void);9293static JSON_INLINE94json_t *json_incref(json_t *json)95{96if(json && json->refcount != (size_t)-1)97++json->refcount;98return json;99}100101/* do not call json_delete directly */102void json_delete(json_t *json);103104static JSON_INLINE105void json_decref(json_t *json)106{107if(json && json->refcount != (size_t)-1 && --json->refcount == 0)108json_delete(json);109}110111112/* error reporting */113114#define JSON_ERROR_TEXT_LENGTH 160115#define JSON_ERROR_SOURCE_LENGTH 80116117typedef struct {118int line;119int column;120int position;121char source[JSON_ERROR_SOURCE_LENGTH];122char text[JSON_ERROR_TEXT_LENGTH];123} json_error_t;124125126/* getters, setters, manipulation */127128size_t json_object_size(const json_t *object);129json_t *json_object_get(const json_t *object, const char *key);130int json_object_set_new(json_t *object, const char *key, json_t *value);131int json_object_set_new_nocheck(json_t *object, const char *key, json_t *value);132int json_object_del(json_t *object, const char *key);133int json_object_clear(json_t *object);134int json_object_update(json_t *object, json_t *other);135int json_object_update_existing(json_t *object, json_t *other);136int json_object_update_missing(json_t *object, json_t *other);137void *json_object_iter(json_t *object);138void *json_object_iter_at(json_t *object, const char *key);139void *json_object_key_to_iter(const char *key);140void *json_object_iter_next(json_t *object, void *iter);141const char *json_object_iter_key(void *iter);142json_t *json_object_iter_value(void *iter);143int json_object_iter_set_new(json_t *object, void *iter, json_t *value);144145#define json_object_foreach(object, key, value) \146for(key = json_object_iter_key(json_object_iter(object)); \147key && (value = json_object_iter_value(json_object_key_to_iter(key))); \148key = json_object_iter_key(json_object_iter_next(object, json_object_key_to_iter(key))))149150#define json_array_foreach(array, index, value) \151for(index = 0; \152index < json_array_size(array) && (value = json_array_get(array, index)); \153index++)154155static JSON_INLINE156int json_object_set(json_t *object, const char *key, json_t *value)157{158return json_object_set_new(object, key, json_incref(value));159}160161static JSON_INLINE162int json_object_set_nocheck(json_t *object, const char *key, json_t *value)163{164return json_object_set_new_nocheck(object, key, json_incref(value));165}166167static JSON_INLINE168int json_object_iter_set(json_t *object, void *iter, json_t *value)169{170return json_object_iter_set_new(object, iter, json_incref(value));171}172173size_t json_array_size(const json_t *array);174json_t *json_array_get(const json_t *array, size_t index);175int json_array_set_new(json_t *array, size_t index, json_t *value);176int json_array_append_new(json_t *array, json_t *value);177int json_array_insert_new(json_t *array, size_t index, json_t *value);178int json_array_remove(json_t *array, size_t index);179int json_array_clear(json_t *array);180int json_array_extend(json_t *array, json_t *other);181182static JSON_INLINE183int json_array_set(json_t *array, size_t ind, json_t *value)184{185return json_array_set_new(array, ind, json_incref(value));186}187188static JSON_INLINE189int json_array_append(json_t *array, json_t *value)190{191return json_array_append_new(array, json_incref(value));192}193194static JSON_INLINE195int json_array_insert(json_t *array, size_t ind, json_t *value)196{197return json_array_insert_new(array, ind, json_incref(value));198}199200const char *json_string_value(const json_t *string);201json_int_t json_integer_value(const json_t *integer);202double json_real_value(const json_t *real);203double json_number_value(const json_t *json);204205int json_string_set(json_t *string, const char *value);206int json_string_set_nocheck(json_t *string, const char *value);207int json_integer_set(json_t *integer, json_int_t value);208int json_real_set(json_t *real, double value);209210211/* pack, unpack */212213json_t *json_pack(const char *fmt, ...);214json_t *json_pack_ex(json_error_t *error, size_t flags, const char *fmt, ...);215json_t *json_vpack_ex(json_error_t *error, size_t flags, const char *fmt, va_list ap);216217#define JSON_VALIDATE_ONLY 0x1218#define JSON_STRICT 0x2219220int json_unpack(json_t *root, const char *fmt, ...);221int json_unpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, ...);222int json_vunpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, va_list ap);223224225/* equality */226227int json_equal(json_t *value1, json_t *value2);228229230/* copying */231232json_t *json_copy(json_t *value);233json_t *json_deep_copy(const json_t *value);234235236/* decoding */237238#define JSON_REJECT_DUPLICATES 0x1239#define JSON_DISABLE_EOF_CHECK 0x2240#define JSON_DECODE_ANY 0x4241#define JSON_DECODE_INT_AS_REAL 0x8242243typedef size_t (*json_load_callback_t)(void *buffer, size_t buflen, void *data);244245json_t *json_loads(const char *input, size_t flags, json_error_t *error);246json_t *json_loadb(const char *buffer, size_t buflen, size_t flags, json_error_t *error);247json_t *json_loadf(FILE *input, size_t flags, json_error_t *error);248json_t *json_load_file(const char *path, size_t flags, json_error_t *error);249json_t *json_load_callback(json_load_callback_t callback, void *data, size_t flags, json_error_t *error);250251252/* encoding */253254#define JSON_INDENT(n) (n & 0x1F)255#define JSON_COMPACT 0x20256#define JSON_ENSURE_ASCII 0x40257#define JSON_SORT_KEYS 0x80258#define JSON_PRESERVE_ORDER 0x100259#define JSON_ENCODE_ANY 0x200260#define JSON_ESCAPE_SLASH 0x400261262typedef int (*json_dump_callback_t)(const char *buffer, size_t size, void *data);263264char *json_dumps(const json_t *json, size_t flags);265int json_dumpf(const json_t *json, FILE *output, size_t flags);266int json_dump_file(const json_t *json, const char *path, size_t flags);267int json_dump_callback(const json_t *json, json_dump_callback_t callback, void *data, size_t flags);268269/* custom memory allocation */270271typedef void *(*json_malloc_t)(size_t);272typedef void (*json_free_t)(void *);273274void json_set_alloc_funcs(json_malloc_t malloc_fn, json_free_t free_fn);275276#ifdef __cplusplus277}278#endif279280#endif281282283