{"id":17086456,"url":"https://github.com/dasch/kebab","last_synced_at":"2026-05-10T16:38:39.906Z","repository":{"id":21888555,"uuid":"25212307","full_name":"dasch/kebab","owner":"dasch","description":"A simple and straightforward way to serialize Ruby objects to JSON","archived":false,"fork":false,"pushed_at":"2014-10-15T09:25:17.000Z","size":164,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-22T14:46:33.039Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dasch.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-10-14T15:03:23.000Z","updated_at":"2014-10-14T17:12:29.000Z","dependencies_parsed_at":"2022-08-20T01:31:11.816Z","dependency_job_id":null,"html_url":"https://github.com/dasch/kebab","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/dasch/kebab","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dasch%2Fkebab","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dasch%2Fkebab/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dasch%2Fkebab/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dasch%2Fkebab/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dasch","download_url":"https://codeload.github.com/dasch/kebab/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dasch%2Fkebab/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32864082,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-10T13:40:02.631Z","status":"ssl_error","status_checked_at":"2026-05-10T13:40:02.145Z","response_time":54,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-14T13:28:41.668Z","updated_at":"2026-05-10T16:38:39.886Z","avatar_url":"https://github.com/dasch.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"Kebab\n=====\n\nA simple and straightforward way to serialize Ruby objects to JSON. The only thing you need to do is to add a _serializer class_ (more on that in a bit) that matches a model, and voilá!, you too can serialize JSON!\n\nLet's start with the basics, though. Imagine a set of Active Record models that looks like this:\n\n```ruby\nclass Post \u003c ActiveRecord::Base\n  belongs_to :author\n  has_many :comments\n  \n  validates_presence_of :title, :body\nend\n\nclass Author \u003c ActiveRecord::Base\n  validates_presence_of :name\nend\n\nclass Comment \u003c ActiveRecord::Base\n  belongs_to :post\n  belongs_to :author\n  \n  validates_presence_of :body\nend\n```\n\nSo here's the controller you'd write in order to list and show posts if you wanted to render HTML using ERb:\n\n```ruby\nclass PostsController \u003c ApplicationController\n  def index\n    @posts = Post.all\n  end\n  \n  def show\n    @post = Post.find(params[:id])\n  end\nend\n```\n\nHowever, we'd like to add a JSON API as well, preferably looking something like this:\n\n```js\n// /posts.json\n{\n  \"posts\": [\n    {\n      \"title\": \"...\",\n      \"body\": \"...\",\n      \"author\": { \"name\": \"...\" },\n      \"comments\": [\n        {\n          \"author\": { \"name\": \"...\" },\n          \"body\": \"...\"\n        }\n      ]\n    }\n  ]\n}\n\n// /posts/42.json\n{\n  \"post\": {\n    \"title\": \"...\",\n    \"body\": \"...\",\n    \"author\": { \"name\": \"...\" },\n    \"comments\": [\n      {\n        \"author\": { \"name\": \"...\" },\n        \"body\": \"...\"\n      }\n    ]\n  }\n}\n```\n\nIn order to add support for JSON rendering to this controller, you have to do three things. Nah, I'm kidding, you don't have to change the controller at all! Wohoo!\n\nInstead, you add these cute little classes to `app/serializers/`:\n\n```ruby\n# app/serializers/posts/index_serializer.rb\nclass Posts::IndexSerializer \u003c Kebab::Serializer\n  def posts\n    @posts\n  end\nend\n\n# app/serializers/posts/show_serializer.rb\nclass Posts::ShowSerializer \u003c Kebab::Serializer\n  def post\n    @post\n  end\nend\n\n# app/serializers/post_serializer.rb\nclass PostSerializer \u003c Kebab::Serializer\n  def title\n    @post.title\n  end\n  \n  def body\n    @post.body\n  end\n  \n  def author\n    @post.author\n  end\n  \n  def comments\n    @post.comments\n  end\nend\n\n# app/serializers/comment_serializer.rb\nclass CommentSerializer \u003c Kebab::Serializer\n  def body\n    @post.body\n  end\n  \n  def author\n    @post.author\n  end\nend\n\n# app/serializers/author_serializer.rb\nclass AuthorSerializer \u003c Kebab::Serializer\n  def name\n    @author.name\n  end\nend\n```\n\nWow, there's a lot of stuff going on here! Actually, it's pretty simple: when rendering the JSON response for the `index` action in `PostsController`, `Posts::IndexSerializer` is used. Because you assigned `@posts`, that instance variable will be available in the serializer instance. Each public method in a serializer corresponds to an attribute in the resulting JSON, and its return value will itself be serialized and used as the value. This means that returning an array of `Post` instances will result in each post being serialized – but how does that work? Well, convention triumphs again: `PostSerializer` will be used! This process will be repeated until all the objects have been serialized.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdasch%2Fkebab","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdasch%2Fkebab","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdasch%2Fkebab/lists"}