Be less dumb about JSON parsing

This commit is contained in:
R Tyler Croy 2019-11-28 22:03:20 -08:00
parent 5c121ff064
commit d565f5251e
No known key found for this signature in database
GPG Key ID: E5C92681BEF6CEA2
1 changed files with 3 additions and 7 deletions

View File

@ -29,15 +29,11 @@ function wsConnect (app) {
socket.onmessage = (event) => {
const data = JSON.parse(event.data)
/*
* If this looks like some JSON data, then we might as well try to parse it
*/
const jsony = ['{', '[', ']', '}']
if (jsony.includes(data.payload.charAt(0)) &&
(jsony.includes(data.payload.charAt(data.payload.length - 1)))) {
try {
data.payload = JSON.parse(data.payload)
} catch (e) {
// swallow the error
}
socket.app.$store.commit('receive', data)
}
}