PDA

View Full Version : How to define "fields" mapping in JsonStore or DirectStore to get nested Json



tungchau
2 Mar 2010, 3:16 PM
Hi,
How do I define the "fields" mapping in JsonStore or DirectStore in order to get a nested Json data as the following:


[
{
"id": 11,
"name": "CFG1",
"groups": null,
"reportDefinitions": [
{
"id": 1,
"name": "ReportDef1",
"isFavorite": false,
"lastUsed": "2008-04-18T10:49:20Z"
},
{
"id": 2,
"name": "ReportDef2",
"isFavorite": false,
"lastUsed": "2008-04-18T10:49:20Z"
},
{
"id": 3,
"name": "ReportDef3",
"isFavorite": false,
"lastUsed": "2008-04-18T10:49:20Z"
}
]
},
{
"id": 12,
"name": "CFG2",
"groups": null,
"reportDefinitions": [
{
"id": 1,
"name": "ReportDef1",
"isFavorite": false,
"lastUsed": "2008-04-18T10:49:20Z"
},
{
"id": 2,
"name": "ReportDef2",
"isFavorite": false,
"lastUsed": "2008-04-18T10:49:20Z"
},
{
"id": 3,
"name": "ReportDef3",
"isFavorite": false,
"lastUsed": "2008-04-18T10:49:20Z"
}
]
},
{
"id": 13,
"name": "CFG3",
"groups": null,
"reportDefinitions": [
{
"id": 1,
"name": "ReportDef1",
"isFavorite": false,
"lastUsed": "2008-04-18T10:49:20Z"
},
{
"id": 2,
"name": "ReportDef2",
"isFavorite": false,
"lastUsed": "2008-04-18T10:49:20Z"
},
{
"id": 3,
"name": "ReportDef3",
"isFavorite": false,
"lastUsed": "2008-04-18T10:49:20Z"
}
]
}
]


The reason I need nested Json because my Java classes have the following structure:


public class ReportGroup implements IReportGroup{
private long id;
private String name;
private List<IReportGroup> groups;
private List<IReportDefinition> reportDefinitions;
}

public class ReportDefinition implements IReportDefinition{
private long id;
private String name;
private boolean isFavorite;
private Date lastUsed;
}


I defined my Store as the following:


var myStore = new Ext.data.DirectStore({
paramsAsHash:false,
paramOrder: ['dashboardType'],
directFn: TestServices.loadReportGroups,
idProperty:'id',
fields: ['id', 'name', 'groups', {
name: 'reportDefinitions', type: 'array'
},{
name: 'id', mapping: 'reportDefinitions.id'
},{
name: 'name', mapping: 'reportDefinitions.name'
},{
name: 'isFavorite', mapping: 'reportDefinitions.isFavorite', type: 'boolean'
},{
name: 'lastUsed', mapping: 'reportDefinitions.lastUsed', type: 'date', dateFormat: 'Y-m-d'
}]
});

var call = new Ext.Button({
renderTo: Ext.getBody(),
text: 'Click Me',
handler: function(){
myStore.load({
params: {
dashboardType: "Overview"
}
});
}
}


I am using DirectJNGine (a Java based implementation of Ext Direct API) at http://code.google.com/p/directjngine/ and the JSON reponse I got is the following:


2010-03-02 15:36:32,719 [http-8080-exec-46] DEBUG com.softwarementors.extjs.djn.router.processor.standard.json.JsonRequestProcessor - ResponseData data (JSON{
"tid": 2,
"action": "TestServices",
"method": "loadReportGroups",
"result": [
{
"id": 11,
"name": "CFG1",
"groups": null,
"reportDefinitions": [
{},
{},
{},
{},
{},
{},
{},
{}
]
},
{
"id": 12,
"name": "CFG2",
"groups": null,
"reportDefinitions": [
{},
{},
{},
{},
{},
{},
{},
{}
]
},
{
"id": 13,
"name": "CFG3",
"groups": null,
"reportDefinitions": [
{},
{},
{},
{},
{},
{},
{},
{}
]
}
],
"type": "rpc"
}


I have been reading a lot of posts about nested Json in the forums (including http://www.extjs.com/learn/Ext_FAQ_Grid#nested_JSON) and still could not find an answer. Any advice is greatly appreciated.
Regards,
Tung Chau

tungchau
2 Mar 2010, 5:26 PM
Nevermind. I found out that DirectJNGine returned a wrong JSON because I have my Java data classes implement interfaces. For example, I have ReportGroup implementing IReportGroup and ReportDefinition implementing IReportDefinition and the following:


private List<IReportGroup> groups;
private List<IReportDefinition> reportDefinitions;


It seems like DirectJNGine does not support Interface. It is not actually a DirectJNGine's problem. We need to customize the GSON to generate correct json.