Skip to content

Jackson XML doesn't use my Serializer - json works #2327

@benneq

Description

@benneq

I've written a small Serializer for MongoDB's ObjectId:

public class ObjectIdSerializer extends StdSerializer<ObjectId> {
    public static final ObjectIdSerializer INSTANCE = new ObjectIdSerializer();
    private ObjectIdSerializer() { super(ObjectId.class); }
    @Override public void serialize(ObjectId value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException {
        jgen.writeString(value.toHexString());
    }
}

And a SimpleModule:

@Component
public class CommonJacksonModule extends SimpleModule {
    public CommonJacksonModule() {
        super(new Version(1, 0, 0, null, null, null));
        addSerializer(ObjectId.class, ObjectIdSerializer.INSTANCE);
    }
}

When using Accept=application/json it works as expected and I get something like "5498374f44ae6f110dd0abcd". But using Accept=application/xml I get something like:

<new>false</new>
<timeSecond>1419261775</timeSecond>
<inc>231792298</inc>
<machine>1152282385</machine>
<time>1419261775000</time>
<date>2014-12-22T15:22:55.000+0000</date>
<timestamp>1419261775</timestamp>

What's the problem here?

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions