grape-entity
grape-entity copied to clipboard
to_xml doesn't work out of the box
Right now grape-entity supports conversion to json by using 'multi_json'. Do you think it's a good idea to require "active_support/core_ext/hash/conversions.rb" to enable xml formatting for to_xml method?
If you add that in your app, I assume you get that behavior and everything works with Grape's format :xml
?
Yes, it works.
As for me it's a little inconsistency in gem's behavior. It's strange that json works without any movements and xml doesn't.
If it's ok, then we can close this issue.
@agileanimal should make that call - at the least he/we might want to document this
thanks for fast response!
I am looking at this.
I personally don't use xml unless it is inside a plist, but I do think that there should be consistency.
That being said, Grape is used without Rails, so don't think the right solution is to introduce a dependencies on Active Support.
There is already dependency on ActiveSupport :)
https://github.com/intridea/grape/blob/master/grape.gemspec#L20
I'm thinking that the proper thing to do here is actually to strip out to_json
and remove the MultiJson dependency. Entity is a way of creating a structured representation of data. Let Grape do the serialization of that structure. I say we leave things be with serializable_hash
and as_json
and allow consumers of the library to do the actual JSON/XML conversion.
@dblock @agileanimal thoughts on this?
@mbleigh Right on.
@mbleigh I agree too.
Another option to consider: configurable serializers. Consider something like the following in eg a Rails initializer:
Grape::Entity.configure do
json do |ent, opts|
MultiJson.dump(e)
end
xml do |ent, opts|
ent.to_xml(opts)
end
end
In the case of a Rails application, a default railtie can be provided to setup serializers for json and xml. If someone is using standalone Grape (or even just Grape::Entity in a Rack app), they just need to initialize the serializer somewhere. In any case, I'm interested in preserving the utility of Grape::Entity outside of just Grape apps.
I don't think leaving out to_json
and to_xml
inhibits usability outside of Grape, and it keeps the dependencies of grape-entity
to a minimum.
MultiJson.dump(some_entity.as_json)
@SegFaultAX I feel that your proposal does the same job as Grape formatters, in possibly a more elegant way. I would be open to allow blocks for formatters in Grape proper, currently you have to do a lambda.
So we had an interesting chat here with some action items. Where're my pull requests? What I am paying you guys for? j/k :)