Skip to content

Instantly share code, notes, and snippets.

@pjkelly
Created July 21, 2011 02:16
Show Gist options
  • Save pjkelly/1096376 to your computer and use it in GitHub Desktop.
Save pjkelly/1096376 to your computer and use it in GitHub Desktop.
JSON Encoding in Rails 1.2.x
# In Rails 1.2.x, the default JSON encoder doesn't quote
# attributes. To force quoting, set the following option
# to false. From the ActiveSupport::JSON docs:
#
# When +true+, Hash#to_json will omit quoting string or symbol keys
# if the keys are valid JavaScript identifiers. Note that this is
# technically improper JSON (all object keys must be quoted), so if
# you need strict JSON compliance, set this option to +false+.
# mattr_accessor :unquote_hash_key_identifiers
# @@unquote_hash_key_identifiers = true
ActiveSupport::JSON.unquote_hash_key_identifiers = false
collection = Product.find(:all).collect { |p| p.attributes }.to_json
puts collection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment