{"id":13778372,"url":"https://github.com/davydovanton/hanami-serializer","last_synced_at":"2025-04-17T01:54:00.196Z","repository":{"id":148172875,"uuid":"88630115","full_name":"davydovanton/hanami-serializer","owner":"davydovanton","description":"Serializer library for hanami applications","archived":false,"fork":false,"pushed_at":"2019-06-11T19:49:50.000Z","size":22,"stargazers_count":21,"open_issues_count":6,"forks_count":9,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-29T05:51:17.890Z","etag":null,"topics":["hanami","json-api","serializer"],"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/davydovanton.png","metadata":{"files":{"readme":"README.md","changelog":null,"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}},"created_at":"2017-04-18T13:46:31.000Z","updated_at":"2024-04-19T12:39:51.000Z","dependencies_parsed_at":"2023-05-19T08:45:34.187Z","dependency_job_id":null,"html_url":"https://github.com/davydovanton/hanami-serializer","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davydovanton%2Fhanami-serializer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davydovanton%2Fhanami-serializer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davydovanton%2Fhanami-serializer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davydovanton%2Fhanami-serializer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/davydovanton","download_url":"https://codeload.github.com/davydovanton/hanami-serializer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249299839,"owners_count":21246915,"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":["hanami","json-api","serializer"],"created_at":"2024-08-03T18:00:53.355Z","updated_at":"2025-04-17T01:54:00.177Z","avatar_url":"https://github.com/davydovanton.png","language":"Ruby","funding_links":[],"categories":["Hanami Gem List"],"sub_categories":["Building APIs"],"readme":"# NOT MAINTAINED\n\n# Hanami::Serializer\n\nSimple solution for serializing you data in hanami apps.\n\n* [Installation](#installation)\n* [Usage](#usage)\n  * [Action helpers](#action-helpers)\n    * [Example](#example)\n    * [Custom serializer class](#custom-serializer-class)\n  * [Serializers](#serializers)\n    * [Nested](#nested)\n      * [Type](#type)\n      * [Serializer](#serializer)\n    * [Shared](#shared)\n* [Contributing](#contributing)\n* [License](#license)\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'hanami-serializer'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install hanami-serializer\n\nCreate 'Types' module:\n\n```ruby\n# lib/types.rb\n\nmodule Types\n  include Dry::Types.module\nend\n```\n\nCreate and add `serializers` folder to application:\n\n```ruby\n# apps/api/application.rb\n\nload_paths \u003c\u003c %w[\n  controllers\n  serializers\n]\n```\n\n## Usage\n### Action helpers\n* `#send_json` - casts object as json and sets it to action `body`\n* `#serializer` - returns serializer class for current action\n\n#### Example\n```ruby\n# api/controllers/controller/index.rb\n\nmodule Api::Controllers::Controller\n  class Show\n    include Api::Action\n    include Hanami::Serializer::Action\n\n    def call(params)\n      object = repo.find(params[:id])\n\n      serializer # =\u003e Api::Serializers::Controller::Show\n\n      object = serializer.new(object)\n      send_json(object)\n\n      # simular to\n      #\n      #   self.status = 200\n      #   self.body = JSON.generate(object)\n    end\n  end\nend\n```\n\n#### Custom serializer class\nIf you want to use custom serializer class you can override `#serializer` method like this:\n\n```ruby\n# api/controllers/controller/index.rb\n\nmodule Api::Controllers::Controller\n  class Update\n    include Api::Action\n    include Hanami::Serializer::Action\n\n    def call(params)\n      serializer # =\u003e Api::Serializers::Controller::Create\n\n      # code\n    end\n\n    def serializer\n      @serializer ||= Api::Serializers::Controller::Create\n    end\n  end\nend\n```\n\n### Serializers\nCreate simple serializer for each action:\n\n```ruby\n# api/serializers/controller/index.rb\n\nmodule Api::Serializers\n  module Controller\n    class Show \u003c Hanami::Serializer::Base\n      # put here attributes needful for action\n      attribute :id,   Types::Id\n      attribute :name, Types::UserName\n    end\n  end\nend\n```\n\nAnd after that you can use it like a usual ruby object:\n```ruby\nuser = User.new(id: 1, name: 'anton', login: 'davydovanton')\n\nserializer = Api::Serializers::Contributors::Index.new(user)\n\nserializer.to_json        # =\u003e '{ \"id\":1, \"name\": \"anton\" }'\nserializer.call           # =\u003e '{ \"id\":1, \"name\": \"anton\" }'\nJSON.generate(serializer) # =\u003e '{ \"id\":1, \"name\": \"anton\" }'\n```\n\n### Nested\nYou can use nested data structures. You have 2 ways how to use it\n\n#### Type\nWe can create new [hash type](http://dry-rb.org/gems/dry-types/hash-schemas/) of attribute:\n\n```ruby\nclass UserWithAvatarSerializer \u003c Hanami::Serializer::Base\n  attribute :name, Types::String\n\n  attribute :avatar, Types::Hash.schema(\n    upload_file_name: Types::String,\n    upload_file_size: Types::Coercible::Int\n  )\nend\n```\n\n#### Serializer\nWe can user other serializer as a type for attribute:\n\n```ruby\nclass AvatarSerializer \u003c Hanami::Serializer::Base\n  attribute :upload_file_name, Types::String\n  attribute :upload_file_size, Types::Coercible::Int\nend\n\nclass NestedUserSerializer \u003c Hanami::Serializer::Base\n  attribute :name, Types::String\n  attribute :avatar, AvatarSerializer\nend\n```\n\n### Shared\nYou can share your serializer code using general classes. For this you need:\n\n1. Create model-specific serializer\n2. Use oop inheritance for sharing model-specific attributes\n\n```ruby\n# api/serializers/user.rb\n\nmodule Api::Serializers\n  class User \u003c Hanami::Serializer::Base\n    attribute :name, Types::UserName\n  end\nend\n```\n\n```ruby\n# api/serializers/users/index.rb\nmodule Api::Serializers\n  module Users\n    class Index \u003c User\n      # put here other attributes needful for action\n      attribute :id, Types::Id\n    end\n  end\nend\n\n# api/serializers/users/show.rb\nmodule Api::Serializers\n  module Users\n    class Show \u003c User\n      # put here other attributes needful for action\n      attribute :posts, Types::Posts\n    end\n  end\nend\n```\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/davydovanton/hanami-serializer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.\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\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavydovanton%2Fhanami-serializer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdavydovanton%2Fhanami-serializer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavydovanton%2Fhanami-serializer/lists"}