Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix character encoding for special characters in JSON output #524

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

Closed
sretenD opened this issue May 28, 2025 · 2 comments
Closed

Fix character encoding for special characters in JSON output #524

sretenD opened this issue May 28, 2025 · 2 comments

Comments

@sretenD
Copy link

sretenD commented May 28, 2025

The application is producing undecoded characters like šđćž due to an encoding error during JSON or HTML writing. The m2json.m file needs to be modified to ensure proper UTF-8 encoding before escaping special characters. The suggested solution is to add the unicode2native(val, 'UTF-8') function before line 43 in m2json.m.

41|          else
42|              val = unicode2native(val, 'UTF-8'); % Convert to UTF-8
43|              val = checkescape(val); % add escape characters
44|              valstr = sprintf(\"\"\"%s\"\"\", val);
@robertoffmoura
Copy link
Collaborator

Hi! Thanks for raising this issue. Could you please share an example of the encoding error you're seeing?

@sretenD
Copy link
Author

sretenD commented May 28, 2025

I have resolved the issue. I used this function and call it
% plotly_aux/htmlencode.m
function encoded = htmlencode(str)
% Converts special characters to HTML entities
encoded = str;
% Basic HTML entities
encoded = strrep(encoded, '&', '&');
encoded = strrep(encoded, '<', '<');
encoded = strrep(encoded, '>', '>');
encoded = strrep(encoded, '"', '"');
encoded = strrep(encoded, '''', ''');
% Unicode handling: convert non-ASCII to &#xXXXX;
encoded = regexprep(encoded, '[^\x00-\x7F]', ...
'${sprintf(''&#x%04X;'', double($0))}');
end

so in m2jason.m
val = checkescape(val); % add escape characters
noTags = regexprep(val, '<[^>]+>', '');
val = htmlencode(noTags); % NEW: encode special/unicode chars
valstr = sprintf("""%s""", val);

in plotlyoffline.m
% handle plot div specs
noTags = regexprep(char(java.util.UUID.randomUUID), '<[^>]+>', '');
id = htmlencode(noTags);

    noTags = regexprep(linkText, '<[^>]+>', '');
    linkText = htmlencode(noTags);

so I cleared the issue.

@sretenD sretenD closed this as completed May 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants