JSWrapper using JSList - problems using nested objects (JSIO)

39 views
Skip to first unread message

Peter Blazejewicz

unread,
Oct 8, 2007, 7:43:08 PM10/8/07
to Google Web Toolkit
Hi all,

I've spent some time recently playing with json data via gwt. It works
fine if used with JSON built-in library.
Then I tried to use javascript interop library:
http://code.google.com/p/gwt-api-interop/

and I'm stuck on how to bind childs within item (array of objects),

please consider:

JSON data:

{
"stat":"ok",
"photos":
{
"photo":
[
{"id":"1"},
{"id":"2"}
]
}
}

(as mentioned it parses via toolkit JSON library without issue),

I've tried following JSIO bindings:

/* RESULTS */


package com.mycompany.project.client;

import com.google.gwt.jsio.client.JSList;
import com.google.gwt.jsio.client.JSWrapper;

/**
* @gwt.beanProperties
*/
public interface ResultsWrapper extends JSWrapper {
public String getStat();
/**
*
* @gwt.typeArgs <com.mycompany.project.client.Photo>
*/
public JSList getPhotos();
}

/* PHOTO */

package com.mycompany.project.client;

import com.google.gwt.jsio.client.JSWrapper;

/**
* @gwt.beanProperties
*/
public interface Photo extends JSWrapper {
public String getId();
}


tested that way:

// JSON object
String jsonData = "{\"stat\":\"ok\", \"photos\":{\"photo\":[{\"id\":
\"1\"},{\"id\":\"2\"}]}}";
JSONObject json = JSONParser.parse(jsonData).isObject();
debug("json: " + json.toString());
// JSIO section
ResultsWrapper results = (ResultsWrapper) GWT
.create(ResultsWrapper.class);
try {
results.setJSONData(jsonData);
debug("results: " + results.getStat());
List photos = results.getPhotos();
} catch (JSONWrapperException e) {
debug(e.getMessage());
}


Where I'm stack is that it seems that there is no binding to Photo
class via JSList (underlying list is always empty and throws
exceptions at runtime at first time its methods/getters are used),


If someone has some idea please share,

tia,
regards,
Peter

Peter Blazejewicz

unread,
Oct 10, 2007, 8:38:33 PM10/10/07
to Google Web Toolkit
hi,
just bumping up, I've noticed someone also interested in jsio,
Peter

On Oct 9, 1:43 am, Peter Blazejewicz <peter.blazejew...@gmail.com>
wrote:
> Hi all,

Peter Blazejewicz

unread,
Oct 15, 2007, 8:28:07 PM10/15/07
to Google Web Toolkit
hi all,

Bob Vawter from GWT Team gave me a hand off-list with solving that:
>>> my "photos' is not backing object that could be used by JSList.
There is additional wrapper instance required,

so for someone interested in fast (REALLY fast, JSIO saves around 60%
of my typing if not more) code writing for wrapping large JSON data,
my example JSON data parsing could be implemented that way:

original JSON data:

{"stat":"ok", "photos":{"photo":[{"id":"1", "title":"My title"},
{"id":"2", "title":"My second title"}]}}


now JSIO classes to wrap that object (names of classes are example
ones):


package com.mycompany.project.client;

import com.google.gwt.jsio.client.JSWrapper;

/**
* @gwt.beanProperties
*/
public interface Photos extends JSWrapper {
public String getStat();

public PhotoAPI getPhotos();
}

wrapper for Photo Javascript objects list:

package com.mycompany.project.client;

import com.google.gwt.jsio.client.JSList;
import com.google.gwt.jsio.client.JSWrapper;

/**
* @gwt.beanProperties
*/
public interface PhotoWrapper extends JSWrapper {
/**
* @gwt.typeArgs <com.mycompany.project.client.Photo>
*/
public JSList getPhoto();
}


package com.mycompany.project.client;

import com.google.gwt.jsio.client.JSWrapper;

/**
* @gwt.beanProperties
*/
public interface Photo extends JSWrapper {

public abstract String getId();
public abstract String getTitle();
}


