接上一篇
com.fasterxml.jackson.databind.JsonSerializer
、
com.fasterxml.jackson.databind.JsonDeserializer
是与 Converter 类似的功能,需要使用
JsonSerialize.using
、
JsonDeserialize.using
指定。
Example
Java object
public class CurrencyRate {
private String pair;
private double rate;
@JsonSerialize(using = LocalDateTimeSerializer.class)
@JsonDeserialize(using = LocalDatetimeDeserializer.class)
private LocalDateTime lastUpdated;
...
}
Serializer
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
public class LocalDateTimeSerializer extends JsonSerializer<LocalDateTime> {
static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
@Override
public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeString(value.format(DATE_FORMATTER));
}
}
Deserializer
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import java.io.IOException;
import java.time.LocalDateTime;
public class LocalDatetimeDeserializer extends JsonDeserializer<LocalDateTime> {
@Override
public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
return LocalDateTime.parse(p.getText(), LocalDateTimeSerializer.DATE_FORMATTER);
}
}
Main class
public class ExampleMain {
public static void main(String[] args) throws IOException {
System.out.println("-- Java object to JSON --");
CurrencyRate cr = new CurrencyRate();
cr.setPair("USD/JPY");
cr.setRate(109.15);
cr.setLastUpdated(LocalDateTime.now());
System.out.println("Java object: " + cr);
ObjectMapper om = new ObjectMapper();
String s2 = om.writeValueAsString(cr);
System.out.println("JSON string: " + s2);
System.out.println("-- JSON to Java object --");
CurrencyRate cr2 = om.readValue(s2, CurrencyRate.class);
System.out.println("Java Object: " + cr2);
}
}
-- Java object to JSON --
Java object: CurrencyRate{pair='USD/JPY', rate=109.15, lastUpdated=2021-04-16T20:49:07.472971200}
JSON string: {"pair":"USD/JPY","rate":109.15,"lastUpdated":"2021年4月16日 下午8:49:07"}
-- JSON to Java object --
Java Object: CurrencyRate{pair='USD/JPY', rate=109.15, lastUpdated=2021-04-16T20:49:07}
使用 ObjectMapper 注册 Serializer、Deserializer
public class CurrencyRate {
private String pair;
private double rate;
private LocalDateTime lastUpdated;
...
}
public class ExampleMain2 {
public static void main(String[] args) throws IOException {
System.out.println("-- Java object to JSON --");
CurrencyRate cr = new CurrencyRate();
cr.setPair("USD/JPY");
cr.setRate(109.15);
cr.setLastUpdated(LocalDateTime.now());
System.out.println("Java object: " + cr);
// 注册
ObjectMapper om = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer());
module.addDeserializer(LocalDateTime.class, new LocalDatetimeDeserializer());
om.registerModule(module);
String s2 = om.writeValueAsString(cr);
System.out.println("JSON string: " + s2);
System.out.println("-- JSON to Java object --");
CurrencyRate cr2 = om.readValue(s2, CurrencyRate.class);
System.out.println("Java Object: " + cr2);
}
}
-- Java object to JSON --
Java object: CurrencyRate{pair='USD/JPY', rate=109.15, lastUpdated=2021-04-16T21:19:09.140337700}
JSON string: {"pair":"USD/JPY","rate":109.15,"lastUpdated":"2021年4月16日 下午9:19:09"}
-- JSON to Java object --
Java Object: CurrencyRate{pair='USD/JPY', rate=109.15, lastUpdated=2021-04-16T21:19:09}
原文链接
-
Jackson JSON - Using @JsonSerialize and @JsonDeserialize with JsonSerializer and JsonDeserializer for custom conversion
-
Jackson JSON - Registering custom serializer and deserializer