Skip to content

Commit

Permalink
safeishJSON to workaround invalid JSON from devices
Browse files Browse the repository at this point in the history
  • Loading branch information
aholstenson committed Jan 25, 2018
1 parent 67865ec commit 2639ab7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const debug = require('debug');
const Packet = require('./packet');
const tokens = require('./tokens');

const safeishJSON = require('./safeishJSON');

const PORT = 54321;

const ERRORS = {
Expand Down Expand Up @@ -360,7 +362,7 @@ class DeviceInfo {

this.debug('<- Message: `' + str + '`');
try {
let object = JSON.parse(str);
let object = safeishJSON(str);

const p = this.promises.get(object.id);
if(! p) return;
Expand Down
12 changes: 12 additions & 0 deletions lib/safeishJSON.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

module.exports = function(str) {
try {
return JSON.parse(str);
} catch(ex) {
// Case 1: Load for subdevices fail as they return empty values
str = str.replace('[,]', '[null,null]');

return JSON.parse(str);
}
};

0 comments on commit 2639ab7

Please sign in to comment.