Convert Figma logo to code with AI

Netflix logofast_jsonapi

No Longer Maintained - A lightning fast JSON:API serializer for Ruby Objects.

5,073
425
5,073
106

Top Related Projects

Jbuilder: generate JSON objects with a Builder-style DSL

A fast JSON:API serializer for Ruby (fork of Netflix/fast_jsonapi)

3,132

Optimized JSON

Simple, Fast, and Declarative Serialization Library for Ruby

Quick Overview

The Netflix/fast_jsonapi is a high-performance JSON serializer for Ruby. It is designed to be a fast and efficient way to serialize Ruby objects into JSON format, and to deserialize JSON data into Ruby objects.

Pros

  • High Performance: The library is designed to be highly performant, with benchmarks showing it to be significantly faster than other popular JSON serializers like ActiveModelSerializers.
  • Flexible Configuration: The library provides a flexible configuration system that allows developers to customize the serialization process to their specific needs.
  • Efficient Memory Usage: The library is designed to be memory-efficient, making it a good choice for applications that need to handle large amounts of data.
  • Extensive Documentation: The project has extensive documentation, including detailed guides and examples, making it easy for developers to get started with the library.

Cons

  • Limited Scope: The library is focused solely on JSON serialization and deserialization, and does not provide any additional functionality beyond that.
  • Dependency on ActiveSupport: The library relies on the ActiveSupport library, which may not be desirable for some projects that are trying to minimize their dependencies.
  • Potential Compatibility Issues: As the library is tightly coupled with the Ruby on Rails ecosystem, it may not be as easily integrated into non-Rails Ruby projects.
  • Potential Learning Curve: While the library is well-documented, the complexity of the configuration system may present a learning curve for some developers.

Code Examples

Here are a few examples of how to use the fast_jsonapi library:

  1. Basic Serialization:
class UserSerializer
  include FastJsonapi::ObjectSerializer
  attributes :name, :email
end

user = User.new(name: 'John Doe', email: 'john@example.com')
serialized_user = UserSerializer.new(user).serialized_json
# => '{"data":{"id":"1","type":"user","attributes":{"name":"John Doe","email":"john@example.com"}}}'
  1. Serializing Relationships:
class PostSerializer
  include FastJsonapi::ObjectSerializer
  attributes :title, :content
  has_many :comments
end

class CommentSerializer
  include FastJsonapi::ObjectSerializer
  attributes :body, :author
end

post = Post.new(title: 'My Blog Post', content: 'This is the content of my blog post.')
post.comments = [
  Comment.new(body: 'Great post!', author: 'Jane Doe'),
  Comment.new(body: 'I agree, well written.', author: 'John Smith')
]

serialized_post = PostSerializer.new(post).serialized_json
# => '{"data":{"id":"1","type":"post","attributes":{"title":"My Blog Post","content":"This is the content of my blog post."},"relationships":{"comments":{"data":[{"id":"1","type":"comment"},{"id":"2","type":"comment"}]}}},"included":[{"id":"1","type":"comment","attributes":{"body":"Great post!","author":"Jane Doe"}},{"id":"2","type":"comment","attributes":{"body":"I agree, well written.","author":"John Smith"}}]}'
  1. Customizing Serialization:
class UserSerializer
  include FastJsonapi::ObjectSerializer
  attributes :name, :email
  attribute :full_name do |object|
    "#{object.first_name} #{object.last_name}"
  end
end

user = User.new(first_name: 'John', last_name: 'Doe', email: 'john@example.com')
serialized_user = UserSerializer.new(user).serialized_json
# => '{"data":{"id":"1","type":"user","attributes":{"name":"John Doe","email":"john@example.com","full_name":"John Doe"}}}'

Getting Started

To get started with the fast_jsonapi library, follow these steps:

  1. Add the library to your Gemfile:
gem 'fast_jsonapi'
  1. Run bundle install to install the library.

  2. Create a serializer class for your model:

class UserSerializer

Competitor Comparisons

Jbuilder: generate JSON objects with a Builder-style DSL

Pros of Jbuilder

  • Integrated with Rails, providing a familiar and consistent experience
  • Flexible and customizable, allowing for complex JSON structures
  • Supports partial views and caching for improved performance

Cons of Jbuilder

  • Generally slower performance compared to fast_jsonapi
  • More verbose syntax, requiring more code for simple serializations
  • Lacks built-in support for JSON:API specification

Code Comparison

Jbuilder:

