Use frozen string literals

Co-authored-by: Jean Boussier <jean.boussier@gmail.com>
This commit is contained in:
Étienne Barrié 2024-10-21 12:04:56 +02:00 committed by Hiroshi SHIBATA
parent 5f97468958
commit 82f7550f65
28 changed files with 56 additions and 64 deletions

View File

@ -1,4 +1,4 @@
#frozen_string_literal: false # frozen_string_literal: true
require 'json/common' require 'json/common'
## ##

View File

@ -1,4 +1,4 @@
#frozen_string_literal: false # frozen_string_literal: true
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
require 'json' require 'json'
end end

View File

@ -1,4 +1,4 @@
#frozen_string_literal: false # frozen_string_literal: true
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
require 'json' require 'json'
end end

View File

@ -1,4 +1,4 @@
#frozen_string_literal: false # frozen_string_literal: true
# This file requires the implementations of ruby core's custom objects for # This file requires the implementations of ruby core's custom objects for
# serialisation/deserialisation. # serialisation/deserialisation.

View File

@ -1,4 +1,4 @@
#frozen_string_literal: false # frozen_string_literal: true
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
require 'json' require 'json'
end end

View File

@ -1,4 +1,4 @@
#frozen_string_literal: false # frozen_string_literal: true
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
require 'json' require 'json'
end end

View File

@ -1,4 +1,4 @@
#frozen_string_literal: false # frozen_string_literal: true
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
require 'json' require 'json'
end end

View File

@ -1,4 +1,4 @@
#frozen_string_literal: false # frozen_string_literal: true
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
require 'json' require 'json'
end end

View File

@ -1,4 +1,4 @@
#frozen_string_literal: false # frozen_string_literal: true
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
require 'json' require 'json'
end end

View File

@ -1,4 +1,4 @@
#frozen_string_literal: false # frozen_string_literal: true
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
require 'json' require 'json'
end end

View File

@ -1,4 +1,4 @@
#frozen_string_literal: false # frozen_string_literal: true
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
require 'json' require 'json'
end end

View File

@ -1,4 +1,4 @@
#frozen_string_literal: false # frozen_string_literal: true
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
require 'json' require 'json'
end end

View File

@ -1,5 +1,4 @@
# frozen_string_literal: true
#frozen_string_literal: false
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
require 'json' require 'json'
end end

View File

@ -1,4 +1,4 @@
#frozen_string_literal: false # frozen_string_literal: true
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
require 'json' require 'json'
end end

View File

@ -1,4 +1,4 @@
#frozen_string_literal: false #frozen_string_literal: true
require 'json/version' require 'json/version'
module JSON module JSON
@ -117,23 +117,17 @@ module JSON
attr_accessor :state attr_accessor :state
end end
DEFAULT_CREATE_ID = 'json_class'.freeze
private_constant :DEFAULT_CREATE_ID
CREATE_ID_TLS_KEY = "JSON.create_id".freeze
private_constant :CREATE_ID_TLS_KEY
# Sets create identifier, which is used to decide if the _json_create_ # Sets create identifier, which is used to decide if the _json_create_
# hook of a class should be called; initial value is +json_class+: # hook of a class should be called; initial value is +json_class+:
# JSON.create_id # => 'json_class' # JSON.create_id # => 'json_class'
def self.create_id=(new_value) def self.create_id=(new_value)
Thread.current[CREATE_ID_TLS_KEY] = new_value.dup.freeze Thread.current[:"JSON.create_id"] = new_value.dup.freeze
end end
# Returns the current create identifier. # Returns the current create identifier.
# See also JSON.create_id=. # See also JSON.create_id=.
def self.create_id def self.create_id
Thread.current[CREATE_ID_TLS_KEY] || DEFAULT_CREATE_ID Thread.current[:"JSON.create_id"] || 'json_class'
end end
NaN = 0.0/0 NaN = 0.0/0

View File

@ -1,4 +1,4 @@
#frozen_string_literal: false # frozen_string_literal: true
begin begin
require 'ostruct' require 'ostruct'
rescue LoadError rescue LoadError

View File

@ -1,4 +1,4 @@
# frozen_string_literal: false # frozen_string_literal: true
module JSON module JSON
# JSON version # JSON version
VERSION = '2.7.2' VERSION = '2.7.2'

View File

@ -1,4 +1,4 @@
# frozen_string_literal: false # frozen_string_literal: true
require 'mkmf' require 'mkmf'
have_func("rb_enc_raise", "ruby.h") have_func("rb_enc_raise", "ruby.h")

View File

@ -1,4 +1,4 @@
#frozen_string_literal: false # frozen_string_literal: true
require_relative 'test_helper' require_relative 'test_helper'
require 'json/add/core' require 'json/add/core'
require 'json/add/complex' require 'json/add/complex'
@ -114,8 +114,7 @@ class JSONAdditionTest < Test::Unit::TestCase
end end
def test_raw_strings def test_raw_strings
raw = '' raw = ''.b
raw.respond_to?(:encode!) and raw.encode!(Encoding::ASCII_8BIT)
raw_array = [] raw_array = []
for i in 0..255 for i in 0..255
raw << i raw << i

