Skip to content

Commit d4f83f9

Browse files
committed
Remove as_json from value and tests
Removing `as_json` here revealed that these tests failed because they return a non-json compliant value of NaNNumber and InfinitNumber. While we've supported this for awhile one of the issues with this support is that once we hit `jsonify` all the values _should_ already be json-compliant values. These tests I removed were added in rails#26933 - but in the comments on that PR they were supposed to be deprecated and removed in the next cycle - but that work never happened. See rails#26933 (comment) We want to remove this `as_json` here because Rails JSON is very slow. One of the reasons it's slow is because in some cases we actually loop over the JSON we're parsing 3 times! Here are the passes: 1. Call as_json on a tree of objects 1a. Special objects pass the options down. These are Object, Struct, Enumerable, Array, Hash 1b. This first pass eliminates Structs, Enumerables, and Objects Structs replaced with hashes, Enumerables -> Arrays, Objects -> Hash 1c. New tree contains only: String, Number, Hash, Array, nil, true, false 2. Tree is walked again 2a. Convert Strings to EscapedStrings 2b. Can retun bad values from as_json (like the infinity and nan) 3. Convert to json By removing this `as_json` we reduce one of the passes for this case. The argument here is that the first pass should never have returned a bad value so we shouldn't have to walk those values a second time - the value should be `nil` (which it what it will be on pass 3). [Eileen M. Uchitelle & Aaron Patterson]
1 parent aaacfd9 commit d4f83f9

File tree

2 files changed

+1
-21
lines changed

2 files changed

+1
-21
lines changed

activesupport/lib/active_support/json/encoding.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def jsonify(value)
9191
when String
9292
EscapedString.new(value)
9393
when Numeric, NilClass, TrueClass, FalseClass
94-
value.as_json
94+
value
9595
when Hash
9696
Hash[value.map { |k, v| [jsonify(k), jsonify(v)] }]
9797
when Array

activesupport/test/json/encoding_test.rb

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -443,26 +443,6 @@ def test_exception_to_json
443443
assert_equal '"foo"', ActiveSupport::JSON.encode(exception)
444444
end
445445

446-
class InfiniteNumber
447-
def as_json(options = nil)
448-
{ "number" => Float::INFINITY }
449-
end
450-
end
451-
452-
def test_to_json_works_when_as_json_returns_infinite_number
453-
assert_equal '{"number":null}', InfiniteNumber.new.to_json
454-
end
455-
456-
class NaNNumber
457-
def as_json(options = nil)
458-
{ "number" => Float::NAN }
459-
end
460-
end
461-
462-
def test_to_json_works_when_as_json_returns_NaN_number
463-
assert_equal '{"number":null}', NaNNumber.new.to_json
464-
end
465-
466446
def test_to_json_works_on_io_objects
467447
assert_equal STDOUT.to_s.to_json, STDOUT.to_json
468448
end

0 commit comments

Comments
 (0)