All Projects → davydovanton → hanami-serializer

davydovanton / hanami-serializer

Licence: MIT license
Serializer library for hanami applications

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to hanami-serializer

Jsonapi Serializer
A Node.js framework agnostic library for (de)serializing your data to JSON API
Stars: ✭ 651 (+2859.09%)
Mutual labels:  json-api, serializer
json-api-serializer
Node.js/browser framework agnostic JSON API (http://jsonapi.org/) serializer.
Stars: ✭ 141 (+540.91%)
Mutual labels:  json-api, serializer
jsonapi-mapper
JSON API-Compliant Serialization for your Node ORM
Stars: ✭ 41 (+86.36%)
Mutual labels:  json-api, serializer
BarterOnly
An ecommerce platform to buy or exchange items at your convenience
Stars: ✭ 16 (-27.27%)
Mutual labels:  json-api, serializer
Jsonapi.rb
Lightweight, simple and maintained JSON:API support for your next Ruby HTTP API.
Stars: ✭ 116 (+427.27%)
Mutual labels:  json-api, serializer
Dictfier
Python library to convert/serialize class instances(Objects) both flat and nested into a dictionary data structure. It's very useful in converting Python Objects into JSON format
Stars: ✭ 67 (+204.55%)
Mutual labels:  json-api, serializer
laravel5-jsonapi-dingo
Laravel5 JSONAPI and Dingo together to build APIs fast
Stars: ✭ 29 (+31.82%)
Mutual labels:  json-api, serializer
Symfony Jsonapi
JSON API Transformer Bundle for Symfony 2 and Symfony 3
Stars: ✭ 114 (+418.18%)
Mutual labels:  json-api, serializer
Jsonapi Utils
Build JSON API-compliant APIs on Rails with no (or less) learning curve.
Stars: ✭ 191 (+768.18%)
Mutual labels:  json-api, serializer
php-json-api
JSON API transformer outputting valid (PSR-7) API Responses.
Stars: ✭ 68 (+209.09%)
Mutual labels:  json-api, serializer
laravel5-hal-json
Laravel 5 HAL+JSON API Transformer Package
Stars: ✭ 15 (-31.82%)
Mutual labels:  serializer
mina-hanami
🌸 Mina plugin for Hanami
Stars: ✭ 13 (-40.91%)
Mutual labels:  hanami
Swiftfall
Wrapper for Scryfall API written in Swift
Stars: ✭ 21 (-4.55%)
Mutual labels:  json-api
id-mask
IDMask is a Java library for masking internal ids (e.g. from your DB) when they need to be published to hide their actual value and to prevent forging. It has support optional randomisation has a wide support for various Java types including long, UUID and BigInteger. This library bases its security on strong cryptographic primitives.
Stars: ✭ 39 (+77.27%)
Mutual labels:  serializer
jsonapi-serializer-formats
💎 Gem to enrich jsonapi-serializer with multiple formats
Stars: ✭ 20 (-9.09%)
Mutual labels:  serializer
jsonld-streaming-serializer.js
A fast and lightweight streaming JSON-LD serializer for JavaScript
Stars: ✭ 20 (-9.09%)
Mutual labels:  serializer
laravel5-api
A modular controller for exposing Laravel 5 Eloquent models as a REST API
Stars: ✭ 13 (-40.91%)
Mutual labels:  json-api
playsonify
An opinionated micro-framework to help you build practical JSON APIs with Play Framework (or akka-http)
Stars: ✭ 42 (+90.91%)
Mutual labels:  json-api
dr scaffold
scaffold django rest apis like a champion 🚀
Stars: ✭ 116 (+427.27%)
Mutual labels:  json-api
hprose-as3
Hprose for ActionScript 3.0
Stars: ✭ 18 (-18.18%)
Mutual labels:  serializer

NOT MAINTAINED

Hanami::Serializer

Simple solution for serializing you data in hanami apps.

Installation

Add this line to your application's Gemfile:

gem 'hanami-serializer'

And then execute:

$ bundle

Or install it yourself as:

$ gem install hanami-serializer

Create 'Types' module:

# lib/types.rb

module Types
  include Dry::Types.module
end

Create and add serializers folder to application:

# apps/api/application.rb

load_paths << %w[
  controllers
  serializers
]

Usage

Action helpers

  • #send_json - casts object as json and sets it to action body
  • #serializer - returns serializer class for current action

Example

# api/controllers/controller/index.rb

module Api::Controllers::Controller
  class Show
    include Api::Action
    include Hanami::Serializer::Action

    def call(params)
      object = repo.find(params[:id])

      serializer # => Api::Serializers::Controller::Show

      object = serializer.new(object)
      send_json(object)

      # simular to
      #
      #   self.status = 200
      #   self.body = JSON.generate(object)
    end
  end
end

Custom serializer class

If you want to use custom serializer class you can override #serializer method like this:

# api/controllers/controller/index.rb

module Api::Controllers::Controller
  class Update
    include Api::Action
    include Hanami::Serializer::Action

    def call(params)
      serializer # => Api::Serializers::Controller::Create

      # code
    end

    def serializer
      @serializer ||= Api::Serializers::Controller::Create
    end
  end
end

Serializers

Create simple serializer for each action:

# api/serializers/controller/index.rb

module Api::Serializers
  module Controller
    class Show < Hanami::Serializer::Base
      # put here attributes needful for action
      attribute :id,   Types::Id
      attribute :name, Types::UserName
    end
  end
end

And after that you can use it like a usual ruby object:

user = User.new(id: 1, name: 'anton', login: 'davydovanton')

serializer = Api::Serializers::Contributors::Index.new(user)

serializer.to_json        # => '{ "id":1, "name": "anton" }'
serializer.call           # => '{ "id":1, "name": "anton" }'
JSON.generate(serializer) # => '{ "id":1, "name": "anton" }'

Nested

You can use nested data structures. You have 2 ways how to use it

Type

We can create new hash type of attribute:

class UserWithAvatarSerializer < Hanami::Serializer::Base
  attribute :name, Types::String

  attribute :avatar, Types::Hash.schema(
    upload_file_name: Types::String,
    upload_file_size: Types::Coercible::Int
  )
end

Serializer

We can user other serializer as a type for attribute:

class AvatarSerializer < Hanami::Serializer::Base
  attribute :upload_file_name, Types::String
  attribute :upload_file_size, Types::Coercible::Int
end

class NestedUserSerializer < Hanami::Serializer::Base
  attribute :name, Types::String
  attribute :avatar, AvatarSerializer
end

Shared

You can share your serializer code using general classes. For this you need:

  1. Create model-specific serializer
  2. Use oop inheritance for sharing model-specific attributes
# api/serializers/user.rb

module Api::Serializers
  class User < Hanami::Serializer::Base
    attribute :name, Types::UserName
  end
end
# api/serializers/users/index.rb
module Api::Serializers
  module Users
    class Index < User
      # put here other attributes needful for action
      attribute :id, Types::Id
    end
  end
end

# api/serializers/users/show.rb
module Api::Serializers
  module Users
    class Show < User
      # put here other attributes needful for action
      attribute :posts, Types::Posts
    end
  end
end

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/davydovanton/hanami-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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].