Skip to content

#as_json confusion in Rails #39

@wyattisimo

Description

@wyattisimo

If I'm using grape-entity in Rails, and I do something like this:

module Entities
  class User < Grape::Entity
    root :users, :user
    expose :id
    expose :name
  end
end

and then I do something like this:

Entities::User.represent(user).as_json

I'll end up with something like this:

{ "user" => { :id => 1234, :name => "Danny Boy" } }

Notice that the keys are a mixture of symbols and strings. This is because the result of Entities::User.represent(user) is a Hash, and I think the as_json method there actually comes from ActiveModel, which uses strings for the keys. But the value for the "user" key is a Grape::Entity, and Grape::Entity#as_json uses symbols for the keys.

I think it would be good to have better control over the Hash that results from using a root element. Maybe create a subclass of Hash that implements as_json and any other relevant/useful methods?

Thoughts?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions