GregFreeman Posted August 25, 2014 Report Share Posted August 25, 2014 So, I'm just trying to recursively go through a directory list and build up a jsTree structure in JSON to send from a web service to a browser. But, I am having a heck of a time doing it. I was hoping I could just build this up into a cluster and pass it to the flatten to JSON function but for obvious recursive loading reasons that won't work because my structure will have an array of itself inside it as the children variable. When going the references route like you normally would a tree, I now am having a hard time going back up the call stack to build up the JSON structure. I could post what I have tried but I assume it won't be much help. I figured I'd just see if someone here wants to take a crack at it for me so I don't keep spinning my wheels. Feel free to scrap the li/a attributes for now. // Expected format of the node (there are no required fields) { id : "string" // will be autogenerated if omitted text : "string" // node text icon : "string" // string for custom state : { opened : boolean // is the node open disabled : boolean // is the node disabled selected : boolean // is the node selected }, children : [] // array of strings or objects li_attr : {} // attributes for the generated LI node a_attr : {} // attributes for the generated A node } Quote Link to comment
ned Posted August 25, 2014 Report Share Posted August 25, 2014 Do you actually need to assemble a recursive data structure in LabVIEW, or you just need to generate the JSON from some source data (a directory structure or similar)? If you just need the JSON, here's what I think I'd do, although I haven't tried coding it (and am on LabVIEW 2012 right now so no access to Flatten to JSON). If this isn't clear I can try coding it although might be easier if you post your code as a starting point to show the source of your data. Instead of attempting to do something recursive, use a queue (this could just be an array in a shift register around a while loop). The datatype of the queue will be a cluster of at least two elements: the data source, and the current position within the accumulate JSON string at which to insert a new child. Build up a cluster matching your datatype, but leave children as some simple scalar. Put the top-level data source into your queue. Also put a string in a shift register. Now, in a while loop, continue until the array is empty: - take the first item off the queue, and fill into the cluster. Flatten to JSON. - insert the JSON into the accumulator string at the current index. - find the new index at which to insert any children of the current node - enqueue, at the front, any children of that node, along with the string index I'm probably oversimplifying since tracking where to insert children in the string may be a challenge, but I suspect it can be made to work. If you care about the ordering of the children then enqueue them in reverse order, since you'll always be inserting new children at the same string index (pushing any existing children further into the string). Make sure you do this depth-first, by putting new children at the front of the queue, not the back. Quote Link to comment
GregFreeman Posted August 26, 2014 Author Report Share Posted August 26, 2014 (edited) I had another thought, since yes, I just need the JSON. Rather than make the children an array of by value nodes (which I can't because I'd have a recursive node cluster), I can make them an array of strings. I can go through recursive call, formatting all children nodes to an array of JSON strings and flatten that array to a single string to be passed up the stack. Then, I just have a bunch of nested JSON strings already formatted, and when I get to the top level, I can just do flatten to JSON string...I think... We'll see if it works. Edited August 26, 2014 by GregFreeman Quote Link to comment
GregFreeman Posted August 26, 2014 Author Report Share Posted August 26, 2014 I had another thought, since yes, I just need the JSON. Rather than make the children an array of by value nodes (which I can't because I'd have a recursive node cluster), I can make them an array of strings. I can go through recursive call, formatting all children nodes to an array of JSON strings and flatten that array to a single string to be passed up the stack. Then, I just have a bunch of nested JSON strings already formatted, and when I get to the top level, I can just do flatten to JSON string...I think... We'll see if it works. Won't work because of string vs object formatting in JSON. Quote Link to comment
ShaunR Posted August 26, 2014 Report Share Posted August 26, 2014 So, I'm just trying to recursively go through a directory list and build up a jsTree structure in JSON to send from a web service to a browser. But, I am having a heck of a time doing it. I was hoping I could just build this up into a cluster and pass it to the flatten to JSON function but for obvious recursive loading reasons that won't work because my structure will have an array of itself inside it as the children variable. When going the references route like you normally would a tree, I now am having a hard time going back up the call stack to build up the JSON structure. I could post what I have tried but I assume it won't be much help. I figured I'd just see if someone here wants to take a crack at it for me so I don't keep spinning my wheels. Feel free to scrap the li/a attributes for now. // Expected format of the node (there are no required fields) { id : "string" // will be autogenerated if omitted text : "string" // node text icon : "string" // string for custom state : { opened : boolean // is the node open disabled : boolean // is the node disabled selected : boolean // is the node selected }, children : [] // array of strings or objects li_attr : {} // attributes for the generated LI node a_attr : {} // attributes for the generated A node } Have you tried using the JSON API? That allows nested clusters of arbitrary types, unlike the native LabVIEW one. 2 Quote Link to comment
GregFreeman Posted August 26, 2014 Author Report Share Posted August 26, 2014 (edited) Have you tried using the JSON API? That allows nested clusters of arbitrary types, unlike the native LabVIEW one. I haven't. I'll give it a whirl. Was hoping to roll my own "real simple one" because I thought making it for a specific use case wouldn't be too big a deal. But it's proving to be more time consuming than I thought. Edited August 26, 2014 by GregFreeman Quote Link to comment
GregFreeman Posted August 26, 2014 Author Report Share Posted August 26, 2014 Have you tried using the JSON API? That allows nested clusters of arbitrary types, unlike the native LabVIEW one. Well that became trivial. Done in 10 minutes. Quote Link to comment
Aristos Queue Posted August 26, 2014 Report Share Posted August 26, 2014 It works, but it involves a lot of data copying and reflection analysis. Does it scale sufficient for your needs? Quote Link to comment
GregFreeman Posted August 26, 2014 Author Report Share Posted August 26, 2014 It works, but it involves a lot of data copying and reflection analysis. Does it scale sufficient for your needs? For now, yes. Won't be a deep directory structure. If I build something that is to be more than a one-off subVI, I can consider other routes. Quote Link to comment
drjdpowell Posted August 29, 2014 Report Share Posted August 29, 2014 It works, but it involves a lot of data copying and reflection analysis. Greg could build the compound JSON LVOOP object programmatically using other methods in the JSON LabVIEW package, rather than build a cluster and call the “Set Variant†VI that requires most of the data copying. Quote Link to comment
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.