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

parsing array of arrays #239

Open
wangfowen opened this issue Sep 14, 2020 · 0 comments
Open

parsing array of arrays #239

wangfowen opened this issue Sep 14, 2020 · 0 comments

Comments

@wangfowen
Copy link

wangfowen commented Sep 14, 2020

Hi there,

I've looked through the documentation and test examples and don't seem to see a way to parse array of arrays. Some JSON I'm trying to parse for example has:

{ other_properties..., choices: [[<choice obj 1_1>], [<choice obj 2_1>, <choice obj 2_2>]] }

I thought standalone collection might work:

class MyRepresenter
   ...
   collection :choices, decorator: ChoicesRepresenter
end

class ChoicesRepresenter
 include Representable::JSON::Collection

 items class: Choice do
   ...
 end
end

If I just do ChoicesRepresenter.new([]).from_json(choices) on an array passed in it works, but nested as the decorator gives this error:

NoMethodError (undefined method `call' for nil:NilClass)

I also tried:

class MyRepresenter
  ...
  collection :choices,
    instance: lambda { |choices| ChoicesRepresenter.for_collection.new([]).from_json(choices.to_json) }
end

class ChoicesRepresenter
  ...

  collection_representer class: Choice
end

That from_json I was also able to verify works on an array passed to it, but in the instance did not work. This gave me:

NoMethodError (undefined method `has_key?' for #Array:0x00007fab964c5a38)

I've also tried variations of the two with values, to_a, nested collection where i didn't declare the name, extend. None of them worked.

@wangfowen wangfowen changed the title nested collections parsing array of arrays Sep 14, 2020
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

1 participant