krim / light_serializer

Light and Fast serializer

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Light Serializer

Light and Fast serializer

Gem Version Build Status Maintainability Test Coverage

LS 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.

Performant comparison

All performant comparison results are available here. You can run benchmark by yourself.

git clone git@github.com:krim/light_serializer.git
cd light_serializer
ruby bench.rb

Table of Contents

Install

Add this line to your application's Gemfile:

gem 'light_serializer'

Execute:

$ bundle install

Usage

Basic

If 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].

You may define a simple serializer like so:

class UserSeriaizer < LightSerializer::Serializer
  attributes :uuid, :first_name, :last_name, :email
end

and then, in your code:

puts UserSeriaizer.new(user).to_json # Output is a JSON string

And the output would look like:

{
  "uuid": "733f0758-8f21-4719-875f-262c3ec743af",
  "first_name": "John",
  "last_name": "Doe",
  "email": "john.doe@some.fake.email.domain"
}

Custom method names

You can rename the resulting JSON keys in both fields and associations by defining custom method:

class UserSeriaizer < LightSerializer::Serializer
  attributes :id, :first_name, :last_name, :email

  def id
    object.uuid
  end
end

This will result in JSON that looks something like this:

{
  "id": "92a5c732-2874-41e4-98fc-4123cd6cfa86",
  "first_name": "John",
  "last_name": "Doe",
  "email": "john.doe@some.fake.email.domain"
}

Associations

You may include associated objects. Say, for example, a user has projects:

class ProjectSerializer < LightSerializer::Serializer
  attributes :uuid, :name
end

class UserSeriaizer < LightSerializer::Serializer
  attributes(
    :uuid,
    :first_name,
    :last_name,
    :email,
    { projects: ProjectSerializer }
  )
end

Usage:

puts UserSeriaizer.new(user).to_json

Output:

{
  "uuid": "733f0758-8f21-4719-875f-262c3ec743af",
  "first_name": "John",
  "last_name": "Doe",
  "email": "john.doe@some.fake.email.domain",
  "projects": [
    {
      "uuid": "dca94051-4195-42bc-a9aa-eb99f7723c82",
      "name": "Beach Cleanup"
    },
    {
      "uuid": "eb881bb5-9a51-4d27-8a29-b264c30e6160",
      "name": "Storefront Revamp"
    }
  ]
}

Custom root

By default, returned JSON doesn't have root. You may specify it by the option in serializer's initializer:

puts UserSeriaizer.new(user, root: :person).to_json

Output:

{
  "person": {
    "uuid": "733f0758-8f21-4719-875f-262c3ec743af",
    "first_name": "John",
    "last_name": "Doe",
    "email": "john.doe@some.fake.email.domain"
  }
}

Collection serializer

Use LightSerializer::SerializeCollection to serialize collection of objects. You have to specify seriaizer, and can specify root and context.

puts LightSerializer::SerializeCollection.new(users, serializer: UserSerializer, root: :users).to_json

Output:

{
  "users": [
    {
      "uuid": "733f0758-8f21-4719-875f-262c3ec743af",
      "first_name": "John",
      "last_name": "Doe",
      "email": "john.doe@some.fake.email.domain"
    },
    {
      "uuid": "262c3ec743af-4719-4719-8f21-733f0758",
      "first_name": "Foo",
      "last_name": "Bar",
      "email": "foo.bar@some.fake.email.domain"
    },
    {
      ...
    }
  ]
}

About

Light and Fast serializer

License:MIT License


Languages

Language:Ruby 99.2%Language:Shell 0.8%