json for ruby [jruby] install and usage
$> sudo gem install json
$> sudo jruby -S gem install json_pure
require 'json'
class Range
def to_json(*a)
{
'json_class' => self.class.name,
'data' => [ first, last, exclude_end? ]
}.to_json(*a)
end
def self.json_create(o)
new(*o['data'])
end
end
puts (1..10).to_json
p JSON.parse((1..10).to_json)
puts JSON.parse((1..10).to_json) == (1..10)
json =<<-"JSON"
{
"hasItems": true,
"totalItems": 5,
"orderId": "xxx-xxxxx-xxx",
"hasDetails": true,
"details": [{"item": "2323-2323", "number": 2, "price": 2.00}, {"item": "2323-2324", "number": 3, "price": 3.00}]
}
JSON
p JSON.parse(json)["hasItems"] # => true
p JSON.parse(json)["totalItems"] # => 5
notice: keys and values in json string must be quoted.