Skip to content

Commit 2c27755

Browse files
authored
Merge pull request #637 from nobu/brevity
Use `String#b` and `String#+`
2 parents d48f7ff + 7efa2c3 commit 2c27755

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

lib/json/pure/generator.rb

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ module JSON
5050
# Convert a UTF8 encoded Ruby string _string_ to a JSON string, encoded with
5151
# UTF16 big endian characters as \u????, and return it.
5252
def utf8_to_json(string, script_safe = false) # :nodoc:
53-
string = string.dup
54-
string.force_encoding(::Encoding::ASCII_8BIT)
53+
string = string.b
5554
if script_safe
5655
string.gsub!(SCRIPT_SAFE_ESCAPE_PATTERN) { SCRIPT_SAFE_MAP[$&] || $& }
5756
else
@@ -62,8 +61,7 @@ def utf8_to_json(string, script_safe = false) # :nodoc:
6261
end
6362

6463
def utf8_to_json_ascii(string, script_safe = false) # :nodoc:
65-
string = string.dup
66-
string.force_encoding(::Encoding::ASCII_8BIT)
64+
string = string.b
6765
map = script_safe ? SCRIPT_SAFE_MAP : MAP
6866
string.gsub!(/[\/"\\\x0-\x1f]/n) { map[$&] || $& }
6967
string.gsub!(/(
@@ -409,16 +407,14 @@ def json_shift(state)
409407

410408
def json_transform(state)
411409
delim = ",#{state.object_nl}"
412-
result = "{#{state.object_nl}"
413-
result = result.dup if result.frozen? # RUBY_VERSION < 3.0
410+
result = +"{#{state.object_nl}"
414411
depth = state.depth += 1
415412
first = true
416413
indent = !state.object_nl.empty?
417414
each { |key, value|
418415
result << delim unless first
419416
result << state.indent * depth if indent
420-
result = "#{result}#{key.to_s.to_json(state)}#{state.space_before}:#{state.space}"
421-
result = result.dup if result.frozen? # RUBY_VERSION < 3.0
417+
result = +"#{result}#{key.to_s.to_json(state)}#{state.space_before}:#{state.space}"
422418
if state.strict? && !(false == value || true == value || nil == value || String === value || Array === value || Hash === value || Integer === value || Float === value)
423419
raise GeneratorError, "#{value.class} not allowed in JSON"
424420
elsif value.respond_to?(:to_json)

0 commit comments

Comments
 (0)