I have a web service client that needs to send json data in an HTTP POST. I have a need to provide an empty json object in one of the fields. I cannot omit the field, it must be an object, and I should not supply any fields inside this object because that would change the result. Only an empty object will do.
Can this be done in jackson solely using annotations? If there is any serialization or mapping configuration, I need that to apply only to this class. I'm hoping for a magic option to JsonInclude
or JsonSerialize
.
Desired serialization output:
{
"field1": "value1",
"field2": "value2",
"field3": {}
}
This is pretty close to my Java class:
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class BeanClass implements Serializable {
private static final long serialVersionUID = 1L;
@JsonProperty("field1")
private String field1;
@JsonProperty("field2")
private String field2;
@JsonProperty("field3")
private EmptyBean field3;
}
And the EmptyBean
class pretty much looks like this:
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class EmptyBean implements Serializable {
private static final long serialVersionUID = 1L;
}
A way to turn off the FAIL_ON_EMPTY_BEAN
serialization option with an annotation would get this done for me. This answer looks promising but focuses on configuration which looks like it would apply to my whole application (and I don't want that).
I am hoping to solve this solely through annotations if possible. But as long as I have a way to change the mapping only for this class, I'll be happy.