Charlie Sharpsteen 7248afc490 Hardwire multi_json backend
The `multi_json` gem dynamically selects a JSON implementation from a list of
candidates. Since we cannot control which gems are installed on a user's
machine, this patch hardwires `multi_json` to use the included copy of `ok_json`.

`ok_json` is a pure-Ruby JSON encoder/decoder that is bundled with
`multi_json`. `ok_json` may not be as fast as other choices, but speed is not
critical for our application.

Closes Homebrew/homebrew#8574.

Signed-off-by: Charlie Sharpsteen <source@sharpsteen.net>
2011-11-13 19:23:00 -08:00

49 lines
1.2 KiB
Ruby

require "vendor/multi_json/vendor/ok_json" unless defined?(::OkJson)
module MultiJson
module Engines
class OkJson
ParseError = ::OkJson::Error
def self.decode(string, options = {}) #:nodoc:
string = string.read if string.respond_to?(:read)
result = ::OkJson.decode(string)
options[:symbolize_keys] ? symbolize_keys(result) : result
end
def self.encode(object) #:nodoc:
::OkJson.valenc(stringify_keys(object))
end
def self.symbolize_keys(object) #:nodoc:
modify_keys(object) do |key|
key.is_a?(String) ? key.to_sym : key
end
end
def self.stringify_keys(object) #:nodoc:
modify_keys(object) do |key|
key.is_a?(Symbol) ? key.to_s : key
end
end
def self.modify_keys(object, &modifier) #:nodoc:
case object
when Array
object.map do |value|
modify_keys(value, &modifier)
end
when Hash
object.inject({}) do |result, (key, value)|
new_key = modifier.call(key)
new_value = modify_keys(value, &modifier)
result.merge! new_key => new_value
end
else
object
end
end
end
end
end