Coder Social home page Coder Social logo

fast_serializer's Introduction

Gem Version Build Status Maintainability Test Coverage

fast_serializer

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.

Performance πŸš€

  • 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

Compatibility πŸ‘Œ

I tried to keep the API as close as possible to active_model_serializer implementation because we all got used to it.

Features

  • conditional rendering
  • inheritence
  • included/excluded associations

Installation

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

Usage

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 => "[email protected]",
       :full_name => "Jamar Graham",
       :phone => "627.051.6039 x1475",
       :has_one_relationship => {
         :id => 6218322696,
         :email=>"[email protected]",
         :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 => "[email protected]",
     :full_name => "Jamar Graham",
     :phone => "627.051.6039 x1475",
     :has_one_relationship => {
       :id => 6218322696,
       :email=>"[email protected]",
       :full_name => "Clay Kuphal",
       :phone => "1-604-682-0732 x882"
     }
   }

Development

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.

Contributing

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.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the FastSerializer project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

fast_serializer's People

Contributors

estepnv avatar dependabot[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.