Hi Thomas,

> Any reason you're not coding this directly in JSNI, without the eval()?
Great point. I was able to fix one JSNI function (mentioned above)
that just gets the title & id with the solution you suggested. Thank
you.

But unfortunately, there is one even more complex method, that gets
data from Google Spreadsheet feed and here I have to use the "evil"
function. These feeds are quite complex and keys in these feeds are
dynamically generated based on the names of columns in a spreadsheet.

Here is an example JSON feed:
https://spreadsheets.google.com/feeds/list/o03712292828507838454.6243940683656382600/od6/public/values?alt=json

If you check that feed you will notice, that keys where are stored
spreadsheet values are prefixed with 'gsx' + name of the column. There
are three columns in the spreadsheet - year, revenue and profit and
three corresponding JSON keys gsx$year, gsx$revenue and gsx$profit.

My code looks like this:
    public static native String parseSpreadsheetFeed(String json) /*-{
        if (json != null) {
            var obj = JSON.parse(json);
            var entries = obj.feed.entry;

            // iterate over entries
            for (var i in entries) {

                // iterate over keys
                for (var j in entries[i]) {
                    var prefix = j.split('$')[0];

                    // process only keys prefixed with 'gsx'
                    if (prefix == 'gsx') {
                        eval('var data = obj.feed.entry[' + i + '].' +
j + '.$t');
                        ...

And here I can't just replace the eval method with:
var data = obj.feed.entry[i].j.$t;

That wouldn't work.

I could probably rewrite that method using GWT, but in Javacript it is
much more elegant code and easy to understand.

The quick & dirty solution would be to check the obfuscated code and
use the renamed variable name in eval function, e.g.:
eval('var data = f.feed.entry[' + i + '].' + j + '.$t');

And pray that the name won't change in future :o) For now it is quite
acceptable solution, because this the only place in my code where I
use this and I can put there some checks, so in future builds I will
get immediate error saying the variable name has change during
compilation.

If anyone else has a better solution, please let me know.

The ideal solution would be, if we could annotate some JSNI variables
so the GWT compiler will not rename them during the obfuscation
process. The we could easily reference them in the code, typically
from eval function. Is there anything like this?


On Nov 20, 3:46 pm, Thomas Broyer <[email protected]> wrote:
> On Sunday, November 20, 2011 12:14:46 PM UTC+1, jogo wrote:
>
> > Ok. Finally I've found the sneaky bug.
>
> > The code was failing actually later in the code where was another eval
> > function referencing the obj variable eval('var title =
> > obj.feed.entry[' + i + '].title');
>
> Any reason you're not coding this directly in JSNI, without the eval()?
>
>     var title = obj.feed.entry[i].title;

-- 
You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-web-toolkit?hl=en.

Reply via email to