Although a JsonReader is frequently used to convert JSON data
from a server to AutoBeans that can be displayed in a widget, the underlying
concepts may be easier to understand by considering a simple example that
converts JSON data into beans and retrieves the bean's properties:

public class MyTest implements EntryPoint {
// A Java String containing JSON data. We've simplified the source of
// the JSON data for the purposes of the example. Typically it would be
// the result of invoking a web service, using classes like RequestBuilder,
// HttpProxy and ListLoader. For a complete example, see
// JsonGridExample.
private static final String SAMPLE_JSON = "{ \"cityList\" : [ { \"name\": \"Tokyo\" , \"population\" : 32450000 }, { \"name\" : \"New York\" , \"population\" : 19750000 } ] }";
// A representation of the JSON root object.
// The JSON root object contains a list of City objects.
// The AutoBean framework creates a bean from this interface.
public interface JsonRootObject {
// Returns a list of the City beans contained in the JSON root object.
// List<City> matches the type of the root object in the JSON data.
// CityList matches the name of the "cityList" field in the JSON data.
List<City> getCityList();
}
// A representation of the name, population object in the JSON data.
// A list of these objects is contained in JsonRootObject.
// The AutoBean framework creates a bean from this interface.
public interface City {
// Returns the name of the city.
// String matches the type of the name field in the JSON data.
// Name matches the name of the "name" field in the JSON data.
String getName();
// Returns the population of the city.
// Integer matches the type of the population field in the JSON data.
// Population matches the name of the "population" field in the JSON data.
Integer getPopulation();
}
// The AutoBean framework provides a factory that can
// create AutoBeans, just by extending AutoBeanFactory.
public interface JsonRootObjectAutoBeanFactory extends AutoBeanFactory {
// Returns the JSON root object (a list of cities).
// JsonRootObject type parameter matches root JSON type.
// The method name is arbitrary (no corresponding name in the JSON data).
AutoBean<JsonRootObject> jsonRootObject();
}
// Creates a CityList from JSON data in one step.
// To convert from JSON data, extend a JsonReader and override
// createReturnData to return the desired type.
public class CityListReader extends JsonReader<ListLoadResult<City>, JsonRootObject> {
public CityListReader(AutoBeanFactory factory, Class<JsonRootObject> rootBeanType) {
super(factory, rootBeanType);
}
protected ListLoadResult<City> createReturnData(Object loadConfig, JsonRootObject incomingData) {
return new ListLoadResultBean<City>(incomingData.getCityList());
}
}
public void onModuleLoad() {
JsonRootObjectAutoBeanFactory factory = GWT.create(JsonRootObjectAutoBeanFactory.class);
CityListReader reader = new CityListReader(factory, JsonRootObject.class);
ListLoadResult<City> cities = reader.read(null, SAMPLE_JSON);
List<City> cityList = cities.getData();
for (City city : cityList) {
System.out.println("name=" + city.getName() + ", population=" + city.getPopulation());
}
}
}