AnsweredAssumed Answered

Using Javascript to render JSON: an important gotchas

Question asked by martin.cowie on May 13, 2008
Latest reply on May 13, 2008 by martin.cowie
Here's the brief: in development of my web scripts, using FreeMarker to output JSON is proving something of an impediment.

Bearing in mind that JavaScript comes with it's own 'Obect.toSource()' method - I thought to use the existing code formatter already baked into Rhino.

However, when running .toSource() on an object that contains embeded Java objects, such as those plucked from the Alfresco API - even simple ones like java.lang.String(), the method would fail with the exception
Method "toSource" called on incompatible object.

The solution, in my case, is to convert strings from Java to Javascript, by appending an empty string. Once done, I can serialise my result in javascript with "result.json = someObject.toSource()"; and my FTL is a simple "${json}".

If anyone knows how to call "someObject.toSource()" from the freemarker side, that would be nicer still.

M.

Outcomes