欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

ESP8266,添加 cJSON_AddNumberToObject 后,cJSON_Print 输出 NULL。

最编程 2024-10-13 16:23:12
...
/* Render the number nicely from the given item into a string. */ static cJSON_bool print_number(const cJSON * const item, printbuffer * const output_buffer) { unsigned char *output_pointer = NULL; // double d = item->valuedouble; int d = item->valueint; int length = 0; size_t i = 0; unsigned char number_buffer[26] = {0}; /* temporary buffer to print the number into */ unsigned char decimal_point = get_decimal_point(); // double test = 0.0; if (output_buffer == NULL) { return false; } /* This checks for NaN and Infinity */ // if (isnan(d) || isinf(d)) // { // length = sprintf((char*)number_buffer, "null"); // } // else // { // /* Try 15 decimal places of precision to avoid nonsignificant nonzero digits */ // length = sprintf((char*)number_buffer, "%1.15g", d); // // /* Check whether the original double can be recovered */ // if ((sscanf((char*)number_buffer, "%lg", &test) != 1) || !compare_double((double)test, d)) // { // /* If not, print with 17 decimal places of precision */ // length = sprintf((char*)number_buffer, "%1.17g", d); // } // } length = sprintf((char*)number_buffer, "%d", d); /* sprintf failed or buffer overrun occurred */ if ((length < 0) || (length > (int)(sizeof(number_buffer) - 1))) { return false; } /* reserve appropriate space in the output */ output_pointer = ensure(output_buffer, (size_t)length + sizeof("")); if (output_pointer == NULL) { return false; } /* copy the printed number to the output and replace locale * dependent decimal point with '.' */ for (i = 0; i < ((size_t)length); i++) { if (number_buffer[i] == decimal_point) { output_pointer[i] = '.'; continue; } output_pointer[i] = number_buffer[i]; } output_pointer[i] = '\0'; output_buffer->offset += (size_t)length; return true; }

推荐阅读