0

I'm writing some javascript to flatten and unflatten nested JSON structures. So for example, an object that looks like this {"bim":[1,2,[6,7]]} would be converted into another JSON object that looks like this {'@@#root#@@': { bim: 'bim/1' },'bim/1': [ 1, 2, '2/4' ],'2/4': [ 6, 7 ] }

where @@#root#@@ is an identifier that I'm using to determine which object is the root of the entire structure. So far I've been able to rebuild the original JSON structure for most of my test cases. But there's a couple test cases that keep failing. I've provided two examples of these

{ '@@#root#@@': { bim: 'bim/1' },'bim/1': [ 1, 2, '2/4', 5,'4/8'],'2/4':[ 6, 7 ],'4/8': { '35': 55 } }

and

{ '@@#root#@@': { bim: 'bim/1' },'bim/1': [ 1, 2, '2/4', '3/6' ],'2/4':{ '34': 55 },'3/6': [ 6, 7 ] }

In both of the cases there is an array that contains both an array and a JSON object. This seems to be the only case under which my tests fail. In all other cases they run just fine and the original JSON structure is returned.

function rebuild( parent){
 _.forEach(parent, function(value, key){
        if(notVisited(value) && isRef(value)){
            visited.push(value);
            remove(parent, key);
            parent[key] = globObj[value]; 
            parent = parent[key];
            rebuild(parent);
        }
    });
};

globObj is an object that maps the references to their objects. I'm trying to do a depth first search where I recursively replace references with their original objects when I run into them. Is there something that I'm missing?

0 Answers0