0

I am implementing a REST server using Spring Boot, JAXB2 and Jackson. This server supports JSON and XML and it is based on official specifications.

I am currently having a serialization problem specific to the JSON format and I do not know how it can be solved?

The specification defines several primitive types as string, integer, etc. that can be extended and depending of this extension, the result of the serialization is not the same.

The Java classes

public class PrimitiveType {

    @XmlAttribute
    private String id
    @XmlElement(name = "extension")
    private List<Extension> extensions = new ArrayList<>();

    // Getters and Setters
}

public class StringType extends PrimitiveType {

    @XmlAttribute
    private String value;

    // Getter and Setter
}

JSON

// Without id and/or extension(s)
"code" : "abc"

// With id and/or extension(s)
"code ": "abc",
"_code": {
    "id": "1",
    "extension" : [ {
        "url" : "http://mydomain/ext/1",
        "valueString" : "abc-1"
    }]
}

I do not have any problem with the XML but it is not the same for the JSON. I do not know how I can add a property dynamically on the same level. I had a look on the JsonSerializer but it seems that it allows to change the serialization on the object itself.

Does anyone had a chance to do this kind of things before?

sylmandel
  • 51
  • 5
  • can you please post, what shall be correct json? – Optional Oct 09 '17 at 09:08
  • Can you tell us what JSON output you are expecting? Cause I don't understand the problem. – Matt Oct 09 '17 at 09:08
  • @Matt I have updated my post, remove the XML and keep only the two expected JSON outputs. Here is an extract of the specifications about the JSON representation : 'Primitive types are represented differently; the actual primitive value appears as a JSON string or number property. If an internal id or extensions are present, they appear in a JSON object with the name of the primitive value property with "_" prepended.' [link](https://www.hl7.org/fhir/json.html#primitive) – sylmandel Oct 10 '17 at 08:31

0 Answers0