title: Parse JSON to custom 'tree-like' file structure
body: <p>I have a folder/file system of custom classes, where Folders can have childs of either Folders or Files. Files cannot have children. When I have added folders and files, I would like to save the structure to be able to restore it. Right now i store the structure as a JSON string and my problem is how to parse it back to the custom classes.</p>
  
  <p>A JSON string could look like: </p>
  
  <pre><code>{
    "name": "Root",
    "type": "Folder",
    "img": null,
    "checked": false,
    "children": [
      {
        "name": "Folder1",
        "type": "Folder",
        "img": "img/folder.png",
        "checked": false,
        "children": [
  
        ]
      },
      {
        "name": "Desert.jpg",
        "type": "image/jpeg",
        "img": "img/file.png",
        "checked": false,
        "data": {
          "lastModifiedDate": "2009-07-14T05:32:31.000Z",
          "size": 845941
        }
      }
    ]
  }
  </code></pre>
  
  <p>Where children with <code>type ==='Folder'</code> is of my custom Folder class and <code>type !== 'Folder'</code> is an object of my custom File class.</p>
  
  <p>How should i parse this JSON to be able to create new custom objects and store them in my file structure?
  I have tried to do this, but i get stuck at the first level. I'm not that good at recursion which I suppose that I should do.
  My code so far that works when there is only files/folders in the root and no subfolders/subfiles</p>
  
  <pre><code>function importJson(json){
      var jsonObj = JSON.parse(json);
      //This line needs to be fixed when/if recursion?
      var currentFolder = rootFolder; //a global variable which is my rootFolder
  
      for(var i = 0, obj = jsonObj.children; i &lt; obj.length; i++){
          if(obj[i].type === 'Folder'){
              var folder = new Folder(obj[i].name, 'Folder');
              folder.img = obj[i].img;
              currentFolder.add(folder);
              console.log('A folder, check for children?');
          }
          else{
              var file = new File(obj[i].name, obj[i].type);
              file.data = obj[i].data;
              file.img = obj[i].img;
              currentFolder.add(file);
              console.log('a file');
          }               
      }
  }
  </code></pre>