Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MALI-1357] Manage special characters transmitted to the MiniApp #279

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions js-miniapp-bridge/src/common-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down Expand Up @@ -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, '\\');
Expand All @@ -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);
Expand Down
Loading