سؤال

I'm trying to produce JSON from an object in my ant rhino . JSON.stringify(obj) is not supported, but there is a NativeJSON class that has a stringify method. The problem though is how to call it from Ant/Rhino? I can get hold of the context, but I don't know how to get hold of the scope. This also seems more complicated than it should be, so maybe there's a more straightforward solution?

This is definitely related, except it doesn't deal with using this from Ant (which does not have an obviously exposed context/scope): Access Rhino's native JSON.Stringify from Java

NativeJSON.stringify(cx, scope, jsObject, null, null);
هل كانت مفيدة؟

المحلول

I used the following instead of the standard ant script/javascript tag. This allows JSON.stringify to be called.

        <sequential>
            <script language="javascript" manager="bsf">
                <classpath>
                </classpath><![CDATA[
                    var shell = org.mozilla.javascript.tools.shell.Main;
                    var args = ["-e","var a='STRING';"];
                    shell.exec(args);

                    var shellGlobal = shell.global;

                    @{text}
            ]]></script>
        </sequential>
    </macrodef>
...
<rhinoscript><![CDATA[
   JSON.stringify(obj);
]]</rhinoscript>

نصائح أخرى

For those like me who ran across this question, JSON.stringify is now supported in Rhino versions 1.7R5+.

So JSON.stringify will work in normal script block:

<script>
<![CDATA[
   JSON.stringify(obj);
]]
</script>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top