Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ext/json/ext/generator/depend
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
generator.o: generator.c $(srcdir)/../fbuffer/fbuffer.h
generator.o: generator.c $(srcdir)/../vendor/fpconv.c
24 changes: 21 additions & 3 deletions ext/json/ext/generator/generator.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "ruby.h"
#include "../fbuffer/fbuffer.h"
#include "../vendor/fpconv.c"

#include <math.h>
#include <ctype.h>
Expand Down Expand Up @@ -1054,8 +1055,9 @@ static void generate_json_float(FBuffer *buffer, struct generate_json_data *data
{
double value = RFLOAT_VALUE(obj);
char allow_nan = state->allow_nan;
if (!allow_nan) {
if (isinf(value) || isnan(value)) {
if (isinf(value) || isnan(value)) {
/* for NaN and Infinity values we either raise an error or rely on Float#to_s. */
if (!allow_nan) {
if (state->strict && state->as_json) {
VALUE casted_obj = rb_proc_call_with_block(state->as_json, 1, &obj, Qnil);
if (casted_obj != obj) {
Expand All @@ -1067,8 +1069,24 @@ static void generate_json_float(FBuffer *buffer, struct generate_json_data *data
}
raise_generator_error(obj, "%"PRIsVALUE" not allowed in JSON", rb_funcall(obj, i_to_s, 0));
}

VALUE tmp = rb_funcall(obj, i_to_s, 0);
fbuffer_append_str(buffer, tmp);
return;
}
fbuffer_append_str(buffer, rb_funcall(obj, i_to_s, 0));

/* This implementation writes directly into the buffer. We reserve
* the 24 characters that fpconv_dtoa states as its maximum, plus
* 2 more characters for the potential ".0" suffix.
*/
fbuffer_inc_capa(buffer, 26);
char* d = buffer->ptr + buffer->len;
int len = fpconv_dtoa(value, d);

/* fpconv_dtoa converts a float to its shortest string representation,
* but it adds a ".0" if this is a plain integer.
*/
buffer->len += len;
}

static void generate_json_fragment(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj)
Expand Down
Loading