JSON.stringify on Java object

Jim Laskey (Oracle) james.laskey at oracle.com
Mon Sep 16 18:27:02 PDT 2013


We will not likely support stringify of Java Objects.  It's hard to justify doing stringify without parse and then getting into the whole serialization security nightmare that plagued Rhino.  If you RFE this feature it would be very restricted.

I'm not sure if you can accomplish the same effect with stringify replacer parameter https://developer.mozilla.org/en-US/docs/Using_native_JSON .  Otherwise you'll have to implement your own utility.


-- Jim

On 2013-09-16, at 10:14 PM, Viktor Gamov <viktor.gamov at gmail.com> wrote:

> Hello Nashorn team,
> I have a question regarding conversion java object to json using JSON.stringify. Is it possible?
> Here is my test method
> @Test
> public void convertToJson() throws ScriptException, NoSuchMethodException {
>    Person person = new Person();
>    person.setFirstName("Vik");
>    person.setLastName("Gamov");
>    person.setSsn("111-11-11");
>    Invocable invocable = (Invocable) engine;
>    Object x = invocable.invokeMethod(engine.eval("JSON"), "stringify", person);
>    System.out.println(x);
> }
> Prints null. Please advise.
> Thanks		
> -- 
> With Best Regards, 
> Vik Gamov

More information about the nashorn-dev mailing list