aboutsummaryrefslogtreecommitdiff
path: root/src/jansson.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/jansson.h')
-rw-r--r--src/jansson.h134
1 files changed, 123 insertions, 11 deletions
diff --git a/src/jansson.h b/src/jansson.h
index c923a90..c2b567b 100644
--- a/src/jansson.h
+++ b/src/jansson.h
@@ -14,6 +14,10 @@
#include <jansson_config.h>
+#if USE_GNU_MP
+#include <gmp.h>
+#endif
+
#ifdef __cplusplus
extern "C" {
#endif
@@ -36,6 +40,9 @@ extern "C" {
/* types */
+
+struct json_context; /* forward reference */
+
typedef enum {
JSON_OBJECT,
JSON_ARRAY,
@@ -44,7 +51,9 @@ typedef enum {
JSON_REAL,
JSON_TRUE,
JSON_FALSE,
- JSON_NULL
+ JSON_NULL,
+ JSON_BIGINTEGER,
+ JSON_BIGREAL
} json_type;
typedef struct json_t {
@@ -66,13 +75,32 @@ typedef long json_int_t;
#endif /* JSON_INTEGER_IS_LONG_LONG */
#endif
+#ifndef JSON_BIGZ_TYPE
+#define JSON_BIGZ_TYPE void
+#endif
+#ifndef JSON_BIGR_TYPE
+#define JSON_BIGR_TYPE void
+#endif
+
+typedef JSON_BIGZ_TYPE * json_bigz_t;
+typedef JSON_BIGR_TYPE * json_bigr_t;
+typedef JSON_BIGZ_TYPE const * json_bigz_const_t;
+typedef JSON_BIGR_TYPE const * json_bigr_const_t;
+
+
#define json_typeof(json) ((json)->type)
#define json_is_object(json) ((json) && json_typeof(json) == JSON_OBJECT)
#define json_is_array(json) ((json) && json_typeof(json) == JSON_ARRAY)
#define json_is_string(json) ((json) && json_typeof(json) == JSON_STRING)
#define json_is_integer(json) ((json) && json_typeof(json) == JSON_INTEGER)
+#define json_is_biginteger(json) (json && json_typeof(json) == JSON_BIGINTEGER)
+#define json_is_anyinteger(json) (json_is_integer(json) || json_is_biginteger(json))
#define json_is_real(json) ((json) && json_typeof(json) == JSON_REAL)
+#define json_is_bigreal(json) (json && json_typeof(json) == JSON_BIGREAL)
+#define json_is_anyreal(json) (json_is_real(json) || json_is_bigreal(json))
#define json_is_number(json) (json_is_integer(json) || json_is_real(json))
+#define json_is_bignumber(json) (json_is_biginteger(json) || json_is_bigreal(json))
+#define json_is_anynumber(json) (json_is_number(json) || json_is_bignumber(json))
#define json_is_true(json) ((json) && json_typeof(json) == JSON_TRUE)
#define json_is_false(json) ((json) && json_typeof(json) == JSON_FALSE)
#define json_boolean_value json_is_true
@@ -93,6 +121,8 @@ json_t *json_true(void);
json_t *json_false(void);
#define json_boolean(val) ((val) ? json_true() : json_false())
json_t *json_null(void);
+json_t *json_biginteger(json_bigz_const_t value);
+json_t *json_bigreal(json_bigr_const_t value);
static JSON_INLINE
json_t *json_incref(json_t *json)
@@ -214,6 +244,12 @@ int json_string_setn_nocheck(json_t *string, const char *value, size_t len);
int json_integer_set(json_t *integer, json_int_t value);
int json_real_set(json_t *real, double value);
+json_bigz_const_t json_biginteger_value(const json_t *biginteger);
+int json_biginteger_set(json_t *biginteger, json_bigz_const_t mpz);
+
+json_bigr_const_t json_bigreal_value(const json_t *bigreal);
+int json_bigreal_set(json_t *bigreal, json_bigr_const_t mpf);
+
/* pack, unpack */
json_t *json_pack(const char *fmt, ...);
@@ -258,13 +294,17 @@ json_t *json_load_callback(json_load_callback_t callback, void *data, size_t fla
/* encoding */
-#define JSON_INDENT(n) (n & 0x1F)
-#define JSON_COMPACT 0x20
-#define JSON_ENSURE_ASCII 0x40
-#define JSON_SORT_KEYS 0x80
-#define JSON_PRESERVE_ORDER 0x100
-#define JSON_ENCODE_ANY 0x200
-#define JSON_ESCAPE_SLASH 0x400
+#define JSON_INDENT(n) (n & 0x1F)
+#define JSON_COMPACT 0x20
+#define JSON_ENSURE_ASCII 0x40
+#define JSON_SORT_KEYS 0x80
+#define JSON_PRESERVE_ORDER 0x100
+#define JSON_ENCODE_ANY 0x200
+#define JSON_ESCAPE_SLASH 0x400
+#define JSON_USE_BIGINT 0x800
+#define JSON_USE_BIGINT_ALWAYS 0x1000
+#define JSON_USE_BIGREAL 0x2000
+#define JSON_USE_BIGREAL_ALWAYS 0x4000
typedef int (*json_dump_callback_t)(const char *buffer, size_t size, void *data);
@@ -273,12 +313,84 @@ int json_dumpf(const json_t *json, FILE *output, size_t flags);
int json_dump_file(const json_t *json, const char *path, size_t flags);
int json_dump_callback(const json_t *json, json_dump_callback_t callback, void *data, size_t flags);
-/* custom memory allocation */
+/* custom memory functions */
+
+typedef void *(*json_malloc_t)(size_t); /* allocate memory */
+typedef void (*json_free_t)(void *); /* free memory */
+typedef void *(*json_realloc_t)(void *, size_t); /* change allocation size */
+typedef void (*json_overwrite_t)(void *, size_t); /* overwrite memory */
+typedef char *(*json_strdup_t)(const char *); /* duplicate string */
-typedef void *(*json_malloc_t)(size_t);
-typedef void (*json_free_t)(void *);
+typedef struct {
+ json_malloc_t malloc_fn;
+ json_free_t free_fn;
+ json_realloc_t realloc_fn;
+ json_overwrite_t overwrite_fn;
+ json_strdup_t strdup_fn;
+} json_memory_funcs_t;
void json_set_alloc_funcs(json_malloc_t malloc_fn, json_free_t free_fn);
+void json_set_realloc_func(json_realloc_t realloc_fn);
+void json_set_overwrite_func( json_overwrite_t overwrite_fn );
+
+
+/* big integers */
+
+typedef json_bigz_t (*json_bigint_copy_t)(json_bigz_const_t bignum,
+ const json_memory_funcs_t *memfuncs);
+typedef void (*json_bigint_del_t)(json_bigz_t bignum,
+ const json_memory_funcs_t *memfuncs);
+typedef int (*json_bigint_cmp_t)(json_bigz_const_t bignum1,
+ json_bigz_const_t bignum2,
+ const json_memory_funcs_t *memfuncs);
+typedef int (*json_bigint_to_str_t)(json_bigz_const_t bignum,
+ char *buffer, size_t size,
+ const json_memory_funcs_t *memfuncs);
+typedef json_bigz_t (*json_bigint_from_str_t)(const char *value,
+ const json_memory_funcs_t *memfuncs);
+typedef json_bigz_t (*json_bigint_from_int_t)(json_int_t value,
+ const json_memory_funcs_t *memfuncs);
+
+typedef struct {
+ json_bigint_copy_t copy_fn;
+ json_bigint_del_t delete_fn;
+ json_bigint_cmp_t compare_fn;
+ json_bigint_to_str_t to_string_fn;
+ json_bigint_from_str_t from_string_fn;
+ json_bigint_from_int_t from_int_fn;
+} json_bigint_funcs_t;
+
+void json_set_biginteger_funcs(const json_bigint_funcs_t* functions);
+
+
+/* Big reals */
+
+typedef json_bigr_t (*json_bigreal_copy_t)(json_bigr_const_t bigreal,
+ const json_memory_funcs_t *memfuncs);
+typedef void (*json_bigreal_del_t)(json_bigr_t bigreal,
+ const json_memory_funcs_t *memfuncs);
+typedef int (*json_bigreal_cmp_t)(json_bigr_const_t bigreal1,
+ json_bigr_const_t bigreal2,
+ const json_memory_funcs_t *memfuncs);
+typedef int (*json_bigreal_to_str_t)(json_bigr_const_t bigreal,
+ char *buffer, size_t size,
+ const json_memory_funcs_t *memfuncs);
+typedef json_bigr_t (*json_bigreal_from_str_t)(const char *value,
+ const json_memory_funcs_t *memfuncs);
+typedef json_bigr_t (*json_bigreal_from_real_t)(double value,
+ const json_memory_funcs_t *memfuncs);
+
+typedef struct {
+ json_bigreal_copy_t copy_fn;
+ json_bigreal_del_t delete_fn;
+ json_bigreal_cmp_t compare_fn;
+ json_bigreal_to_str_t to_string_fn;
+ json_bigreal_from_str_t from_string_fn;
+ json_bigreal_from_real_t from_real_fn;
+} json_bigreal_funcs_t;
+
+void json_set_bigreal_funcs(const json_bigreal_funcs_t* functions);
+
#ifdef __cplusplus
}