1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

auto mode to auto parse string to JS Object

This commit is contained in:
Stephen McLaughlin 2022-04-14 16:39:35 +01:00 committed by Steve-Mcl
parent 8f5d3dc49c
commit 78f456911a
8 changed files with 15 additions and 12 deletions

View File

@ -235,8 +235,7 @@ module.exports = function(RED) {
if (v5isUtf8 || isUtf8(payload)) { if (v5isUtf8 || isUtf8(payload)) {
try { try {
payload = JSON.parse(payload.toString()); 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; node.error(RED._("mqtt.errors.invalid-json-parse"),{payload:payload, topic:topic, qos:packet.qos, retain:packet.retain}); return;
} }
} }
@ -255,8 +254,7 @@ module.exports = function(RED) {
case "object": case "object":
try { try {
payload = JSON.parse(payload.toString()); 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; node.error(RED._("mqtt.errors.invalid-json-parse"),{payload:payload, topic:topic, qos:packet.qos, retain:packet.retain}); return;
} }
break; break;
@ -268,6 +266,11 @@ module.exports = function(RED) {
} }
} else if (isUtf8(payload)) { } else if (isUtf8(payload)) {
payload = payload.toString(); //auto String 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 { } //else {
//leave as buffer //leave as buffer
//} //}

View File

@ -422,7 +422,7 @@
"buffer": "Einen binären Buffer", "buffer": "Einen binären Buffer",
"string": "Ein String", "string": "Ein String",
"base64": "Ein Base64-kodierter 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" "json": "Ein analysiertes (parsed) JSON-Objekt"
}, },
"true": "wahr", "true": "wahr",

View File

@ -454,7 +454,7 @@
"buffer": "a Buffer", "buffer": "a Buffer",
"string": "a String", "string": "a String",
"base64": "a Base64 encoded 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" "json": "a parsed JSON object"
}, },
"true": "true", "true": "true",

View File

@ -454,7 +454,7 @@
"buffer": "バイナリバッファ", "buffer": "バイナリバッファ",
"string": "文字列", "string": "文字列",
"base64": "Base64文字列", "base64": "Base64文字列",
"auto": "自動判定(文字列もしくはバイナリバッファ)", "auto": "自動判定(JSONオブジェクト、文字列もしくはバイナリバッファ)",
"json": "JSONオブジェクト" "json": "JSONオブジェクト"
}, },
"true": "する", "true": "する",

View File

@ -361,7 +361,7 @@
"buffer": "바이너리 버퍼", "buffer": "바이너리 버퍼",
"string": "문자열", "string": "문자열",
"base64": "Base64문자열", "base64": "Base64문자열",
"auto": "자동판정(문자열혹은 바이너리버퍼)", "auto": "자동판정(JSON오브젝트, 문자열혹은 바이너리버퍼)",
"json": "JSON오브젝트" "json": "JSON오브젝트"
}, },
"true": "한다", "true": "한다",

View File

@ -384,7 +384,7 @@
"buffer": "буфер", "buffer": "буфер",
"string": "строка", "string": "строка",
"base64": "строка в кодировке Base64", "base64": "строка в кодировке Base64",
"auto": "автоопределение (строка или буфер)", "auto": "автоопределение (разобрать объект JSON, строка или буфер)",
"json": "объект JSON" "json": "объект JSON"
}, },
"true": "да", "true": "да",

View File

@ -381,7 +381,7 @@
"buffer": "Buffer", "buffer": "Buffer",
"string": "字符串", "string": "字符串",
"base64": "Base64编码字符串", "base64": "Base64编码字符串",
"auto": "自动检测 (字符串或buffer)", "auto": "自动检测 (已解析的JSON对象、字符串或buffer)",
"json": "解析的JSON对象" "json": "解析的JSON对象"
}, },
"true": "是", "true": "是",

View File

@ -385,7 +385,7 @@
"buffer": "Buffer", "buffer": "Buffer",
"string": "字串", "string": "字串",
"base64": "Base64編碼字串", "base64": "Base64編碼字串",
"auto": "自動檢測 (字符串或buffer)", "auto": "自动检测 (已解析的JSON对象、字符串或buffer)",
"json": "解析的JSON對象" "json": "解析的JSON對象"
}, },
"true": "是", "true": "是",