Advanced use of to_xml and to_json

Hello,

I’m working on a Rails app which has a REST API. I manage two formats
JSON and XML.
Very often to simplify the use if the API, I’m making includes. But when
you add params to to_json and to_xml like :only, :except, it’s not only
applied on the root object but on all the objects included.

Do you know libs or methods to answer to these different problems:

  • Tell never return a user password in the API
  • Tell always include a relationship or a method in each user serialized
    in the API
  • Serialize objects depending on context. For exemple, when a user ask
    for the user list, he should see its complete profile, a partial profile
    of its friends and just the name of others…
  • Don’t have to copy paste the parameters for xml and json

Thank you very much,
Camille R.

Hi Camille,

On Sat, 2010-02-20 at 17:15 +0100, Camille R. wrote:

Hello,

I’m working on a Rails app which has a REST API. I manage two formats
JSON and XML.
Very often to simplify the use if the API, I’m making includes. But when
you add params to to_json and to_xml like :only, :except, it’s not only
applied on the root object but on all the objects included.

I use Builder when the to_xml method is not flexible enough.

http://api.rubyonrails.org/classes/Builder/XmlMarkup.html

For json, there is no current equivalent AFAIK, but googling ‘rails
builder json’ gave some trails to follow.

HTH,
Bill