arrange to_json #82

Open
tejo opened this Issue Oct 31, 2011 · 5 comments

Projects

None yet

4 participants

@tejo

Hi there,

I need to serialize an arranged tree of node to json format. If I call node.arrange.to_json I get something like:

{"#Node:0x1057dc7a0":{"#Node:0x1057ca2a8":{}}}

this is because arrange returns an hash.

actually I can get this working looping through the hash and calling to_json for every node object.

There is a better way to do that?

Thanks

Matteo

@jeremedia

Came here to see if anyone else is having this problem. My expereince is exactly the same as tejo's.

@pbellon

Same problem for me here using method as_json.

@tejo

one user asked me how I managed to solve this problem.

basically I've added a recursive method like this in my model:

def self.json_tree(nodes)
  nodes.map do |node, sub_nodes|
    {:name => node.name, :location => node.location, :date => node.published_date, :link => link, :id => node.id, :children => json_tree(sub_nodes, is_admin).compact } 
  end
end

Then I simply call to_json to the returned nested hash

I send to this method is something like this:

Node.arrange_nodes(Node.ordered_by_ancestry_and(:published_date))

Hope this can help.

@jeremedia

Very helpful. Thanks a bunch.

@tomhaines

I am also encountering this same issue

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