{"id":15659386,"url":"https://github.com/krim/light_serializer","last_synced_at":"2026-03-09T19:13:13.025Z","repository":{"id":33241659,"uuid":"152039585","full_name":"krim/light_serializer","owner":"krim","description":"Light and Fast serializer","archived":false,"fork":false,"pushed_at":"2023-01-16T02:25:30.000Z","size":71,"stargazers_count":4,"open_issues_count":1,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-19T19:12:20.517Z","etag":null,"topics":["ruby","ruby-gem","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/krim.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":"2018-10-08T07:51:08.000Z","updated_at":"2022-11-15T09:55:51.000Z","dependencies_parsed_at":"2023-01-16T22:45:27.256Z","dependency_job_id":null,"html_url":"https://github.com/krim/light_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/krim%2Flight_serializer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krim%2Flight_serializer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krim%2Flight_serializer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krim%2Flight_serializer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/krim","download_url":"https://codeload.github.com/krim/light_serializer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252562366,"owners_count":21768279,"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","ruby-gem","serialization"],"created_at":"2024-10-03T13:16:35.623Z","updated_at":"2026-03-09T19:13:08.004Z","avatar_url":"https://github.com/krim.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Light Serializer\n\nLight and Fast serializer\n\n[![Gem Version](https://badge.fury.io/rb/light_serializer.svg)](https://badge.fury.io/rb/light_serializer)\n[![Build Status](https://travis-ci.org/krim/light_serializer.svg?branch=master)](https://travis-ci.org/krim/light_serializer)\n[![Maintainability](https://api.codeclimate.com/v1/badges/b9167078ec6b75117d0d/maintainability)](https://codeclimate.com/github/krim/light_serializer/maintainability)\n[![Test Coverage](https://api.codeclimate.com/v1/badges/b9167078ec6b75117d0d/test_coverage)](https://codeclimate.com/github/krim/light_serializer/test_coverage)\n\nLS is a JSON Object Presenter that takes business objects and breaks them down into simple hashes and serializes them to JSON. It can be used in Rails(or other ruby application) in place of other serializers (like JBuilder or ActiveModelSerializers). It is designed to be simple, direct, and performant.\n\n### Performant comparison\nAll performant comparison results are available [here](https://github.com/krim/light_serializer/blob/master/performance_comparison.md). You can run benchmark by yourself.\n```bash\ngit clone git@github.com:krim/light_serializer.git\ncd light_serializer\nruby bench.rb\n\n```\n\n## Table of Contents\n\n* [Install](#install)\n* [Usage](#usage)\n  * [Basic](#basic)\n  * [Custom method names](#custom-method-names)\n  * [Associations](#associations)\n  * [Custom root](#custom-root)\n  * [Collection serializer](#collection-serializer)\n  \n## Install\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'light_serializer'\n```\n\nExecute:\n\n```bash\n$ bundle install\n```\n\n\n## Usage\n### Basic\nIf you have an object you would like serialized, simply create a serializer. Say, for example, you have a User record with the following attributes `[:uuid, :email, :first_name, :last_name, :address]`.\n\nYou may define a simple serializer like so:\n\n```ruby\nclass UserSeriaizer \u003c LightSerializer::Serializer\n  attributes :uuid, :first_name, :last_name, :email\nend\n```\n\nand then, in your code:\n```ruby\nputs UserSeriaizer.new(user).to_json # Output is a JSON string\n```\n\nAnd the output would look like:\n\n```json\n{\n  \"uuid\": \"733f0758-8f21-4719-875f-262c3ec743af\",\n  \"first_name\": \"John\",\n  \"last_name\": \"Doe\",\n  \"email\": \"john.doe@some.fake.email.domain\"\n}\n```\n\n### Custom method names\nYou can rename the resulting JSON keys in both fields and associations by defining custom method:\n\n```ruby\nclass UserSeriaizer \u003c LightSerializer::Serializer\n  attributes :id, :first_name, :last_name, :email\n\n  def id\n    object.uuid\n  end\nend\n```\n\nThis will result in JSON that looks something like this:\n\n```json\n{\n  \"id\": \"92a5c732-2874-41e4-98fc-4123cd6cfa86\",\n  \"first_name\": \"John\",\n  \"last_name\": \"Doe\",\n  \"email\": \"john.doe@some.fake.email.domain\"\n}\n```\n\n### Associations\nYou may include associated objects. Say, for example, a user has projects:\n\n```ruby\nclass ProjectSerializer \u003c LightSerializer::Serializer\n  attributes :uuid, :name\nend\n\nclass UserSeriaizer \u003c LightSerializer::Serializer\n  attributes(\n    :uuid,\n    :first_name,\n    :last_name,\n    :email,\n    { projects: ProjectSerializer }\n  )\nend\n```\n\nUsage:\n```ruby\nputs UserSeriaizer.new(user).to_json\n```\n\nOutput:\n```json\n{\n  \"uuid\": \"733f0758-8f21-4719-875f-262c3ec743af\",\n  \"first_name\": \"John\",\n  \"last_name\": \"Doe\",\n  \"email\": \"john.doe@some.fake.email.domain\",\n  \"projects\": [\n    {\n      \"uuid\": \"dca94051-4195-42bc-a9aa-eb99f7723c82\",\n      \"name\": \"Beach Cleanup\"\n    },\n    {\n      \"uuid\": \"eb881bb5-9a51-4d27-8a29-b264c30e6160\",\n      \"name\": \"Storefront Revamp\"\n    }\n  ]\n}\n```\n\n### Custom root\nBy default, returned JSON doesn't have root. You may specify it by the option in serializer's initializer:\n```ruby\nputs UserSeriaizer.new(user, root: :person).to_json\n```\n\nOutput:\n```json\n{\n  \"person\": {\n    \"uuid\": \"733f0758-8f21-4719-875f-262c3ec743af\",\n    \"first_name\": \"John\",\n    \"last_name\": \"Doe\",\n    \"email\": \"john.doe@some.fake.email.domain\"\n  }\n}\n```\n\n### Collection serializer\nUse `LightSerializer::SerializeCollection` to serialize collection of objects. You have to specify seriaizer, and can specify root and context.\n```ruby\nputs LightSerializer::SerializeCollection.new(users, serializer: UserSerializer, root: :users).to_json\n```\n\nOutput:\n```json\n{\n  \"users\": [\n    {\n      \"uuid\": \"733f0758-8f21-4719-875f-262c3ec743af\",\n      \"first_name\": \"John\",\n      \"last_name\": \"Doe\",\n      \"email\": \"john.doe@some.fake.email.domain\"\n    },\n    {\n      \"uuid\": \"262c3ec743af-4719-4719-8f21-733f0758\",\n      \"first_name\": \"Foo\",\n      \"last_name\": \"Bar\",\n      \"email\": \"foo.bar@some.fake.email.domain\"\n    },\n    {\n      ...\n    }\n  ]\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkrim%2Flight_serializer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkrim%2Flight_serializer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkrim%2Flight_serializer/lists"}