Result
- the desired return data to use from the DataReader
,
usually ListLoadResult
Base
- the base type of data to be read from the incoming datapublic class JsonReader<Result,Base> extends AbstractAutoBeanReader<Result,Base,String>
A DataReader
implementation that reads JSON data and builds it
into the given AutoBean
type, using other types from the given
factory.
Subclasses can override AbstractAutoBeanReader.createReturnData(Object, Object)
to control
what object is returned by the reader.
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());
}
}
}
See http://www.json.org for a brief (yet nearly complete) description of JSON.
Constructor and Description |
---|
JsonReader(AutoBeanFactory factory,
Class<Base> rootBeanType)
Creates a JsonReader capable of converting JSON data into one or more
AutoBeans.
|
Modifier and Type | Method and Description |
---|---|
protected Splittable |
readSplittable(Object loadConfig,
String data)
Implemented by subclasses to provide a
Splittable based on the
incoming data. |
createReturnData, read
public JsonReader(AutoBeanFactory factory, Class<Base> rootBeanType)
factory
- AutoBeanFactory instance capable of building all of the
required classesrootBeanType
- AutoBean based type to represent the root object in the
JSON dataprotected Splittable readSplittable(Object loadConfig, String data)
AbstractAutoBeanReader
Splittable
based on the
incoming data.readSplittable
in class AbstractAutoBeanReader<Result,Base,String>
loadConfig
- the load configdata
- the data to readCopyright © 2018. All rights reserved.