Discussion:
Setting UTF-8 on toJSON function
(too old to reply)
yn2000
2018-03-28 19:22:04 UTC
Permalink
Hi there,
I wonder there is a way to set UTF-8 on toJSON function.
For example:
work.setAttribute("uid","123");
work.setAttribute("givenName","中文字符");
work.setAttribute("sn","测试");
work.setAttribute("cn","中文字符 测试");
task.logmsg("JSON: " + work.toJSON());

Output:
JSON: {"givenName":"\u4E2D\u6587\u5B57\u7B26","uid":"123","sn":"\u6D4B\u8BD5","cn":"\u4E2D\u6587\u5B57\u7B26 \u6D4B\u8BD5"}

I was hoping to get:
JSON: {"givenName":"中文字符","uid":"123","sn":"测试","cn":"中文字符 测试"}

Thanks. YN.
Eddie Hartman
2018-03-29 10:27:28 UTC
Permalink
Post by yn2000
Hi there,
I wonder there is a way to set UTF-8 on toJSON function.
work.setAttribute("uid","123");
work.setAttribute("givenName","中文字符");
work.setAttribute("sn","测试");
work.setAttribute("cn","中文字符 测试");
task.logmsg("JSON: " + work.toJSON());
JSON: {"givenName":"\u4E2D\u6587\u5B57\u7B26","uid":"123","sn":"\u6D4B\u8BD5","cn":"\u4E2D\u6587\u5B57\u7B26 \u6D4B\u8BD5"}
JSON: {"givenName":"中文字符","uid":"123","sn":"测试","cn":"中文字符 测试"}
Thanks. YN.
Have you tried to do it using a Java String method getBytes?

utf8Json = new java.lang.String(work.toJSON().getBytes("UTF-8"));

Haven't tried this myself, but Google seemed to think it was a good idea :)
yn2000
2018-03-29 17:42:21 UTC
Permalink
Thanks Eddie,
The outcome is the same:
utf8Json: {"givenName":"\u4E2D\u6587\u5B57\u7B26","uid":"123","sn":"\u6D4B\u8BD5","cn":"\u4E2D\u6587\u5B57\u7B26 \u6D4B\u8BD5"}

I think it is about the default parameter inside the 'toJSON()' function.
So far, the only solution that I got is to use ParserFunctionComponent (in Passive mode) where I can include UTF-8 in the function parameter.
Here is the script that I use now:

json = system.newEntry();
json.setAttribute("uid","123");
json.setAttribute("givenName","中文字符");
json.setAttribute("sn","测试");
json.setAttribute("cn","中文字符 测试");
ParserFunctionComponent.callreply(json);
json.removeAttribute("value");
task.logmsg("json: " + json);

Thanks. YN.
yn2000
2018-03-29 23:08:16 UTC
Permalink
Thanks Eddie,
The outcome is the same:
utf8Json: {"givenName":"\u4E2D\u6587\u5B57\u7B26","uid":"123","sn":"\u6D4B\u8BD5","cn":"\u4E2D\u6587\u5B57\u7B26 \u6D4B\u8BD5"}

After playing around further, I just realized that the 'work' entity is already in JSON format, meaning that I can just send the whole entity. (Well, I'll use a different entity in real life, but now, I wonder, why I used toJSON() function in the first place.)

Rgds. YN.
yn2000
2018-04-02 20:13:51 UTC
Permalink
Update...

First: After checking my old code again, I found out that I do need toJSON() function (not for the sample in this post), because this function automatically escapes special characters.

Second: My mistake, the output of this function is actually correct. For example: "givenName":"\u4E2D\u6587\u5B57\u7B26" is actually the clear text format of "givenName":"中文字符", so to speak. So, it is a matter of the TDI Console or the target GUI who should interpret the value when displaying it.

Thanks. YN.
Alon Kendler
2018-07-16 11:39:29 UTC
Permalink
Post by yn2000
Update...
First: After checking my old code again, I found out that I do need toJSON() function (not for the sample in this post), because this function automatically escapes special characters.
Second: My mistake, the output of this function is actually correct. For example: "givenName":"\u4E2D\u6587\u5B57\u7B26" is actually the clear text format of "givenName":"中文字符", so to speak. So, it is a matter of the TDI Console or the target GUI who should interpret the value when displaying it.
Thanks. YN.
Hi YN,
I've encountered the same issue.
Have you found a solution?

Alon

Loading...