0

I am trying to pass a Javascript function as the JSONObject element in primefaces.

In my Java backing bean, I have:

JSONObject serie = new JSONObject();
serie.put("name", "Single data serie");
...
serie.put( <other JSON fields and objects> )
...
serie.put("method", "function() { return '1'; }");

Then I send the object serie to frontend and use that JSONObject in my Javascript. Everyting works fine, except that "method" is being considered as String, not as function.

Is there any way to send the function as function, not String?

I use the

org.primefaces.json.JSONObject

class. I know, that there is also the class

org.json.JSONObject

but it seems to me as the same.

Regards,

Mateusz

Mateusz Moroz
  • 312
  • 2
  • 6
  • 19
  • Ok, I worked it out as described here: [http://stackoverflow.com/questions/10854094/java-add-function-to-json-object-without-using-quotes](http://stackoverflow.com/questions/10854094/java-add-function-to-json-object-without-using-quotes) I implemented the JSFunction class, that implements the JSONString interface and it works. – Mateusz Moroz Mar 11 '15 at 16:33

1 Answers1

0

Sounds like bad design, but you're probably looking for eval();

Some code sample:

eval("function x() { return '1'; } x();"); 

prints "1".

Everv0id
  • 1,862
  • 3
  • 25
  • 47
  • Hi. Are you sure, that this will work with the json property? I see here only a replacement of the JSON property value from: `"method": "function() { ...}"` to `"method": "eval(\"function....\")"` Literally both properties are Strings. Regards, – Mateusz Moroz Mar 11 '15 at 16:26
  • Hi again. I worked it out, see my comment to the original question. – Mateusz Moroz Mar 11 '15 at 16:34