Skip to content

Commit 4060aa9

Browse files
etiennebarriebyroot
andcommitted
Fix JSON::Coder to call as_json proc for NaN and Infinity
Co-authored-by: Jean Boussier <jean.boussier@gmail.com>
1 parent c472d72 commit 4060aa9

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

ext/json/ext/generator/generator.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,13 +1031,19 @@ static void generate_json_float(FBuffer *buffer, struct generate_json_data *data
10311031
{
10321032
double value = RFLOAT_VALUE(obj);
10331033
char allow_nan = state->allow_nan;
1034-
VALUE tmp = rb_funcall(obj, i_to_s, 0);
10351034
if (!allow_nan) {
10361035
if (isinf(value) || isnan(value)) {
1037-
raise_generator_error(obj, "%"PRIsVALUE" not allowed in JSON", tmp);
1036+
if (state->strict && state->as_json) {
1037+
obj = rb_proc_call_with_block(state->as_json, 1, &obj, Qnil);
1038+
state->depth++;
1039+
generate_json(buffer, data, state, obj);
1040+
state->depth--;
1041+
return;
1042+
}
1043+
raise_generator_error(obj, "%"PRIsVALUE" not allowed in JSON", rb_funcall(obj, i_to_s, 0));
10381044
}
10391045
}
1040-
fbuffer_append_str(buffer, tmp);
1046+
fbuffer_append_str(buffer, rb_funcall(obj, i_to_s, 0));
10411047
}
10421048

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

0 commit comments

Comments
 (0)