fast_serializer_ruby
is a lightweight ruby object to hash transformer.
This library intends to solve such a typical and on the other hand important problem as efficient ruby object to hash transformation.
- running on ruby 2.7 is at least 6 times faster than AMS (benchmarks was borrowed from fast_jsonapi repository)
- running on ruby 2.7 it consumes 6 times less RAM
- running on jruby 9.2.7.0 is at least 4 times faster than AMS after warming up
I tried to keep the API as close as possible to active_model_serializer implementation because we all got used to it.
- conditional rendering
- inheritence
- included/excluded associations
Add this line to your application's Gemfile:
gem 'fast_serializer_ruby'
And then execute:
$ bundle
Or install it yourself as:
$ gem install fast_serializer_ruby
fast_serializer
supports default schema definition using class methods
class ResourceSerializer
include FastSerializer::Schema::Mixin
root :resource
attributes :id, :email, :phone
attribute(:string_id, if: -> { params[:stringify] }) { resource.id.to_s }
attribute(:float_id, unless: :stringify?) { object.id.to_f }
attribute(:full_name) { params[:only_first_name] ? resource.first_name : "#{resource.first_name} #{resource.last_name}" }
has_one :has_one_relationship, serializer: ResourceSerializer
has_many :has_many_relationship, serializer: ResourceSerializer
def stringify?
params[:stringify]
end
end
ResourceSerializer.new(resource, meta: {foo: "bar"}, only_first_name: false, stringify: true, exclude: [:has_many_relationship]).serializable_hash
=> {
:resource => {
:id => 7873392581,
:email => "houston@luettgen.info",
:full_name => "Jamar Graham",
:phone => "627.051.6039 x1475",
:has_one_relationship => {
:id => 6218322696,
:email=>"terrellrobel@pagac.info",
:full_name => "Clay Kuphal",
:phone => "1-604-682-0732 x882"
}
},
meta: { foo: "bar" }
}
Also fast_serializer supports runtime schema definition
schema = FastSerializer::Schema.new(resource)
schema.attribute(:id)
schema.attribute(:email)
schema.attribute(:full_name) { |resource| "#{resource.first_name} #{resource.last_name}"}
schema.attribute(:phone)
schema.has_one(:has_one_relationship, schema: schema)
schema.serializable_hash
=> {
:id => 7873392581,
:email => "houston@luettgen.info",
:full_name => "Jamar Graham",
:phone => "627.051.6039 x1475",
:has_one_relationship => {
:id => 6218322696,
:email=>"terrellrobel@pagac.info",
:full_name => "Clay Kuphal",
:phone => "1-604-682-0732 x882"
}
}
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
Bug reports and pull requests are welcome on GitHub at https://github.com/estepnv/fast_serializer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the FastSerializer project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.