APS Excellence in Physics Education Award
November 2019

Education Prize Logo
Science SPORE Prize
November 2011

NSF Logo
The Open Source Physics Project is supported by NSF DUE-0442581.

unserialize problem with boolean variables post and replies

Return to the unserialize problem with boolean variables thread
Login to post to this thread

unserialize problem with boolean variables Ejss
John Val
13 Posts

Dera all,

I tried to set boolean values with the function

_model._userUnserialize

function present in the simulation js file

This does not work since in this function does the following

    if(json.flag) flag = json.flag;

now if you set the value in the json string as "flag":false

this line reulst in not setting flag to false since json.flag is set to be false!!!

In the next version of EJS this should be rewritten to

    if(! (json.flag===null) ) flag = json.flag;

I now have written my own unserialize function for flags which you can put in you custom function which you should call before calling the _model._userUnserialize or model unserialize function in your javascript

model custo functions:

_model.unserializeFlags = function(json) {
    if(! (json.flag ===null )) flag= json.flag;
   _model.update();
};

in your exernal script you than call

_model.unserializeFlags(ejsjsonflags);
_model.unserialize(ejsjson);


John


Replies to unserialize problem with boolean variables Ejss

Re: unserialize problem with boolean variables Ejss -
Francisco Esquembre
237 Posts

You are right. Our fault!


> In the next version of EJS this should be rewritten t
>
>
>     if(! (json.flag===null) ) flag = json.flag


I have changed this to

if (typeof json.flag != "undefined") flag = json.flag;

It will be there in new releases.

Paco



Re: Re: unserialize problem with boolean variables Ejss -
John Val
13 Posts

Dear Francisco,

Thanks for taking this in account. It will sure help me!

John



OSP Projects:
Open Source Physics - EJS Modeling
Tracker
Physlet Physics
Physlet Quantum Physics
STP Book