qzind / qz-print

Archive for legacy qz-print versions (1.8, 1.9). See https://github.com/qzind/tray for modern versions.
Other
141 stars 101 forks source link

JSON.stringify causes double-quotes around Array variables #76

Closed tresf closed 9 years ago

tresf commented 9 years ago

Some JavaScript frameworks use a prototype toJSON function, can incorrectly wrap quotes around JSON objects, completely breaking the QZ Tray application.

{"method":"setPrinter","params":[1],"init":false}

Becomes this:

{"method":"setPrinter","params":"[1]","init":false}
  [ unnecessary quotes ] -------^

Better explained here: http://stackoverflow.com/questions/710586/json-stringify-array-bizarreness-with-prototype-js

The recommendation is to force the explicit use of the browser's JSON.stringify, which may involve temporarily reassigning the prototyped versions in js/qz-websocket.js#L109 just prior to use.

delete Array.prototype.toJSON
var msg = JSON.stringify(objMsg);

or

var tmp = Array.prototype.toJSON;
delete Array.prototype.toJSON;
var msg = JSON.stringify(objMsg);
Array.prototype.toJSON = tmp;