31
What you should know about JSON serialization solution in Ruby
Alba is a new Ruby library (rubygems) for JSON serialization I've been recently developing.
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.
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.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
.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:
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.
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.
Alba aims to be the defacto library to serialize objects into JSON in Ruby. It'll provide these advantages over the alternatives:
31