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?
If I'm using grape-entity in Rails, and I do something like this:
and then I do something like this:
I'll end up with something like this:
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 theas_jsonmethod there actually comes from ActiveModel, which uses strings for the keys. But the value for the "user" key is a Grape::Entity, andGrape::Entity#as_jsonuses 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_jsonand any other relevant/useful methods?Thoughts?