Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mikhailvs/simple-serializer
Easily serialize any object.
https://github.com/mikhailvs/simple-serializer
json-serialization rails ruby serialization serialize serialize-objects serializer
Last synced: about 11 hours ago
JSON representation
Easily serialize any object.
- Host: GitHub
- URL: https://github.com/mikhailvs/simple-serializer
- Owner: mikhailvs
- License: mit
- Created: 2018-11-12T18:11:42.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2024-05-16T19:41:18.000Z (9 months ago)
- Last Synced: 2024-12-24T09:04:16.607Z (about 2 months ago)
- Topics: json-serialization, rails, ruby, serialization, serialize, serialize-objects, serializer
- Language: Ruby
- Homepage:
- Size: 11.7 KB
- Stars: 4
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SimpleSerializer
[![Gem Version](https://badge.fury.io/rb/simple-serializer.svg)](https://badge.fury.io/rb/simple-serializer)## Install
Gemfile:
```ruby
gem 'simple-serializer'
```Shell:
```sh
gem install simple-serializer
```## Usage
```ruby
require 'simple-serializer'class Account < ApplicationRecord; end
class AccountSerializer < SimpleSerializer
# define multiple attributes to include in the serialized object
attributes :id, :created_at, :updated_at# define one attribute at a time
attribute :email_address# provide an alias for an attribute (object_attr_name: :new_name)
attribute customer_phone_number: :phone_number# define a proc for computed attributes
attribute(:name) { [first_name, last_name].join(' ') }
# serialize nested objects
serialize(:address) do
attributes :line1, :line2, :city, :state, :zipcode
end
end# You can serialize an object by calling .to_h or .to_json on the serializer
AccountSerializer.to_h(Account.first)# if you pass in an enumerable instead of a single object, each one will be serialized
AccountSerializer.to_h(Account.all)# include YourSerializer.helpers in your object's class to get a more convenient interface
class Account
include AccountSerializer.helpers
end# this is the same as calling AccountSerializer.to_h[Account.first]
Account.first.to_h```