{"id":25874258,"url":"https://github.com/pioz/to_j","last_synced_at":"2026-04-16T04:02:53.441Z","repository":{"id":32402985,"uuid":"132625143","full_name":"pioz/to_j","owner":"pioz","description":"Fast JSON serializer for Rails based on Jbuilder with concept of views","archived":false,"fork":false,"pushed_at":"2023-01-31T04:04:49.000Z","size":148,"stargazers_count":0,"open_issues_count":8,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-12-05T18:48:16.304Z","etag":null,"topics":["jbuilder","json","rails","serializer"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pioz.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"MIT-LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-05-08T14:59:23.000Z","updated_at":"2022-03-08T22:05:29.000Z","dependencies_parsed_at":"2023-02-16T15:46:15.360Z","dependency_job_id":null,"html_url":"https://github.com/pioz/to_j","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/pioz/to_j","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pioz%2Fto_j","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pioz%2Fto_j/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pioz%2Fto_j/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pioz%2Fto_j/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pioz","download_url":"https://codeload.github.com/pioz/to_j/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pioz%2Fto_j/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31870516,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-15T15:24:51.572Z","status":"online","status_checked_at":"2026-04-16T02:00:06.042Z","response_time":69,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["jbuilder","json","rails","serializer"],"created_at":"2025-03-02T09:19:38.709Z","updated_at":"2026-04-16T04:02:53.420Z","avatar_url":"https://github.com/pioz.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ToJ\n\n[![Build Status](https://travis-ci.org/pioz/to_j.svg?branch=master)](https://travis-ci.org/pioz/to_j)\n[![Coverage Status](https://coveralls.io/repos/github/pioz/to_j/badge.svg?branch=master\u0026service=github)](https://coveralls.io/github/pioz/to_j)\n[![Github Actions](https://github.com/pioz/to_j/workflows/CI/badge.svg)](https://github.com/pioz/to_j/actions)\n\nToJ is a helpful gem that allows you to build json from your active model\nobject or collection. ToJ allows you to define a Serializer with many methods\ncalled `views`. A view is a method that describe how to generate json. So\nif you want to generate a json with only the author data you can use\n`author.to_j`, but if you want add also the books you can use\n`author.to_j(view: :with_books)`.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'to_j'\n```\n\nAnd then execute:\n\n```bash\n$ bundle\n```\n\nOr install it yourself as:\n\n```bash\n$ gem install to_j\n```\n\n## Usage\n\nIn your models include the ToJ module:\n\n```ruby\nclass Author \u003c ApplicationRecord\n  include ToJ\n  has_many :books, dependent: :destroy\nend\n\nclass Book \u003c ApplicationRecord\n  include ToJ\n  belongs_to :author\n\n  def nice_title\n    self.title.titleize\n  end\nend\n```\n\nThen in the `app/serializers` directory create a serializer for each model:\n\n```ruby\n# app/serializers/author_serializer.rb\nmodule AuthorSerializer\n\n  def default(author, options = {})\n    self.extract!(author, :id, :name)\n    if options[:user]\u0026.admin?\n      self.extract!(author, :email)\n    end\n  end\n\n  def with_books(author, options = {})\n    default(author, options)\n    self.books do\n      self.array!(author.books.map { |book| book.to_j(options) })\n    end\n  end\n\nend\n```\n\n```ruby\n# app/serializers/book_serializer.rb\nmodule BookSerializer\n\n  def default(book, options = {})\n    self.extract!(book, :id, :title, :nice_title)\n  end\n\n  def only_title(book, options = {})\n    self.extract!(book, :title)\n  end\n\nend\n```\n\nThe default view is called if the option `:view` is not present on `to_j`\noptions param.\n\nNow you can call:\n\n```ruby\nAuthor.limit(100).to_j\n# [{\"id\"=\u003e1028680, \"name\"=\u003e\"Kristopher\", {...}, ...]\nAuthor.limit(100).to_j(view: :with_books)\n# [{\"id\"=\u003e1028680, \"name\"=\u003e\"Kristopher\", \"books\"=\u003e[{\"id\"=\u003e192756823, \"title\"=\u003e\"If not now, when?\", \"nice_title\"=\u003e\"If Not Now, When?\"}, {...}, ...]\nBook.first.to_j(view: :only_title)\n# {\"title\"=\u003e\"Jesting pilate\"}\n```\n\nor in your controllers:\n\n```ruby\n\ndef index\n  @authors = Author.limit(100)\n  respond_to do |format|\n    format.html\n    format.json { render json: @authors.to_j(user: current_user) }\n  end\nend\n\ndef show\n  @author = Author.find(params[:id])\n  respond_to do |format|\n    format.html\n    format.json { render json: @author.to_j(view: :with_books) }\n  end\nend\n\n```\n\nIf a serializer or `default` method is not defined all table columns will be\nadded in the json (default Rails behavior).\n\n### Under the hood there is Jbuilder\n\nThe `self` object in serializer's methods are a Jbuilder object so you can use\nthe [Jbuilder](https://github.com/rails/jbuilder/) DSL to create and\npersonalized your json views.\n\n## Performance\n\nIn the `test` directory I've write a simple benchmark to check the performance of this gem with other common serializers:\n\n- [as_json](http://api.rubyonrails.org/classes/ActiveModel/Serializers/JSON.html#method-i-as_json)\n- [active_model_serializers](https://github.com/rails-api/active_model_serializers/)\n- [Jbuilder](https://github.com/rails/jbuilder/)\n\nHere the results to build a json of `10_000` authors with a total of `100_000` books:\n\n```\nRun options: --seed 333\n\n# Running:\n\n         user        system    total     real\nto_j      9.950000   0.340000  10.290000 ( 10.367328)\nas_json  15.460000   0.400000  15.860000 ( 15.942882)\nams      16.000000   0.460000  16.460000 ( 16.578348)\njbuilder  9.940000   0.380000  10.320000 ( 10.305331)\n\n```\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpioz%2Fto_j","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpioz%2Fto_j","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpioz%2Fto_j/lists"}