1

I have the following classes:

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class User {

    private String id;
    private List<Reference> references;
.....
}
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Reference {

    @JacksonXmlProperty(isAttribute = true)
    private String ref;

    public Reference(final String ref) {
        this.ref = ref;
    }

    public Reference() { }

    public String getRef() {
        return ref;
    }

}

When serializing to XML the format is as expected, but when I try to serialize to JSON I get the following

"users" : [
  {
      "references" : [
      {
        "ref": "referenceID"
      }
    ]
  }
]

And I need it to be:

"users" : [
  {
      "references" : [
        "referenceID"
    ]
  }
]

the braces enclosing the reference list I need it to be ignored without the attribute name

zepol
  • 187
  • 5
  • 20

1 Answers1

2

You can annotate the ref field in your Reference class with the JsonValue annotation that indicates that the value of annotated accessor is to be used as the single value to serialize for the instance:

@Data
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Reference {

    @JacksonXmlProperty(isAttribute = true)
    @JsonValue //<-- the new annotation
    private String ref;

    public Reference(final String ref) {
        this.ref = ref;
    }

    public Reference() { }

    public String getRef() {
        return ref;
    }

}

User user = new User();
user.setReferences(List.of(new Reference("referenceID")));
//it prints {"references":["referenceID"]}
System.out.println(jsonMapper.writeValueAsString(user));

Edit: it seems that the JsonValue annotation invalidates the serialization of the class as expected by the OP; to solve this problem one way is the use of a mixin class for the Reference class and putting inside the JsonValue annotation, the original Reference class will be untouched:

@Data
public class MixInReference {
    @JsonValue
    private String ref;
}


ObjectMapper jsonMapper = new ObjectMapper();
//Reference class is still the original class
jsonMapper.addMixIn(Reference.class, MixInReference.class);
////it prints {"references":["referenceID"]}
System.out.println(jsonMapper.writeValueAsString(user));
dariosicily
  • 4,239
  • 2
  • 11
  • 17
  • It worked, but I had to add it to the get method , now I am having an issue with the XML parser because the schema definition says it should have both and so the validation is failing because the reference value is not an attribute – zepol Jan 31 '22 at 16:39
  • I'm not sure about your issue, there is a xml schema and the validation is failing because of the json annotation ? – dariosicily Jan 31 '22 at 17:27
  • I need to be able to serialize to both JSON and XML using the same objects, with XML I was working as expected each reference has the ref value as an attribute e.g. ``, now after adding the @JsonValue it works with the JSON serialization but not with the XML, the `ref` is not an attribute and it's serialized like `value` so it fails because it's expecting the attribute – zepol Jan 31 '22 at 17:59
  • 1
    @zepol I understood now, I updated my code to solve the xml serialization problem. – dariosicily Jan 31 '22 at 20:10