乐闻世界logo
搜索文章和话题

How do I use a custom Serializer with Jackson?

1个答案

1

In Jackson, custom serialization handlers allow developers to control how objects are converted to JSON. This is achieved by implementing the JsonSerializer<T> interface and registering it with the ObjectMapper. Below, I will demonstrate how to implement and use a custom serialization handler through a specific example.

Step 1: Define a Custom JsonSerializer

Suppose we have an Event class containing a LocalDateTime field for a time value, and we want to output this field in a specific format.

First, define a class extending JsonSerializer<T>:

java
import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.ser.std.StdSerializer; import java.io.IOException; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class CustomLocalDateTimeSerializer extends StdSerializer<LocalDateTime> { private static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); public CustomLocalDateTimeSerializer() { this(null); } public CustomLocalDateTimeSerializer(Class<LocalDateTime> t) { super(t); } @Override public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider provider) throws IOException { gen.writeString(formatter.format(value)); } }

In this custom serializer, we define how to convert a LocalDateTime object into a formatted string.

Step 2: Apply the Custom Serializer Using the @JsonSerialize Annotation

Next, apply this custom serializer to the entity class:

java
import com.fasterxml.jackson.databind.annotation.JsonSerialize; import java.time.LocalDateTime; public class Event { private String name; @JsonSerialize(using = CustomLocalDateTimeSerializer.class) private LocalDateTime eventTime; // Constructors, Getters, and Setters public Event(String name, LocalDateTime eventTime) { this.name = name; this.eventTime = eventTime; } public String getName() { return name; } public void setName(String name) { this.name = name; } public LocalDateTime getEventTime() { return eventTime; } public void setEventTime(LocalDateTime eventTime) { this.eventTime = eventTime; } }

Step 3: Serialize Objects

Finally, create an Event object and serialize it using ObjectMapper:

java
import com.fasterxml.jackson.databind.ObjectMapper; public class Main { public static void main(String[] args) { try { ObjectMapper mapper = new ObjectMapper(); Event event = new Event("Conference", LocalDateTime.now()); String result = mapper.writeValueAsString(event); System.out.println(result); // Output: {"name":"Conference","eventTime":"2021-04-12 15:23:01"} } catch (Exception e) { e.printStackTrace(); } } }

In this example, the LocalDateTime field eventTime uses the CustomLocalDateTimeSerializer to serialize, outputting the date and time in the custom format as JSON. This enables flexible control over the serialization of specific fields.

2024年6月29日 12:07 回复

你的答案