Skip to content

Commit

Permalink
Update parson to v1.5.1
Browse files Browse the repository at this point in the history
  • Loading branch information
beutlich committed Mar 11, 2023
1 parent 1bb7d38 commit d4f4724
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 36 deletions.
75 changes: 44 additions & 31 deletions ExternData/Resources/C-Sources/parson/parson.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
SPDX-License-Identifier: MIT
Parson 1.4.0 (https://github.com/kgabis/parson)
Copyright (c) 2012 - 2022 Krzysztof Gabis
Parson 1.5.1 (https://github.com/kgabis/parson)
Copyright (c) 2012 - 2023 Krzysztof Gabis
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -31,8 +31,8 @@
#include "parson.h"

#define PARSON_IMPL_VERSION_MAJOR 1
#define PARSON_IMPL_VERSION_MINOR 4
#define PARSON_IMPL_VERSION_PATCH 0
#define PARSON_IMPL_VERSION_MINOR 5
#define PARSON_IMPL_VERSION_PATCH 1

#if (PARSON_VERSION_MAJOR != PARSON_IMPL_VERSION_MAJOR)\
|| (PARSON_VERSION_MINOR != PARSON_IMPL_VERSION_MINOR)\
Expand Down Expand Up @@ -71,6 +71,10 @@
#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

#ifndef PARSON_INDENT_STR
#define PARSON_INDENT_STR " "
#endif

#define SIZEOF_TOKEN(a) (sizeof(a) - 1)
#define SKIP_CHAR(str) ((*str)++)
#define SKIP_WHITESPACES(str) while (isspace((unsigned char)(**str))) { SKIP_CHAR(str); }
Expand All @@ -94,6 +98,8 @@ static int parson_escape_slashes = 1;

static char *parson_float_format = NULL;

static JSON_Number_Serialization_Function parson_number_serialization_function = NULL;

#define IS_CONT(b) (((unsigned char)(b) & 0xC0) == 0x80) /* is utf-8 continuation byte */

typedef int parson_bool_t;
Expand Down Expand Up @@ -192,8 +198,6 @@ static JSON_Value * parse_value(const char **string, size_t nesting);
/* Serialization */
static int json_serialize_to_buffer_r(const JSON_Value *value, char *buf, int level, parson_bool_t is_pretty, char *num_buf);
static int json_serialize_string(const char *string, size_t len, char *buf);
static int append_indent(char *buf, int level);
static int append_string(char *buf, const char *string);

/* Various */
static char * read_file(const char * filename) {
Expand Down Expand Up @@ -1095,15 +1099,27 @@ static JSON_Value * parse_null_value(const char **string) {
}

/* Serialization */
#define APPEND_STRING(str) do { written = append_string(buf, (str));\
if (written < 0) { return -1; }\
if (buf != NULL) { buf += written; }\
written_total += written; } while(0)

#define APPEND_INDENT(level) do { written = append_indent(buf, (level));\
if (written < 0) { return -1; }\
if (buf != NULL) { buf += written; }\
written_total += written; } while(0)
/* APPEND_STRING() is only called on string literals.
It's a bit hacky because it makes plenty of assumptions about the external state
and should eventually be tidied up into a function (same goes for APPEND_INDENT)
*/
#define APPEND_STRING(str) do {\
written = SIZEOF_TOKEN((str));\
if (buf != NULL) {\
memcpy(buf, (str), written);\
buf[written] = '\0';\
buf += written;\
}\
written_total += written;\
} while (0)

#define APPEND_INDENT(level) do {\
int level_i = 0;\
for (level_i = 0; level_i < (level); level_i++) {\
APPEND_STRING(PARSON_INDENT_STR);\
}\
} while (0)

