Suppose I have the following three classes (getters and setters left out for brevity):
@JsonAutoDetect
public class InfoCollection{
private InfoType1 info1;
private InfoType2 info2;
}
@JsonAutoDetect
public class InfoType1{
private String fieldA;
}
@JsonAutoDetect
public class InfoType2{
private String fieldB;
}
I"m trying to write a JsonSerializer.serialize()
function that serializes an InfoCollection
object in this format:
{
"allInfo":{
"fieldA":"foo",
"fieldB":"bar"
}
}
This is what I have now:
jsonGenerator.writeStartObject();
jsonGenerator.writeFieldName("allInfo");
jsonGenerator.writeObject(myInfoCollection.getInfo1());
jsonGenerator.writeObject(myInfoCollection.getInfo2());
jsonGenerator.writeEndObject();
which is causing the following exception:
org.codehaus.jackson.JsonGenerationException: Can not start an object, expecting field name
Am I missing something small or am I totally going about this the wrong way?
NOTE: A couple of the proposed solutions so far involve writing each individual field of InfoType1
and InfoType2
. I am looking for a solution that does not require this because I'd like to use the solution on huge classes with many fields.