json.array! @posts do |post|
  json.id post.id
  json.title post.title
  json.author do
    json.name post.author.name
    json.email post.author.email
  end
end

fast_jsonapi:

class PostSerializer
  include FastJsonapi::ObjectSerializer
  attributes :title
  belongs_to :author
end

PostSerializer.new(@posts).serialized_json

fast_jsonapi provides a more concise and declarative approach to serialization, while Jbuilder offers more flexibility in constructing JSON structures. fast_jsonapi is generally faster and adheres to the JSON:API specification, making it a good choice for API-heavy applications. Jbuilder, being a part of the Rails ecosystem, may be more suitable for traditional Rails applications where deep integration with other Rails features is desired.

A fast JSON:API serializer for Ruby (fork of Netflix/fast_jsonapi)

Pros of jsonapi-serializer

  • More actively maintained with recent updates
  • Supports both serialization and deserialization
  • Offers more flexibility in customizing relationships and attributes

Cons of jsonapi-serializer

  • Generally slower performance compared to fast_jsonapi
  • May require more setup and configuration for complex scenarios
  • Less optimized for handling large datasets

Code Comparison

fast_jsonapi:

class MovieSerializer
  include FastJsonapi::ObjectSerializer
  attributes :name, :year
  has_many :actors
end

jsonapi-serializer:

class MovieSerializer
  include JSONAPI::Serializer
  attributes :name, :year
  has_many :actors
end

Both libraries use similar syntax for basic serialization, making it easy to switch between them. However, jsonapi-serializer offers more advanced features for complex scenarios, while fast_jsonapi focuses on performance optimization.

fast_jsonapi is known for its speed and efficiency, especially when dealing with large datasets. It achieves this through caching and other optimizations. On the other hand, jsonapi-serializer provides more flexibility and features, including deserialization capabilities, which can be beneficial for projects requiring two-way data transformation.

When choosing between these libraries, consider your project's specific needs, such as performance requirements, data complexity, and the need for deserialization functionality.

3,132

Optimized JSON

Pros of oj

  • Faster JSON parsing and generation compared to fast_jsonapi
  • More lightweight and focused solely on JSON operations
  • Supports both MRI and JRuby

Cons of oj

  • Less feature-rich for API serialization compared to fast_jsonapi
  • Requires more manual configuration for complex object serialization
  • May require additional gems for full API functionality

Code Comparison

oj:

require 'oj'

data = { name: 'John', age: 30 }
json = Oj.dump(data)
parsed = Oj.load(json)

fast_jsonapi:

class PersonSerializer
  include FastJsonapi::ObjectSerializer
  attributes :name, :age
end

serializer = PersonSerializer.new(person)
json = serializer.serialized_json

Summary

oj is a high-performance JSON parser and generator, focusing on speed and efficiency. It's ideal for projects that require fast JSON processing but may need additional setup for complex API serialization.

fast_jsonapi is a more comprehensive solution for API serialization, offering out-of-the-box features for structuring and serializing complex objects. It may be slower than oj but provides a more streamlined experience for building JSON APIs.

Choose oj for raw JSON performance or fast_jsonapi for easier API development with structured serialization.

Simple, Fast, and Declarative Serialization Library for Ruby

Pros of Blueprinter

  • More flexible and customizable serialization options
  • Simpler setup and configuration process
  • Better support for nested associations and complex object structures

Cons of Blueprinter

  • Generally slower performance compared to fast_jsonapi
  • Less opinionated, which may lead to inconsistent implementations across projects
  • Smaller community and fewer resources available

Code Comparison

Blueprinter:

class UserBlueprint < Blueprinter::Base
  fields :id, :name, :email
  association :posts, blueprint: PostBlueprint
end

fast_jsonapi:

class UserSerializer
  include FastJsonapi::ObjectSerializer
  attributes :name, :email
  has_many :posts
end

Both libraries aim to simplify JSON serialization in Ruby applications, but they take different approaches. Blueprinter offers more flexibility and easier setup, while fast_jsonapi focuses on performance and adheres to the JSON:API specification.

Blueprinter allows for more customization in how objects are serialized, making it easier to handle complex data structures. However, this flexibility comes at the cost of performance, as fast_jsonapi is generally faster due to its more optimized implementation.

fast_jsonapi has a larger community and more resources available, which can be beneficial for developers seeking support or looking for established best practices. On the other hand, Blueprinter's simpler setup process may be preferable for smaller projects or those requiring quick implementation.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Fast JSON API — :warning: This project is no longer maintained!!!! :warning:

Build Status

A lightning fast JSON:API serializer for Ruby Objects.

Since this project is no longer maintained, please consider using alternatives or the forked project jsonapi-serializer/jsonapi-serializer!

Performance Comparison

We compare serialization times with Active Model Serializer as part of RSpec performance tests included on this library. We want to ensure that with every change on this library, serialization time is at least 25 times faster than Active Model Serializers on up to current benchmark of 1000 records. Please read the performance document for any questions related to methodology.

Benchmark times for 250 records

$ rspec
Active Model Serializer serialized 250 records in 138.71 ms
Fast JSON API serialized 250 records in 3.01 ms

Table of Contents

Features

  • Declaration syntax similar to Active Model Serializer
  • Support for belongs_to, has_many and has_one
  • Support for compound documents (included)
  • Optimized serialization of compound documents
  • Caching

Installation

Add this line to your application's Gemfile:

gem 'fast_jsonapi'

Execute:

$ bundle install

Usage

Rails Generator

You can use the bundled generator if you are using the library inside of a Rails project:

rails g serializer Movie name year

This will create a new serializer in app/serializers/movie_serializer.rb

Model Definition

class Movie
  attr_accessor :id, :name, :year, :actor_ids, :owner_id, :movie_type_id
end

Serializer Definition

class MovieSerializer
  include FastJsonapi::ObjectSerializer
  set_type :movie  # optional
  set_id :owner_id # optional
  attributes :name, :year
  has_many :actors
  belongs_to :owner, record_type: :user
  belongs_to :movie_type
end

Sample Object

movie = Movie.new
movie.id = 232
movie.name = 'test movie'
movie.actor_ids = [1, 2, 3]
movie.owner_id = 3
movie.movie_type_id = 1
movie

Object Serialization

Return a hash

hash = MovieSerializer.new(movie).serializable_hash

Return Serialized JSON

json_string = MovieSerializer.new(movie).serialized_json

Serialized Output

{
  "data": {
    "id": "3",
    "type": "movie",
    "attributes": {
      "name": "test movie",
      "year": null
    },
    "relationships": {
      "actors": {
        "data": [
          {
            "id": "1",
            "type": "actor"
          },
          {
            "id": "2",
            "type": "actor"
          }
        ]
      },
      "owner": {
        "data": {
          "id": "3",
          "type": "user"
        }
      }
    }
  }
}

Key Transforms

By default fast_jsonapi underscores the key names. It supports the same key transforms that are supported by AMS. Here is the syntax of specifying a key transform

class MovieSerializer
  include FastJsonapi::ObjectSerializer
  # Available options :camel, :camel_lower, :dash, :underscore(default)
  set_key_transform :camel
end

Here are examples of how these options transform the keys

set_key_transform :camel # "some_key" => "SomeKey"
set_key_transform :camel_lower # "some_key" => "someKey"
set_key_transform :dash # "some_key" => "some-key"
set_key_transform :underscore # "some_key" => "some_key"

Attributes

Attributes are defined in FastJsonapi using the attributes method. This method is also aliased as attribute, which is useful when defining a single attribute.

By default, attributes are read directly from the model property of the same name. In this example, name is expected to be a property of the object being serialized:

class MovieSerializer
  include FastJsonapi::ObjectSerializer

  attribute :name
end

Custom attributes that must be serialized but do not exist on the model can be declared using Ruby block syntax:

class MovieSerializer
  include FastJsonapi::ObjectSerializer

  attributes :name, :year

  attribute :name_with_year do |object|
    "#{object.name} (#{object.year})"
  end
end

The block syntax can also be used to override the property on the object:

class MovieSerializer
  include FastJsonapi::ObjectSerializer

  attribute :name do |object|
    "#{object.name} Part 2"
  end
end

Attributes can also use a different name by passing the original method or accessor with a proc shortcut:

class MovieSerializer
  include FastJsonapi::ObjectSerializer

  attributes :name

  attribute :released_in_year, &:year
end

Links Per Object

Links are defined in FastJsonapi using the link method. By default, links are read directly from the model property of the same name. In this example, public_url is expected to be a property of the object being serialized.

You can configure the method to use on the object for example a link with key self will get set to the value returned by a method called url on the movie object.

You can also use a block to define a url as shown in custom_url. You can access params in these blocks as well as shown in personalized_url