View File

@ -1,4 +1,4 @@
#frozen_string_literal: false # frozen_string_literal: true
require_relative 'test_helper' require_relative 'test_helper'
require 'stringio' require 'stringio'
require 'tempfile' require 'tempfile'

View File

@ -1,4 +1,4 @@
# frozen_string_literal: false # frozen_string_literal: true
require_relative 'test_helper' require_relative 'test_helper'
class JSONEncodingTest < Test::Unit::TestCase class JSONEncodingTest < Test::Unit::TestCase
@ -26,8 +26,8 @@ class JSONEncodingTest < Test::Unit::TestCase
end end
def test_generate def test_generate
assert_equal @generated, JSON.generate(@parsed, :ascii_only => true) assert_equal @generated, JSON.generate(@parsed, ascii_only: true)
assert_equal @generated, JSON.generate(@utf_16_data, :ascii_only => true) assert_equal @generated, JSON.generate(@utf_16_data, ascii_only: true)
end end
def test_unicode def test_unicode
@ -39,35 +39,35 @@ class JSONEncodingTest < Test::Unit::TestCase
assert_equal "\"#{0x7f.chr}\"", 0x7f.chr.to_json assert_equal "\"#{0x7f.chr}\"", 0x7f.chr.to_json
utf8 = [ "© ≠ €! \01" ] utf8 = [ "© ≠ €! \01" ]
json = '["© ≠ €! \u0001"]' json = '["© ≠ €! \u0001"]'
assert_equal json, utf8.to_json(:ascii_only => false) assert_equal json, utf8.to_json(ascii_only: false)
assert_equal utf8, parse(json) assert_equal utf8, parse(json)
json = '["\u00a9 \u2260 \u20ac! \u0001"]' json = '["\u00a9 \u2260 \u20ac! \u0001"]'
assert_equal json, utf8.to_json(:ascii_only => true) assert_equal json, utf8.to_json(ascii_only: true)
assert_equal utf8, parse(json) assert_equal utf8, parse(json)
utf8 = ["\343\201\202\343\201\204\343\201\206\343\201\210\343\201\212"] utf8 = ["\343\201\202\343\201\204\343\201\206\343\201\210\343\201\212"]
json = "[\"\343\201\202\343\201\204\343\201\206\343\201\210\343\201\212\"]" json = "[\"\343\201\202\343\201\204\343\201\206\343\201\210\343\201\212\"]"
assert_equal utf8, parse(json) assert_equal utf8, parse(json)
assert_equal json, utf8.to_json(:ascii_only => false) assert_equal json, utf8.to_json(ascii_only: false)
utf8 = ["\343\201\202\343\201\204\343\201\206\343\201\210\343\201\212"] utf8 = ["\343\201\202\343\201\204\343\201\206\343\201\210\343\201\212"]
assert_equal utf8, parse(json) assert_equal utf8, parse(json)
json = "[\"\\u3042\\u3044\\u3046\\u3048\\u304a\"]" json = "[\"\\u3042\\u3044\\u3046\\u3048\\u304a\"]"
assert_equal json, utf8.to_json(:ascii_only => true) assert_equal json, utf8.to_json(ascii_only: true)
assert_equal utf8, parse(json) assert_equal utf8, parse(json)
utf8 = ['საქართველო'] utf8 = ['საქართველო']
json = '["საქართველო"]' json = '["საქართველო"]'
assert_equal json, utf8.to_json(:ascii_only => false) assert_equal json, utf8.to_json(ascii_only: false)
json = "[\"\\u10e1\\u10d0\\u10e5\\u10d0\\u10e0\\u10d7\\u10d5\\u10d4\\u10da\\u10dd\"]" json = "[\"\\u10e1\\u10d0\\u10e5\\u10d0\\u10e0\\u10d7\\u10d5\\u10d4\\u10da\\u10dd\"]"
assert_equal json, utf8.to_json(:ascii_only => true) assert_equal json, utf8.to_json(ascii_only: true)
assert_equal utf8, parse(json) assert_equal utf8, parse(json)
assert_equal '["Ã"]', generate(["Ã"], :ascii_only => false) assert_equal '["Ã"]', generate(["Ã"], ascii_only: false)
assert_equal '["\\u00c3"]', generate(["Ã"], :ascii_only => true) assert_equal '["\\u00c3"]', generate(["Ã"], ascii_only: true)
assert_equal [""], parse('["\u20ac"]') assert_equal [""], parse('["\u20ac"]')
utf8 = ["\xf0\xa0\x80\x81"] utf8 = ["\xf0\xa0\x80\x81"]
json = "[\"\xf0\xa0\x80\x81\"]" json = "[\"\xf0\xa0\x80\x81\"]"
assert_equal json, generate(utf8, :ascii_only => false) assert_equal json, generate(utf8, ascii_only: false)
assert_equal utf8, parse(json) assert_equal utf8, parse(json)
json = '["\ud840\udc01"]' json = '["\ud840\udc01"]'
assert_equal json, generate(utf8, :ascii_only => true) assert_equal json, generate(utf8, ascii_only: true)
assert_equal utf8, parse(json) assert_equal utf8, parse(json)
assert_raise(JSON::ParserError) { parse('"\u"') } assert_raise(JSON::ParserError) { parse('"\u"') }
assert_raise(JSON::ParserError) { parse('"\ud800"') } assert_raise(JSON::ParserError) { parse('"\ud800"') }
@ -75,21 +75,21 @@ class JSONEncodingTest < Test::Unit::TestCase
def test_chars def test_chars
(0..0x7f).each do |i| (0..0x7f).each do |i|
json = '["\u%04x"]' % i json = '"\u%04x"' % i
i = i.chr i = i.chr
assert_equal i, parse(json).first[0] assert_equal i, parse(json)[0]
if i == ?\b if i == ?\b
generated = generate(["" << i]) generated = generate(i)
assert '["\b"]' == generated || '["\10"]' == generated assert '"\b"' == generated || '"\10"' == generated
elsif [?\n, ?\r, ?\t, ?\f].include?(i) elsif [?\n, ?\r, ?\t, ?\f].include?(i)
assert_equal '[' << ('' << i).dump << ']', generate(["" << i]) assert_equal i.dump, generate(i)
elsif i.chr < 0x20.chr elsif i.chr < 0x20.chr
assert_equal json, generate(["" << i]) assert_equal json, generate(i)
end end
end end
assert_raise(JSON::GeneratorError) do assert_raise(JSON::GeneratorError) do
generate(["\x80"], :ascii_only => true) generate(["\x80"], ascii_only: true)
end end
assert_equal "\302\200", parse('["\u0080"]').first assert_equal "\302\200", parse('"\u0080"')
end end
end end

