Facing problem in JSON encoding and decoding using DirectJNgine
Hi,
We are evaluating DirectJNgine for server side stack for ExtJs 4.1 and we are not able to find answers of following queries in case of Json encoding decoding by using DJN:
- When we actually need to write our own Json encoding decoding code while we can easily use Ext.Data.DirectStore for directly sending client side data to server side and vice-a-versa.
- Is it necessary to write inner static class for serialization and deserialization for each of the complex object like UserDetail object in subclass of DefaultGsonBuilderConfigurator.java.
- Is it needed to write serialization-deserialization logic in inner static classes of the class that is subclass of the DefaultGsonBuilderConfigurator.java or we can write these logic in non static inner classes.
- Suppose we have written separate inner static classes for serialization and deserialization of two objects like Date and UserDetail and using these inner static classes in configure() method as follows:
Code:
builder.registerTypeAdapter( Date.class, new JsonDateSerializer() ); builder.registerTypeAdapter( UserDetail.class, new JsonUserDetailSerializer() );
And now in one of my request to server, I want to execute only date serilization and in other request I want to execute only UserDetail serialization. So, how it can be done? Should we have to use some if-else like conditions or DirectJNgine provides some built-in functionality to handle this type of situation?
But manual serialization - deserialization needed in case of complex object in DJN
On the basis of your statement as follows:
Quote:
You should *very* rarely need to write custom encoders/decoders, DJN takes care of everything 99% of the time.
I have tested serialization and deserialization between json and java with some complex json object, but I found that it was not done automatically and I have to write code by iterating json object and assigning corresponding values to java object. Followings are the codes:
Javascript code:
Code:
Ext.onReady(function(){
var aDate = {year: 2005, month: 3, day: 20};
var userJson = {name: 'Jitendra', age: 3, city: 'Delhi'};
var companies = [
{
"id": "17",
"name": "Emkay Entertainments",
"address": "Nobel House, Regent Centre",
"manager": {
"firstName": "John",
"lastName": "Doe"
},
"employees": [ {
"firstName": "Brian",
"lastName": "Hunt"
},
{
"firstName": "Mick",
"lastName": "Henning"
}
]
},
{
"id": "18",
"name": "The Empire",
"address": "Milton Keynes Leisure Plaza",
"manager": {
"firstName": "Ana",
"lastName": "Johnsnon"
},
"employees": [
{
"firstName": "Erick",
"lastName": "O'Neil"
},
{
"firstName": "George",
"lastName": "Halloway"
}
]
}
];
Ext.Direct.addProvider(
Ext.app.REMOTING_API
);
//testing with custom hierarchical json object for serialization and deserialization
CompanyAction.getCompanyDetail(companies, function(result, e) {
var t = e.getTransaction();
alert(result.name);
});
CompanyAction.test("jks",null, function(result, e) {
var t = e.getTransaction();
});
});
Now the json object is having a company list and each company has a manager object and a list of employee object. following are my java classes:
1) CompanyAction.java --- exposed java class
Code:
package company;
import java.util.ArrayList;
import com.softwarementors.extjs.djn.config.annotations.DirectMethod;
public class CompanyAction {
@DirectMethod
public ArrayList<Company> getCompanyDetail(ArrayList<Company> companies){
if(companies.size()>0)
System.out.println("company name = "+companies.get(0).getName());
return companies;
}
@DirectMethod
public String test(String a, String b){
return "tested";
}
}
2) Company.java
Code:
package company;
import java.util.ArrayList;
public class Company {
private String id;
private String name;
private String address;
private Manager manager;
private ArrayList<Employee> employees;
// code for some getters and setters
}
3) Employee.java
Code:
package company;
public class Employee {
private String firstName;
private String lastName;
//code for getters and setters
}
4) Manager.java
Code:
package company;
public class Manager {
private String firstName;
private String lastName;
//code for getters and setters
}
Now, I have read your document and also done a lot of googling, but I didn't find any type of configuration between Json object attributes and Java class attributes in DJN so that the values of Json object attributes can be directly assigned to corresponding java class attributes. How does the DJN control will automatically decide that the value of first name of Manager attribute of Json object will be assigned to the First Name of Manager Java Object but not to that of Employee Java Object. I have done this by manually writing serialization deserialization code, but in DWR it can be done automatically by just some configuration
Actually, this issue has become a bottleneck in our evaluation process and due to this some of our team members are suggesting DWR as alternate solution.
So, please if there is any configuration based approach or annotation based approch to solve this issue, then suggest me.