mirror of
https://github.com/espressif/ESP8266_RTOS_SDK.git
synced 2025-09-19 17:04:47 +08:00
Merge branch 'feature/add_cjson_number_codec' into 'master'
Add function to encode json number when "sprintf" not support float See merge request sdk/ESP8266_RTOS_SDK!548
This commit is contained in:
@ -56,6 +56,10 @@
|
|||||||
#pragma GCC visibility pop
|
#pragma GCC visibility pop
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef CJSON_SPRINTF_FLOAT
|
||||||
|
#define CJSON_SPRINTF_FLOAT 0
|
||||||
|
#endif
|
||||||
|
|
||||||
#include "cJSON.h"
|
#include "cJSON.h"
|
||||||
|
|
||||||
/* define our own boolean type */
|
/* define our own boolean type */
|
||||||
@ -480,7 +484,6 @@ static cJSON_bool print_number(const cJSON * const item, printbuffer * const out
|
|||||||
size_t i = 0;
|
size_t i = 0;
|
||||||
unsigned char number_buffer[26]; /* temporary buffer to print the number into */
|
unsigned char number_buffer[26]; /* temporary buffer to print the number into */
|
||||||
unsigned char decimal_point = get_decimal_point();
|
unsigned char decimal_point = get_decimal_point();
|
||||||
double test;
|
|
||||||
|
|
||||||
if (output_buffer == NULL)
|
if (output_buffer == NULL)
|
||||||
{
|
{
|
||||||
@ -494,6 +497,9 @@ static cJSON_bool print_number(const cJSON * const item, printbuffer * const out
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
#if CJSON_SPRINTF_FLOAT
|
||||||
|
double test;
|
||||||
|
|
||||||
/* Try 15 decimal places of precision to avoid nonsignificant nonzero digits */
|
/* Try 15 decimal places of precision to avoid nonsignificant nonzero digits */
|
||||||
length = sprintf((char*)number_buffer, "%1.15g", d);
|
length = sprintf((char*)number_buffer, "%1.15g", d);
|
||||||
|
|
||||||
@ -503,6 +509,45 @@ static cJSON_bool print_number(const cJSON * const item, printbuffer * const out
|
|||||||
/* If not, print with 17 decimal places of precision */
|
/* If not, print with 17 decimal places of precision */
|
||||||
length = sprintf((char*)number_buffer, "%1.17g", d);
|
length = sprintf((char*)number_buffer, "%1.17g", d);
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
long d32 = (long)d;
|
||||||
|
|
||||||
|
length = sprintf((char*)number_buffer, "%ld", d32);
|
||||||
|
|
||||||
|
if ((double)d32 != d) {
|
||||||
|
size_t precision = 14;
|
||||||
|
unsigned char *pbuf = number_buffer;
|
||||||
|
|
||||||
|
if (d < 0.0) {
|
||||||
|
d = (double)d32 - d + 0.00000000000001;
|
||||||
|
} else {
|
||||||
|
d = d - (double)d32;
|
||||||
|
}
|
||||||
|
|
||||||
|
pbuf = &number_buffer[length];
|
||||||
|
*pbuf++ = '.';
|
||||||
|
length++;
|
||||||
|
|
||||||
|
while (d > 0.0 && precision--) {
|
||||||
|
d *= 10.0;
|
||||||
|
unsigned char tmp = (unsigned char)d;
|
||||||
|
|
||||||
|
*pbuf++ = tmp + '0';
|
||||||
|
length++;
|
||||||
|
|
||||||
|
d -= (double)tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
pbuf = &number_buffer[length - 1];
|
||||||
|
|
||||||
|
while (*pbuf == '0') {
|
||||||
|
pbuf--;
|
||||||
|
length--;
|
||||||
|
}
|
||||||
|
|
||||||
|
*++pbuf = 0;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/* sprintf failed or buffer overrun occured */
|
/* sprintf failed or buffer overrun occured */
|
||||||
|
@ -4,3 +4,7 @@
|
|||||||
COMPONENT_ADD_INCLUDEDIRS += cJSON
|
COMPONENT_ADD_INCLUDEDIRS += cJSON
|
||||||
|
|
||||||
COMPONENT_SRCDIRS := cJSON
|
COMPONENT_SRCDIRS := cJSON
|
||||||
|
|
||||||
|
ifdef CONFIG_NEWLIB_LIBRARY_LEVEL_NORMAL
|
||||||
|
CFLAGS += -DCJSON_SPRINTF_FLOAT=1
|
||||||
|
endif
|
||||||
|
@ -11,7 +11,7 @@ config NEWLIB_ENABLE
|
|||||||
|
|
||||||
choice NEWLIB_LIBRARY_LEVEL
|
choice NEWLIB_LIBRARY_LEVEL
|
||||||
prompt "newlib level"
|
prompt "newlib level"
|
||||||
default NEWLIB_LIBRARY_LEVEL_NORMAL
|
default NEWLIB_LIBRARY_LEVEL_NANO
|
||||||
depends on NEWLIB_ENABLE
|
depends on NEWLIB_ENABLE
|
||||||
help
|
help
|
||||||
Choose newlib library level.
|
Choose newlib library level.
|
||||||
|
Reference in New Issue
Block a user