com.sencha.gxt.data.shared.loader
Class JsonReader<Result,Base>

java.lang.Object
  extended by com.sencha.gxt.data.shared.loader.AbstractAutoBeanReader<Result,Base,java.lang.String>
      extended by com.sencha.gxt.data.shared.loader.JsonReader<Result,Base>
Type Parameters:
Result - the desired return data to use from the DataReader, usually ListLoadResult
Base - the base type of data to be read from the incoming data
All Implemented Interfaces:
DataReader<Result,java.lang.String>

public class JsonReader<Result,Base>
extends AbstractAutoBeanReader<Result,Base,java.lang.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 Summary
JsonReader(AutoBeanFactory factory, java.lang.Class<Base> rootBeanType)
          Creates a JsonReader capable of converting JSON data into one or more AutoBeans.
 
Method Summary
 
Methods inherited from class com.sencha.gxt.data.shared.loader.AbstractAutoBeanReader
read
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JsonReader

public JsonReader(AutoBeanFactory factory,
                  java.lang.Class<Base> rootBeanType)
Creates a JsonReader capable of converting JSON data into one or more AutoBeans.

Parameters:
factory - AutoBeanFactory instance capable of building all of the required classes
rootBeanType - AutoBean based type to represent the root object in the JSON data


Copyright © 2012. All Rights Reserved.