{"id":16549879,"url":"https://github.com/uiur/base_serializer","last_synced_at":"2026-05-09T08:10:13.634Z","repository":{"id":141423322,"uuid":"559963746","full_name":"uiur/base_serializer","owner":"uiur","description":"A JSON object presenter (like active_model_serializers)","archived":false,"fork":false,"pushed_at":"2022-12-07T00:55:35.000Z","size":22,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-04T15:30:43.245Z","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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/uiur.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-10-31T13:18:12.000Z","updated_at":"2025-02-24T03:47:01.000Z","dependencies_parsed_at":null,"dependency_job_id":"0552e102-e451-483d-b163-51daee5963b8","html_url":"https://github.com/uiur/base_serializer","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/uiur/base_serializer","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uiur%2Fbase_serializer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uiur%2Fbase_serializer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uiur%2Fbase_serializer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uiur%2Fbase_serializer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/uiur","download_url":"https://codeload.github.com/uiur/base_serializer/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uiur%2Fbase_serializer/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32811678,"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":[],"created_at":"2024-10-11T19:31:49.002Z","updated_at":"2026-05-09T08:10:13.597Z","avatar_url":"https://github.com/uiur.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# BaseSerializer\n\n**base_serializer** is a JSON object presenter like active_model_serializers.\n\nThe implementation is one file (\u003c 200 lines). It's easy to customize.\n\nbase_serializer doesn't have as many features as active_model_serializers, but the small set of features are enough to build a JSON API.\n\nIn a simple [benchmark](benchmark/), base_serializer is ~8x faster than active_model_serializers.\n\nIt's can be combined with Rails and any Ruby web frameworks.\n## Usage\n`serialize` method is used to render JSON serializable hash.\n\nFor example,\n\n```ruby\nclass ProductSerializer\n  include ::BaseSerializer\n  field :id, :name, :price, :created_at\nend\n\nProduct = Struct.new(:id, :name, :price, :created_at, keyword_init: true)\nproduct =\n  Product.new(\n    id: 1,\n    name: \"foo\",\n    price: 12.3,\n    created_at: Time.now\n  )\n\npp ProductSerializer.serialize(product)\n#=\u003e {:id=\u003e1, :name=\u003e\"foo\", :price=\u003e12.3, :created_at=\u003e\"2022-10-31T22:08:10.573+09:00\"}\n\n# `serialize` can take array of objects as an argument\n# It renders array of serialized hash\npp ProductSerializer.serialize([product])\n#=\u003e [{:id=\u003e1, :name=\u003e\"foo\", :price=\u003e12.3, :created_at=\u003e\"2022-11-12T15:29:33.820+09:00\"}]\n\n# It can render only selected fields\npp ProductSerializer.serialize([product], fields: [:id, :name])\n#=\u003e [{:id=\u003e1, :name=\u003e\"foo\"}]\n```\n\nSo in your Rails controller, you can write like this:\n\n```ruby\ndef index\n  products = Product.all\n  render json: ProductSerializer.serialize(products)\nend\n```\n\n### Association\nIt can render nested objects with has_many or belongs_to associations.\n\nbase_serializer just uses the `field` method to define associations.\n\n```ruby\nclass CommentSerializer\n  include ::BaseSerializer\n  field :id, :content\nend\n\nclass PostSerializer\n  include ::BaseSerializer\n  field :id, :title\n  field :comments, serializer: CommentSerializer\nend\n\npost =\n  OpenStruct.new(\n    id: 1,\n    title: \"foo\",\n    comments: [\n      OpenStruct.new(id: 2, content: \"bar\"),\n    ]\n  )\n\npp PostSerializer.serialize(post)\n#=\u003e {:id=\u003e1, :title=\u003e\"foo\", :comments=\u003e[{:id=\u003e2, :content=\u003e\"bar\"}]}\n\n# Fields of nested object like post.comments can be selected\npp PostSerializer.serialize(post, fields: [\n  :id,\n  :title,\n  comments: [:id]\n])\n#=\u003e {:id=\u003e1, :title=\u003e\"foo\", :comments=\u003e[{:id=\u003e2}]}\n```\n\n### Optional fields\n`default: false` option can be used to mark a field as optional.\n\nOptional fields are rendered only when fields are specified in `fields: [..]`.\n\n```ruby\nclass CommentSerializer\n  include ::BaseSerializer\n  field :id, :content\nend\n\nclass PostSerializer\n  include ::BaseSerializer\n  field :id, :title  # default: true (if not specified)\n  field :content, default: false\n  field :comments, serializer: CommentSerializer, default: false\nend\n\npost =\n  OpenStruct.new(\n    id: 1,\n    title: \"foo\",\n    content: 'foo content',\n    comments: [\n      OpenStruct.new(id: 2, content: \"bar\"),\n    ]\n  )\n\n# It renders only default fields (id and title) when fields are not specified\npp PostSerializer.serialize(post)\n#=\u003e {:id=\u003e1, :title=\u003e\"foo\"}\n\n# Optional fields are rendered when they are selected.\npp PostSerializer.serialize(post, fields: [:id, :title, :content, :comments])\n#=\u003e {:id=\u003e1, :title=\u003e\"foo\", :content=\u003e\"foo content\", :comments=\u003e[{:id=\u003e2, :content=\u003e\"bar\"}]}\n\npp PostSerializer.serialize(post, fields: [\n  :*,  # :* means all of default fields.\n  :comments\n])\n#=\u003e {:id=\u003e1, :title=\u003e\"foo\", :comments=\u003e[{:id=\u003e2, :content=\u003e\"bar\"}]}\n```\n\n### Defining methods in serializer\nDefining or overriding methods is allowed.\n\n```ruby\nclass ProductSerializer\n  include ::BaseSerializer\n  field :id, :name\n\n  def id\n    # The source object can be accessed by `object`\n    \"product-#{object.id}\"\n  end\nend\n\nProduct = Struct.new(:id, :name, keyword_init: true)\nproduct =\n  Product.new(\n    id: 1,\n    name: \"foo\",\n  )\n\npp ProductSerializer.serialize(product)\n#=\u003e {:id=\u003e\"product-1\", :name=\u003e\"foo\"}\n```\n\nAlso, it calls methods like `is_new?` of a source object when serializing.\n\n```ruby\nclass ProductSerializer\n  include ::BaseSerializer\n  field :id      # This calls ProductSerializer#id because it's defined.\n  field :name\n  field :is_new  # This calls Product#is_new? or Product#is_new if the method name is defined.\n\n  def id\n    \"product-#{object.id}\"\n  end\nend\n```\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'base_serializer', github: 'uiur/base_serializer', branch: 'main'\n```\n\nAnd then execute:\n\n    $ bundle install\n\nOr install it yourself as:\n\n    $ gem install base_serializer\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.\n\nTo install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/uiur/base_serializer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/uiur/base_serializer/blob/main/CODE_OF_CONDUCT.md).\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n\n## Code of Conduct\n\nEveryone interacting in the BaseSerializer project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/uiur/base_serializer/blob/main/CODE_OF_CONDUCT.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fuiur%2Fbase_serializer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fuiur%2Fbase_serializer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fuiur%2Fbase_serializer/lists"}