|
|
@ -31,8 +31,8 @@ |
|
|
|
#include "parson.h" |
|
|
|
#include "parson.h" |
|
|
|
|
|
|
|
|
|
|
|
#define PARSON_IMPL_VERSION_MAJOR 1 |
|
|
|
#define PARSON_IMPL_VERSION_MAJOR 1 |
|
|
|
#define PARSON_IMPL_VERSION_MINOR 2 |
|
|
|
#define PARSON_IMPL_VERSION_MINOR 3 |
|
|
|
#define PARSON_IMPL_VERSION_PATCH 1 |
|
|
|
#define PARSON_IMPL_VERSION_PATCH 0 |
|
|
|
|
|
|
|
|
|
|
|
#if (PARSON_VERSION_MAJOR != PARSON_IMPL_VERSION_MAJOR)\ |
|
|
|
#if (PARSON_VERSION_MAJOR != PARSON_IMPL_VERSION_MAJOR)\ |
|
|
|
|| (PARSON_VERSION_MINOR != PARSON_IMPL_VERSION_MINOR)\
|
|
|
|
|| (PARSON_VERSION_MINOR != PARSON_IMPL_VERSION_MINOR)\
|
|
|
@ -63,8 +63,13 @@ |
|
|
|
#define STARTING_CAPACITY 16 |
|
|
|
#define STARTING_CAPACITY 16 |
|
|
|
#define MAX_NESTING 2048 |
|
|
|
#define MAX_NESTING 2048 |
|
|
|
|
|
|
|
|
|
|
|
#define FLOAT_FORMAT "%1.17g" /* do not increase precision without incresing NUM_BUF_SIZE */ |
|
|
|
#ifndef PARSON_DEFAULT_FLOAT_FORMAT |
|
|
|
#define NUM_BUF_SIZE 64 /* double printed with "%1.17g" shouldn't be longer than 25 bytes so let's be paranoid and use 64 */ |
|
|
|
#define PARSON_DEFAULT_FLOAT_FORMAT "%1.17g" /* do not increase precision without incresing NUM_BUF_SIZE */ |
|
|
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef PARSON_NUM_BUF_SIZE |
|
|
|
|
|
|
|
#define PARSON_NUM_BUF_SIZE 64 /* double printed with "%1.17g" shouldn't be longer than 25 bytes so let's be paranoid and use 64 */ |
|
|
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
#define SIZEOF_TOKEN(a) (sizeof(a) - 1) |
|
|
|
#define SIZEOF_TOKEN(a) (sizeof(a) - 1) |
|
|
|
#define SKIP_CHAR(str) ((*str)++) |
|
|
|
#define SKIP_CHAR(str) ((*str)++) |
|
|
@ -87,6 +92,8 @@ static JSON_Free_Function parson_free = free; |
|
|
|
|
|
|
|
|
|
|
|
static int parson_escape_slashes = 1; |
|
|
|
static int parson_escape_slashes = 1; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static char *parson_float_format = NULL; |
|
|
|
|
|
|
|
|
|
|
|
#define IS_CONT(b) (((unsigned char)(b) & 0xC0) == 0x80) /* is utf-8 continuation byte */ |
|
|
|
#define IS_CONT(b) (((unsigned char)(b) & 0xC0) == 0x80) /* is utf-8 continuation byte */ |
|
|
|
|
|
|
|
|
|
|
|
typedef int parson_bool_t; |
|
|
|
typedef int parson_bool_t; |
|
|
@ -1212,7 +1219,11 @@ static int json_serialize_to_buffer_r(const JSON_Value *value, char *buf, int le |
|
|
|
if (buf != NULL) { |
|
|
|
if (buf != NULL) { |
|
|
|
num_buf = buf; |
|
|
|
num_buf = buf; |
|
|
|
} |
|
|
|
} |
|
|
|
written = sprintf(num_buf, FLOAT_FORMAT, num); |
|
|
|
if (parson_float_format) { |
|
|
|
|
|
|
|
written = sprintf(num_buf, parson_float_format, num); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
written = sprintf(num_buf, PARSON_DEFAULT_FLOAT_FORMAT, num); |
|
|
|
|
|
|
|
} |
|
|
|
if (written < 0) { |
|
|
|
if (written < 0) { |
|
|
|
return -1; |
|
|
|
return -1; |
|
|
|
} |
|
|
|
} |
|
|
@ -1757,7 +1768,7 @@ JSON_Value * json_value_deep_copy(const JSON_Value *value) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
size_t json_serialization_size(const JSON_Value *value) { |
|
|
|
size_t json_serialization_size(const JSON_Value *value) { |
|
|
|
char num_buf[NUM_BUF_SIZE]; /* recursively allocating buffer on stack is a bad idea, so let's do it only once */ |
|
|
|
char num_buf[PARSON_NUM_BUF_SIZE]; /* recursively allocating buffer on stack is a bad idea, so let's do it only once */ |
|
|
|
int res = json_serialize_to_buffer_r(value, NULL, 0, PARSON_FALSE, num_buf); |
|
|
|
int res = json_serialize_to_buffer_r(value, NULL, 0, PARSON_FALSE, num_buf); |
|
|
|
return res < 0 ? 0 : (size_t)(res) + 1; |
|
|
|
return res < 0 ? 0 : (size_t)(res) + 1; |
|
|
|
} |
|
|
|
} |
|
|
@ -1817,7 +1828,7 @@ char * json_serialize_to_string(const JSON_Value *value) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
size_t json_serialization_size_pretty(const JSON_Value *value) { |
|
|
|
size_t json_serialization_size_pretty(const JSON_Value *value) { |
|
|
|
char num_buf[NUM_BUF_SIZE]; /* recursively allocating buffer on stack is a bad idea, so let's do it only once */ |
|
|
|
char num_buf[PARSON_NUM_BUF_SIZE]; /* recursively allocating buffer on stack is a bad idea, so let's do it only once */ |
|
|
|
int res = json_serialize_to_buffer_r(value, NULL, 0, PARSON_TRUE, num_buf); |
|
|
|
int res = json_serialize_to_buffer_r(value, NULL, 0, PARSON_TRUE, num_buf); |
|
|
|
return res < 0 ? 0 : (size_t)(res) + 1; |
|
|
|
return res < 0 ? 0 : (size_t)(res) + 1; |
|
|
|
} |
|
|
|
} |
|
|
@ -2422,3 +2433,14 @@ void json_set_allocation_functions(JSON_Malloc_Function malloc_fun, JSON_Free_Fu |
|
|
|
void json_set_escape_slashes(int escape_slashes) { |
|
|
|
void json_set_escape_slashes(int escape_slashes) { |
|
|
|
parson_escape_slashes = escape_slashes; |
|
|
|
parson_escape_slashes = escape_slashes; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void json_set_float_serialization_format(const char *format) { |
|
|
|
|
|
|
|
if (parson_float_format) { |
|
|
|
|
|
|
|
parson_free(parson_float_format); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (!format) { |
|
|
|
|
|
|
|
parson_float_format = NULL; |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
parson_float_format = parson_strdup(format); |
|
|
|
|
|
|
|
} |
|
|
|