Rails and Json Api

How are people doing Rails with JsonApi at the moment?

It seems like ActiveModelSerializer 10 may not be ready yet and I don’t know how ready/hard to transition to jsonapi-resources is. How are people handling this?

1 Like

Deserialization is still not ready, just checked. Depending on the level of compliance with JSON API you need, ActiveModelSerializers should work fine for reading data. For saving you can customize a serializer to something AMS understands.

I tried and tried to get AMS working but kept running into issues. Now I’m using the below which work amazing!

Rails: GitHub - cerebris/jsonapi-resources: A resource-focused Rails library for developing JSON:API compliant servers.

Ember: GitHub - pixelhandler/ember-jsonapi-resources: Lightweight persistence for an Ember CLI app following the JSON API 1.0 spec

1 Like

Awesome! Didn’t know about that addon

@feedthebayer why did you choose ember-jsonapi-resources over ember-data json-api adapter?

Hey @varblob, I haven’t adopted JSONApi yet. I’m curious on why you chose to go with it, instead of the activerecord schema that’s default to Rails?

@Frank_Jensen I haven’t made the call yet but I would like to switch to jsonapi because that is the direction ember is moving. I also would like to support jsonapi because I think it makes sense to have a standardized serialization protocol for json. It’s really annoying to have to keep making almost the same decisions about the api every time I make an app. I also hope that with the standardization of jsonapi the tooling for consumption of other apis will get better. If the api you’re using doesn’t have a client lib for your language then you have to do custom stuff. Even if there is a client shim there are often differences in the way they handle errors / side-loading / paging etc that all need to be custom. There is basically no reason for this to be true except that there is no standard json serialization format.

My biggest concern to switching is that it’s not ready for prime time yet. There aren’t that many users of json-api-resources yet and that seems to be the only rails lib that supports it completely at the moment. I’m also worried about a lack of libs to fill the gap for other areas where you may need to generate or access jsonapi objects without them being de/serialized. For instance request testing or mocking the server response, would be much less straightforward since jsonapi has a more complex structure. Basically if I ever have to actually interact with raw jsonapi I probably don’t want to use it. I make an exception for console netpanel for the moment but outside of that I don’t want to touch the stuff.

@varblob, here is the back-story of ember-jsonapi-resources which really resonated with me.

We are in the process of building an app with Ember Data’s json-api adapter and json-api resources in rails. So far it has been remarkably easy.

I have also worked on getting a Phoenix app to talk to ember data through json-api and it was also fairly straight forward.

With both cases, having a standard to work against has been really helpful. There are less questions to second guess later on and any client knows what they are signing up For with the api.

The only place json-api has been difficult was with Swift, but that is a whole other thing

I am using Active Model serializers. For deserialization I have the following filter on all my controllers that allows Rails to parse the JSON API format:

def json_api

  if request.headers["Content-Type"] ==  'application/vnd.api+json'
  	req = JSON.parse(request.body.read)
  	data = req["data"]
  	r = {}
  	r = data["attributes"]
  	params[data["type"].singularize] = ActiveSupport::HashWithIndifferentAccess.new(r)
  end

end

This works well, getting all the data into params as expected, but doesn’t allow for any embedded records. For those I’ve continued using nested_attributes fields, that are simply empty in Ember Data when loading records.