{"id":16506803,"url":"https://github.com/outlawandy/attr_serializer","last_synced_at":"2026-05-09T05:03:35.516Z","repository":{"id":62553714,"uuid":"92857617","full_name":"OutlawAndy/attr_serializer","owner":"OutlawAndy","description":"easy JSON serialization for ActiveRecord models","archived":false,"fork":false,"pushed_at":"2022-09-25T20:45:43.000Z","size":10,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2026-04-03T15:56:36.522Z","etag":null,"topics":["activerecord","activerecord-models","json","json-serialization","rails","rails-api","serialization"],"latest_commit_sha":null,"homepage":null,"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/OutlawAndy.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-05-30T17:15:34.000Z","updated_at":"2022-09-25T20:44:20.000Z","dependencies_parsed_at":"2022-11-03T04:30:35.496Z","dependency_job_id":null,"html_url":"https://github.com/OutlawAndy/attr_serializer","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/OutlawAndy/attr_serializer","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OutlawAndy%2Fattr_serializer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OutlawAndy%2Fattr_serializer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OutlawAndy%2Fattr_serializer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OutlawAndy%2Fattr_serializer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/OutlawAndy","download_url":"https://codeload.github.com/OutlawAndy/attr_serializer/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OutlawAndy%2Fattr_serializer/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32807861,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-08T08:22:46.396Z","status":"online","status_checked_at":"2026-05-09T02:00:06.633Z","response_time":123,"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":["activerecord","activerecord-models","json","json-serialization","rails","rails-api","serialization"],"created_at":"2024-10-11T15:22:18.295Z","updated_at":"2026-05-09T05:03:35.500Z","avatar_url":"https://github.com/OutlawAndy.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# AttrSerializer  [![Gem Version](https://badge.fury.io/rb/attr_serializer.svg)](https://badge.fury.io/rb/attr_serializer)\neasy JSON serialization for ActiveRecord models\n\n\n## Installation\n\nAdd this line to your application's Gemfile\n\n```ruby\ngem 'attr_serializer'\n```\n\nOr install it yourself\n\n    $ gem install attr_serializer\n\n## Usage\n\nin your ActiveRecord models\n```ruby\nclass User \u003c ActiveRecord::Base\n  attr_serializer :id, :name\nend\n```\n\nNow, whenever `to_json` is called on an instance or collection of `User` objects, only `id` \u0026 `name` will be serialized\n```ruby\nclass UsersController \u003c ApplicationController\n\n  def show\n    render json: User.find(123)\n  end\n\nend\n```\n```json\n{\"id\": 123, \"name\": \"Bob Jones\"}\n```\n\nAny attribute or method name can be supplied to `attr_serializer`\n```ruby\nclass User \u003c ActiveRecord::Base\n  attr_serializer :id, :name, :some_data\n\n  def some_data\n    \"not in database\"\n  end\nend\n```\n```json\n{\"id\": 123, \"name\": \"Bob Jones\", \"some_data\": \"not in database\"}\n```\n\nYou may also specify alternate keys for the resulting `json` by passing a hash as the last argument to `attr_serializer`\n```ruby\nclass User \u003c ActiveRecord::Base\n  attr_serializer :id, :name, someData: :some_data\n\n  def some_data\n    \"not in database\"\n  end\nend\n```\n```json\n{\"id\": 123, \"name\": \"Bob Jones\", \"someData\": \"not in database\"}\n```\n\n`attr_serializer` is also inherited accross STI models\n```ruby\nclass Admin \u003c User\n  attr_serializer :admin_stuff\n\n  def admin_stuff\n    \"super important\"\n  end\nend\nAdmin.first.to_json\n#=\u003e \"{\\\"id\\\": 124, \\\"name\\\": \\\"Admin Bob\\\", \\\"someData\\\": \\\"not in database\\\", \\\"admin_stuff\\\": \\\"super important\\\"}\"\n```\n\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/OutlawAndy/attr_serializer.\n\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foutlawandy%2Fattr_serializer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Foutlawandy%2Fattr_serializer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foutlawandy%2Fattr_serializer/lists"}