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
On Oct 9, 1:43 am, Peter Blazejewicz <peter.blazejew...@gmail.com>
wrote:
> 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:
regards,
Peter
On Oct 16, 2:28 am, Peter Blazejewicz <peter.blazejew...@gmail.com>
On Oct 16, 10:45 am, Peter Blazejewicz <peter.blazejew...@gmail.com>
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