View File

@ -1,4 +1,4 @@
#frozen_string_literal: false # frozen_string_literal: true
require_relative 'test_helper' require_relative 'test_helper'
class JSONExtParserTest < Test::Unit::TestCase class JSONExtParserTest < Test::Unit::TestCase

View File

@ -1,4 +1,4 @@
#frozen_string_literal: false # frozen_string_literal: true
require_relative 'test_helper' require_relative 'test_helper'
class JSONFixturesTest < Test::Unit::TestCase class JSONFixturesTest < Test::Unit::TestCase

View File

@ -1,5 +1,5 @@
#!/usr/bin/env ruby #!/usr/bin/env ruby
# frozen_string_literal: false # frozen_string_literal: true
require_relative 'test_helper' require_relative 'test_helper'
@ -304,12 +304,12 @@ EOT
state.configure(:indent => '1') state.configure(:indent => '1')
assert_equal '1', state.indent assert_equal '1', state.indent
state = JSON.state.new state = JSON.state.new
foo = 'foo' foo = 'foo'.dup
assert_raise(TypeError) do assert_raise(TypeError) do
state.configure(foo) state.configure(foo)
end end
def foo.to_h def foo.to_h
{ :indent => '2' } { indent: '2' }
end end
state.configure(foo) state.configure(foo)
assert_equal '2', state.indent assert_equal '2', state.indent

View File

@ -1,4 +1,4 @@
#frozen_string_literal: false # frozen_string_literal: true
require_relative 'test_helper' require_relative 'test_helper'
class JSONGenericObjectTest < Test::Unit::TestCase class JSONGenericObjectTest < Test::Unit::TestCase

View File

@ -1,5 +1,4 @@
# encoding: utf-8 # frozen_string_literal: true
# frozen_string_literal: false
require_relative 'test_helper' require_relative 'test_helper'
require 'stringio' require 'stringio'
require 'tempfile' require 'tempfile'
@ -36,7 +35,8 @@ class JSONParserTest < Test::Unit::TestCase
pend if RUBY_ENGINE == 'truffleruby' pend if RUBY_ENGINE == 'truffleruby'
bug10705 = '[ruby-core:67386] [Bug #10705]' bug10705 = '[ruby-core:67386] [Bug #10705]'
json = ".\"\xE2\x88\x9A\"".force_encoding(Encoding::UTF_8) json = ".\"\xE2\x88\x9A\""
assert_equal(Encoding::UTF_8, json.encoding)
e = assert_raise(JSON::ParserError) { e = assert_raise(JSON::ParserError) {
JSON::Ext::Parser.new(json).parse JSON::Ext::Parser.new(json).parse
} }

View File

@ -1,4 +1,4 @@
#frozen_string_literal: false # frozen_string_literal: true
require_relative 'test_helper' require_relative 'test_helper'
require 'time' require 'time'

View File

@ -1,4 +1,4 @@
# frozen_string_literal: false # frozen_string_literal: true
require_relative 'test_helper' require_relative 'test_helper'