class MovieSerializer
  include FastJsonapi::ObjectSerializer

  link :public_url

  link :self, :url

  link :custom_url do |object|
    "http://movies.com/#{object.name}-(#{object.year})"
  end

  link :personalized_url do |object, params|
    "http://movies.com/#{object.name}-#{params[:user].reference_code}"
  end
end

Links on a Relationship

You can specify relationship links by using the links: option on the serializer. Relationship links in JSON API are useful if you want to load a parent document and then load associated documents later due to size constraints (see related resource links)

class MovieSerializer
  include FastJsonapi::ObjectSerializer

  has_many :actors, links: {
    self: :url,
    related: -> (object) {
      "https://movies.com/#{object.id}/actors"
    }
  }
end

This will create a self reference for the relationship, and a related link for loading the actors relationship later. NB: This will not automatically disable loading the data in the relationship, you'll need to do that using the lazy_load_data option:

  has_many :actors, lazy_load_data: true, links: {
    self: :url,
    related: -> (object) {
      "https://movies.com/#{object.id}/actors"
    }
  }

Meta Per Resource

For every resource in the collection, you can include a meta object containing non-standard meta-information about a resource that can not be represented as an attribute or relationship.

class MovieSerializer
  include FastJsonapi::ObjectSerializer

  meta do |movie|
    {
      years_since_release: Date.current.year - movie.year
    }
  end
end

Compound Document

Support for top-level and nested included associations through options[:include].

options = {}
options[:meta] = { total: 2 }
options[:links] = {
  self: '...',
  next: '...',
  prev: '...'
}
options[:include] = [:actors, :'actors.agency', :'actors.agency.state']
MovieSerializer.new([movie, movie], options).serialized_json

Collection Serialization

options[:meta] = { total: 2 }
options[:links] = {
  self: '...',
  next: '...',
  prev: '...'
}
hash = MovieSerializer.new([movie, movie], options).serializable_hash
json_string = MovieSerializer.new([movie, movie], options).serialized_json

Control Over Collection Serialization

You can use is_collection option to have better control over collection serialization.

If this option is not provided or nil autedetect logic is used to try understand if provided resource is a single object or collection.

Autodetect logic is compatible with most DB toolkits (ActiveRecord, Sequel, etc.) but cannot guarantee that single vs collection will be always detected properly.

options[:is_collection]

was introduced to be able to have precise control this behavior

  • nil or not provided: will try to autodetect single vs collection (please, see notes above)
  • true will always treat input resource as collection
  • false will always treat input resource as single object

Caching

Requires a cache_key method be defined on model:

class MovieSerializer
  include FastJsonapi::ObjectSerializer
  set_type :movie  # optional
  cache_options enabled: true, cache_length: 12.hours
  attributes :name, :year
end

Params

In some cases, attribute values might require more information than what is available on the record, for example, access privileges or other information related to a current authenticated user. The options[:params] value covers these cases by allowing you to pass in a hash of additional parameters necessary for your use case.

Leveraging the new params is easy, when you define a custom attribute or relationship with a block you opt-in to using params by adding it as a block parameter.

class MovieSerializer
  include FastJsonapi::ObjectSerializer

  attributes :name, :year
  attribute :can_view_early do |movie, params|
    # in here, params is a hash containing the `:current_user` key
    params[:current_user].is_employee? ? true : false
  end

  belongs_to :primary_agent do |movie, params|
    # in here, params is a hash containing the `:current_user` key
    params[:current_user].is_employee? ? true : false
  end
end

# ...
current_user = User.find(cookies[:current_user_id])
serializer = MovieSerializer.new(movie, {params: {current_user: current_user}})
serializer.serializable_hash

Custom attributes and relationships that only receive the resource are still possible by defining the block to only receive one argument.

Conditional Attributes

Conditional attributes can be defined by passing a Proc to the if key on the attribute method. Return true if the attribute should be serialized, and false if not. The record and any params passed to the serializer are available inside the Proc as the first and second parameters, respectively.

class MovieSerializer
  include FastJsonapi::ObjectSerializer

  attributes :name, :year
  attribute :release_year, if: Proc.new { |record|
    # Release year will only be serialized if it's greater than 1990
    record.release_year > 1990
  }

  attribute :director, if: Proc.new { |record, params|
    # The director will be serialized only if the :admin key of params is true
    params && params[:admin] == true
  }
end

# ...
current_user = User.find(cookies[:current_user_id])
serializer = MovieSerializer.new(movie, { params: { admin: current_user.admin? }})
serializer.serializable_hash

