I'm trying to serialze a Map using Jackson 2, the default ObjectMapper() gives something like:
{"{http://google.com/}myName":"myValue"}
It probably uses QName.toString() method to serialize the qname key.
I tried following to register a custom serializer for QName, but it is not getting invoked.
public static class QnameSerializer extends JsonSerializer<QName> {
@Override
public void serialize(QName value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException {
jgen.writeString(value.getLocalPart());
}
}
public static void main(String[] args) throws JsonProcessingException {
SimpleModule module = new SimpleModule();
module.addKeySerializer(QName.class, new QnameSerializer());
// tried module.addSerializer(QName.class, new QnameSerializer()) also
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(module);
Map<QName, String> m =ImmutableMap.of(new QName("http://google.com/","myName","pre"), "myValue");
System.out.println(mapper.writeValueAsString(m));
}
Am I missing something?
--
You received this message because you are subscribed to the Google Groups "jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jackson-user...@googlegroups.com.
To post to this group, send email to jackso...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.