LCOV - code coverage report
Current view: top level - usr/include - jansson.h (source / functions) Hit Total Coverage
Test: coverage report for v4-17-test 1498b464 Lines: 8 20 40.0 %
Date: 2024-06-13 04:01:37 Functions: 1 3 33.3 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2009-2016 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
      18             : extern "C" {
      19             : #endif
      20             : 
      21             : /* version */
      22             : 
      23             : #define JANSSON_MAJOR_VERSION  2
      24             : #define JANSSON_MINOR_VERSION  11
      25             : #define JANSSON_MICRO_VERSION  0
      26             : 
      27             : /* Micro version is omitted if it's 0 */
      28             : #define JANSSON_VERSION  "2.11"
      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             : /* If __atomic or __sync builtins are available the library is thread
      37             :  * safe for all read-only functions plus reference counting. */
      38             : #if JSON_HAVE_ATOMIC_BUILTINS || JSON_HAVE_SYNC_BUILTINS
      39             : #define JANSSON_THREAD_SAFE_REFCOUNT 1
      40             : #endif
      41             : 
      42             : /* types */
      43             : 
      44             : typedef enum {
      45             :     JSON_OBJECT,
      46             :     JSON_ARRAY,
      47             :     JSON_STRING,
      48             :     JSON_INTEGER,
      49             :     JSON_REAL,
      50             :     JSON_TRUE,
      51             :     JSON_FALSE,
      52             :     JSON_NULL
      53             : } json_type;
      54             : 
      55             : typedef struct json_t {
      56             :     json_type type;
      57             :     volatile size_t refcount;
      58             : } json_t;
      59             : 
      60             : #ifndef JANSSON_USING_CMAKE /* disabled if using cmake */
      61             : #if JSON_INTEGER_IS_LONG_LONG
      62             : #ifdef _WIN32
      63             : #define JSON_INTEGER_FORMAT "I64d"
      64             : #else
      65             : #define JSON_INTEGER_FORMAT "lld"
      66             : #endif
      67             : typedef long long json_int_t;
      68             : #else
      69             : #define JSON_INTEGER_FORMAT "ld"
      70             : typedef long json_int_t;
      71             : #endif /* JSON_INTEGER_IS_LONG_LONG */
      72             : #endif
      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_real(json)     ((json) && json_typeof(json) == JSON_REAL)
      80             : #define json_is_number(json)   (json_is_integer(json) || json_is_real(json))
      81             : #define json_is_true(json)     ((json) && json_typeof(json) == JSON_TRUE)
      82             : #define json_is_false(json)    ((json) && json_typeof(json) == JSON_FALSE)
      83             : #define json_boolean_value     json_is_true
      84             : #define json_is_boolean(json)  (json_is_true(json) || json_is_false(json))
      85             : #define json_is_null(json)     ((json) && json_typeof(json) == JSON_NULL)
      86             : 
      87             : /* construction, destruction, reference counting */
      88             : 
      89             : json_t *json_object(void);
      90             : json_t *json_array(void);
      91             : json_t *json_string(const char *value);
      92             : json_t *json_stringn(const char *value, size_t len);
      93             : json_t *json_string_nocheck(const char *value);
      94             : json_t *json_stringn_nocheck(const char *value, size_t len);
      95             : json_t *json_integer(json_int_t value);
      96             : json_t *json_real(double value);
      97             : json_t *json_true(void);
      98             : json_t *json_false(void);
      99             : #define json_boolean(val)      ((val) ? json_true() : json_false())
     100             : json_t *json_null(void);
     101             : 
     102             : /* do not call JSON_INTERNAL_INCREF or JSON_INTERNAL_DECREF directly */
     103             : #if JSON_HAVE_ATOMIC_BUILTINS
     104             : #define JSON_INTERNAL_INCREF(json) __atomic_add_fetch(&json->refcount, 1, __ATOMIC_ACQUIRE)
     105             : #define JSON_INTERNAL_DECREF(json) __atomic_sub_fetch(&json->refcount, 1, __ATOMIC_RELEASE)
     106             : #elif JSON_HAVE_SYNC_BUILTINS
     107             : #define JSON_INTERNAL_INCREF(json) __sync_add_and_fetch(&json->refcount, 1)
     108             : #define JSON_INTERNAL_DECREF(json) __sync_sub_and_fetch(&json->refcount, 1)
     109             : #else
     110             : #define JSON_INTERNAL_INCREF(json) (++json->refcount)
     111             : #define JSON_INTERNAL_DECREF(json) (--json->refcount)
     112             : #endif
     113             : 
     114             : static JSON_INLINE
     115           0 : json_t *json_incref(json_t *json)
     116             : {
     117           0 :     if(json && json->refcount != (size_t)-1)
     118           0 :         JSON_INTERNAL_INCREF(json);
     119           0 :     return json;
     120             : }
     121             : 
     122           0 : /* do not call json_delete directly */
     123           0 : void json_delete(json_t *json);
     124           0 : 
     125           0 : static JSON_INLINE
     126      367666 : void json_decref(json_t *json)
     127             : {
     128      367666 :     if(json && json->refcount != (size_t)-1 && JSON_INTERNAL_DECREF(json) == 0)
     129      367666 :         json_delete(json);
     130      367666 : }
     131       60258 : 
     132       60258 : #if defined(__GNUC__) || defined(__clang__)
     133       60258 : static JSON_INLINE
     134       60258 : void json_decrefp(json_t **json)
     135             : {
     136             :     if(json) {
     137             :         json_decref(*json);
     138             :         *json = NULL;
     139             :     }
     140             : }
     141             : 
     142             : #define json_auto_t json_t __attribute__((cleanup(json_decrefp)))
     143             : #endif
     144             : 
     145             : 
     146             : /* error reporting */
     147             : 
     148             : #define JSON_ERROR_TEXT_LENGTH    160
     149             : #define JSON_ERROR_SOURCE_LENGTH   80
     150             : 
     151             : typedef struct json_error_t {
     152             :     int line;
     153             :     int column;
     154             :     int position;
     155             :     char source[JSON_ERROR_SOURCE_LENGTH];
     156             :     char text[JSON_ERROR_TEXT_LENGTH];
     157             : } json_error_t;
     158             : 
     159             : enum json_error_code {
     160             :     json_error_unknown,
     161             :     json_error_out_of_memory,
     162             :     json_error_stack_overflow,
     163             :     json_error_cannot_open_file,
     164             :     json_error_invalid_argument,
     165             :     json_error_invalid_utf8,
     166             :     json_error_premature_end_of_input,
     167             :     json_error_end_of_input_expected,
     168             :     json_error_invalid_syntax,
     169             :     json_error_invalid_format,
     170             :     json_error_wrong_type,
     171             :     json_error_null_character,
     172             :     json_error_null_value,
     173             :     json_error_null_byte_in_key,
     174             :     json_error_duplicate_key,
     175             :     json_error_numeric_overflow,
     176             :     json_error_item_not_found,
     177             :     json_error_index_out_of_range
     178             : };
     179             : 
     180             : static JSON_INLINE enum json_error_code json_error_code(const json_error_t *e) {
     181             :     return (enum json_error_code)e->text[JSON_ERROR_TEXT_LENGTH - 1];
     182             : }
     183             : 
     184             : /* getters, setters, manipulation */
     185             : 
     186             : void json_object_seed(size_t seed);
     187             : size_t json_object_size(const json_t *object);
     188             : json_t *json_object_get(const json_t *object, const char *key);
     189             : int json_object_set_new(json_t *object, const char *key, json_t *value);
     190             : int json_object_set_new_nocheck(json_t *object, const char *key, json_t *value);
     191             : int json_object_del(json_t *object, const char *key);
     192             : int json_object_clear(json_t *object);
     193             : int json_object_update(json_t *object, json_t *other);
     194             : int json_object_update_existing(json_t *object, json_t *other);
     195             : int json_object_update_missing(json_t *object, json_t *other);
     196             : void *json_object_iter(json_t *object);
     197             : void *json_object_iter_at(json_t *object, const char *key);
     198             : void *json_object_key_to_iter(const char *key);
     199             : void *json_object_iter_next(json_t *object, void *iter);
     200             : const char *json_object_iter_key(void *iter);
     201             : json_t *json_object_iter_value(void *iter);
     202             : int json_object_iter_set_new(json_t *object, void *iter, json_t *value);
     203             : 
     204             : #define json_object_foreach(object, key, value) \
     205             :     for(key = json_object_iter_key(json_object_iter(object)); \
     206             :         key && (value = json_object_iter_value(json_object_key_to_iter(key))); \
     207             :         key = json_object_iter_key(json_object_iter_next(object, json_object_key_to_iter(key))))
     208             : 
     209             : #define json_object_foreach_safe(object, n, key, value)     \
     210             :     for(key = json_object_iter_key(json_object_iter(object)), \
     211             :             n = json_object_iter_next(object, json_object_key_to_iter(key)); \
     212             :         key && (value = json_object_iter_value(json_object_key_to_iter(key))); \
     213             :         key = json_object_iter_key(n), \
     214             :             n = json_object_iter_next(object, json_object_key_to_iter(key)))
     215             : 
     216             : #define json_array_foreach(array, index, value) \
     217             :         for(index = 0; \
     218             :                 index < json_array_size(array) && (value = json_array_get(array, index)); \
     219             :                 index++)
     220             : 
     221             : static JSON_INLINE
     222           0 : int json_object_set(json_t *object, const char *key, json_t *value)
     223             : {
     224           0 :     return json_object_set_new(object, key, json_incref(value));
     225           0 : }
     226           0 : 
     227             : static JSON_INLINE
     228             : int json_object_set_nocheck(json_t *object, const char *key, json_t *value)
     229             : {
     230             :     return json_object_set_new_nocheck(object, key, json_incref(value));
     231             : }
     232             : 
     233             : static JSON_INLINE
     234             : int json_object_iter_set(json_t *object, void *iter, json_t *value)
     235             : {
     236             :     return json_object_iter_set_new(object, iter, json_incref(value));
     237             : }
     238             : 
     239             : size_t json_array_size(const json_t *array);
     240             : json_t *json_array_get(const json_t *array, size_t index);
     241             : int json_array_set_new(json_t *array, size_t index, json_t *value);
     242             : int json_array_append_new(json_t *array, json_t *value);
     243             : int json_array_insert_new(json_t *array, size_t index, json_t *value);
     244             : int json_array_remove(json_t *array, size_t index);
     245             : int json_array_clear(json_t *array);
     246             : int json_array_extend(json_t *array, json_t *other);
     247             : 
     248             : static JSON_INLINE
     249             : int json_array_set(json_t *array, size_t ind, json_t *value)
     250             : {
     251             :     return json_array_set_new(array, ind, json_incref(value));
     252             : }
     253             : 
     254             : static JSON_INLINE
     255             : int json_array_append(json_t *array, json_t *value)
     256             : {
     257             :     return json_array_append_new(array, json_incref(value));
     258             : }
     259             : 
     260             : static JSON_INLINE
     261             : int json_array_insert(json_t *array, size_t ind, json_t *value)
     262             : {
     263             :     return json_array_insert_new(array, ind, json_incref(value));
     264             : }
     265             : 
     266             : const char *json_string_value(const json_t *string);
     267             : size_t json_string_length(const json_t *string);
     268             : json_int_t json_integer_value(const json_t *integer);
     269             : double json_real_value(const json_t *real);
     270             : double json_number_value(const json_t *json);
     271             : 
     272             : int json_string_set(json_t *string, const char *value);
     273             : int json_string_setn(json_t *string, const char *value, size_t len);
     274             : int json_string_set_nocheck(json_t *string, const char *value);
     275             : int json_string_setn_nocheck(json_t *string, const char *value, size_t len);
     276             : int json_integer_set(json_t *integer, json_int_t value);
     277             : int json_real_set(json_t *real, double value);
     278             : 
     279             : /* pack, unpack */
     280             : 
     281             : json_t *json_pack(const char *fmt, ...);
     282             : json_t *json_pack_ex(json_error_t *error, size_t flags, const char *fmt, ...);
     283             : json_t *json_vpack_ex(json_error_t *error, size_t flags, const char *fmt, va_list ap);
     284             : 
     285             : #define JSON_VALIDATE_ONLY  0x1
     286             : #define JSON_STRICT         0x2
     287             : 
     288             : int json_unpack(json_t *root, const char *fmt, ...);
     289             : int json_unpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, ...);
     290             : int json_vunpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, va_list ap);
     291             : 
     292             : /* sprintf */
     293             : 
     294             : json_t *json_sprintf(const char *fmt, ...);
     295             : json_t *json_vsprintf(const char *fmt, va_list ap);
     296             : 
     297             : 
     298             : /* equality */
     299             : 
     300             : int json_equal(const json_t *value1, const json_t *value2);
     301             : 
     302             : 
     303             : /* copying */
     304             : 
     305             : json_t *json_copy(json_t *value);
     306             : json_t *json_deep_copy(const json_t *value);
     307             : 
     308             : 
     309             : /* decoding */
     310             : 
     311             : #define JSON_REJECT_DUPLICATES  0x1
     312             : #define JSON_DISABLE_EOF_CHECK  0x2
     313             : #define JSON_DECODE_ANY         0x4
     314             : #define JSON_DECODE_INT_AS_REAL 0x8
     315             : #define JSON_ALLOW_NUL          0x10
     316             : 
     317             : typedef size_t (*json_load_callback_t)(void *buffer, size_t buflen, void *data);
     318             : 
     319             : json_t *json_loads(const char *input, size_t flags, json_error_t *error);
     320             : json_t *json_loadb(const char *buffer, size_t buflen, size_t flags, json_error_t *error);
     321             : json_t *json_loadf(FILE *input, size_t flags, json_error_t *error);
     322             : json_t *json_loadfd(int input, size_t flags, json_error_t *error);
     323             : json_t *json_load_file(const char *path, size_t flags, json_error_t *error);
     324             : json_t *json_load_callback(json_load_callback_t callback, void *data, size_t flags, json_error_t *error);
     325             : 
     326             : 
     327             : /* encoding */
     328             : 
     329             : #define JSON_MAX_INDENT         0x1F
     330             : #define JSON_INDENT(n)          ((n) & JSON_MAX_INDENT)
     331             : #define JSON_COMPACT            0x20
     332             : #define JSON_ENSURE_ASCII       0x40
     333             : #define JSON_SORT_KEYS          0x80
     334             : #define JSON_PRESERVE_ORDER     0x100
     335             : #define JSON_ENCODE_ANY         0x200
     336             : #define JSON_ESCAPE_SLASH       0x400
     337             : #define JSON_REAL_PRECISION(n)  (((n) & 0x1F) << 11)
     338             : #define JSON_EMBED              0x10000
     339             : 
     340             : typedef int (*json_dump_callback_t)(const char *buffer, size_t size, void *data);
     341             : 
     342             : char *json_dumps(const json_t *json, size_t flags);
     343             : size_t json_dumpb(const json_t *json, char *buffer, size_t size, size_t flags);
     344             : int json_dumpf(const json_t *json, FILE *output, size_t flags);
     345             : int json_dumpfd(const json_t *json, int output, size_t flags);
     346             : int json_dump_file(const json_t *json, const char *path, size_t flags);
     347             : int json_dump_callback(const json_t *json, json_dump_callback_t callback, void *data, size_t flags);
     348             : 
     349             : /* custom memory allocation */
     350             : 
     351             : typedef void *(*json_malloc_t)(size_t);
     352             : typedef void (*json_free_t)(void *);
     353             : 
     354             : void json_set_alloc_funcs(json_malloc_t malloc_fn, json_free_t free_fn);
     355             : void json_get_alloc_funcs(json_malloc_t *malloc_fn, json_free_t *free_fn);
     356             : 
     357             : #ifdef __cplusplus
     358             : }
     359             : #endif
     360             : 
     361             : #endif

Generated by: LCOV version 1.13