From 6eb5542f88cb78b2b099be34e381f264a83e1a22 Mon Sep 17 00:00:00 2001 From: Leo Date: Wed, 18 Oct 2023 16:56:23 +0900 Subject: [PATCH] [MALI-1357] Manage special characters transmitted to the MiniApp --- js-miniapp-bridge/src/common-bridge.ts | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/js-miniapp-bridge/src/common-bridge.ts b/js-miniapp-bridge/src/common-bridge.ts index 239926c0..22184bb1 100644 --- a/js-miniapp-bridge/src/common-bridge.ts +++ b/js-miniapp-bridge/src/common-bridge.ts @@ -147,7 +147,11 @@ export class MiniAppBridge { // and decoded here. let result = value; if (eventType === 'miniappreceivejsoninfo') { - result = convertUnicodeCharacters(value); + if (this.platform === 'iOS') { + result = convertUnicodeCharacters(value); + } else { + result = convertUnicodeCharactersForAndroid(value); + } } const event = new CustomEvent(eventType, { detail: { message: result }, @@ -871,7 +875,7 @@ function convertUnicodeCharacters(value) { //This will decode the message string that is sent from Native const decoded = Buffer.from(value, 'base64').toString('utf8'); //Few characters like currency, etc., is not decoded properly, - // We use folllowing method to decoded it. + // We use following method to decoded it. const octalString = decodeOctalEscape(decoded); const stringifyMessage = JSON.stringify(octalString); const replaced = stringifyMessage.replace(/\\\\/g, '\\'); @@ -882,6 +886,21 @@ function convertUnicodeCharacters(value) { } } +function convertUnicodeCharactersForAndroid(value) { + //This will decode the message string that is sent from Native + const decoded = Buffer.from(value, 'base64').toString('utf8'); + //Few characters like currency, etc., is not decoded properly, + // We use following method to decoded it. + const octalString = decodeOctalEscape(decoded); + const stringifyMessage = JSON.stringify(octalString); + const replaced = stringifyMessage.replace(/\\\\/g, '\\'); + if (isValidJson(stringifyMessage) === true) { + return JSON.parse(stringifyMessage); + } else { + return JSON.parse(replaced); + } +} + function isValidJson(str) { try { JSON.parse(str);