Xojo badly escape an apostrophe in a json item (for websdkcontrol ?)

stuck with that bug today.

if you add some string containing an apostrophe in a json like “it’s me”
xojo doesn’t add an escape char \ before it
if you add the escape yourself, “it’s me” then xojo adds two \ !!!

Capture d’écran 2021-10-24 à 16.16.47
Capture d’écran 2021-10-24 à 16.17.02

<https://xojo.com/issue/66306>

Why do you consider this a bug? Single quotes do not need to be escaped according to the standard.

2 Likes

because I send this JSON to a websdkcontrol, and the parsejson method in the javascript environment does not recognize it. the websdkcontrol gets destroyed because of a bad syntax.

so may be it’s only a websdk related problem ? if you assume a single quote doesn’t need to be escaped ?
I’ve seen some posts (on stackexchange) where people wanted to escape single quotes for javascript usage…

may be @Greg_O_Lone can explain more to us ?

Only necessary when your string is surround by single quotes needing such escaping. Below the 3 vars leads to the same JSON value.

// Javascript:
j1 = ["it's me"];
j2 = eval('["it\'s me"]');
j3 = eval("[\"it's me\"]");

in my actual problem, I make a json object as a string, containing a single quote ’
js.value("dbvalue") = "it's me"
this is sent to the browser side of the websdkcontrol, and parsed

super.updateControl(data);
let js = $.parseJSON(data);
this.mDataJSon = js.dbValue;

the js.dbValue is badly formed or empty. this leads to a javascript error on the browser.

how can I workaround this ?

Let’s wait someone used to use the “websdk” to chime in, but now your example differs from the first case, a

js.value("dbvalue") = "it's me"

Should render a JSON like

{"dbvalue": "it's me"}

and not an array as previously reported.

Javascript is a case sensitive language, maybe you are missing it here?
this.mDataJSon = js.dbvalue;

thanks I already checked that. it’s a typing failure only. the case is the same between xojo and the javascript

sorry for the mix, but it’s the same with an array or a dictionnary
Capture d’écran 2021-10-24 à 18.16.42

This is correct, without Xojo adding the extra backslash you will lose it when JS renders the value:

so I just replaced any single quote “’” with “\u0027” and … it works !

but it should be correctly handled ?

If you can, add a proof of the problem to your case.

I would like to see what you see because I still don’t understand your problem.

well, to sum up :
in a websdkuicontrol class, in the serialize event, you need to build a jsonitem to send to the browser.
if like me you want to send strings containing single quotes, you’re stuck
if you send it raw, it does not get decoded in the parseJson method in the browser side
if you escape it “\’”, xojo adds another \ and it gets “\\’” to the bowser which is not decoded again.

the solution was for me to replace it with the “\u0027” and it is correctly decoded in the browser parseJson.

you have to escape the double quote " to send it into a json, I thought it would be the same for single quote, but as Kem said earlier, in the json standard you don’t need to escape single quotes.
so what is the best option ?

anyway I hope some future reader looking for a workaround will come here.

no idea if it helps but did you try encoding your data that goes to the web socket ? if in case you are using that ? so far on Xojo side is known on loosing encodings here and there and the best way to do that is to define encodings when sending data over, I encountered that a lot with special characters and same as your case . Indeed " ’ " should always work in this case but you never know.

Hope it helps.

yes I always send utf8 encoded strings in the serialize event. good practice with websdk !

In my opinion the best way to pass data to websdk is to use base64 encoding.

In Xojo you use:
var t as String = “string in utf8”
js.value(“stringVal”) = EncodeBase64(t,1)

In JS:
let js = $.parseJSON(data);
let str = XojoWeb.DecodeBase64(js.stringVal);

1 Like

Case closed as “not a bug”. They say you are doing some kind of misuse of the framework.

I did not understand it that way. only what Kem said : escaping single quote is not required.

but that there may be a bug with only the websdk, I have to make another bug report with another test project for web.

:+1:t2: Yep. A simple test to show and prove your point. If it gets the status of a confirmed bug, it must be fixed, if not, you may get an explanation about what’s wrong in the process being done and the best way of doing it.

my workaround with \u0027 is working so fine that I may not take the time to make a small test project !