Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Create a JSON builder for resource representations

#5425

Rails does not include a way to create a custom JSON representation of a resource that follows the patterns of creating other representations.

From my understanding, a View defines the format and templates of how a resource is sent back to the client and one can easily define HTML and XML representations through the use of files like "show.html.erb" and "show.xml.builder". However, when trying to create a custom JSON representation, one must go into their Model file and override the as_json method, which is inconsistent with how other representations are defined.

Also, when trying to define custom JSON representations that include associations, one must rely on the associations to define their JSON representation when the parent resource may not care about using all of that data.

For example, consider a student that has_many courses. A developer might want their JSON representation to include courses like so:

{
  //...a bunch of data about the student...//
  courses: [
    <uri root>/courses/123,
    <uri root>/courses/143,
    <uri root>/courses/1653,
    <uri root>/courses/15436
  ]
}

however, as_json only allows a developer to access the courses resource like so:

def as_json(options={})
  {
    # ...student data...
    :courses => self.courses
  }
end

which makes the JSON representation of a student's courses bound to the JSON representation of a course:

{
  /*...a bunch of data about the student...*/
  courses: [
    { /*...all course data...*/ },
    { /*...all course data...*/ },
    { /*...all course data...*/ },
    { /*...all course data...*/ },
  ]
}

Lastly, there is a method to use the XML Builder view to create a JSON representation, but is a method that requires some hacking of code to do:

http://web.archive.org/web/20080205212821/http://blog.tagteamintera... (See "Sexy REST in Rails" section)

Edited by Rohit Arondekar for formating.

Reported by John Ryding · August 21st, 2010 @ 05:24 PM

State: stale
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Rohit Arondekar
    Rohit Arondekar
    • State changed from new to open
    • Importance changed from to Low

    Opening ticket to try and get more people to take a look and give their views.

    August 25th, 2010 @ 03:28 AM

  2. Ryan Bigg
    Ryan Bigg
    • Tag cleared.

    Automatic cleanup of spam.

    October 11th, 2010 @ 12:09 PM

  3. Ryan Bigg
    Ryan Bigg

    Automatic cleanup of spam.

    October 19th, 2010 @ 08:26 AM

  4. Jeff Kreeftmeijer
  5. Santiago Pastorino
    Santiago Pastorino

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    February 2nd, 2011 @ 04:20 PM

  6. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 04:20 PM

  7. bingbing
  8. Joël AZEMAR
    Joël AZEMAR
    • Tag set to builder, json, tempate, view

    The integration of a json builder's templates like .json.builder is it planned?

    May 8th, 2011 @ 11:16 AM