Conditional Relationships

Conditional relationships can be defined by passing a Proc to the if key. Return true if the relationship should be serialized, and false if not. The record and any params passed to the serializer are available inside the Proc as the first and second parameters, respectively.

class MovieSerializer
  include FastJsonapi::ObjectSerializer

  # Actors will only be serialized if the record has any associated actors
  has_many :actors, if: Proc.new { |record| record.actors.any? }

  # Owner will only be serialized if the :admin key of params is true
  belongs_to :owner, if: Proc.new { |record, params| params && params[:admin] == true }
end

# ...
current_user = User.find(cookies[:current_user_id])
serializer = MovieSerializer.new(movie, { params: { admin: current_user.admin? }})
serializer.serializable_hash

Sparse Fieldsets

Attributes and relationships can be selectively returned per record type by using the fields option.

class MovieSerializer
  include FastJsonapi::ObjectSerializer

  attributes :name, :year
end

serializer = MovieSerializer.new(movie, { fields: { movie: [:name] } })
serializer.serializable_hash

Using helper methods

You can mix-in code from another ruby module into your serializer class to reuse functions across your app.

Since a serializer is evaluated in a the context of a class rather than an instance of a class, you need to make sure that your methods act as class methods when mixed in.

Using ActiveSupport::Concern

module AvatarHelper
  extend ActiveSupport::Concern

  class_methods do
    def avatar_url(user)
      user.image.url
    end
  end
end

class UserSerializer
  include FastJsonapi::ObjectSerializer

  include AvatarHelper # mixes in your helper method as class method

  set_type :user

  attributes :name, :email

  attribute :avatar do |user|
    avatar_url(user)
  end
end

Using Plain Old Ruby
module AvatarHelper
  def avatar_url(user)
    user.image.url
  end
end

class UserSerializer
  include FastJsonapi::ObjectSerializer

  extend AvatarHelper # mixes in your helper method as class method

  set_type :user

  attributes :name, :email

  attribute :avatar do |user|
    avatar_url(user)
  end
end

Customizable Options

OptionPurposeExample
set_typeType name of Objectset_type :movie
keyKey of Objectbelongs_to :owner, key: :user
set_idID of Objectset_id :owner_id or ```set_id {
cache_optionsHash to enable caching and set cache lengthcache_options enabled: true, cache_length: 12.hours, race_condition_ttl: 10.seconds
id_method_nameSet custom method name to get ID of an object (If block is provided for the relationship, id_method_name is invoked on the return value of the block instead of the resource object)has_many :locations, id_method_name: :place_ids
object_method_nameSet custom method name to get related objectshas_many :locations, object_method_name: :places
record_typeSet custom Object Type for a relationshipbelongs_to :owner, record_type: :user
serializerSet custom Serializer for a relationshiphas_many :actors, serializer: :custom_actor or has_many :actors, serializer: MyApp::Api::V1::ActorSerializer
polymorphicAllows different record types for a polymorphic associationhas_many :targets, polymorphic: true
polymorphicSets custom record types for each object class in a polymorphic associationhas_many :targets, polymorphic: { Person => :person, Group => :group }

Instrumentation

fast_jsonapi also has builtin Skylight integration. To enable, add the following to an initializer:

require 'fast_jsonapi/instrumentation/skylight'

Skylight relies on ActiveSupport::Notifications to track these two core methods. If you would like to use these notifications without using Skylight, simply require the instrumentation integration:

require 'fast_jsonapi/instrumentation'

The two instrumented notifcations are supplied by these two constants:

  • FastJsonapi::ObjectSerializer::SERIALIZABLE_HASH_NOTIFICATION
  • FastJsonapi::ObjectSerializer::SERIALIZED_JSON_NOTIFICATION

It is also possible to instrument one method without the other by using one of the following require statements:

require 'fast_jsonapi/instrumentation/serializable_hash'
require 'fast_jsonapi/instrumentation/serialized_json'

Same goes for the Skylight integration:

require 'fast_jsonapi/instrumentation/skylight/normalizers/serializable_hash'
require 'fast_jsonapi/instrumentation/skylight/normalizers/serialized_json'

Contributing

Please see contribution check for more details on contributing

Running Tests

We use RSpec for testing. We have unit tests, functional tests and performance tests. To run tests use the following command:

rspec

To run tests without the performance tests (for quicker test runs):

rspec spec --tag ~performance:true

To run tests only performance tests:

rspec spec --tag performance:true