Skip to content

Commit

Permalink
fixes #7764 - add quirks mode to JSON.dump
Browse files Browse the repository at this point in the history
  • Loading branch information
shlomizadok authored and ares committed Feb 26, 2015
1 parent 1625a0d commit 8dcb01a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
9 changes: 8 additions & 1 deletion app/models/lookup_key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,14 @@ def default_value_before_type_cast
def value_before_type_cast(val)
case key_type.to_sym
when :json, :array
val = JSON.dump val
begin
val = JSON.dump(val)
rescue JSON::GeneratorError => error
## http://projects.theforeman.org/issues/9553
## @TODO: remove when upgrading to json >= 1.8
logger.debug "Fallback to quirks mode from error: '#{error}'"
val = JSON.dump_in_quirks_mode(val)
end
when :yaml, :hash
val = YAML.dump val
val.sub!(/\A---\s*$\n/, '')
Expand Down
24 changes: 24 additions & 0 deletions config/initializers/json.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'json/version'
module JSON
class << self
def dump_in_quirks_mode(obj, anIO = nil, limit = nil)
if anIO && limit.nil?
an_io_obj = anIO.to_io if anIO.respond_to?(:to_io)
unless anIO.respond_to?(:write)
limit = anIO
an_io_obj = nil
end
end
limit ||= 0
result = generate(obj, :allow_nan => true, :max_nesting => limit, :quirks_mode => true)
if an_io_obj
an_io_obj.write result
an_io_obj
else
result
end
rescue JSON::NestingError
raise ArgumentError, "exceed depth limit"
end
end
end

0 comments on commit 8dcb01a

Please sign in to comment.