Ruby objects and JSON serialization (without Rails)

前端 未结 11 1933
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-28 03:32

I\'m trying to understand the JSON serialization landscape in Ruby. I\'m new to Ruby.

Is there any good JSON serialization options if you are not working with Rails?

11条回答
  •  自闭症患者
    2020-11-28 04:06

    Jbuilder is a gem built by rails community. But it works well in non-rails environments and have a cool set of features.

    # suppose we have a sample object as below
    sampleObj.name #=> foo
    sampleObj.last_name #=> bar
    
    # using jbuilder we can convert it to json:
    Jbuilder.encode do |json|
      json.name sampleObj.name
      json.last_name sampleObj.last_name
    end #=> "{:\"name\" => \"foo\", :\"last_name\" => \"bar\"}"
    

提交回复
热议问题