JSON.stringify() problem

I can't figure out, why is this not working? Am I missing something obvious?

var doFsu = DOpus.FSUtil;
var doFct = DOpus.Create;
var paths = {
    dirs: doFct.Vector,
    files: doFct.Vector
}

paths.dirs.push_back('whatever/bogus/path');

var json = JSON.stringify(paths, null, 2);

DOpus.Output('count: ' + paths.dirs.count);
DOpus.Output('dir: ' + paths.dirs(0));
DOpus.Output('paths: ' + paths);
DOpus.Output('json: ' + json);

2020-02-27_06-54-37__dopus
Notice the empty json string.

Help, please.

The JSON object doesn't know what an Opus Vector object is. You probably need to write a little function to convert the Vector to a JScript array.

1 Like

@Jon thank you.

Although, I'm really confused with this one. The array obviously has the paths in it, but JSON.stringify() outputs them as null... :confused:

var paths = {
    dirs: new Array()
}
var counter = 0;
for (var el = new Enumerator(DOpus.listers); !el.atEnd(); el.moveNext()) {
    for (var et = new Enumerator(el.item().tabs); !et.atEnd(); et.moveNext()) {
        paths.dirs[counter] = et.item().path;
        counter++;
    }
}
var json = JSON.stringify(paths, null, 2);
DOpus.Output('dirs count: ' + paths.dirs.length);
DOpus.Output('first dir: ' + paths.dirs[0]);
DOpus.Output(json);

2020-02-28_04-58-35__dopus

It's because JSON also doesn't know what the Opus Path object is or how to convert it into a string.

Change this line:

paths.dirs[counter] = et.item().path;

to this, so it explicitly converts the path to a string before adding it to the array:

paths.dirs[counter] = (et.item().path + "");
1 Like

@Leo thank you.

The gothcas sometimes stumps me for hours on end :confounded:

1 Like