Serialize Set of UUID using Jackson

不打扰是莪最后的温柔 提交于 2019-12-22 09:49:50

问题


I found that jackson comes equipped with a UUID serizlizer/deserializer that can be used like this:

@Data
@NoArgsConstructor
public class MyClass {

    @JsonSerialize(using=UUIDSerializer.class)
    @JsonDeserialize(using=UUDIDeserializer.class)
    private UUID myUUID;

}

And then using ObjectMapper on MyClass will correctly serialize/deserialize the myUUID field.

However, my class has a set of UUIDs that I want to serialize. I tried annotating the field the same way as above, but it complains that Set cannot be converted to UUID (as I half expected).

I know I can create my own serializer/deserializers by extending JsonSerializer/JsonDeserializer, but this feels hacky. Is there another solution I can use? I also don't have the option to configure the ObjectMapper with my classes, since I don't have access to the ObjectMapper. I am using Amazon SWF and it automatically uses Jackson.


回答1:


Jackson should automatically use UUID serializers, deserializers, so your annotations should not be necessary.

But as to annotation usage, as suggested, (de)serializer for content (instead of value itself!) does need to use contentUsing property of the annotation -- otherwise Jackson will try to apply given (de)serializer directly for the value, with reported mismatch,



来源:https://stackoverflow.com/questions/34300625/serialize-set-of-uuid-using-jackson

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!