Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use String#b and String#+ #637

Merged
merged 2 commits into from
Oct 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions lib/json/pure/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ module JSON
# Convert a UTF8 encoded Ruby string _string_ to a JSON string, encoded with
# UTF16 big endian characters as \u????, and return it.
def utf8_to_json(string, script_safe = false) # :nodoc:
string = string.dup
string.force_encoding(::Encoding::ASCII_8BIT)
string = string.b
if script_safe
string.gsub!(SCRIPT_SAFE_ESCAPE_PATTERN) { SCRIPT_SAFE_MAP[$&] || $& }
else
Expand All @@ -62,8 +61,7 @@ def utf8_to_json(string, script_safe = false) # :nodoc:
end

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

def json_transform(state)
delim = ",#{state.object_nl}"
result = "{#{state.object_nl}"
result = result.dup if result.frozen? # RUBY_VERSION < 3.0
result = +"{#{state.object_nl}"
depth = state.depth += 1
first = true
indent = !state.object_nl.empty?
each { |key, value|
result << delim unless first
result << state.indent * depth if indent
result = "#{result}#{key.to_s.to_json(state)}#{state.space_before}:#{state.space}"
result = result.dup if result.frozen? # RUBY_VERSION < 3.0
result = +"#{result}#{key.to_s.to_json(state)}#{state.space_before}:#{state.space}"
if state.strict? && !(false == value || true == value || nil == value || String === value || Array === value || Hash === value || Integer === value || Float === value)
raise GeneratorError, "#{value.class} not allowed in JSON"
elsif value.respond_to?(:to_json)
Expand Down
Loading