Page 1 of 1

Sometime smartfox adds quotes arround my uservariable

Posted: 16 Oct 2013, 18:25
by jdv145
I noticed when setting a uservariable in the js-client with a custom json-string, smartfox adds quotes around them. I had a hunch this might me caused by smartfox using json itself or something. I tried adding white space to the start and end and then no quotes where added. Since whitespace has no influence on the json-data, i'm using this fix:

Code: Select all

        var str = " \n " + JSON.stringify(newObj) + " \n "; 
        var uv = new SFS2X.Entities.Variables.SFSUserVariable("filter", str);
For now it works, but it's a bit hacky.... and its better if smartfox doesn't change my strings, not even by just adding quotes.

I'm a bit low on time, so i didn't dig very deep into it... so if i'm mistaken, please correct me :).

Re: Sometime smartfox adds quotes arround my uservariable

Posted: 17 Oct 2013, 10:29
by Bax
You should please better explain. Where exactly do you see such additional quotes appear?
We tried creating a simple object like this:
var obj = {"one":"this is one","two":2}
After JSONizing it and setting a User Variable, in the AdminTool we see the string reported as expected:
{"one":"this is one","two":2}
So nothing wrong here.
Can you elaborate? Thanks.

Re: Sometime smartfox adds quotes arround my uservariable

Posted: 17 Oct 2013, 12:40
by jdv145
Thank for your speedy response. I try to be a little more clearer this time, but i thought maybe you guys could have guessed the cause of it by just mentioning some stray quotes.

Anyway this is my code:

Code: Select all


	var obj = {k1 :'some val 1', k2: 'some val 2'}
	var str = JSON.stringify(obj);
	console.log("=|" + str + "|=");
	var uv = new SFS2X.Entities.Variables.SFSUserVariable("jsonStr", str);
	sfs.send(new SFS2X.Requests.System.SetUserVariablesRequest([uv]));

	sfs.addEventListener(SFS2X.SFSEvent.USER_VARIABLES_UPDATE, function(){
        console.log( " -|" +$rootScope.sfs.mySelf.getVariable("jsonStr").value+"|-" );
    }, this);
The output is below... i added -| and |- to make the boundaries clear.

Code: Select all

=|{"k1":"some val 1","k2":"some val 2"}|=
-|"{"k1":"some val 1","k2":"some val 2"}"|- 
now with the same code as before but added whitespace to str:

Code: Select all

   var str = " " + JSON.stringify(obj) + " ";
output as expected:

Code: Select all

=| {"k1":"some val 1","k2":"some val 2"} |= 
-| {"k1":"some val 1","k2":"some val 2"} |- 

Re: Sometime smartfox adds quotes arround my uservariable

Posted: 18 Oct 2013, 08:22
by Bax
Thank you for the clarification and example.
Actually this is the expected behavior, because the API use JSON internally. So as you are placing a JSON string inside a JSON, the API add those extra quotes to treat it as a string. This happens with any string starting and ending with { } or [ ].
You can add a single extra space at the beginning or the end of your JSON string, or strip the extra quotes when you are expecting a JSON string.

Re: Sometime smartfox adds quotes arround my uservariable

Posted: 18 Oct 2013, 13:09
by jdv145
Thanks for your explanation. Now i at least understand why whitespaces 'protect' my json string :D