grape-entity icon indicating copy to clipboard operation
grape-entity copied to clipboard

to_xml doesn't work out of the box

Open alovak opened this issue 12 years ago • 13 comments

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?

alovak avatar Jan 31 '13 12:01 alovak

If you add that in your app, I assume you get that behavior and everything works with Grape's format :xml?

dblock avatar Jan 31 '13 12:01 dblock

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.

alovak avatar Jan 31 '13 12:01 alovak

@agileanimal should make that call - at the least he/we might want to document this

dblock avatar Jan 31 '13 12:01 dblock

thanks for fast response!

alovak avatar Jan 31 '13 12:01 alovak

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.

idyll avatar Feb 01 '13 05:02 idyll

There is already dependency on ActiveSupport :)

https://github.com/intridea/grape/blob/master/grape.gemspec#L20

alovak avatar Feb 01 '13 19:02 alovak

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 avatar Mar 29 '13 09:03 mbleigh

@mbleigh Right on.

dblock avatar Mar 29 '13 16:03 dblock

@mbleigh I agree too.

idyll avatar Mar 29 '13 16:03 idyll

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.

SegFaultAX avatar Mar 29 '13 20:03 SegFaultAX

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)

mbleigh avatar Mar 29 '13 20:03 mbleigh

@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.

dblock avatar Mar 29 '13 21:03 dblock

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 :)

dblock avatar Sep 16 '13 15:09 dblock