Sometime smartfox adds quotes arround my uservariable

Post here your questions about the HTML5 / JavaScript for SFS2X

Moderators: Lapo, Bax

jdv145
Posts: 21
Joined: 11 May 2013, 13:13

Sometime smartfox adds quotes arround my uservariable

Postby jdv145 » 16 Oct 2013, 18:25

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 :).
User avatar
Bax
Site Admin
Posts: 4609
Joined: 29 Mar 2005, 09:50
Location: Italy
Contact:

Re: Sometime smartfox adds quotes arround my uservariable

Postby Bax » 17 Oct 2013, 10:29

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.
Paolo Bax
The SmartFoxServer Team
jdv145
Posts: 21
Joined: 11 May 2013, 13:13

Re: Sometime smartfox adds quotes arround my uservariable

Postby jdv145 » 17 Oct 2013, 12:40

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"} |-
User avatar
Bax
Site Admin
Posts: 4609
Joined: 29 Mar 2005, 09:50
Location: Italy
Contact:

Re: Sometime smartfox adds quotes arround my uservariable

Postby Bax » 18 Oct 2013, 08:22

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.
Paolo Bax
The SmartFoxServer Team
jdv145
Posts: 21
Joined: 11 May 2013, 13:13

Re: Sometime smartfox adds quotes arround my uservariable

Postby jdv145 » 18 Oct 2013, 13:09

Thanks for your explanation. Now i at least understand why whitespaces 'protect' my json string :D

Return to “SFS2X HTML5 / JavaScript API”

Who is online

Users browsing this forum: No registered users and 19 guests