Reading JSON into BeanModel instead of DataModel
I have a Grid based on a HttpProxy that access a web service returning JSON.
I have a Java bean implementing the BeanModelTag that matches the property of the returned JSON.
I need to display some JSON properties on the Grid and access all JSON properties when I click on a row of the Grid.
The ugly way I found out is not to use my Java bean and instead
Ideally I would have a HttpProxy and a DataReader that would read my JSON into a BeanModel with the underlying Java Bean, so I can always access my Java bean when I need to access some properties.
- use a JsonLoadResultReader
- and defined a custom ModelType, adding all the JSON properties (which i would have in my Java bean already)
- then load ModelData into my ListStore
- when I 'm reading this ModelData on e.g. a SelectionEvent, I need to do a model.get(<json property string>)
Is there any easy way to do that?
Or if I would need to write my own DataReader, which JSON unmarshaller library would be suited for this job?
A GXT 2 BeanModel is really just a ModelData, with some additional wiring to make it be able to read from other Java Beans. If you are starting with JSON, it makes no difference if the properties are read into a BeanModel or a ModelData - its the same thing. Why do you need to put the properties from the JSON data into both a java bean and a BeanModel? You'll have two copies of your data, and it'll take longer this way to read in the objects. What do you gain from this?
In GXT 3, we support GWT's AutoBeans, which allow you to write simple beans that map to the underlying JSON data. These look and act like normal Java Beans, but you don't need to write the implementation, just define the getters and setters. This is the default way of reading JSON (and XML) data in GXT 3. If you are starting a new project, I'd suggest starting with GXT 3.
I have the bean that is used on the server to generate the JSON, the bean is part of my domain model and used in various places. I expose a common interface that serializes it as a JSON.
On the client that maintains the Grid, I'm using a JsonLoadResultReader, which forces me to configure a ModelType, so I need to keep a list of field names (constants naming the member names of my bean). I need to use these constants also to read fields from my ModelData (e.g. when I pass a ModelData instance via a SelectionChangedEvent on my Grid).
Instead of having to maintain this additional list of field names, it would be much cleaner and less error-prone if I could just read the JSON into my already existing bean and access the members directly.
So, ideally I would pass my bean instead of a ModelType to the JsonLoadResultReader and then get a list of BeanModel wrapping my bean instead of a list of ModelData.
Since there are BeanModelFactories already that generating BeanModels out of beans, it thought there must be a way to read JSON into a BeanModel instead of a DataModel.
I hope this clarifies my use case.
At the moment I have to stick to GXT 2.X, and I'm not sure whether Autobeans would be the solution. (Can a Autobean be used in a ListLoader for a Grid or does it come with some BeanModel?)
Tags for this Thread