1

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.

Community
  • 1
  • 1
wberry
  • 18,519
  • 8
  • 53
  • 85
  • Facing same problem. Did you find any workaround (except creating custom deserializer)? – MD TAREQ HASSAN Mar 13 '19 at 09:20
  • This was long enough ago that I have no memory how it turned out. I was asking for a friend. If you figure it out you can earn Necromancer maybe. – wberry Mar 15 '19 at 13:00

0 Answers0