1

I'm battling with my JSON rendering. I want to comply with the simple JSend response format.

Basically what I want is the status and the data in my response.

I've set up my Serializer default adapter to json :

ActiveModelSerializers.config.adapter = :json

My serializer is simple :

class RecordSerializer < ActiveModel::Serializer
  attributes :id, :raw
end

So when I do this :

record_list = @current_user.records
render json: record_list

I got this :

{
    "records": [
        {
            "id": 1,
            "raw": "aaa"
        },
        {
            "id": 2,
            "raw": "bbb"
        }
    ]
}

Now the tricky part, if I want to add a data key on top, it override my records key instead of adding it on top.

render json: record_list, root: "data"

Give this :

{
    "data": [
        {
            "id": 1,
            "raw": "aaa"
        },
        {
            "id": 2,
            "raw": "bbb"
        }
    ]
}

Also, how can I add other keys like the status key ? As soon as I create a custom Hash, the serializer is ignored. So for example this :

render json: {:status=>"success", :code=>200, :message=>"Hello", data: record_list}

Will returns this :

{
    "status": "success",
    "code": 200,
    "message": "Hello",
    "data": [
        {
            "id": 1,
            "raw": "aaa",
            "created_at": "2018-07-16T19:49:32.960Z",
            "updated_at": "2018-07-16T19:49:32.960Z",
        },
        {
            "id": 2,
            "raw": "bbb",
            "created_at": "2018-07-16T20:01:55.804Z",
            "updated_at": "2018-07-16T20:01:55.804Z",
        }
    ]
}

So I think I understand that render json: my_variable alone will understand to transform my_variable into the choosen json serializer adapter but cannot with a custom Hash ?

Of course I could do this :

record_list = {}
record_list["records"] = @current_user.records.select(:id, :raw)

But I feel like this is not the right way and then the Serializer would be useless.

Thanks !

7
  • have you thought about opting out of serializers and using jbuilder instead? Commented Jul 17, 2018 at 19:00
  • you might want to look into json:api and graphQL which both have gems that will set you up with generated serializers that work almost out of the box and provide a lot more structure and functionality than jsend Commented Jul 17, 2018 at 22:20
  • @robertoplancarte JSON API and graphQL seems heavy for my usage so this is why I wanted something light. Commented Jul 18, 2018 at 9:10
  • @andrew21 I will look at it but this seems to bypass the issue and not really solve it. Thanks anyway for the suggestion Commented Jul 18, 2018 at 9:12
  • For my Rails-based API application I initially started with representable for generating JSON responses but as my JSON templates grown it didn't provided a clean approach to reuse already defined representations and thus I switched to used rabl-rails and since then I am successfully and satisfactorily using it for generating JSON responses. (contd in next comment) Commented Jul 18, 2018 at 16:10

1 Answer 1

2

I've chosen to give it a try to the Fast JSON API gem from the Netflix team. Great customisation possible, fast and lightweight.

Sign up to request clarification or add additional context in comments.

1 Comment

pls be noted - As of today, its not maintained.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.