Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Representing heterogenous array objects? #100

Open
mlartz opened this issue Jul 16, 2014 · 3 comments
Open

Representing heterogenous array objects? #100

mlartz opened this issue Jul 16, 2014 · 3 comments

Comments

@mlartz
Copy link

mlartz commented Jul 16, 2014

Is it possible to represent an object as a heterogeneous, index-based, JSON array, instead of a JSON object (hash)?

So instead of the following:

{ "name": "Album Name", "num_tracks": 10, "isbn": "ISBN", "etc": "etc" }

I'd like to use the following:

["Album Name", 10, "ISBN", "etc"]

I've had good luck using a :reader with the right index, however the :writer is passed a JSON object ({}) instead of an array ([]), so my output JSON looks more like:

{ "0": "Album Name", "1": 10, "2": "ISBN", "3": "etc" }

Is this supported?

@apotonick
Copy link
Member

Hi Michael,

cool avatar :) it's not supported out-of-the-box. However, just anything can be achieved with representable 🍷

This is a very special representation - the consumer needs to have knowledge about position and semantic of every item. The order matters here, right?

If I had to do that now, I'd have an intermediate object that transforms my data model into an array and back... Is that array the document itself, or is that a nested property? Damn, I wanted to chill out tonight now you made me think how to implement that......

@apotonick
Copy link
Member

Ah, here's a first draft 😁

module BookRepresenter
  include Representable::Hash

  property :name
  property :isbn

  def to_hash(*)
    hash = super
    array = []
    array << hash["name"]
    array << hash["isbn"]
    array
  end
end

This is only one way but gives you an idea. I'm not really sure how I'd support that from representable......... maybe I need to know the answers to the questions above first.

Now, time to chill. 🍺

@apotonick
Copy link
Member

BTW, congratulations for opening issue #100 on representable! 🎆

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants