jean-emmanuel / open-stage-control

Libre and modular OSC / MIDI controller
https://openstagecontrol.ammd.net
GNU General Public License v3.0
705 stars 90 forks source link

JSON.parse in script #836

Closed patricecolet closed 9 months ago

patricecolet commented 9 months ago

'node open-stage-control --port 5000 --custom-module getConfig.js --load open-stage-control/sessions/modulePre.json')

a JSON object is sent to a global script from some widget "onValue" script:

let module = getProp('parent','variables').module set('compositionScript','{"target":"module","id":' + module + ',"command":"cue","args":' + get(this) + '}')

here is the global script:

let message = JSON.parse(get(this)) console.log(message) if (message.target === "module") { switch (message.command) { case "cue" : console.log("module " + message.id + " wants cue " + message.args) break; } }

The message is not parsed without JSON.parse(), console.log() shows nothing

With JSON.parse() it works but when I reload the page the console complains even if it works, here is the console output:

{"target":"module","id":80,"command":"cue","args":0,"test":"blob"} module 80 wants cue 0 compositionScript.onValue javascript error: SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

patricecolet commented 9 months ago

I can not reproduce the issue

jean-emmanuel commented 9 months ago

On a sidenote, you don't need to stringify the object and then parse it on the other end, you can simply pass a plain object and consume it as is in the script's onValue (also using value directly would be more efficient than calling get(this)) .

patricecolet commented 9 months ago

Curiously, javascript doesn't parse when the code come from custom module without JSON.parse()

jean-emmanuel commented 9 months ago

Then you could parse it if needed:


var data = typeof value === "string" ? JSON.parse(value) : value