I'm developing an Spring MVC application that uses Jackson for JSON views.
Suppose that I have two classes like following.
class A {
String prop;
B other;
// getters setters
}
class B {
Integer id;
String name;
}
What i'm trying to resolve is to get a JSON string like following.
for a fully initialized instance of A
{prop:"something", name:"otherthing"} // in here the class B
// is replaced with its propery name
insted of
{prop:"something, other:{id:5,name:"otherthing"}}
NOTE: the two classes mentioned above are Entity classes for Hibernate.
I have used @JsonIgnore to eliminate some properties from JSON output, Is it posible to
replace a sub-class with its property using annotation?
This is not yet possible except by using custom serializer, although there is a feature request ( http://jira.codehaus.org/browse/JACKSON-132 ) that might make it in Jackson 1.9 (as it has relatively high vote count, meaning a few users would really like to see it implemented).
If you're able to use a library other than Jackson for your JSON serialization, then you can use GSON (http://code.google.com/p/google-gson/) with a custom GsonBuilder object, which may get you close to what you're trying to acheive.
If you check out this blog post it shows how to customize the GSON serializer in a few different ways. I'm using it with a custom ExclusionStrategy to correctly translate my model objects (which are full of circular references) to JSON for a RESTful webservice using Spring MVC. (Note that I am only using it to serialize, not to deserialize.)
I've got my GsonBuilder's set up inside different View beans, which you can see here. If that looks like it might help you out and you have any questions, let me know.