Skip to content

Commit 5be7a76

Browse files
committed
jsonwriter_size_t(), jsonwriter_int(), jsonwriter_dblf(): write NaN if appropriate
1 parent a298a75 commit 5be7a76

File tree

1 file changed

+30
-21
lines changed

1 file changed

+30
-21
lines changed

jsonwriter.c

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <stdio.h>
22
#include <string.h>
33
#include <jsonwriter.h>
4+
#include <math.h>
45

56
#ifdef INCLUDE_UTILS
67
#include "utils.c"
@@ -308,26 +309,30 @@ int jsonwriter_dblf(jsonwriter_handle data, long double d, const char *format_st
308309
unsigned char trim_trailing_zeros_after_dec) {
309310
if(data->depth < JSONWRITER_MAX_NESTING) {
310311
jsonwriter_indent(data, 0);
311-
if(format_string && !strstr(format_string, "Lf")) // TO DO: return error code
312-
fprintf(stderr, "Warning: format string passed to jsonwriter_dblf() does not contain Lf: %s\n", format_string);
313-
format_string = format_string ? format_string : "%0.15Lf";
314-
int len = snprintf(data->tmp, sizeof(data->tmp), format_string, d);
315-
// TO DO: check if len < 0 or len > sizeof(data->tmp)
316-
if(len <= 0 || (size_t)len > sizeof(data->tmp)) { // TO DO: return error code
317-
fprintf(stderr, "Warning! jsonwriter_dblf failed to print, outputting zero value\n");
318-
jsonwriter_output_buff_write(&data->out, (const unsigned char *)"0", 1);
312+
if(isnan(d)) {
313+
jsonwriter_output_buff_write(&data->out, (unsigned char *)"\"NaN\"", 5);
319314
} else {
320-
if(trim_trailing_zeros_after_dec && memchr(data->tmp, '.', len)) {
321-
while(len && data->tmp[len-1] == '0')
322-
len--;
323-
if(len && data->tmp[len-1] == '.')
324-
len--;
325-
if(!len) {
326-
*data->tmp = '0';
327-
len = 1;
315+
if(format_string && !strstr(format_string, "Lf")) // TO DO: return error code
316+
fprintf(stderr, "Warning: format string passed to jsonwriter_dblf() does not contain Lf: %s\n", format_string);
317+
format_string = format_string ? format_string : "%0.15Lf";
318+
int len = snprintf(data->tmp, sizeof(data->tmp), format_string, d);
319+
// TO DO: check if len < 0 or len > sizeof(data->tmp)
320+
if(len <= 0 || (size_t)len > sizeof(data->tmp)) { // TO DO: return error code
321+
fprintf(stderr, "Warning! jsonwriter_dblf failed to print, outputting zero value\n");
322+
jsonwriter_output_buff_write(&data->out, (const unsigned char *)"0", 1);
323+
} else {
324+
if(trim_trailing_zeros_after_dec && memchr(data->tmp, '.', len)) {
325+
while(len && data->tmp[len-1] == '0')
326+
len--;
327+
if(len && data->tmp[len-1] == '.')
328+
len--;
329+
if(!len) {
330+
*data->tmp = '0';
331+
len = 1;
332+
}
328333
}
334+
jsonwriter_output_buff_write(&data->out, (unsigned char *)data->tmp, len);
329335
}
330-
jsonwriter_output_buff_write(&data->out, (unsigned char *)data->tmp, len);
331336
}
332337
return 0;
333338
}
@@ -342,8 +347,10 @@ int jsonwriter_size_t(jsonwriter_handle data, size_t sz) {
342347
if(data->depth < JSONWRITER_MAX_NESTING) {
343348
jsonwriter_indent(data, 0);
344349
int len = snprintf(data->tmp, sizeof(data->tmp), "%zu", sz);
345-
// TO DO: check if len < 0 or len > sizeof(data->tmp)
346-
jsonwriter_output_buff_write(&data->out, (unsigned char *)data->tmp, len);
350+
if(len < 0 || len >= (int)sizeof(data->tmp))
351+
jsonwriter_output_buff_write(&data->out, (unsigned char *)"\"NaN\"", 5);
352+
else
353+
jsonwriter_output_buff_write(&data->out, (unsigned char *)data->tmp, len);
347354
return 0;
348355
}
349356
return 1;
@@ -353,8 +360,10 @@ int jsonwriter_int(jsonwriter_handle data, jsw_int64 i) {
353360
if(data->depth < JSONWRITER_MAX_NESTING) {
354361
jsonwriter_indent(data, 0);
355362
int len = snprintf(data->tmp, sizeof(data->tmp), JSW_INT64_PRINTF_FMT, i);
356-
// TO DO: check if len < 0 or len > sizeof(data->tmp)
357-
jsonwriter_output_buff_write(&data->out, (unsigned char *)data->tmp, len);
363+
if(len < 0 || len >= (int)sizeof(data->tmp))
364+
jsonwriter_output_buff_write(&data->out, (unsigned char *)"\"NaN\"", 5);
365+
else
366+
jsonwriter_output_buff_write(&data->out, (unsigned char *)data->tmp, len);
358367
return 0;
359368
}
360369
return 1;

0 commit comments

Comments
 (0)