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

Nested level is different between JSON and XML formats #324

Open
mikhailov opened this issue Sep 5, 2012 · 6 comments
Open

Nested level is different between JSON and XML formats #324

mikhailov opened this issue Sep 5, 2012 · 6 comments

Comments

@mikhailov
Copy link

I was surprised to see that child block fetches an associations instead of instance's variable with the same name, so I was going to use node block to force to use my scoped data-set, not just the associations.

What I have:

child :questions do
  attributes :id, :interview_id, :time
end

What I'd like to have:

child :questions.available do
  attributes :id, :interview_id, :time
end

available is a scope I wanted to provide with API.

I decided to use the node block like the following:

node :questions do
  @questions.map do |q|
     { id: q.id, interview_id: q.interview_id, time: q.time } 
  end
end

It shows the same XML, but invalid nested JSON data set, to make it properly in JSON I do:

node :questions do
  @questions.map do |q|
    { question: { id: q.id, interview_id: q.interview_id, time: q.time }}
  end
end

Now JSON the same as with child, but XML is different, nested level is invalid.

@databyte
Copy link
Collaborator

databyte commented Sep 5, 2012

Hmm, I would try something like:

node :questions do |q|
  partial 'questions/index', q.available
end

# questions/index.rabl
attributes :id, :interview_id, :time

@mikhailov
Copy link
Author

@databyte I'll check it out, but I would like to see the same data structure across multiple formats XML and JSON for a node block without extra partials.

@databyte
Copy link
Collaborator

databyte commented Sep 6, 2012

Well JSON and XML technically have different protocols. The problem with JSON in your case is that the hash itself needs to be labeled. If you had a questions node and embedded an array of hashes, it should work.

valid JSON:

{ "questions": [ {"id": 1}, {"id": 2} ] }

@mikhailov
Copy link
Author

@databyte thanks for the answer, but let me explain a little bit more. While I used to have dataset built with child, the XML and JSON responses were identically. Once I tried to use node I can't do the same. The general concern still not answered - Is this possible to use scoped explicit data, but not implicit associations?

@databyte
Copy link
Collaborator

databyte commented Sep 6, 2012

So you still want to use:

child :questions.available do
  attributes :id, :interview_id, :time
end

Or are you asking about how you can manipulate node to generate the same output?

In general, I'm a fan of presenters and I don't personally like to make complex RABL template files because it obfuscates my intent. My general answer for complex templates is presenters and decorators but your example seems simple enough that I don't see why it would be needed. I just don't think I understand your question well enough - and that's probably because I have to see the output of your node code in both JSON and XML.

Personally, I just don't have any projects that output both JSON and XML that need to match schemas. In fact, if I were to build a project - I'd guess my JSON would actually be more terse than my XML and my JSON would probably also include RESTful URIs to other resources instead of embedding everything within a single resource. Because I would most likely parse my XML in its entirety or transform it but with the JSON I would manipulate client-side immediately by pulling out what I need or queuing up more async requests. But again, that's just me.

So maybe sample output from node code would highlight the problem better?

@databyte
Copy link
Collaborator

Leaving open, v1 gets some XML love.

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

No branches or pull requests

2 participants