Skip to content

Commit b5d887d

Browse files
authored
Merge pull request #746 from etiennebarrie/fix-json-coder-NaN-Infinity
Fix JSON::Coder to call as_json proc for NaN and Infinity
2 parents c472d72 + 91d061d commit b5d887d

File tree

4 files changed

+61
-23
lines changed

4 files changed

+61
-23
lines changed

ext/json/ext/generator/generator.c

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -841,15 +841,19 @@ json_object_i(VALUE key, VALUE val, VALUE _arg)
841841
return ST_CONTINUE;
842842
}
843843

844-
static void generate_json_object(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj)
844+
static inline long increase_depth(JSON_Generator_State *state)
845845
{
846-
long max_nesting = state->max_nesting;
847846
long depth = ++state->depth;
848-
int j;
849-
850-
if (max_nesting != 0 && depth > max_nesting) {
847+
if (RB_UNLIKELY(depth > state->max_nesting && state->max_nesting)) {
851848
rb_raise(eNestingError, "nesting of %ld is too deep", --state->depth);
852849
}
850+
return depth;
851+
}
852+
853+
static void generate_json_object(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj)
854+
{
855+
int j;
856+
long depth = increase_depth(state);
853857

854858
if (RHASH_SIZE(obj) == 0) {
855859
fbuffer_append(buffer, "{}", 2);
@@ -879,12 +883,8 @@ static void generate_json_object(FBuffer *buffer, struct generate_json_data *dat
879883

880884
static void generate_json_array(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj)
881885
{
882-
long max_nesting = state->max_nesting;
883-
long depth = ++state->depth;
884886
int i, j;
885-
if (max_nesting != 0 && depth > max_nesting) {
886-
rb_raise(eNestingError, "nesting of %ld is too deep", --state->depth);
887-
}
887+
long depth = increase_depth(state);
888888

889889
if (RARRAY_LEN(obj) == 0) {
890890
fbuffer_append(buffer, "[]", 2);
@@ -1031,13 +1031,21 @@ 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+
VALUE casted_obj = rb_proc_call_with_block(state->as_json, 1, &obj, Qnil);
1038+
if (casted_obj != obj) {
1039+
increase_depth(state);
1040+
generate_json(buffer, data, state, casted_obj);
1041+
state->depth--;
1042+
return;
1043+
}
1044+
}
1045+
raise_generator_error(obj, "%"PRIsVALUE" not allowed in JSON", rb_funcall(obj, i_to_s, 0));
10381046
}
10391047
}
1040-
fbuffer_append_str(buffer, tmp);
1048+
fbuffer_append_str(buffer, rb_funcall(obj, i_to_s, 0));
10411049
}
10421050

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

java/src/json/ext/Generator.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,17 @@ void generate(ThreadContext context, Session session, RubyFloat object, OutputSt
261261
double value = object.getValue();
262262

263263
if (Double.isInfinite(value) || Double.isNaN(value)) {
264-
if (!session.getState(context).allowNaN()) {
264+
GeneratorState state = session.getState(context);
265+
266+
if (!state.allowNaN()) {
267+
if (state.strict() && state.getAsJSON() != null) {
268+
IRubyObject castedValue = state.getAsJSON().call(context, object);
269+
if (castedValue != object) {
270+
getHandlerFor(context.runtime, castedValue).generate(context, session, castedValue, buffer);
271+
return;
272+
}
273+
}
274+
265275
throw Utils.buildGeneratorError(context, object, object + " not allowed in JSON").toThrowable();
266276
}
267277
}

lib/json/truffle_ruby/generator.rb

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -570,18 +570,23 @@ def to_json(*) to_s end
570570

571571
module Float
572572
# Returns a JSON string representation for this Float number.
573-
def to_json(state = nil, *)
573+
def to_json(state = nil, *args)
574574
state = State.from_state(state)
575-
case
576-
when infinite?
577-
if state.allow_nan?
578-
to_s
579-
else
580-
raise GeneratorError.new("#{self} not allowed in JSON", self)
581-
end
582-
when nan?
575+
if infinite? || nan?
583576
if state.allow_nan?
584577
to_s
578+
elsif state.strict? && state.as_json
579+
casted_value = state.as_json.call(self)
580+
581+
if casted_value.equal?(self)
582+
raise GeneratorError.new("#{self} not allowed in JSON", self)
583+
end
584+
585+
state.check_max_nesting
586+
state.depth += 1
587+
result = casted_value.to_json(state, *args)
588+
state.depth -= 1
589+
result
585590
else
586591
raise GeneratorError.new("#{self} not allowed in JSON", self)
587592
end

test/json/json_coder_test.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,19 @@ def test_json_coder_load_options
3535
coder = JSON::Coder.new(symbolize_names: true)
3636
assert_equal({a: 1}, coder.load('{"a":1}'))
3737
end
38+
39+
def test_json_coder_dump_NaN_or_Infinity
40+
coder = JSON::Coder.new(&:inspect)
41+
assert_equal "NaN", coder.load(coder.dump(Float::NAN))
42+
assert_equal "Infinity", coder.load(coder.dump(Float::INFINITY))
43+
assert_equal "-Infinity", coder.load(coder.dump(-Float::INFINITY))
44+
end
45+
46+
def test_json_coder_dump_NaN_or_Infinity_loop
47+
coder = JSON::Coder.new(&:itself)
48+
error = assert_raise JSON::GeneratorError do
49+
coder.dump(Float::NAN)
50+
end
51+
assert_include error.message, "NaN not allowed in JSON"
52+
end
3853
end

0 commit comments

Comments
 (0)