json-serializer alternatives and similar gems
Based on the "API Builder" category.
Alternatively, view json-serializer alternatives based on common mentions on social networks and blogs.
-
Grape
An opinionated framework for creating REST-like APIs in Ruby. -
ActiveModel::Serializers
ActiveModel::Serializer implementation and Rails hooks -
Fast JSON API
No Longer Maintained - A lightning fast JSON:API serializer for Ruby Objects. -
jbuilder
Jbuilder: generate JSON objects with a Builder-style DSL -
rabl
General ruby templating with json, bson, xml, plist and msgpack support -
JSONAPI::Resources
A resource-focused Rails library for developing JSON:API compliant servers. -
Her
Her is an ORM (Object Relational Mapper) that maps REST resources to Ruby objects. It is designed to build applications that are powered by a RESTful API instead of a database. -
JSON
A fast JSON:API serializer for Ruby (fork of Netflix/fast_jsonapi) -
Blueprinter
Simple, Fast, and Declarative Serialization Library for Ruby -
versionist
A plugin for versioning Rails based RESTful APIs. -
Pliny
An opinionated toolkit for writing excellent APIs in Ruby. -
Version Cake
:cake: Version Cake is an unobtrusive way to version APIs in your Rails or Rack apps -
Praxis
Praxis is a framework that focuses on both the design and implementation aspects of creating APIs. -
jsonapi-rb
Efficiently produce and consume JSON API documents. -
Pragma
An expressive, opinionated ecosystem for building beautiful RESTful APIs with Ruby. -
YASL
Yet Another Serialization Library - A pure Ruby auto-serialization library that works across different Ruby implementations like Opal and JRuby as an automatic alternative to YAML/Marshal. Unlike Marshal, it does not raise errors for unserializable objects, thus provides a highly productive friction-free auto-serialization experience.
WorkOS - The modern identity platform for B2B SaaS
* Code Quality Rankings and insights are calculated and provided by Lumnify.
They vary from L1 to L5 with "L5" being the highest.
Do you think we are missing an alternative of json-serializer or a related project?
README
json-serializer
Customizes JSON output through serializer objects.
Installation
Add this line to your application's Gemfile:
gem "json-serializer"
And then execute:
$ bundle
Or install it yourself as:
$ gem install json-serializer
Usage
Here's a simple example:
require "json_serializer"
class UserSerializer < JsonSerializer
attribute :id
attribute :first_name
attribute :last_name
end
In this case, we defined a new serializer class and specified the attributes we would like to include in the serialized form.
user = User.create(first_name: "Sonny", last_name: "Moore", admin: true)
UserSerializer.new(user).to_json
# => "{\"id\":1,\"first_name\":\"Sonny\",\"last_name\":\"Moore\"}"
You can add a root to the outputted json through the :root
option:
user = User.create(first_name: "Sonny", last_name: "Moore", admin: true)
UserSerializer.new(user).to_json(root: :user)
# => "{\"user\":{\"id\":1,\"first_name\":\"Sonny\",\"last_name\":\"Moore\"}}"
Arrays
A serializer can be used for objects contained in an array:
require "json_serializer"
class PostSerializer < JsonSerializer
attribute :id
attribute :title
attribute :body
end
posts = []
posts << Post.create(title: "Post 1", body: "Hello!")
posts << Post.create(title: "Post 2", body: "Goodbye!")
PostSerializer.new(posts).to_json
Given the example above, it will return a json output like:
[
{ "id": 1, "title": "Post 1", "body": "Hello!" },
{ "id": 2, "title": "Post 2", "body": "Goodbye!" }
]
Attributes
By default, before looking up the attribute on the object, it checks the presence of a method with the name of the attribute. This allow serializes to include properties in addition to the object attributes or customize the result of a specified attribute. You can access the object being serialized with the +object+ method.
require "json_serializer"
class UserSerializer < JsonSerializer
attribute :id
attribute :first_name
attribute :last_name
attribute :full_name
def full_name
object.first_name + " " + object.last_name
end
end
user = User.create(first_name: "Sonny", last_name: "Moore")
UserSerializer.new(user).to_json
# => "{\"id\":1,\"first_name\":\"Sonny\",\"last_name\":\"Moore\",\"full_name\":\"Sonny Moore\"}"
If you would like direct, low-level control of attribute serialization, you can
completely override to_hash
method to return the hash you need:
require "json_serializer"
class UserSerializer < JsonSerializer
attribute :id
attribute :first_name
attribute :last_name
attr_reader :current_user
def initialize(object, current_user)
super(object)
@current_user = current_user
end
def to_hash
hash = super
hash.merge!(admin: object.admin) if current_user.admin?
hash
end
end
Attributes with Custom Serializer
You can specify a serializer class for a defined attribute. This is very useful for serializing each element of an association.
require "json_serializer"
class UserSerializer < JsonSerializer
attribute :id
attribute :username
end
class PostSerializer < JsonSerializer
attribute :id
attribute :title
attribute :user, :UserSerializer
attribute :comments, :CommentSerializer
end
class CommentSerializer < JsonSerializer
attribute :id
attribute :content
attribute :user, :UserSerializer
end
admin = User.create(username: "admin", admin: true)
user = User.create(username: "user")
post = Post.create(title: "Hello!", user: admin)
post.comments << Comment.create(content: "First comment", user: user)
PostSerializer.new(post).to_json
The example above returns the following json output:
{
"id": 1,
"title": "Hello!",
"user":
{
"id": 1,
"username": "admin"
},
"comments":
[
{
"id": 1,
"content": "First comment",
"user":
{
"id": 2,
"username": "user"
}
}
]
}
Contributing
Fork the project with:
$ git clone [email protected]:frodsan/json-serializer.git
To install dependencies, use:
$ bundle install
To run the test suite, do:
$ rake test
For bug reports and pull requests use GitHub.
License
This gem is released under the MIT License.
*Note that all licence references and agreements mentioned in the json-serializer README section above
are relevant to that project's source code only.