3 ms·
And when those changes are made, I can change my version to: response = {:content => "Hello, #{params[:name]}"} respond_to do |wants| wants.json {
by latch 14y ago
And when those changes are made, I can change my version to:
response = {:content => "Hello, #{params[:name]}"}
respond_to do |wants|
wants.json { response.to_json }
wants.xml { response.to_xml }
end
- fizx 14y agoI've been trying to ignore this particular flame-war, but your example is doing fundamentally less, because it's not binding objects to json (which was what you originally asked for). I actually cannot write your example easily in Sinatra/Ruby/etc, without making my object either a Hash or an ORM subclass, customizing serialization, or pulling in uncommon libraries. Here's your complete example, in reasonable Ruby. It's longer than the Java. class Greeting < Struct.new(:content) def content "Hello, #{super.content}" end def to_xml {:content => content}.to_xml end def to_json {:content => content}.to_json end end get '/hello' do response = { :content => Greeting.new(params[:name]) } respond_to do |wants| wants.json { response.to_json } wants.xml { response.to_xml } end end If I create an imaginary library that does exactly what we'd want here, it'd look like the following. It's marginally shorter than the java, but hard to argue anything about it being fundamentally better. class Greeting include JsonObject field :content def content "Hello, #{@content}" end end get '/hello' do response = { :content => Greeting.new(params[:name]) } respond_to do |wants| wants.json { response.to_json } wants.xml { response.to_xml } end end