Hey guys I have a large block of JSON and I need to transform it into a different structure. I''ve thought about this a lot and while I could probably write a method, I''m not sure it would be fast. Could anyone assist? I''ve put a block of actual data here: http://pastie.caboo.se/135161 (by the way, this site rocks: http://curiousconcept.com/jsonformatter - it indented my json for me!) The structure is like this (abbreviated) [ { id:''blah'', status:{ id:1, name:''some name'' }, depth:1 }, { id:''blah'', status:{ id:1, name:''some name'' }, depth:2 } { id:''blah'', status:{ id:1, name:''some name'' }, depth:3 } ] and I need to transform it like so [ { id:''blah'', status:''some name'', children: [ { id:''blah'', status:''some name'', children: [ { id:''blah'', status:''some name'', children: [ ] ] ] } ] As you can see, the depth property indicates the item depth in the tree. It will not just get deeper, sometimes the next item should actually get attached to the parent.. it''s tree data but flat. The json is loaded from a server call or generated from dom objects which are all flat, but I need it in this form to perform some extra processing (calculating parent status off child status) and the dom is flat to allow the objects to get moved around without having to worry about parent-child relationships. Most importantly, it has to be *fast*, i don''t care too much if it is not very readable. Alternatively, give me some pointers as to which techniques to use and I think I can probably do it. I''d like to think i''m pretty good at prototype but this one has me stumped. Proto 1.6 is preferred. I''ll be forever in your debt :) Thanks in advance, Gareth --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
If you can, do it server side! Gareth Evans wrote:> Hey guys > > I have a large block of JSON and I need to transform it into a different > structure. > I''ve thought about this a lot and while I could probably write a method, I''m > not sure it would be fast. > Could anyone assist? > > I''ve put a block of actual data here: > > http://pastie.caboo.se/135161 > > (by the way, this site rocks: http://curiousconcept.com/jsonformatter - it > indented my json for me!) > > The structure is like this (abbreviated) > > [ > { id:''blah'', status:{ id:1, name:''some name'' }, depth:1 }, > { id:''blah'', status:{ id:1, name:''some name'' }, depth:2 } > { id:''blah'', status:{ id:1, name:''some name'' }, depth:3 } > ] > > and I need to transform it like so > > [ > { id:''blah'', > status:''some name'', > children: [ > { id:''blah'', > status:''some name'', > children: [ > { id:''blah'', > status:''some name'', > children: [ > ] > ] > ] > } > ] > > As you can see, the depth property indicates the item depth in the tree. > It will not just get deeper, sometimes the next item should actually get > attached to the parent.. it''s tree data but flat. > > The json is loaded from a server call or generated from dom objects which > are all flat, but I need it in this form to perform some extra processing > (calculating parent status off child status) and the dom is flat to allow > the objects to get moved around without having to worry about parent-child > relationships. > > Most importantly, it has to be *fast*, i don''t care too much if it is not > very readable. > Alternatively, give me some pointers as to which techniques to use and I > think I can probably do it. > > I''d like to think i''m pretty good at prototype but this one has me stumped. > Proto 1.6 is preferred. > > I''ll be forever in your debt :) > > Thanks in advance, > > Gareth--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
OK; I checked your code, but there''s one thing you dont explain, is how you want to handle that kind of case: [ { id:''blah'', status:{ id:1, name:''some name'' }, depth:1 }, { id:''blah'', status:{ id:1, name:''some name'' }, depth:2 } { id:''foo'', status:{ id:1, name:''some name'' }, depth:2 } { id:''blah'', status:{ id:1, name:''some name'' }, depth:3 } ] --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
even server side, i''m not sure of the correct approach to reformat the data. I''d prefer to do it client side, simply because everything else bar database interfaces is on client and when nodes are added/removed/moved on the client side, I need to keep the tree data up to date. In the code you highlighted, I guess you''re asking where foo should be- I have changed the ids below. [ { id:''one'', status:{ id:1, name:''some name'' }, depth:1 }, { id:''two'', status:{ id:1, name:''some name'' }, depth:2 } { id:''six'', status:{ id:1, name:''some name'' }, depth:3 } { id:''three'', status:{ id:1, name:''some name'' }, depth:2 } { id:''four'', status:{ id:1, name:''some name'' }, depth:3 } { id:''five'', status:{ id:1, name:''some name'' }, depth:3 } { id:''seven'', status:{ id:1, name:''some name'' }, depth:2 } ] one --two ------six --three ------four ------five --seven does that make more sense? So when the depth increases, that node should be attached to the last node at the previous depth level. On Jan 5, 2008 2:41 PM, Tobie Langel <tobie.langel-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > OK; I checked your code, but there''s one thing you dont explain, is > how you want to handle that kind of case: > > [ > { id:''blah'', status:{ id:1, name:''some name'' }, depth:1 }, > { id:''blah'', status:{ id:1, name:''some name'' }, depth:2 } > { id:''foo'', status:{ id:1, name:''some name'' }, depth:2 } > { id:''blah'', status:{ id:1, name:''some name'' }, depth:3 } > ] > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Can you have more than one root node ? (with depth == 0) On Jan 5, 4:22 pm, "Gareth Evans" <agr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> even server side, i''m not sure of the correct approach to reformat the data. > > I''d prefer to do it client side, simply because everything else bar database > interfaces is on client and when nodes are added/removed/moved on the client > side, I need to keep the tree data up to date. > > In the code you highlighted, I guess you''re asking where foo should be- I > have changed the ids below. > > [ > { id:''one'', status:{ id:1, name:''some name'' }, depth:1 }, > { id:''two'', status:{ id:1, name:''some name'' }, depth:2 } > { id:''six'', status:{ id:1, name:''some name'' }, depth:3 } > { id:''three'', status:{ id:1, name:''some name'' }, depth:2 } > { id:''four'', status:{ id:1, name:''some name'' }, depth:3 } > { id:''five'', status:{ id:1, name:''some name'' }, depth:3 } > { id:''seven'', status:{ id:1, name:''some name'' }, depth:2 } > ] > > one > --two > ------six > --three > ------four > ------five > --seven > > does that make more sense? > > So when the depth increases, that node should be attached to the last node > at the previous depth level. > > On Jan 5, 2008 2:41 PM, Tobie Langel <tobie.lan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > OK; I checked your code, but there''s one thing you dont explain, is > > how you want to handle that kind of case: > > > [ > > { id:''blah'', status:{ id:1, name:''some name'' }, depth:1 }, > > { id:''blah'', status:{ id:1, name:''some name'' }, depth:2 } > > { id:''foo'', status:{ id:1, name:''some name'' }, depth:2 } > > { id:''blah'', status:{ id:1, name:''some name'' }, depth:3 } > > ]--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
... and does that root node always come first ? On Jan 5, 4:22 pm, "Gareth Evans" <agr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> even server side, i''m not sure of the correct approach to reformat the data. > > I''d prefer to do it client side, simply because everything else bar database > interfaces is on client and when nodes are added/removed/moved on the client > side, I need to keep the tree data up to date. > > In the code you highlighted, I guess you''re asking where foo should be- I > have changed the ids below. > > [ > { id:''one'', status:{ id:1, name:''some name'' }, depth:1 }, > { id:''two'', status:{ id:1, name:''some name'' }, depth:2 } > { id:''six'', status:{ id:1, name:''some name'' }, depth:3 } > { id:''three'', status:{ id:1, name:''some name'' }, depth:2 } > { id:''four'', status:{ id:1, name:''some name'' }, depth:3 } > { id:''five'', status:{ id:1, name:''some name'' }, depth:3 } > { id:''seven'', status:{ id:1, name:''some name'' }, depth:2 } > ] > > one > --two > ------six > --three > ------four > ------five > --seven > > does that make more sense? > > So when the depth increases, that node should be attached to the last node > at the previous depth level. > > On Jan 5, 2008 2:41 PM, Tobie Langel <tobie.lan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > OK; I checked your code, but there''s one thing you dont explain, is > > how you want to handle that kind of case: > > > [ > > { id:''blah'', status:{ id:1, name:''some name'' }, depth:1 }, > > { id:''blah'', status:{ id:1, name:''some name'' }, depth:2 } > > { id:''foo'', status:{ id:1, name:''some name'' }, depth:2 } > > { id:''blah'', status:{ id:1, name:''some name'' }, depth:3 } > > ]--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Try this: http://pastie.textmate.org/135400 On Jan 5, 5:03 pm, Tobie Langel <tobie.lan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> ... and does that root node always come first ? > > On Jan 5, 4:22 pm, "Gareth Evans" <agr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > even server side, i''m not sure of the correct approach to reformat the data. > > > I''d prefer to do it client side, simply because everything else bar database > > interfaces is on client and when nodes are added/removed/moved on the client > > side, I need to keep the tree data up to date. > > > In the code you highlighted, I guess you''re asking where foo should be- I > > have changed the ids below. > > > [ > > { id:''one'', status:{ id:1, name:''some name'' }, depth:1 }, > > { id:''two'', status:{ id:1, name:''some name'' }, depth:2 } > > { id:''six'', status:{ id:1, name:''some name'' }, depth:3 } > > { id:''three'', status:{ id:1, name:''some name'' }, depth:2 } > > { id:''four'', status:{ id:1, name:''some name'' }, depth:3 } > > { id:''five'', status:{ id:1, name:''some name'' }, depth:3 } > > { id:''seven'', status:{ id:1, name:''some name'' }, depth:2 } > > ] > > > one > > --two > > ------six > > --three > > ------four > > ------five > > --seven > > > does that make more sense? > > > So when the depth increases, that node should be attached to the last node > > at the previous depth level. > > > On Jan 5, 2008 2:41 PM, Tobie Langel <tobie.lan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > OK; I checked your code, but there''s one thing you dont explain, is > > > how you want to handle that kind of case: > > > > [ > > > { id:''blah'', status:{ id:1, name:''some name'' }, depth:1 }, > > > { id:''blah'', status:{ id:1, name:''some name'' }, depth:2 } > > > { id:''foo'', status:{ id:1, name:''some name'' }, depth:2 } > > > { id:''blah'', status:{ id:1, name:''some name'' }, depth:3 } > > > ]--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Wow, Tobie; that''s awesome. I just tested on the full data set and it returns almost instantaneously. One small prob tho, and that''s it seems to be repeating nodes that have been added as children, further in the dataset. I''m still analysing your code trying to figure out how it works, but i''m totally in awe. I''ve attached data.json which is the full source data and result.json which is the result after your function. I will attempt to figure out whats wrong myself too :) Gareth On Jan 6, 2008 5:57 AM, Tobie Langel <tobie.langel-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Try this: http://pastie.textmate.org/135400 > > On Jan 5, 5:03pm, Tobie Langel <tobie.lan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > ... and does that root node always come first ? > > > > On Jan 5, 4:22pm, "Gareth Evans" <agr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > even server side, i''m not sure of the correct approach to reformat the > data. > > > > > I''d prefer to do it client side, simply because everything else bar > database > > > interfaces is on client and when nodes are added/removed/moved on the > client > > > side, I need to keep the tree data up to date. > > > > > In the code you highlighted, I guess you''re asking where foo should > be- I > > > have changed the ids below. > > > > > [ > > > { id:''one'', status:{ id:1, name:''some name'' }, depth:1 }, > > > { id:''two'', status:{ id:1, name:''some name'' }, depth:2 } > > > { id:''six'', status:{ id:1, name:''some name'' }, depth:3 } > > > { id:''three'', status:{ id:1, name:''some name'' }, depth:2 } > > > { id:''four'', status:{ id:1, name:''some name'' }, depth:3 } > > > { id:''five'', status:{ id:1, name:''some name'' }, depth:3 } > > > { id:''seven'', status:{ id:1, name:''some name'' }, depth:2 } > > > ] > > > > > one > > > --two > > > ------six > > > --three > > > ------four > > > ------five > > > --seven > > > > > does that make more sense? > > > > > So when the depth increases, that node should be attached to the last > node > > > at the previous depth level. > > > > > On Jan 5, 2008 2:41 PM, Tobie Langel <tobie.lan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > OK; I checked your code, but there''s one thing you dont explain, is > > > > how you want to handle that kind of case: > > > > > > [ > > > > { id:''blah'', status:{ id:1, name:''some name'' }, depth:1 }, > > > > { id:''blah'', status:{ id:1, name:''some name'' }, depth:2 } > > > > { id:''foo'', status:{ id:1, name:''some name'' }, depth:2 } > > > > { id:''blah'', status:{ id:1, name:''some name'' }, depth:3 } > > > > ] > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Yes you can have more than one root node, as that would imply there were multiple top level projects. (the items are tasks, but that should be clear from the data) On Jan 6, 2008 5:03 AM, Tobie Langel <tobie.langel-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > ... and does that root node always come first ? > > On Jan 5, 4:22pm, "Gareth Evans" <agr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > even server side, i''m not sure of the correct approach to reformat the > data. > > > > I''d prefer to do it client side, simply because everything else bar > database > > interfaces is on client and when nodes are added/removed/moved on the > client > > side, I need to keep the tree data up to date. > > > > In the code you highlighted, I guess you''re asking where foo should be- > I > > have changed the ids below. > > > > [ > > { id:''one'', status:{ id:1, name:''some name'' }, depth:1 }, > > { id:''two'', status:{ id:1, name:''some name'' }, depth:2 } > > { id:''six'', status:{ id:1, name:''some name'' }, depth:3 } > > { id:''three'', status:{ id:1, name:''some name'' }, depth:2 } > > { id:''four'', status:{ id:1, name:''some name'' }, depth:3 } > > { id:''five'', status:{ id:1, name:''some name'' }, depth:3 } > > { id:''seven'', status:{ id:1, name:''some name'' }, depth:2 } > > ] > > > > one > > --two > > ------six > > --three > > ------four > > ------five > > --seven > > > > does that make more sense? > > > > So when the depth increases, that node should be attached to the last > node > > at the previous depth level. > > > > On Jan 5, 2008 2:41 PM, Tobie Langel <tobie.lan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > OK; I checked your code, but there''s one thing you dont explain, is > > > how you want to handle that kind of case: > > > > > [ > > > { id:''blah'', status:{ id:1, name:''some name'' }, depth:1 }, > > > { id:''blah'', status:{ id:1, name:''some name'' }, depth:2 } > > > { id:''foo'', status:{ id:1, name:''some name'' }, depth:2 } > > > { id:''blah'', status:{ id:1, name:''some name'' }, depth:3 } > > > ] > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Haha, didnt attach the files after that On Jan 6, 2008 9:58 AM, Gareth Evans <agrath-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Wow, Tobie; that''s awesome. > > I just tested on the full data set and it returns almost instantaneously. > > One small prob tho, and that''s it seems to be repeating nodes that have > been added as children, further in the dataset. > I''m still analysing your code trying to figure out how it works, but i''m > totally in awe. > I''ve attached data.json which is the full source data and result.jsonwhich is the result after your function. > I will attempt to figure out whats wrong myself too :) > > Gareth > > On Jan 6, 2008 5:57 AM, Tobie Langel <tobie.langel-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Try this: http://pastie.textmate.org/135400 > > > > On Jan 5, 5:03pm, Tobie Langel <tobie.lan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > ... and does that root node always come first ? > > > > > > On Jan 5, 4:22pm, "Gareth Evans" <agr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > even server side, i''m not sure of the correct approach to reformat > > the data. > > > > > > > I''d prefer to do it client side, simply because everything else bar > > database > > > > interfaces is on client and when nodes are added/removed/moved on > > the client > > > > side, I need to keep the tree data up to date. > > > > > > > In the code you highlighted, I guess you''re asking where foo should > > be- I > > > > have changed the ids below. > > > > > > > [ > > > > { id:''one'', status:{ id:1, name:''some name'' }, depth:1 }, > > > > { id:''two'', status:{ id:1, name:''some name'' }, depth:2 } > > > > { id:''six'', status:{ id:1, name:''some name'' }, depth:3 } > > > > { id:''three'', status:{ id:1, name:''some name'' }, depth:2 } > > > > { id:''four'', status:{ id:1, name:''some name'' }, depth:3 } > > > > { id:''five'', status:{ id:1, name:''some name'' }, depth:3 } > > > > { id:''seven'', status:{ id:1, name:''some name'' }, depth:2 } > > > > ] > > > > > > > one > > > > --two > > > > ------six > > > > --three > > > > ------four > > > > ------five > > > > --seven > > > > > > > does that make more sense? > > > > > > > So when the depth increases, that node should be attached to the > > last node > > > > at the previous depth level. > > > > > > > On Jan 5, 2008 2:41 PM, Tobie Langel < tobie.lan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > wrote: > > > > > > > > OK; I checked your code, but there''s one thing you dont explain, > > is > > > > > how you want to handle that kind of case: > > > > > > > > [ > > > > > { id:''blah'', status:{ id:1, name:''some name'' }, depth:1 }, > > > > > { id:''blah'', status:{ id:1, name:''some name'' }, depth:2 } > > > > > { id:''foo'', status:{ id:1, name:''some name'' }, depth:2 } > > > > > { id:''blah'', status:{ id:1, name:''some name'' }, depth:3 } > > > > > ] > > > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Weird. I''ve just ran your data again through var results = data.shift(); data.each(function(current) { var depth = current.depth - 1, tree = results; while (depth--) tree = tree.children.last(); if (''children'' in tree) return tree.children.push(current) tree.children = [current]; }); And I''m not getting any of the issues you mentioned. I suspect however that the formatting JSON service you are using might be untworstworthy, it looks like it''s not respecting array order. If you want to speed it up yet more, try using a regular for loop. Best, Tobie Gareth Evans wrote:> Haha, didnt attach the files after that > > On Jan 6, 2008 9:58 AM, Gareth Evans <agrath-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Wow, Tobie; that''s awesome. > > > > I just tested on the full data set and it returns almost instantaneously. > > > > One small prob tho, and that''s it seems to be repeating nodes that have > > been added as children, further in the dataset. > > I''m still analysing your code trying to figure out how it works, but i''m > > totally in awe. > > I''ve attached data.json which is the full source data and result.jsonwhich is the result after your function. > > I will attempt to figure out whats wrong myself too :) > > > > Gareth > > > > On Jan 6, 2008 5:57 AM, Tobie Langel <tobie.langel-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > > Try this: http://pastie.textmate.org/135400 > > > > > > On Jan 5, 5:03pm, Tobie Langel <tobie.lan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > ... and does that root node always come first ? > > > > > > > > On Jan 5, 4:22pm, "Gareth Evans" <agr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > > > even server side, i''m not sure of the correct approach to reformat > > > the data. > > > > > > > > > I''d prefer to do it client side, simply because everything else bar > > > database > > > > > interfaces is on client and when nodes are added/removed/moved on > > > the client > > > > > side, I need to keep the tree data up to date. > > > > > > > > > In the code you highlighted, I guess you''re asking where foo should > > > be- I > > > > > have changed the ids below. > > > > > > > > > [ > > > > > { id:''one'', status:{ id:1, name:''some name'' }, depth:1 }, > > > > > { id:''two'', status:{ id:1, name:''some name'' }, depth:2 } > > > > > { id:''six'', status:{ id:1, name:''some name'' }, depth:3 } > > > > > { id:''three'', status:{ id:1, name:''some name'' }, depth:2 } > > > > > { id:''four'', status:{ id:1, name:''some name'' }, depth:3 } > > > > > { id:''five'', status:{ id:1, name:''some name'' }, depth:3 } > > > > > { id:''seven'', status:{ id:1, name:''some name'' }, depth:2 } > > > > > ] > > > > > > > > > one > > > > > --two > > > > > ------six > > > > > --three > > > > > ------four > > > > > ------five > > > > > --seven > > > > > > > > > does that make more sense? > > > > > > > > > So when the depth increases, that node should be attached to the > > > last node > > > > > at the previous depth level. > > > > > > > > > On Jan 5, 2008 2:41 PM, Tobie Langel < tobie.lan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > > wrote: > > > > > > > > > > OK; I checked your code, but there''s one thing you dont explain, > > > is > > > > > > how you want to handle that kind of case: > > > > > > > > > > [ > > > > > > { id:''blah'', status:{ id:1, name:''some name'' }, depth:1 }, > > > > > > { id:''blah'', status:{ id:1, name:''some name'' }, depth:2 } > > > > > > { id:''foo'', status:{ id:1, name:''some name'' }, depth:2 } > > > > > > { id:''blah'', status:{ id:1, name:''some name'' }, depth:3 } > > > > > > ] > > > > > > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
untworstworthy <--- now that''s one serious typo. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Okay, sweet. I didnt even suspsect the json formatter, but yeah it could very well not be respecting order. i"ll see if I can get a pass-result here. Sorry for the false alarm :) You''ve been most helpful :) On Jan 6, 2008 1:24 PM, Tobie Langel <tobie.langel-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > untworstworthy <--- now that''s one serious typo. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Okay I''ve looked into this some more, even without the formatting I think we still have a problem. I''ve pasted a couple of blocks of json before with some bolding. Input: [{"id":1,"label":"CRM","status":{"id":1,"name":"in progress","image":" inprogress.png"},"depth":0,"ordinal":1,"resource":{"id":0,"name":"None"}},{"id":65,"label":"Dashboard","status":{"id":1,"name":"in progress","image":"inprogress.png"},"depth":1,"ordinal":1,"resource":{"id":0,"name":"None"}},{"id":67,"label":"Callcenter","status":{"id":1,"name":"in progress","image":"inprogress.png"},"depth":2,"ordinal":2,"resource":{"id":0,"name":"None"}},{"id":71,"label":"Operations","status":{"id":1,"name":"in progress","image":"inprogress.png"},"depth":2,"ordinal":3,"resource":{"id":0,"name":"None"}},{"id":69,"label":"DBA","status":{"id":1,"name":"in progress","image":"inprogress.png"},"depth":2,"ordinal":4,"resource":{"id":0,"name":"None"}},{"id":70,"label":"Manager","status":{"id":1,"name":"in progress","image":"inprogress.png"},"depth":2,"ordinal":5,"resource":{"id":0,"name":"None"}},{"id":68,"label":"Disable multiple dashboard feature","status":{"id":1,"name":"in progress","image":" inprogress.png "},"depth":3,"ordinal":6,"resource":{"id":0,"name":"None"}},{"id":72,"label":" *Add dashboard items to single dashboard* ","status":{"id":2,"name":"complete","image":"complete.png"},"depth":4,"ordinal":8,"resource":{"id":3,"name":"Dean"}},{"id":73,"label":"New applications","status":{"id":2,"name":"complete","image":"complete.png"},"depth":5,"ordinal":9,"resource":{"id":3,"name":"Dean"}},{"id":74,"label":"Pending Claims","status":{"id":2,"name":"complete","image":"complete.png "},"depth":5,"ordinal":10,"resource":{"id":3,"name":"Dean"}},{"id":75,"label":"Notes","status":{"id":2,"name":"complete","image":" complete.png "},"depth":5,"ordinal":11,"resource":{"id":3,"name":"Dean"}},{"id":76,"label":"Cancellations","status":{"id":2,"name":"complete","image":" complete.png"},"depth":5,"ordinal":12,"resource":{"id":3,"name":"Dean"}},{"id":77,"label":"Customer","status":{"id":1,"name":"in progress","image":"inprogress.png"},"depth":1,"ordinal":13,"resource":{"id":0,"name":"None"}},{"id":78,"label":"Listing page","status":{"id":2,"name":"complete","image":"complete.png"},"depth":2,"ordinal":14,"resource":{"id":0,"name":"None"}},{"id":79,"label":"Edit Page","status":{"id":1,"name":"in progress","image":"inprogress.png "},"depth":2,"ordinal":15,"resource":{"id":0,"name":"None"}},{"id":80,"label":"Summary","status":{"id":2,"name":"complete","image":" complete.png"},"depth":2,"ordinal":16,"resource":{"id":0,"name":"None"}},{"id":82,"label":"Pets secondary","status":{"id":2,"name":"complete","image":"complete.png"},"depth":3,"ordinal":17,"resource":{"id":0,"name":"None"}},{"id":81,"label":"Main Details","status":{"id":2,"name":"complete","image":"complete.png "},"depth":3,"ordinal":18,"resource":{"id":0,"name":"None"}} ] output: [{"id": 65, "label": "Dashboard", "status": {"id": 1, "name": "in progress", "image": "inprogress.png"}, "depth": 1, "ordinal": 1, "resource": {"id": 0, "name": "None"}, "children": [{"id": 67, "label": "Callcenter", "status": {"id": 1, "name": "in progress", "image": "inprogress.png"}, "depth": 2, "ordinal": 2, "resource": {"id": 0, "name": "None"}}, {"id": 71, "label": "Operations", "status": {"id": 1, "name": "in progress", "image": " inprogress.png"}, "depth": 2, "ordinal": 3, "resource": {"id": 0, "name": "None"}}, {"id": 69, "label": "DBA", "status": {"id": 1, "name": "in progress", "image": "inprogress.png"}, "depth": 2, "ordinal": 4, "resource": {"id": 0, "name": "None"}}, {"id": 70, "label": "Manager", "status": {"id": 1, "name": "in progress", "image": "inprogress.png"}, "depth": 2, "ordinal": 5, "resource": {"id": 0, "name": "None"}, "children": [{"id": 68, "label": "Disable multiple dashboard feature", "status": {"id": 1, "name": "in progress", "image": "inprogress.png"}, "depth": 3, "ordinal": 6, "resource": {"id": 0, "name": "None"}, "children": [{"id": 72, "label": "*Add dashboard items to single* dashboard", "status": {"id": 2, "name": "complete", "image": "complete.png"}, "depth": 4, "ordinal": 8, "resource": {"id": 3, "name": "Dean"}, "children": [{"id": 73, "label": "New applications", "status": {"id": 2, "name": "complete", "image": "complete.png"}, "depth": 5, "ordinal": 9, "resource": {"id": 3, "name": "Dean"}}, {"id": 74, "label": "Pending Claims", "status": {"id": 2, "name": "complete", "image": " complete.png"}, "depth": 5, "ordinal": 10, "resource": {"id": 3, "name": "Dean"}}, {"id": 75, "label": "Notes", "status": {"id": 2, "name": "complete", "image": "complete.png"}, "depth": 5, "ordinal": 11, "resource": {"id": 3, "name": "Dean"}}, {"id": 76, "label": "Cancellations", "status": {"id": 2, "name": "complete", "image": "complete.png"}, "depth": 5, "ordinal": 12, "resource": {"id": 3, "name": "Dean"}}]}]}]}]}, {"id": 67, "label": "Callcenter", "status": {"id": 1, "name": "in progress", "image": " inprogress.png"}, "depth": 2, "ordinal": 2, "resource": {"id": 0, "name": "None"}}, {"id": 71, "label": "Operations", "status": {"id": 1, "name": "in progress", "image": "inprogress.png"}, "depth": 2, "ordinal": 3, "resource": {"id": 0, "name": "None"}}, {"id": 69, "label": "DBA", "status": {"id": 1, "name": "in progress", "image": "inprogress.png"}, "depth": 2, "ordinal": 4, "resource": {"id": 0, "name": "None"}}, {"id": 70, "label": "Manager", "status": {"id": 1, "name": "in progress", "image": "inprogress.png"}, "depth": 2, "ordinal": 5, "resource": {"id": 0, "name": "None"}, "children": [{"id": 68, "label": "Disable multiple dashboard feature", "status": {"id": 1, "name": "in progress", "image": "inprogress.png"}, "depth": 3, "ordinal": 6, "resource": {"id": 0, "name": "None"}, "children": [{"id": 72, "label": " *Add dashboard items to single* dashboard", "status": {"id": 2, "name": "complete", "image": "complete.png"}, "depth": 4, "ordinal": 8, "resource": {"id": 3, "name": "Dean"}, "children": [{"id": 73, "label": "New applications", "status": {"id": 2, "name": "complete", "image": " complete.png"}, "depth": 5, "ordinal": 9, "resource": {"id": 3, "name": "Dean"}}, {"id": 74, "label": "Pending Claims", "status": {"id": 2, "name": "complete", "image": "complete.png"}, "depth": 5, "ordinal": 10, "resource": {"id": 3, "name": "Dean"}}, {"id": 75, "label": "Notes", "status": {"id": 2, "name": "complete", "image": "complete.png"}, "depth": 5, "ordinal": 11, "resource": {"id": 3, "name": "Dean"}}, {"id": 76, "label": "Cancellations", "status": {"id": 2, "name": "complete", "image": "complete.png"}, "depth": 5, "ordinal": 12, "resource": {"id": 3, "name": "Dean"}}]}]}]}, {"id": 68, "label": "Disable multiple dashboard feature", "status": {"id": 1, "name": "in progress", "image": "inprogress.png"}, "depth": 3, "ordinal": 6, "resource": {"id": 0, "name": "None"}, "children": [{"id": 72, "label": "*Add dashboard items to single* dashboard", "status": {"id": 2, "name": "complete", "image": "complete.png"}, "depth": 4, "ordinal": 8, "resource": {"id": 3, "name": "Dean"}, "children": [{"id": 73, "label": "New applications", "status": {"id": 2, "name": "complete", "image": " complete.png"}, "depth": 5, "ordinal": 9, "resource": {"id": 3, "name": "Dean"}}, {"id": 74, "label": "Pending Claims", "status": {"id": 2, "name": "complete", "image": "complete.png"}, "depth": 5, "ordinal": 10, "resource": {"id": 3, "name": "Dean"}}, {"id": 75, "label": "Notes", "status": {"id": 2, "name": "complete", "image": "complete.png"}, "depth": 5, "ordinal": 11, "resource": {"id": 3, "name": "Dean"}}, {"id": 76, "label": "Cancellations", "status": {"id": 2, "name": "complete", "image": "complete.png"}, "depth": 5, "ordinal": 12, "resource": {"id": 3, "name": "Dean"}}]}]}, {"id": 72, "label": "*Add dashboard items to single* dashboard", "status": {"id": 2, "name": "complete", "image": "complete.png"}, "depth": 4, "ordinal": 8, "resource": {"id": 3, "name": "Dean"}, "children": [{"id": 73, "label": "New applications", "status": {"id": 2, "name": "complete", "image": " complete.png"}, "depth": 5, "ordinal": 9, "resource": {"id": 3, "name": "Dean"}}, {"id": 74, "label": "Pending Claims", "status": {"id": 2, "name": "complete", "image": "complete.png"}, "depth": 5, "ordinal": 10, "resource": {"id": 3, "name": "Dean"}}, {"id": 75, "label": "Notes", "status": {"id": 2, "name": "complete", "image": "complete.png"}, "depth": 5, "ordinal": 11, "resource": {"id": 3, "name": "Dean"}} ] Please note, i''ve truncated both json arrays, the previously attached text files were complete though. Gareth On Jan 6, 2008 2:31 PM, Gareth Evans <agrath-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Okay, sweet. I didnt even suspsect the json formatter, but yeah it could > very well not be respecting order. i"ll see if I can get a pass-result here. > Sorry for the false alarm :) > > You''ve been most helpful :) > > > On Jan 6, 2008 1:24 PM, Tobie Langel <tobie.langel-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > untworstworthy <--- now that''s one serious typo. > > > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
OK, pasted the output of a simplified testcase, hand-indented to make sure everything''s alright: http://pastie.textmate.org/135624 This outputs exactly what I understand you are looking for, but maybe I''m misunderstanding something. Best, Tobie --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Agh, i see... though my actual data is giving a completely different result. I''m going to investigate the source some more, maybe I skip a depth or something silly in it. The json I posted before was actual data, and it went nowhere near the json formatter, just the toJson method on an array, before and after. Gareth On Jan 6, 2008 6:36 PM, Tobie Langel <tobie.langel-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > OK, pasted the output of a simplified testcase, hand-indented to make > sure everything''s alright: > > http://pastie.textmate.org/135624 > > This outputs exactly what I understand you are looking for, but maybe > I''m misunderstanding something. > > Best, > > Tobie > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Gareth, I figured what you are doing. You aren''t using the data returned by results, but the data returned by each. the data''s was in results, not each! Here''s a more compact version, that doesn''t require you to call results at the end (and thus allows chaining: http://pastie.textmate.org/135631 Best, Tobie On Jan 6, 6:47 am, "Gareth Evans" <agr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Agh, i see... though my actual data is giving a completely different result. > > I''m going to investigate the source some more, maybe I skip a depth or > something silly in it. > The json I posted before was actual data, and it went nowhere near the json > formatter, just the toJson method on an array, before and after. > > Gareth > > On Jan 6, 2008 6:36 PM, Tobie Langel <tobie.lan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > OK, pasted the output of a simplified testcase, hand-indented to make > > sure everything''s alright: > > >http://pastie.textmate.org/135624 > > > This outputs exactly what I understand you are looking for, but maybe > > I''m misunderstanding something. > > > Best, > > > Tobie--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
That makes sense, I''ll test in a min, I just started refactoring part of this code and now it wont execute (seems to be failing parsing but error console not throwing anything)... Gareth On Jan 6, 2008 7:17 PM, Tobie Langel <tobie.langel-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Gareth, > > I figured what you are doing. > > You aren''t using the data returned by results, but the data returned > by each. > > the data''s was in results, not each! > > Here''s a more compact version, that doesn''t require you to call > results at the end (and thus allows chaining: > > http://pastie.textmate.org/135631 > > Best, > > Tobie > > On Jan 6, 6:47am, "Gareth Evans" <agr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Agh, i see... though my actual data is giving a completely different > result. > > > > I''m going to investigate the source some more, maybe I skip a depth or > > something silly in it. > > The json I posted before was actual data, and it went nowhere near the > json > > formatter, just the toJson method on an array, before and after. > > > > Gareth > > > > On Jan 6, 2008 6:36 PM, Tobie Langel <tobie.lan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > > > OK, pasted the output of a simplified testcase, hand-indented to make > > > sure everything''s alright: > > > > >http://pastie.textmate.org/135624 > > > > > This outputs exactly what I understand you are looking for, but maybe > > > I''m misunderstanding something. > > > > > Best, > > > > > Tobie > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
And a much clearer version here. commented: http://pastie.textmate.org/135637 uncommented: http://pastie.textmate.org/135638 Enjoy, Tobie On Jan 6, 7:17 am, Tobie Langel <tobie.lan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Gareth, > > I figured what you are doing. > > You aren''t using the data returned by results, but the data returned > by each. > > the data''s was in results, not each! > > Here''s a more compact version, that doesn''t require you to call > results at the end (and thus allows chaining: > > http://pastie.textmate.org/135631 > > Best, > > Tobie > > On Jan 6, 6:47 am, "Gareth Evans" <agr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Agh, i see... though my actual data is giving a completely different result. > > > I''m going to investigate the source some more, maybe I skip a depth or > > something silly in it. > > The json I posted before was actual data, and it went nowhere near the json > > formatter, just the toJson method on an array, before and after. > > > Gareth > > > On Jan 6, 2008 6:36 PM, Tobie Langel <tobie.lan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > OK, pasted the output of a simplified testcase, hand-indented to make > > > sure everything''s alright: > > > >http://pastie.textmate.org/135624 > > > > This outputs exactly what I understand you are looking for, but maybe > > > I''m misunderstanding something. > > > > Best, > > > > Tobie--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---