That should be easy how to use it by example code:

public void onModuleLoad() {
// create native JSON object
JavaScriptObject json = createJSONData();
// use JSIO
// top level photos object
Photos results = (Photos) GWT.create(Photos.class);
// wrap javascript
results.setJavaScriptObject(json);
// child object with node contains array of objects
// so it needs to be wrapped
PhotoWrapper photoWrapper = results.getPhotos();
// for each object in photo wrapper
Iterator iter = photoWrapper.getPhoto().iterator();
Photo photo = null;
while (iter.hasNext()) {
// get instance of photo
photo = (Photo) iter.next();
debug("Photo item:");
debug("id: " + photo.getId() + " title: " + photo.getTitle());
}
}

private native JavaScriptObject createJSONData()/*-{
var json = {"stat":"ok", "photos":{"photo":[{"id":"1", "title":"My
title"},{"id":"2", "title":"My second title"}]}};
return json;
}-*/;

private void debug(String msg) {
System.out.println(msg);
}

hth,
regards,
Peter

On Oct 11, 2:38 am, Peter Blazejewicz <peter.blazejew...@gmail.com>
wrote:

Peter Blazejewicz

unread,
Oct 15, 2007, 8:45:42 PM10/15/07
to Google Web Toolkit
hi,
BTW: what a nasty code but it works (look, PhotoWrapper class usage
can be hidden):
JavaScriptObject json = createJSONData();
Iterator iter = ((Photos) ((Photos)
GWT.create(Photos.class)).setJavaScriptObject(json)).getPhotos().getPhoto().iterator();
while (iter.hasNext()) {
Photo photo = (Photo) iter.next();
debug("Photo item:\n id: " + photo.getId() + " title: "
+ photo.getTitle());
}

regards,
Peter
On Oct 16, 2:28 am, Peter Blazejewicz <peter.blazejew...@gmail.com>

mP

unread,
Oct 16, 2007, 8:27:48 AM10/16/07
to Google Web Toolkit
Im intersted in how it all works... Does it support unmarshalling json
into types which include other types? the json you give is pretty much
just a list of objects with the second level key/values used to
populate the Person objects wich in turn end up in a list. Eg would it
be possible to have a Person with something other than primitive/
String type fields ? I am assuming because there are no annotations
that the mapping is done by the generator by simply mapping property
setters to keys found in the json ?

On Oct 16, 10:45 am, Peter Blazejewicz <peter.blazejew...@gmail.com>

Peter Blazejewicz

unread,
Oct 16, 2007, 4:34:03 PM10/16/07
to Google Web Toolkit
hi Miroslav,

something that way with inheritance used?

BOOK CLASS:


package com.mycompany.project.client;

import com.google.gwt.jsio.client.JSWrapper;

/**
* @gwt.beanProperties
*/
public interface Book extends JSWrapper {
public abstract String getId();

public abstract String getTitle();

public abstract Author getAuthor();

}

PERSON CLASS:

package com.mycompany.project.client;

import com.google.gwt.jsio.client.JSWrapper;

/**
* @gwt.beanProperties
*/
public interface Person extends JSWrapper {
public abstract String getFirstName();

public abstract String getLastName();

}


AUTHOR CLASS: (just as marker here, don't add anything to person):

package com.mycompany.project.client;

/**
* @gwt.beanProperties
*/
public interface Author extends Person {
}


example JSON data:

{
"id":"09888444", "author":
{
"firstName":"Ernest", "lastName":"Hemingway"
},
"title":"some title"
}

Usage:

Book book = (Book) GWT.create(Book.class);
book.setJavaScriptObject(createJSONData());
System.out.println("Book: id:" + book.getId() + " title: "
+ book.getTitle());
Person whoWrote = book.getAuthor();
System.out.println("Author: "+whoWrote.getLastName()+",
"+whoWrote.getFirstName());

>out>
Book: id:09888444 title: some title
Author: Hemingway, Ernes

regards,
Peter

Reply all
Reply to author
Forward
0 new messages