Skip to content

Instantly share code, notes, and snippets.

@danielbeardsley
Created June 14, 2010 21:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielbeardsley/438310 to your computer and use it in GitHub Desktop.
Save danielbeardsley/438310 to your computer and use it in GitHub Desktop.
A Ruby String Class that doesn't quote itself when .to_json is called
# Represents a string that isn't surrounded by quotes when converted to JSON.
# Originally created to be able to create ruby hashes containing javascript
# function definitions that could be converted to JSON easily.
#
# Use it like this:
# {:number = > 10,
# :date => Time.now,
# :regular_string => "Some Quoted Text",
# :unquoted_string => UnQuotedString("function(){ alert('you can create a function'); }")
# }.to_json
#
# -> {'number':10,
# 'date':'2010-06-14T14:08:36-07:00',
# 'string':'Some Quoted Text',
# 'unquoted_string':function(){ alert('you can create a function'); }}
class UnQuotedString < String
def to_json(options = nil)
self
end
end
# Convenience Factory Method
def UnQuotedString(str)
UnQuotedString.new(str)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment