Encode a Ruby string to a JSON string
There are a myriad of JSON gems for Ruby, some pure Ruby some C-based for better performance.
Here is one that offers both pure-Ruby and C:http://flori.github.com/json/
And then its essentially:
require 'json'JSON.encode(something)
A popular JSON encoder/decoder with native C-bindings for performance is Yajl: https://github.com/brianmario/yajl-ruby
UPD from @Stewart:JSON.encode
is provided by rails as is object.to_json
. For uses outside of rails use JSON.generate
which is in ruby 1.9.3 std-lib. – Stewart