Skip to content

Instantly share code, notes, and snippets.

@mudge
Created October 29, 2009 10:59
Show Gist options
  • Save mudge/221351 to your computer and use it in GitHub Desktop.
Save mudge/221351 to your computer and use it in GitHub Desktop.
A Rails initializer to use YAJL instead of ActiveSupport for JSON encoding.
# Use YAJL instead of ActiveSupport::JSON to encode objects to JSON in Rails.
class Hash
def to_json(options = nil)
Yajl::Encoder.encode(as_json(options))
end
end
class Array
def to_json(options = nil)
Yajl::Encoder.encode(as_json(options))
end
def as_json(options = nil)
map { |value| value.as_json(options) }
end
end
class Object
def to_json(options = nil)
Yajl::Encoder.encode(as_json(options))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment