From ce67737cc95439c9de9ce4114eac4db0c47252ec Mon Sep 17 00:00:00 2001 From: Stephen McLaughlin <44235289+Steve-Mcl@users.noreply.github.com> Date: Thu, 14 Apr 2022 16:39:35 +0100 Subject: [PATCH] auto mode to auto parse string to JS Object --- .../@node-red/nodes/core/network/10-mqtt.js | 13 ++++++++----- .../@node-red/nodes/locales/de/messages.json | 2 +- .../@node-red/nodes/locales/en-US/messages.json | 2 +- .../@node-red/nodes/locales/ja/messages.json | 2 +- .../@node-red/nodes/locales/ko/messages.json | 2 +- .../@node-red/nodes/locales/ru/messages.json | 2 +- .../@node-red/nodes/locales/zh-CN/messages.json | 2 +- .../@node-red/nodes/locales/zh-TW/messages.json | 2 +- 8 files changed, 15 insertions(+), 12 deletions(-) diff --git a/packages/node_modules/@node-red/nodes/core/network/10-mqtt.js b/packages/node_modules/@node-red/nodes/core/network/10-mqtt.js index fa4472e54..29870d63c 100644 --- a/packages/node_modules/@node-red/nodes/core/network/10-mqtt.js +++ b/packages/node_modules/@node-red/nodes/core/network/10-mqtt.js @@ -235,8 +235,7 @@ module.exports = function(RED) { if (v5isUtf8 || isUtf8(payload)) { try { payload = JSON.parse(payload.toString()); - } - catch(e) { + } catch(e) { node.error(RED._("mqtt.errors.invalid-json-parse"),{payload:payload, topic:topic, qos:packet.qos, retain:packet.retain}); return; } } @@ -255,19 +254,23 @@ module.exports = function(RED) { case "object": try { payload = JSON.parse(payload.toString()); - } - catch(e) { + } catch(e) { node.error(RED._("mqtt.errors.invalid-json-parse"),{payload:payload, topic:topic, qos:packet.qos, retain:packet.retain}); return; } break; default: - if (v5isUtf8 || isUtf8(payload)) { + if (v5isUtf8 || isUtf8(payload)) { payload = payload.toString(); //auto String } break; } } else if (isUtf8(payload)) { payload = payload.toString(); //auto String + try { + payload = JSON.parse(payload); + } catch(e) { + /* mute error - it simply isnt JSON, just leave payload as a string */ + } } //else { //leave as buffer //} diff --git a/packages/node_modules/@node-red/nodes/locales/de/messages.json b/packages/node_modules/@node-red/nodes/locales/de/messages.json index aa3f1fa49..35ce50f4d 100755 --- a/packages/node_modules/@node-red/nodes/locales/de/messages.json +++ b/packages/node_modules/@node-red/nodes/locales/de/messages.json @@ -422,7 +422,7 @@ "buffer": "Einen binären Buffer", "string": "Ein String", "base64": "Ein Base64-kodierter String", - "auto": "Auto-Erkennung (string oder buffer)", + "auto": "Auto-Erkennung (parsed JSON-Objekt, string oder buffer)", "json": "Ein analysiertes (parsed) JSON-Objekt" }, "true": "wahr", diff --git a/packages/node_modules/@node-red/nodes/locales/en-US/messages.json b/packages/node_modules/@node-red/nodes/locales/en-US/messages.json index ecc5d905c..f6807562f 100755 --- a/packages/node_modules/@node-red/nodes/locales/en-US/messages.json +++ b/packages/node_modules/@node-red/nodes/locales/en-US/messages.json @@ -450,7 +450,7 @@ "buffer": "a Buffer", "string": "a String", "base64": "a Base64 encoded string", - "auto": "auto-detect (string or buffer)", + "auto": "auto-detect (parsed JSON object, string or buffer)", "json": "a parsed JSON object" }, "true": "true", diff --git a/packages/node_modules/@node-red/nodes/locales/ja/messages.json b/packages/node_modules/@node-red/nodes/locales/ja/messages.json index 7784dbe4b..059522f46 100644 --- a/packages/node_modules/@node-red/nodes/locales/ja/messages.json +++ b/packages/node_modules/@node-red/nodes/locales/ja/messages.json @@ -450,7 +450,7 @@ "buffer": "バイナリバッファ", "string": "文字列", "base64": "Base64文字列", - "auto": "自動判定(文字列もしくはバイナリバッファ)", + "auto": "自動判定(JSONオブジェクト、文字列もしくはバイナリバッファ)", "json": "JSONオブジェクト" }, "true": "する", diff --git a/packages/node_modules/@node-red/nodes/locales/ko/messages.json b/packages/node_modules/@node-red/nodes/locales/ko/messages.json index 0566f64ec..b543bba80 100755 --- a/packages/node_modules/@node-red/nodes/locales/ko/messages.json +++ b/packages/node_modules/@node-red/nodes/locales/ko/messages.json @@ -361,7 +361,7 @@ "buffer": "바이너리 버퍼", "string": "문자열", "base64": "Base64문자열", - "auto": "자동판정(문자열혹은 바이너리버퍼)", + "auto": "자동판정(JSON오브젝트, 문자열혹은 바이너리버퍼)", "json": "JSON오브젝트" }, "true": "한다", diff --git a/packages/node_modules/@node-red/nodes/locales/ru/messages.json b/packages/node_modules/@node-red/nodes/locales/ru/messages.json index 72bda67c7..33d750950 100755 --- a/packages/node_modules/@node-red/nodes/locales/ru/messages.json +++ b/packages/node_modules/@node-red/nodes/locales/ru/messages.json @@ -384,7 +384,7 @@ "buffer": "буфер", "string": "строка", "base64": "строка в кодировке Base64", - "auto": "автоопределение (строка или буфер)", + "auto": "автоопределение (разобрать объект JSON, строка или буфер)", "json": "объект JSON" }, "true": "да", diff --git a/packages/node_modules/@node-red/nodes/locales/zh-CN/messages.json b/packages/node_modules/@node-red/nodes/locales/zh-CN/messages.json index ca4de2963..9b972a3bb 100644 --- a/packages/node_modules/@node-red/nodes/locales/zh-CN/messages.json +++ b/packages/node_modules/@node-red/nodes/locales/zh-CN/messages.json @@ -381,7 +381,7 @@ "buffer": "Buffer", "string": "字符串", "base64": "Base64编码字符串", - "auto": "自动检测 (字符串或buffer)", + "auto": "自动检测 (已解析的JSON对象、字符串或buffer)", "json": "解析的JSON对象" }, "true": "是", diff --git a/packages/node_modules/@node-red/nodes/locales/zh-TW/messages.json b/packages/node_modules/@node-red/nodes/locales/zh-TW/messages.json index eef6322a3..03db9d0c4 100644 --- a/packages/node_modules/@node-red/nodes/locales/zh-TW/messages.json +++ b/packages/node_modules/@node-red/nodes/locales/zh-TW/messages.json @@ -385,7 +385,7 @@ "buffer": "Buffer", "string": "字串", "base64": "Base64編碼字串", - "auto": "自動檢測 (字符串或buffer)", + "auto": "自动检测 (已解析的JSON对象、字符串或buffer)", "json": "解析的JSON對象" }, "true": "是",