Skip to content

Commit

Permalink
escape linefeeds in json string to prevent parser exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
peuter committed Apr 10, 2020
1 parent 392be46 commit d13b573
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
4 changes: 3 additions & 1 deletion client/source/class/cv/io/parser/Json.js
@@ -1,5 +1,5 @@
/**
* Sometimes the openHAB1 backend returns invalid JSON (e.g. multiple JSON object in one string)
* Sometimes the backend returns invalid JSON (e.g. multiple JSON object in one string) or there are linefeeds in the values.
* This parser can handle those strings
*/
qx.Class.define('cv.io.parser.Json', {
Expand All @@ -15,6 +15,7 @@ qx.Class.define('cv.io.parser.Json', {
"jquery": function(data) {
var result = {};
try {
data = data.replace(/\n/g, "\\n");
result = JSON.parse(data);
} catch (e) {
data.split("}{").forEach(function(subData, i) {
Expand All @@ -31,6 +32,7 @@ qx.Class.define('cv.io.parser.Json', {
"qx": function(data) {
var result = {};
try {
data = data.replace(/\n/g, "\\n");
result = qx.lang.Json.parse(data);
} catch (e) {
data.split("}{").forEach(function(subData, i) {
Expand Down
2 changes: 1 addition & 1 deletion client/source/class/cv/io/transport/Sse.js
Expand Up @@ -98,7 +98,7 @@ qx.Class.define('cv.io.transport.Sse', {
*/
handleMessage: function (e) {
this.client.record("read", e.data);
var json = JSON.parse(e.data);
var json = cv.io.parser.Json.parse(e.data);
var data = json.d;
this.client.update(data);
this.client.setDataReceived(true);
Expand Down

0 comments on commit d13b573

Please sign in to comment.