Json string automatically parsed on function argument?


#1

In Main process:

var project = new Project() // return {a:12, b:13}
broswerWindow.webContents.on('did-finish-load', function(){
browserWindow.webContents.executeJavascript("
init(${JSON.stringify(project)})
")}

In browserWindow.js

function init(project){
console.log(project); // it prints an Object
}

How? I think it should be string because of JSON.stringify


#2

executeJavascript expects a string, ${JSON.stringify(project)} is giving you a string of the object representation, which is being re-evaluated as an object.

Try wrapping the stringify in ':

browserWindow.webContents.executeJavascript("
init('${JSON.stringify(project)}')})
")}

#3

“being re-evaluated” by who?

by the javascript engine because of the language standard or by chromium ?

PS: this actually an advantage because we can’t pass object directly to executeJavascript() or can we?


#4

I’ll start with your PS, you can’t because it’s expects a string, and this is a way around it.

So what you’re doing is building a string (JSON.stringify(project)), and then putting it into a string init(foobar). This string is then being evaluated as Javascript, meaning the stringified JSON is being understood as a Javascript object, because it’s not wrapped in any ' to mark it as a string.

It’s being evaluated by the executeJavascript(), that’s what it’s doing, it’s interpreting a string, which happens to have a string representation of an object inside.


#5

ah so electron does the re-evaluation behind the scene… thanks for explaining :slight_smile:


#6

I mean, not really, that’s just what the function does. It’s not Electron trying to do anything helpful, it’s a question of what your input is.

So with your template string, you’re turning your JSON into a string, and dumping that string into the rest of the string, so you have one big string with some JSON in it.

executeJavascript reads the string and converts it back to Javascript, because the JSON string isn’t wrapped in quotes (demarking it as a string), it’s treated as an object and not the string you’re expected.


Think about it like this, if you didn’t stringify, you’d be running init using the string [object Object], because that’s how objects are evaluated when using toString. It’s a case of delimitation, like I said before, if you wrapped it in ', the function would recognise your JSON blob as a string and not an object and treat it as you were expecting.


#7

what I mean with electron is the function it has…

anyway, based on your recent explaination

is this how it does the evaluation:

project = {a:1, b:2}

init(${JSON.stringify(project)})
to
init({a: 1, b:2})

Right?


#8

Yeah, so:

project = {a:1, b:2}

executeJavascript("init(${JSON.stringify(project)})")
//becomes:
init({a: 1, b:2})

executeJavascript("init(${project})")
//becomes:
init([object Object])

executeJavascript("init('${JSON.stringify(project)}')")
//becomes:
init('{a: 1, b:2}') //and therefore a string, not an object