11/*
22 SPDX-License-Identifier: MIT
33
4- Parson 1.2.1 ( http://kgabis.github.com/parson/ )
5- Copyright (c) 2012 - 2021 Krzysztof Gabis
4+ Parson 1.3.0 ( http://kgabis.github.com/parson/ )
5+ Copyright (c) 2012 - 2022 Krzysztof Gabis
66
77 Permission is hereby granted, free of charge, to any person obtaining a copy
88 of this software and associated documentation files (the "Software"), to deal
3131#include "parson.h"
3232
3333#define PARSON_IMPL_VERSION_MAJOR 1
34- #define PARSON_IMPL_VERSION_MINOR 2
35- #define PARSON_IMPL_VERSION_PATCH 1
34+ #define PARSON_IMPL_VERSION_MINOR 3
35+ #define PARSON_IMPL_VERSION_PATCH 0
3636
3737#if (PARSON_VERSION_MAJOR != PARSON_IMPL_VERSION_MAJOR )\
3838|| (PARSON_VERSION_MINOR != PARSON_IMPL_VERSION_MINOR )\
6363#define STARTING_CAPACITY 16
6464#define MAX_NESTING 2048
6565
66- #define FLOAT_FORMAT "%1.17g" /* do not increase precision without incresing NUM_BUF_SIZE */
67- #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 */
66+ #ifndef PARSON_DEFAULT_FLOAT_FORMAT
67+ #define PARSON_DEFAULT_FLOAT_FORMAT "%1.17g" /* do not increase precision without incresing NUM_BUF_SIZE */
68+ #endif
69+
70+ #ifndef PARSON_NUM_BUF_SIZE
71+ #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 */
72+ #endif
6873
6974#define SIZEOF_TOKEN (a ) (sizeof(a) - 1)
7075#define SKIP_CHAR (str ) ((*str)++)
@@ -87,6 +92,8 @@ static JSON_Free_Function parson_free = free;
8792
8893static int parson_escape_slashes = 1 ;
8994
95+ static char * parson_float_format = NULL ;
96+
9097#define IS_CONT (b ) (((unsigned char)(b) & 0xC0) == 0x80) /* is utf-8 continuation byte */
9198
9299typedef int parson_bool_t ;
@@ -1212,7 +1219,11 @@ static int json_serialize_to_buffer_r(const JSON_Value *value, char *buf, int le
12121219 if (buf != NULL ) {
12131220 num_buf = buf ;
12141221 }
1215- written = sprintf (num_buf , FLOAT_FORMAT , num );
1222+ if (parson_float_format ) {
1223+ written = sprintf (num_buf , parson_float_format , num );
1224+ } else {
1225+ written = sprintf (num_buf , PARSON_DEFAULT_FLOAT_FORMAT , num );
1226+ }
12161227 if (written < 0 ) {
12171228 return -1 ;
12181229 }
@@ -1757,7 +1768,7 @@ JSON_Value * json_value_deep_copy(const JSON_Value *value) {
17571768}
17581769
17591770size_t json_serialization_size (const JSON_Value * value ) {
1760- char num_buf [NUM_BUF_SIZE ]; /* recursively allocating buffer on stack is a bad idea, so let's do it only once */
1771+ char num_buf [PARSON_NUM_BUF_SIZE ]; /* recursively allocating buffer on stack is a bad idea, so let's do it only once */
17611772 int res = json_serialize_to_buffer_r (value , NULL , 0 , PARSON_FALSE , num_buf );
17621773 return res < 0 ? 0 : (size_t )(res ) + 1 ;
17631774}
@@ -1817,7 +1828,7 @@ char * json_serialize_to_string(const JSON_Value *value) {
18171828}
18181829
18191830size_t json_serialization_size_pretty (const JSON_Value * value ) {
1820- char num_buf [NUM_BUF_SIZE ]; /* recursively allocating buffer on stack is a bad idea, so let's do it only once */
1831+ char num_buf [PARSON_NUM_BUF_SIZE ]; /* recursively allocating buffer on stack is a bad idea, so let's do it only once */
18211832 int res = json_serialize_to_buffer_r (value , NULL , 0 , PARSON_TRUE , num_buf );
18221833 return res < 0 ? 0 : (size_t )(res ) + 1 ;
18231834}
@@ -2422,3 +2433,14 @@ void json_set_allocation_functions(JSON_Malloc_Function malloc_fun, JSON_Free_Fu
24222433void json_set_escape_slashes (int escape_slashes ) {
24232434 parson_escape_slashes = escape_slashes ;
24242435}
2436+
2437+ void json_set_float_serialization_format (const char * format ) {
2438+ if (parson_float_format ) {
2439+ parson_free (parson_float_format );
2440+ }
2441+ if (!format ) {
2442+ parson_float_format = NULL ;
2443+ return ;
2444+ }
2445+ parson_float_format = parson_strdup (format );
2446+ }
0 commit comments