Ringobot log - RingoJS IRC channel: #ringojs on irc.freenode.net
2010-07-07:
[15:35] <mschwartz> earl: regarding the stuff I wrote yesterday...[15:35] <mschwartz> On the server side, I want to JSON encode various objects and send them as response to XHR requests
[15:36] <mschwartz> in the browser, they are JSON decoded using eval() or the native JSON methods
[15:36] <mschwartz> the string returned by the rhino native JSON object does not eval() or decode proper by the browser's native JSON
[15:37] <mschwartz> the output of rhino's JSON.stringify() should not have escapes
[15:37] <mschwartz> and it should not have " at the beginning and end
[15:38] <mschwartz> <?php
[15:38] <mschwartz> echo json_encode(array('a' => 'abc', 'b' => 'def'));
[15:38] <mschwartz> {"a":"abc","b":"def"}
[15:38] <earl> mschwartz: JSON.stringify works just as you expect it
[15:38] <mschwartz> ^^^ no escapes
[15:39] <mschwartz> JSON.stringify() should return what php's json_encode does
[15:39] <oberhamsi> mschwartz, could you paste code which produces escaped json? i still can't reproduce how you get that :)
[15:39] <mschwartz> In helma 1.7.0
[15:39] <earl> mschwartz: the problem is not in JSON.stringify, but in how you handle the result
[15:39] <mschwartz> I replaced the rhino.jar with js.jar from ringojs
[15:40] <mschwartz> in the debugger window, Evaluate tab
[15:40] <mschwartz> JSON.stringify({a:'abc', b: 'def'})
[15:40] <oberhamsi> mschwartz, how do you return the json? with ringo.webapp.response.jsonResponse() ?
[15:40] <mschwartz> returns "{\"a\":\"abc\",\"b\":\"def\"}"
[15:41] <mschwartz> see the \\\\\\ all over the place?
[15:41] <oberhamsi> mschwartz, maybe debugger does that escaping. JSON.stringify does not
[15:41] <mschwartz> in my code
[15:41] <mschwartz> I do res.write(JSON.stringify({ ...});
[15:41] <oberhamsi> mschwartz, you might have that problem if you double stringify
[15:41] <mschwartz> and the browser sees \\\\
[15:42] <oberhamsi> in ringo i mean
[15:42] <mschwartz> I'm not calling stringify twice
[15:42] <oberhamsi> okay, but how do you return the json in ringo?
[15:42] <mschwartz> I'm only using the rhino from ringo, but in helma 1.7.0
[15:42] <earl> mschwartz: sounds like you are having problems with helma, you should talk to #helma or the helma mailing list then
[15:43] <mschwartz> helma 1.7.0 has rhino 1.7r2
[15:43] <mschwartz> 1.7 r3 has native JSON
[15:43] <oberhamsi> mschwartz, lets continue this in #helma? :) i was confused
[15:43] <earl> $ ringo
[15:43] <earl> >> JSON.stringify({foo: "bar"})
[15:43] <earl> {"foo":"bar"}
[15:43] <earl> >> JSON.stringify({foo: "bar"}).toSource()
[15:43] <earl> (new String("{\"foo\":\"bar\"}"))
[15:43] <mschwartz> I'm in #helma
[15:43] <earl> no, you are in #ringojs, and things work as expected in ringo :)
[15:46] <daian> res.write might be doing the escaping you have a problem with
[15:48] <emilis-web20> is it possible to get filename and line that generated an exception?
[15:52] <emilis-web20> or at least get a filename and line number of the current line of code?
[15:58] <mschwartz> I don't think res.write is doing the escaping
[15:58] <mschwartz> it doesn't escape the old Helma's Object.toJSON() created string
[15:58] <emilis-web20> found it: try { throw new Error("message") } catch (e) { print(e.fileName, e.lineNumber) }
[19:11] <mschwartz> I have a simple question, I think
[19:11] <mschwartz> there is a need to cast one java type to another in rhino context
[19:11] <mschwartz> like
[19:12] <mschwartz> you call a class/member function that returns a List
[19:12] <mschwartz> and you need to pass that to another java class/member functiont that takes an Array
[19:36] <olegp> call toArray on the list
[19:37] <olegp> http://java.sun.com/j2se/1.4.2/docs/api/java/util/List.html#toArray()
[20:02] <mschwartz> actually
[20:03] <mschwartz> the issue is calling the google closure compiler
[20:03] <mschwartz> you call JSSourceFile.fromCode(string, string)
[20:03] <mschwartz> it returns a JSSourceFile
[20:03] <mschwartz> (in java)
[20:03] <mschwartz> but JavaScript sees it as SourceFile$someSubClass
[20:03] <mschwartz> and you need to pass a JSSourceFile to the Compiler.compile() method
[20:04] <mschwartz> and this is not documented in an easy to find way....
[20:04] <mschwartz> var JSSourceFilefromCode = java.lang.Class.forName('com.google.javascript.jscomp.JSSourceFile').getMethod('fromCode', [java.lang.String, java.lang.String]);
[20:04] <mschwartz> var code = JSSourceFilefromCode.invoke(null, [String('input.js'), String(s)]);
[20:04] <mschwartz> tada
[20:04] <mschwartz> you get a JSSourceFile
[20:05] <mschwartz> by calling invoke() on what getMethod() returns
