JSON API is a great standard for designing APIs without going through the same bikeshedding headakes every API designer usually has to (representing polymorphic associations, duplicated associated records, etc.). Since the spec has converged to a stable version, it has gained a strong support, especially in the rails community.1 However, most libraries are aimed towards serialization (i.e. generating JSON API compliant JSON), not deserialization (i.e. parsing a JSON API payload into a more ready-to-use format). Implementers are sort of left to do their own deserialization (for
POST requests for instance). We addressed that in ActiveModelSerializers by providing helper methods, although it never felt like it really belonged there.
I created the jsonapi gem to take care of the parsing and validating part of handling JSON API payloads,2 and moved the deserialization logic there, before removing it as it did not really belong there either.
Finally, I took advantage of JSON API defining its own MIME type (the rails implications of which are described in Benjamin Fleischer’s blog) to issue a PR to rails, so that deserialization happens at the earliest possible time, making the implementer able to use the params as if they were supplied via plain JSON or multipart data. That is, upon receiving the following payload:
the value of
params within the controller would be:
Basically, the changes amount to:
- registering the
- declaring a JSON API parameter parser:
- registering the parameter parser: