Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serialize Set of UUID using Jackson

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.

like image 459
Brian Schlenker Avatar asked Dec 15 '15 22:12

Brian Schlenker


People also ask

Does Jackson use serializable?

Introduction of ObjectMapper Class jackson. databind package and can serialize and deserialize two types of objects: Plain Old Java Objects (POJOs)

Does Jackson serialize getters?

Jackson by default uses the getters for serializing and setters for deserializing.

What is Jackson object serialization?

Jackson is a solid and mature JSON serialization/deserialization library for Java. The ObjectMapper API provides a straightforward way to parse and generate JSON response objects with a lot of flexibility. This article discussed the main features that make the library so popular.


1 Answers

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,

like image 98
StaxMan Avatar answered Oct 12 '22 18:10

StaxMan