I know I m recycling an old thread, but it s new to me :)
We use multiple levels for our POJOs for deserializing JSON (with Jackson). Here is a tiny example (made up) of JSON we might get back from a RESTful web service:
{ success: true, response: {
sales: { item: "123", sales: 3, returns: 1 },
inventory: { item: "567", qty: 100 }
}
}
We used to have POJOs set up like:
public class Json1 {
private boolean success;
private JsonResponse response;
}
public class Json1Response {
private JsonResponseSales sales;
private JsonResponseInventory inventory;
}
public class Json1ResponseSales {
private String item;
private int sales;
private int returned;
}
public class Json1ResponseInventory {
private String item;
private int qty;
}
We have lots of these, with a POJO for each web service request we might make. This layout gave us a few niggling doubts:
Notice that this one, relatively simple example gave us four class files to keep up with. Now multiply that by 100s and then a difficulty factor of 3 to account for the fact that most JSON is a lot messier than this. Thousands of files.
The field names are re-used all over the place, and the same field name might have different contents based on which web service. (Imagine that quantities might come back as strings from one web service, int from another, then multiply that by 100s.)
Since these things are tied together in a parent/child relationship, we decided to go with this layout instead.
public class Json1 {
private boolean success;
private JsonResponse response;
public class Json1Response {
private JsonResponseSales sales;
private JsonResponseInventory inventory;
public class Json1ResponseSales {
private String item;
private int sales;
private int returned;
}
public class Json1ResponseInventory {
private String item;
private int qty;
}
}
}
In this case, I m nested two deep, but it might be more. Maybe up to four deep.