I have a class, call it ClassOne
, and it has a few fields with getters/setters, one of which is an object of another class, ClassTwo
. I'm trying to use FlexJSON to serialize out the ClassOne
object, like so:
public class ClassOne{
private String name;
private Long id;
private ClassTwo classTwo;
//Getters+Setters omitted for brevity
}
public class ClassTwo{
private String description;
//Getter+Setter again omitted
}
//Elsewhere
List<ClassOne> list = /*get a list of ClassOne objects*/;
String json = new JSONSerializer().include("name", "id", "classTwo.description").exclude("*").serialize(list);
My problem is, the JSON that results looks something like this:
[{"id":"1","name":"aName","classTwo":{"description":"aDescription"}},{"id":"2","name":"anotherName","classTwo":{"description":"anotherDescription"}}]
The classTwo.description
part is being put into its own separate object in the JSON. But ClassTwo is essentially just an implementation detail; I want the results flattened out like this:
[{"id":"1","name":"aName","description":"aDescription"},{"id":"2","name":"anotherName","description":"anotherDescription"}]
I'd also be fine if the "description" part read "classTwo.description", as long as it's flattened into the classOne
objects' representation.
Is there a way to do this with FlexJSON? The Transformer stuff looks like it should be able to, but I'm too new to the library to be certain, or to figure out how.