Jackson polymorphic serialization generates an incorrect class name
Asked Answered
V

2

8

When I use Jackson polymorphic serialization, it generates a JSON object with an incorrect fully qualified class name.

The code below serializes an XMLGregorianCalendar. The output is:

["java.util.GregorianCalendar",-3600000]

I expected the following:

["javax.xml.datatype.XMLGregorianCalendar",-3600000]

Why does it output java.util.GregorianCalendar?

Or more importantly: How do I fix this?

Code example:

import com.fasterxml.jackson.databind.ObjectMapper;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import java.io.ByteArrayOutputStream;

public class JacksonGregorianProblem {

    public static void main(String[] args) throws java.io.IOException, DatatypeConfigurationException {

        XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar();
        ObjectMapper mapper = new ObjectMapper();
        mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        mapper.writeValue(byteArrayOutputStream, xmlGregorianCalendar);

        System.out.println(byteArrayOutputStream);
    }
}
Vondavonni answered 13/3, 2018 at 14:17 Comment(1)
After checking the source code of Jackson I understand why it shows the unexpected behavior. In the com.fasterxml.jackson.databind.ext.XMLGregorianCalendarSerializer class the XMLGregorianCalendar is cast to a GregorianCalendar. github.com/FasterXML/jackson-databind/blob/… So this explains why the unexpected behavior occurs. – Vondavonni
V
7

To get the expected behavior I have implemented a custom XMLGregorianCalendar serializer. This class takes care of the serialization of the XLMGregorianCalendar and now the output is exactly what I expect. :-)

class XMLGregorianCalendarSerializer extends StdSerializer<XMLGregorianCalendar> {

    public XMLGregorianCalendarSerializer() {
        this(null);
    }

    public XMLGregorianCalendarSerializer(Class<XMLGregorianCalendar> t) {
        super(t);
    }

    @Override
    public void serialize(XMLGregorianCalendar value, JsonGenerator gen, SerializerProvider provider)
        throws IOException
    {
        gen.writeNumber(value.toGregorianCalendar().getTimeInMillis());
    }

    @Override
    public void serializeWithType(XMLGregorianCalendar value, JsonGenerator gen, SerializerProvider provider,
        TypeSerializer typeSerializer) throws IOException
    {
        gen.writeStartArray();
        gen.writeString("javax.xml.datatype.XMLGregorianCalendar");
        serialize(value, gen, provider); // call your customized serialize method
        gen.writeEndArray();
    }
}

You can add this serializer to the object mapper with the code below. It can be pasted in the code example in the question.

    SimpleModule module = new SimpleModule();
    module.addSerializer(XMLGregorianCalendar.class, new XMLGregorianCalendarSerializer());
    mapper.registerModule(module);
Vondavonni answered 16/3, 2018 at 7:56 Comment(6)
Hi! It's work, yes, but it is a hack Maybe you know a better answer? – Exculpate
Hi aarexer, I do not know of a better solution. Furthermore I do not see this as a hack. In my opinion, this is a proper solution. 😎 – Vondavonni
Why i think it's a hack - cause in every project which i want use XMLGregorianCalendar i need copy this code and register my own simple module. And it's not convenient as for me – Exculpate
A related problem is discussed here: github.com/FasterXML/jackson-databind/issues/1791 And this one is tagged for version 2.11. So maybe it will be fixed in 2.11. – Vondavonni
seems it will be fixed in 2.11, yes Thanx – Exculpate
#1791 is a different issue. This issue is fixed in 2.13: github.com/FasterXML/jackson-databind/issues/3217 – Selfconscious
F
0

XMLGregorian calendar suppose to handle serialization and deserialization alone. Therefore I prefer to use the following serializer and deserializer:

private class XMLCalendarDeserializer extends StdDeserializer<XMLGregorianCalendar> {

        private DatatypeFactory factory = DatatypeFactory.newDefaultInstance();
        
        public XMLCalendarDeserializer() {
            super(XMLGregorianCalendar.class);
        }

        
        
        @Override
        public XMLGregorianCalendar deserialize(JsonParser parser, DeserializationContext ctx) throws IOException, JsonProcessingException {
            if (parser.hasToken(JsonToken.VALUE_STRING)) {
                return factory.newXMLGregorianCalendar(parser.getText());
            } else {
                throw new JsonParseException(parser, "not string token");
            }
        }
        
    }
    
    private class XMLCalendarSerializer extends StdSerializer<XMLGregorianCalendar> {

        public XMLCalendarSerializer() {
            super(XMLGregorianCalendar.class);
        }
        
        @Override
        public void serialize(XMLGregorianCalendar val, JsonGenerator gen, SerializerProvider ser) throws IOException {
            gen.writeString(val.toXMLFormat());
        }
        
    }

`

Funnyman answered 22/8, 2021 at 21:9 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.