{"id":15403698,"url":"https://github.com/djezzzl/plain_serializer","last_synced_at":"2025-03-28T01:48:24.053Z","repository":{"id":56888230,"uuid":"264613791","full_name":"djezzzl/plain_serializer","owner":"djezzzl","description":"Ruby Plain Serializer DSL","archived":false,"fork":false,"pushed_at":"2020-05-17T09:50:53.000Z","size":11,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-02T05:41:15.344Z","etag":null,"topics":["ruby","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/djezzzl.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}},"created_at":"2020-05-17T08:12:01.000Z","updated_at":"2020-05-17T09:52:28.000Z","dependencies_parsed_at":"2022-08-21T00:20:58.693Z","dependency_job_id":null,"html_url":"https://github.com/djezzzl/plain_serializer","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/djezzzl%2Fplain_serializer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/djezzzl%2Fplain_serializer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/djezzzl%2Fplain_serializer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/djezzzl%2Fplain_serializer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/djezzzl","download_url":"https://codeload.github.com/djezzzl/plain_serializer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245954720,"owners_count":20699873,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["ruby","serializer"],"created_at":"2024-10-01T16:09:41.931Z","updated_at":"2025-03-28T01:48:24.022Z","avatar_url":"https://github.com/djezzzl.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PlainSerializer\n\n[![Gem Version](https://badge.fury.io/rb/plain_serializer.svg)](https://badge.fury.io/rb/plain_serializer)\n[![CircleCI](https://circleci.com/gh/djezzzl/plain_serializer/tree/master.svg?style=svg)](https://circleci.com/gh/djezzzl/plain_serializer/tree/master)\n[![Maintainability](https://api.codeclimate.com/v1/badges/a99db929535f2ce7f24c/maintainability)](https://codeclimate.com/github/djezzzl/plain_serializer/maintainability)\n\nThe gem provides plain DSL to serialize objects of any kind.\n\nIt includes features such as:\n- [Nested serialization](#nested-serialization)\n- [Pre-defined groups](#pre-defined-groups)\n- [Output modification](#output-modification)\n- [Self serialization](#self-serialization)\n\nOutput is `Hash` object which means you can convert it easily to anything you want.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'plain_serializer'\n```\n\nAnd then execute:\n\n    $ bundle install\n\nOr install it yourself as:\n\n    $ gem install plain_serializer\n\n## Usage\n\nImagine you have an object which has few simple attributes, something like the following:\n\n```ruby\nuser = OpenStruct.new(name: 'John Doe', age: 30)\n``` \n\nYou can define your serializer like:\n\n```ruby\nclass UserSerializer \u003c PlainSerializer::Base\n  define_attribute :name\n  define_attribute :age \nend\n```\n\nWhen you need to serialize your object, you can do it as simple as this:\n\n```ruby\nserializer = UserSerializer.setup(:name, :age)\n\n# for single object\np serializer.serialize(user)\n# =\u003e {:name=\u003e\"John Doe\", :age=\u003e30}\n\n# for collection of objects\np serializer.serialize_collection([user])\n# =\u003e [{:name=\u003e\"John Doe\", :age=\u003e30}]\n```\n\n### Nested Serialization\n\n```ruby\nuser = OpenStruct.new(\n  account: OpenStruct.new(name: 'USA'),\n  properties: [\n    OpenStruct.new(code: 'property1'),\n    OpenStruct.new(code: 'property2')\n  ]\n)\n\nclass PropertySerializer \u003c PlainSerializer::Base\n  define_attribute :code\nend\n\nclass AccountSerializer \u003c PlainSerializer::Base\n  define_attribute :name\nend\n\nclass UserSerializer \u003c PlainSerializer::Base\n  define_serializer :account, AccountSerializer\n  define_collection_serializer :properties, PropertySerializer\nend\n\nserializer = UserSerializer.setup(account: :name, properties: :code)\np serializer.serialize(user) \n# =\u003e {:account=\u003e{:name=\u003e\"USA\"}, :properties=\u003e[{:code=\u003e\"property1\"}, {:code=\u003e\"property2\"}]}\n```\n\n### Pre-defined groups\n\n```ruby\nuser = OpenStruct.new(\n  name: 'John Doe',\n  age: '30',\n  account: OpenStruct.new(name: 'USA')\n)\n\nclass AccountSerializer \u003c PlainSerializer::Base\n  define_attribute :name\nend\n\nclass UserSerializer \u003c PlainSerializer::Base\n  define_attribute :name\n  define_attribute :age\n\n  define_group :personal, [:age, account: :name]\n\n  define_serializer :account, AccountSerializer\nend\n\nserializer = UserSerializer.setup(:name, :personal)\np serializer.serialize(user)\n# =\u003e {:name=\u003e\"John Doe\", :age=\u003e\"30\", :account=\u003e{:name=\u003e\"USA\"}}\n```\n\n### Output modification\n\n```ruby\nuser = OpenStruct.new(name: 'John Doe')\n\nclass UserSerializer \u003c PlainSerializer::Base\n  define_attribute :name\nend\n\nserializer = UserSerializer.setup(:name)\n\np(serializer.serialize(user) do |output|\n  output[:anything] = 'anything_you_want_here'\nend)\n# =\u003e {:name=\u003e\"John Doe\", :anything=\u003e\"anything_you_want_here\"} \n```\n\n### Self serialization\n\n```ruby\nuser = OpenStruct.new(\n  birthday: DateTime.now\n)\n\nclass DateTimeSerializer \u003c PlainSerializer::Base\n  def serialize(entity)\n    entity.iso8601\n  end\nend\n\nclass UserSerializer \u003c PlainSerializer::Base\n  define_serializer :birthday, DateTimeSerializer\nend\n\nserializer = UserSerializer.setup(:birthday)\np serializer.serialize(user)\n# =\u003e {:birthday=\u003e\"2020-05-17T11:46:40+02:00\"}\n\np DateTimeSerializer.setup.serialize(DateTime.now)\n# =\u003e \"2020-05-17T11:46:40+02:00\"\n```\n\n## Development\n\nAfter checking out the repo, run `bundle install` to install dependencies. \nThen, run `rake spec` to run the tests. \n\nTo install this gem onto your local machine, run `bundle exec rake install`. \n\nTo release a new version, update the version number in `version.rb`, and then \nrun `bundle exec rake release`, which will create a git tag for the version, \npush git commits and tags, 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/djezzzl/plain_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/djezzzl/plain_serializer/blob/master/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 PlainSerializer project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/djezzzl/plain_serializer/blob/master/CODE_OF_CONDUCT.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdjezzzl%2Fplain_serializer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdjezzzl%2Fplain_serializer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdjezzzl%2Fplain_serializer/lists"}