Created
June 14, 2010 21:16
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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