static int json_serialize_to_buffer_r(const JSON_Value *value, char *buf, int level, parson_bool_t is_pretty, char *num_buf)
{
Expand Down Expand Up @@ -1225,7 +1241,9 @@ static int json_serialize_to_buffer_r(const JSON_Value *value, char *buf, int le
if (buf != NULL) {
num_buf = buf;
}
if (parson_float_format) {
if (parson_number_serialization_function) {
written = parson_number_serialization_function(num, num_buf);
} else if (parson_float_format) {
written = sprintf(num_buf, parson_float_format, num);
} else {
written = sprintf(num_buf, PARSON_DEFAULT_FLOAT_FORMAT, num);
Expand Down Expand Up @@ -1315,22 +1333,6 @@ static int json_serialize_string(const char *string, size_t len, char *buf) {
return written_total;
}

static int append_indent(char *buf, int level) {
int i;
int written = -1, written_total = 0;
for (i = 0; i < level; i++) {
APPEND_STRING(" ");
}
return written_total;
}

static int append_string(char *buf, const char *string) {
if (buf == NULL) {
return (int)strlen(string);
}
return sprintf(buf, "%s", string);
}

#undef APPEND_STRING
#undef APPEND_INDENT

Expand Down Expand Up @@ -2272,9 +2274,15 @@ JSON_Status json_object_clear(JSON_Object *object) {
}
for (i = 0; i < json_object_get_count(object); i++) {
parson_free(object->names[i]);
object->names[i] = NULL;

json_value_free(object->values[i]);
object->values[i] = NULL;
}
object->count = 0;
for (i = 0; i < object->cell_capacity; i++) {
object->cells[i] = OBJECT_INVALID_IX;
}
return JSONSuccess;
}

Expand Down Expand Up @@ -2443,10 +2451,15 @@ void json_set_escape_slashes(int escape_slashes) {
void json_set_float_serialization_format(const char *format) {
if (parson_float_format) {
parson_free(parson_float_format);
parson_float_format = NULL;
}
if (!format) {
parson_float_format = NULL;
return;
}
parson_float_format = parson_strdup(format);
}

void json_set_number_serialization_function(JSON_Number_Serialization_Function func) {
parson_number_serialization_function = func;
}
20 changes: 15 additions & 5 deletions ExternData/Resources/C-Sources/parson/parson.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
SPDX-License-Identifier: MIT
Parson 1.4.0 (https://github.com/kgabis/parson)
Copyright (c) 2012 - 2022 Krzysztof Gabis
Parson 1.5.1 (https://github.com/kgabis/parson)
Copyright (c) 2012 - 2023 Krzysztof Gabis
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -35,10 +35,10 @@ extern "C"
#endif

#define PARSON_VERSION_MAJOR 1
#define PARSON_VERSION_MINOR 4
#define PARSON_VERSION_PATCH 0
#define PARSON_VERSION_MINOR 5
#define PARSON_VERSION_PATCH 1

#define PARSON_VERSION_STRING "1.4.0"
#define PARSON_VERSION_STRING "1.5.1"

#include <stddef.h> /* size_t */

Expand Down Expand Up @@ -67,6 +67,12 @@ typedef int JSON_Status;
typedef void * (*JSON_Malloc_Function)(size_t);
typedef void (*JSON_Free_Function)(void *);

/* A function used for serializing numbers (see json_set_number_serialization_function).
If 'buf' is null then it should return number of bytes that would've been written
(but not more than PARSON_NUM_BUF_SIZE).
*/
typedef int (*JSON_Number_Serialization_Function)(double num, char *buf);

/* Call only once, before calling any other function from parson API. If not called, malloc and free
from stdlib will be used for all allocations */
void json_set_allocation_functions(JSON_Malloc_Function malloc_fun, JSON_Free_Function free_fun);
Expand All @@ -80,6 +86,10 @@ void json_set_escape_slashes(int escape_slashes);
If format is null then the default format is used. */
void json_set_float_serialization_format(const char *format);

/* Sets a function that will be used for serialization of numbers.
If function is null then the default serialization function is used. */
void json_set_number_serialization_function(JSON_Number_Serialization_Function fun);

/* Parses first JSON value in a file, returns NULL in case of error */
JSON_Value * json_parse_file(const char *filename);

Expand Down

0 comments on commit d4f4724

Please sign in to comment.