Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#7082 closed defect (fixed)

Dojo 1.0.2 to Dojo 1.1/1.1.1 migration - dojo.fromJson

Reported by: guest Owned by: Adam Peller
Priority: high Milestone: 1.2
Component: General Version: 1.1.1
Keywords: Dojo 1.1.1 migration from Dojo 1.0 Cc: yazad3@…
Blocked By: Blocking:

Description

Hi,

We are trying to migrate our application from Dojo 1.0.2 to Dojo 1.1.1. We came across the error Syntax Error () on dojo.js line 20 ie line 2707 in dojo.js.uncompressed.js. The line is return eval("(" + json + ")"); Object

I acknowledge that this is discussed in the release note of Dojo 1.1. However the error is not from any of the files that we are using but in fact seems to come from an internal call from Dojo API itself.

I got this error in 2 out of some 10 odd screens in our application and the patter of files loaded before the error was (from FireBug?): GET http://localhost:8080/itaacOnline/js/dojo/dijit/TitlePane.js (16ms)dojo.js (line 1406) GET http://localhost:8080/itaacOnline/js/dojo/dojo/fx.js (16ms)dojo.js (line 1406) GET http://localhost:8080/itaacOnline/js/dojo/dijit/layout/ContentPane.js (15ms)dojo.js (line 1406) GET http://localhost:8080/itaacOnline/js/dojo/dijit/layout/_LayoutWidget.js (15ms)dojo.js (line 1406) GET http://localhost:8080/itaacOnline/js/dojo/dijit/nls/loading.js (15ms)dojo.js (line 1406) the issue comes after these 5 files are loaded...

I am unable to find the root cause of the issue.

I made a change in the code such that a NULL is returned whenever an empty string or null is received as input and the application seems to work fine.

dojo.fromJson = function(/*String*/ json){

if (json != null && json != "") {

return eval("(" + json + ")"); Object

} else {

console.debug("Returning NULL..."); return null;

}

}

I understand throwing errors from invalid strings is the right thing to do but shouldn't this method return a null when an EMPTY (""/null) string is encountered?

Because of this issue my pages were getting distorted (esp. the menu) and did NOT function at all... If you have another 1.1.x release, I request you to consider looking at this issue at higest priority.

Also please let me know if there is anything I can do to help. I am not a dojo champ, but will try my best. Thank you in advance.

Regards, Yazad Khambata

Change History (10)

comment:1 Changed 11 years ago by bill

