Converting Java class to Javascript object

1,225 views
Skip to first unread message

jCarver

unread,
Nov 4, 2008, 12:38:07 PM11/4/08
to Java2Script
Does this tool have any mechanism for me to convert a plain old java
object to a Javascript object?
I have been hand writing such Javascript classes for a while now, just
wondering if there is a tool to ease my effort.
Thanks.

Sebastian Gurin

unread,
Nov 5, 2008, 7:40:30 AM11/5/08
to java2...@googlegroups.com
hi jcaver!. .. Well, i played with java reflection inside j2s with good results. I think you won't have problems with reflection.

I think your main problem is that since j2s "contaminate" the javascript Object prototype (with methods wait,notifyAll,notify,finalize,clone,getClass,hashCode,equals), use javascript objects as "data objects" / maps it is more complicated than in a simple (non j2s) script. For example, in

Object o = null;
/**
* @j2sNative
* o={"a": "a"};
*/{}

o will have many more properties (functions) than "a". I have tried with "delete o.equals" or "o.equals=null" for deleting j2s Object prototype extensions but they don't seems to work...

I have done some code that translates a java bean to a json string (for easying json-rest comunications), but i'm not sure if that is what you want. Please make us inform of your advances in this respect

regards, hope you can understand my bad english


--
Sebastian Gurin <sgu...@softpoint.org>

jCarver

unread,
Nov 5, 2008, 7:16:03 PM11/5/08
to Java2Script
I had a feeling such a conversion won't be straight forward. Most of
the Java objects i deal with have complex structures and i am not sure
if it is worth trying such a tool. If the object Protoype gets
contaminated as you mentioned, it may not be the wisest path for me to
take.

Thanks for the pointers.

On Nov 5, 4:40 am, Sebastian Gurin <sgu...@softpoint.org> wrote:
> hi jcaver!. .. Well, i played with java reflection inside j2s with good results. I think you won't have problems with reflection.
>
> I think your main problem is that since j2s "contaminate" the javascript Object prototype (with methods wait,notifyAll,notify,finalize,clone,getClass,hashCode,equals), use javascript objects as "data objects" / maps it is more complicated than in a simple (non j2s) script. For example, in
>
> Object o = null;
> /**
>  * @j2sNative
>  * o={"a": "a"};
>  */{}
>
> o will have many more properties (functions) than "a". I have tried with "delete o.equals" or "o.equals=null" for deleting j2s Object prototype extensions but they don't seems to work...
>
> I have done some code that translates a java bean to a json string (for easying json-rest comunications), but i'm not sure if that is what you want. Please make us inform of your advances in this respect
>
> regards, hope you can understand my bad english
>
> On Tue, 4 Nov 2008 09:38:07 -0800 (PST)
>

Sebastian Gurin

unread,
Nov 19, 2008, 7:28:30 AM11/19/08
to java2...@googlegroups.com
in respect to this issue i forgot to say that is possible to create a javascript with a clean prototype like this:

Object o = null;
/**
@j2sNative
function __Obj__() {}
var extensions = ["equals", "hashCode", "getClass", "clone", "finalize", "notify","notifyAll", "wait"]
for(var i=0; i<extensions.length; i++)
__Obj__.prototype[extensions[i]]=null;
o=new __Obj__();
*/{}

My context: i'm working with a j2s app comunicating vía json-rest WS and I a single interface for comunicating in such protocol in the same way both in java and javascript "runtimes"

I am trying make it work some of the json java implementations listed on www.json.org on java2script. My big doubt is if, in javascript side, use some of these java implementations for doing the parsing/deserialization (json->objects) or use javascript native eval() function.

good bye
--
Sebastian Gurin <sgu...@softpoint.org>
Reply all
Reply to author
Forward
0 new messages