tl;dr
Alba is a fast and declarative JSON serializer for Ruby that supports typing, rich error handling and ActiveModelSerializers-like APIs.
What is Alba
Alba is a new Ruby library (rubygems) for JSON serialization I've been recently developing.
Why yet another JSON serializer?
Does it spark joy?
As a Ruby developer, I occasionally compare JSON serialization solution in Ruby ecosystem and I'm not fully satisfied with existing ones. They don't spark joy.
Pros and cons of existing JSON serializers for Ruby
Note that this table is totally subjective and not comprehensive, especially in that it doesn't have JSON:API compatible libraries. It's not planned to support JSON:API with Alba and it's not easy to make sense to compare JSON:API compatible ones with "flat" ones.
If you'd like to point out that I'm wrong, or add something to this list, please comment below or contact me on Twitter!
Name | Pros | Cons |
---|---|---|
ActiveModelSerializers | Stable, lots of articles and documentations | Slower than others, not maintained actively, complex |
jbuilder | Part of Rails official gems, well maintained | Lots of DSLs to remember, slow when partials are overused |
blueprinter | Simple and fast, supports many features | The terminology is special such as "views" and not easy to understand |
representable | Supports JSON, XML and YAML format and many features | Not fast nor simple |
jb | Very simple, fast | When building complex JSON the code gets complex and hard to read |
Overall, I like the speed and simplicity of blueprinter
and feature set representable
provides, but didn't find them the best possible.
I can build it myself!
As a programmer I wondered - why can't I create something thats' better than all of them and satisfy myself? And the answer was "yes!"
I wanted my library to be as simple as possible so I named it Alba. See this section of README.
The primary goal of Alba is to be as simple and fast as blueprinter
and to be as fully featured as representable
.
Hidden goals
I wish my code is something that I can achieve with my best effort. There are some hidden goals of Alba that satisfies me as a programmer:
- Smallest - I don't like reading long code, even when it's written by myself. I wish the code is smaller than 500 loc.
- Best test coverage - Yes I know that code coverage is not a perfect index for test quality, but I'm not confident enough to think my code works as expected without tests. I keep code coverage higher than 99%.
- Best code quality - Thanks to SaaS such as CodeClimate I can tell when my code quality is too low. I try to keep "code smells" absolute zero.
Also I don't want Alba to depend on other gems so that it works well with not only Rails but also in other environments such as Hanami, Sinatra and even standalone. Although some features in Alba depend on ActiveSupport, it's carefully designed not to be required to use Alba itself.
The result
So I've built Alba and the result is impressive.
According to the benchmark Alba is one of the fastest JSON serializer. It also consumes the least amount of memory.
Alba has some unique features such as error handling. It also supports types (only representable
supports this) and circular association control (only ActiveModelSerializers
supports this).
Alba achieves these features and performance by less than 500 lines of code. It's well tested with 99% test coverage.
I'm proud of my work for now, but there's a still long way to go.
The future of Alba
Alba aims to be the defacto library to serialize objects into JSON in Ruby. It'll provide these advantages over the alternatives:
- Speed - Alba will be the fastest among all other JSON serializers
- Features - Alba will support most of the features we require daily for serialization, plus some unique features such as typing and custom hooks
- Simplicity - Alba will not depend on external gems except for one I built for hooks, that makes it easy to use Alba outside of Rails
Top comments (0)