If you can provide a testcase that would be good (actually we can't work on this w/out a testcase). It sounds like the problem is with whoever is calling dojo.fromJson() but unclear if the error is in dojo or in your code. You also might be able to use a debugger to break in that situation and look at the call stack.

comment:2 Changed 11 years ago by Adam Peller

Owner: changed from anonymous to Adam Peller
Priority: highesthigh
severity: blockerminor
Status: newassigned

null and "" are not valid JSON, so we don't parse them. We generally don't do null checks in our methods as a matter of policy; it bloats our code. I'd argue that it also could bloat the caller's code, since you may have to check for this condition. By throwing an exception, we call it out right away. I realize this makes it a bit challenging sometimes to find the errors. Debugging and/or putting temporary debug info in a method like dojo.fromJson are good strategies.

I'm inclined to mark this wontfix, but perhaps it wouldn't hurt to mention this in the inline docs.

comment:3 Changed 11 years ago by Adam Peller

Resolution: fixed
Status: assignedclosed

(In [14208]) Make it clear that dojo.fromJson throws exceptions. Fixes #7082

comment:4 Changed 11 years ago by Adam Peller

see #5725

comment:5 Changed 11 years ago by Adam Peller

I'll mention this in the release notes, too, since it's an interesting edge case. Sorry for the confusion.

comment:6 Changed 11 years ago by yaz_k83

Hi Bill,

How do I get the stack trace with firebug? Please let me know so that I can update this post. Thanks in advance.

Regards, Yazad Khambata

comment:7 in reply to:  2 Changed 11 years ago by yaz_k83

Hi Peller,

I agree with you that "" and null are invalid JSONs... However "null" is valid. If there is a Dojo Control which is indirectly invoking the said method with "" or null. It is a defect in that widget.

Unfortunately at this point i have no means to prove this. Maybe if i get some inputs from you/bill on how to check the stack trace in JavaScript? it would benefit us all. I am using FireBug? with FireFox? for debugging.

Many thanks to you and bill to promptly respond to the ticket.

Regards, Yazad Khambata

Replying to peller:

null and "" are not valid JSON, so we don't parse them. We generally don't do null checks in our methods as a matter of policy; it bloats our code. I'd argue that it also could bloat the caller's code, since you may have to check for this condition. By throwing an exception, we call it out right away. I realize this makes it a bit challenging sometimes to find the errors. Debugging and/or putting temporary debug info in a method like dojo.fromJson are good strategies.

I'm inclined to mark this wontfix, but perhaps it wouldn't hurt to mention this in the inline docs.

comment:8 Changed 11 years ago by bill

Hi, when you are in the SCRIPT tab of firebug you can see the stack on the top row, listed horizontally, or on the right hand side there's a tab for STACK. You would of course need to set a breakpoint.

If you are using Firefox3 you need to download Firebug 1.2, otherwise either 1.1 or 1.0 (I forget which one works with dojo). Can download it from https://addons.mozilla.org/en-US/firefox/addon/1843. It's essential for debugging dojo and javascript stuff in general.

comment:9 Changed 11 years ago by yaz_k83

Hi Peller/Bill?,

Finally I have found the stack trace.... I am using Dojo 1.1.1 and am using the uncompressed version of Dojo.js.

I have modified the fromJson method in dojo.js...

dojo.fromJson = function(/*String*/ json){ summary: Parses a [JSON](http://json.org) string to return a JavaScript? object.

json: a string literal of a JSON item, for instance: '{ "foo": [ "bar", 1, { "baz": "thud" } ] }' if (json != null && json != "") {

return eval("(" + json + ")"); Object

} else {

console.debug("Returning NULL..."); console.trace(); return null;

}

} _

The above trace statement prints: Returning NULL...dojo.js (line 2709)

fromJson("")dojo.js (line 2710) str2obj("", "object")dojo.js (line 512) _loadUri(input#txtParFun)dojo.js (line 603) forEach([div#menubar.menuBar, div#menuMon, div, 143 more...], function(), undefined)dojo.js (line 2909) (no name)([div#menubar.menuBar, div#menuMon, div, 143 more...])dojo.js (line 578) (no name)(body.tundra)dojo.js (line 679) (no name)() MY_FILE.js (line some LINE NO.) loaded()dojo.js (line 640) _callLoaded()dojo.js (line 737) _modulesLoaded()dojo.js (line 718) _loadInit(load )dojo.js (line 1439)

The "(no name)() MY_FILE.js (line some LINE NO.)" refers to the following line in one of our files:

dojo.parser.parse(dojo.body());

Is there something we doing wrong here? It worked fine in Dojo 1.0 but this problem crept in 1.1 and 1.1.1. Please look into this. Thank you in advance.

Regards, Yazad Khambata

comment:10 Changed 11 years ago by bill

Hi Yazad,

BTW you should quote code using triple { and } curly braces. And if you run using the source dojo.js (http://trac.dojotoolkit.org/browser/dojo/trunk/dojo.js) then you'll get a better stack trace).

Anyway, I'm not sure exactly what's going on but you are apparently instantiating some widget with a parameter of type Object, like constraints in this example:

<input id="q03" type="text" name="age" tabIndex=1
	dojoType="dijit.form.NumberTextBox"
	constraints="{places:0,min:0,max:120}"
	/>

except that it's specified as an empty string... if you look in parser.js where it calls str2obj:

var _type = clsInfo.params[name];
params[name] = str2obj(value, _type);

you should check what "name" is (when it fails); that's the name of the parameter. And also "type", which is the dojoType.

Note: See TracTickets for help on using tickets.