rails/rails — pull request #11604 by sergiocampama (2013-07-25 21:35:13+00:00)
Title: JSON serializer options now accept optional names.

'only' and 'methods' options for the JSON serializer now accept hashes
in which the key is the attribute to store and the value is the name in
which the result will be stored. 'include' now accepts a root option
that will be the name the association will be stored under.

person = Person.create(name: "Bob")
person.to_json(only: {name: :real_name})
=> "{\"real_name\":\"Bob\"}"

person.to_json(methods: {full_name: :new_name})
=> "{\"new_name\":\"Bob McKenzie\"}"

person.to_json(only :name, include: {children: {root: :kids, only:
:name}})
=> "{\"name\":\"Bob\",\"kids\":[{\"name\":\"Jimmy\"}]"