From 6508455d821d8bad5b73830c93010f37e4ce6651 Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Thu, 27 Mar 2025 09:54:51 +0100 Subject: [PATCH] Deprecate `JSON.fast_generate` --- CHANGES.md | 1 + lib/json/common.rb | 18 ++++-------------- test/json/json_generator_test.rb | 19 ------------------- 3 files changed, 5 insertions(+), 33 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 159c4f00f..9e00cd543 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,6 @@ # Changes +* Deprecate `JSON.fast_generate` (it's not any faster, so pointless). * Deprecate `JSON.load_default_options`. * Deprecate `JSON.unsafe_load_default_options`. * Deprecate `JSON.dump_default_options`. diff --git a/lib/json/common.rb b/lib/json/common.rb index b2f11afe5..c9f6d7505 100644 --- a/lib/json/common.rb +++ b/lib/json/common.rb @@ -74,16 +74,6 @@ def generator=(generator) # :nodoc: $VERBOSE = old end - def create_fast_state - State.new( - :indent => '', - :space => '', - :object_nl => "", - :array_nl => "", - :max_nesting => false - ) - end - def create_pretty_state State.new( :indent => ' ', @@ -368,12 +358,12 @@ def generate(obj, opts = nil) # # Raises SystemStackError (stack level too deep): # JSON.fast_generate(a) def fast_generate(obj, opts = nil) - if State === opts - state = opts + if RUBY_VERSION >= "3.0" + warn "JSON.fast_generate is deprecated and will be removed in json 3.0.0, just use JSON.generate", uplevel: 1, category: :deprecated else - state = JSON.create_fast_state.configure(opts) + warn "JSON.fast_generate is deprecated and will be removed in json 3.0.0, just use JSON.generate", uplevel: 1 end - state.generate(obj) + generate(obj, opts) end # :call-seq: diff --git a/test/json/json_generator_test.rb b/test/json/json_generator_test.rb index f0dec452f..64636c12c 100755 --- a/test/json/json_generator_test.rb +++ b/test/json/json_generator_test.rb @@ -237,25 +237,6 @@ def test_safe_state }.sort_by { |n,| n.to_s }, state.to_h.sort_by { |n,| n.to_s }) end - def test_fast_state - state = JSON.create_fast_state - assert_equal({ - :allow_nan => false, - :array_nl => "", - :as_json => false, - :ascii_only => false, - :buffer_initial_length => 1024, - :depth => 0, - :script_safe => false, - :strict => false, - :indent => "", - :max_nesting => 0, - :object_nl => "", - :space => "", - :space_before => "", - }.sort_by { |n,| n.to_s }, state.to_h.sort_by { |n,| n.to_s }) - end - def test_allow_nan error = assert_raise(GeneratorError) { generate([JSON::NaN]) } assert_same JSON::NaN, error.invalid_object