From d848933f207e9c602783e5fde23766e05dd5635d Mon Sep 17 00:00:00 2001 From: DOHA Date: Mon, 25 Jul 2016 13:54:02 +0200 Subject: [PATCH] using StdSerializer --- .../jackson/bidirection/CustomListSerializer.java | 13 +++++++++++-- .../jackson/date/CustomDateSerializer.java | 13 +++++++++++-- .../jackson/date/CustomDateTimeSerializer.java | 14 ++++++++++++-- .../date/CustomLocalDateTimeSerializer.java | 14 ++++++++++++-- .../jackson/dtos/withEnum/TypeSerializer.java | 14 ++++++++++++-- .../jackson/serialization/ItemSerializer.java | 15 ++++++++++++--- .../serialization/ItemSerializerOnClass.java | 15 ++++++++++++--- .../serialization/MyDtoNullKeySerializer.java | 14 ++++++++++++-- 8 files changed, 94 insertions(+), 18 deletions(-) diff --git a/jackson/src/test/java/com/baeldung/jackson/bidirection/CustomListSerializer.java b/jackson/src/test/java/com/baeldung/jackson/bidirection/CustomListSerializer.java index 1d8ca011ea..75e0a4ecb7 100644 --- a/jackson/src/test/java/com/baeldung/jackson/bidirection/CustomListSerializer.java +++ b/jackson/src/test/java/com/baeldung/jackson/bidirection/CustomListSerializer.java @@ -6,11 +6,20 @@ import java.util.List; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.ser.std.StdSerializer; -public class CustomListSerializer extends JsonSerializer> { +public class CustomListSerializer extends StdSerializer> { + private static final long serialVersionUID = 3698763098000900856L; + + public CustomListSerializer() { + this(null); + } + + public CustomListSerializer(final Class> t) { + super(t); + } @Override public void serialize(final List items, final JsonGenerator generator, final SerializerProvider provider) throws IOException, JsonProcessingException { final List ids = new ArrayList(); diff --git a/jackson/src/test/java/com/baeldung/jackson/date/CustomDateSerializer.java b/jackson/src/test/java/com/baeldung/jackson/date/CustomDateSerializer.java index 8d435b7b69..d840e1940f 100644 --- a/jackson/src/test/java/com/baeldung/jackson/date/CustomDateSerializer.java +++ b/jackson/src/test/java/com/baeldung/jackson/date/CustomDateSerializer.java @@ -6,13 +6,22 @@ import java.util.Date; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.ser.std.StdSerializer; -public class CustomDateSerializer extends JsonSerializer { +public class CustomDateSerializer extends StdSerializer { + private static final long serialVersionUID = -2894356342227378312L; private SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss"); + public CustomDateSerializer() { + this(null); + } + + public CustomDateSerializer(final Class t) { + super(t); + } + @Override public void serialize(final Date value, final JsonGenerator gen, final SerializerProvider arg2) throws IOException, JsonProcessingException { gen.writeString(formatter.format(value)); diff --git a/jackson/src/test/java/com/baeldung/jackson/date/CustomDateTimeSerializer.java b/jackson/src/test/java/com/baeldung/jackson/date/CustomDateTimeSerializer.java index 88c069419b..ab4f4bbec7 100644 --- a/jackson/src/test/java/com/baeldung/jackson/date/CustomDateTimeSerializer.java +++ b/jackson/src/test/java/com/baeldung/jackson/date/CustomDateTimeSerializer.java @@ -8,10 +8,20 @@ import org.joda.time.format.DateTimeFormatter; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.ser.std.StdSerializer; -public class CustomDateTimeSerializer extends JsonSerializer { +public class CustomDateTimeSerializer extends StdSerializer { + + private static final long serialVersionUID = -3927232057990121460L; + + public CustomDateTimeSerializer() { + this(null); + } + + public CustomDateTimeSerializer(final Class t) { + super(t); + } private static DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm"); diff --git a/jackson/src/test/java/com/baeldung/jackson/date/CustomLocalDateTimeSerializer.java b/jackson/src/test/java/com/baeldung/jackson/date/CustomLocalDateTimeSerializer.java index 3f8f5e098e..dbcf42488e 100644 --- a/jackson/src/test/java/com/baeldung/jackson/date/CustomLocalDateTimeSerializer.java +++ b/jackson/src/test/java/com/baeldung/jackson/date/CustomLocalDateTimeSerializer.java @@ -6,13 +6,23 @@ import java.time.format.DateTimeFormatter; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.ser.std.StdSerializer; -public class CustomLocalDateTimeSerializer extends JsonSerializer { +public class CustomLocalDateTimeSerializer extends StdSerializer { + + private static final long serialVersionUID = -7449444168934819290L; private static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"); + public CustomLocalDateTimeSerializer() { + this(null); + } + + public CustomLocalDateTimeSerializer(final Class t) { + super(t); + } + @Override public void serialize(final LocalDateTime value, final JsonGenerator gen, final SerializerProvider arg2) throws IOException, JsonProcessingException { gen.writeString(formatter.format(value)); diff --git a/jackson/src/test/java/com/baeldung/jackson/dtos/withEnum/TypeSerializer.java b/jackson/src/test/java/com/baeldung/jackson/dtos/withEnum/TypeSerializer.java index c5d5d7e0a8..fc5011137c 100644 --- a/jackson/src/test/java/com/baeldung/jackson/dtos/withEnum/TypeSerializer.java +++ b/jackson/src/test/java/com/baeldung/jackson/dtos/withEnum/TypeSerializer.java @@ -4,10 +4,20 @@ import java.io.IOException; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.ser.std.StdSerializer; -public class TypeSerializer extends JsonSerializer { +public class TypeSerializer extends StdSerializer { + + private static final long serialVersionUID = -7650668914169390772L; + + public TypeSerializer() { + this(null); + } + + public TypeSerializer(final Class t) { + super(t); + } @Override public void serialize(final TypeEnumWithCustomSerializer value, final JsonGenerator generator, final SerializerProvider provider) throws IOException, JsonProcessingException { diff --git a/jackson/src/test/java/com/baeldung/jackson/serialization/ItemSerializer.java b/jackson/src/test/java/com/baeldung/jackson/serialization/ItemSerializer.java index cb93f9cb03..b5624c566a 100644 --- a/jackson/src/test/java/com/baeldung/jackson/serialization/ItemSerializer.java +++ b/jackson/src/test/java/com/baeldung/jackson/serialization/ItemSerializer.java @@ -3,13 +3,22 @@ package com.baeldung.jackson.serialization; import java.io.IOException; import com.baeldung.jackson.dtos.Item; - import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.ser.std.StdSerializer; -public class ItemSerializer extends JsonSerializer { +public class ItemSerializer extends StdSerializer { + + private static final long serialVersionUID = 6739170890621978901L; + + public ItemSerializer() { + this(null); + } + + public ItemSerializer(final Class t) { + super(t); + } @Override public final void serialize(final Item value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException, JsonProcessingException { diff --git a/jackson/src/test/java/com/baeldung/jackson/serialization/ItemSerializerOnClass.java b/jackson/src/test/java/com/baeldung/jackson/serialization/ItemSerializerOnClass.java index 79b450d7f1..1fdf44e17c 100644 --- a/jackson/src/test/java/com/baeldung/jackson/serialization/ItemSerializerOnClass.java +++ b/jackson/src/test/java/com/baeldung/jackson/serialization/ItemSerializerOnClass.java @@ -3,13 +3,22 @@ package com.baeldung.jackson.serialization; import java.io.IOException; import com.baeldung.jackson.dtos.ItemWithSerializer; - import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.ser.std.StdSerializer; -public class ItemSerializerOnClass extends JsonSerializer { +public class ItemSerializerOnClass extends StdSerializer { + + private static final long serialVersionUID = -1760959597313610409L; + + public ItemSerializerOnClass() { + this(null); + } + + public ItemSerializerOnClass(final Class t) { + super(t); + } @Override public final void serialize(final ItemWithSerializer value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException, JsonProcessingException { diff --git a/jackson/src/test/java/com/baeldung/jackson/serialization/MyDtoNullKeySerializer.java b/jackson/src/test/java/com/baeldung/jackson/serialization/MyDtoNullKeySerializer.java index e915378498..d0b2d7f5e9 100644 --- a/jackson/src/test/java/com/baeldung/jackson/serialization/MyDtoNullKeySerializer.java +++ b/jackson/src/test/java/com/baeldung/jackson/serialization/MyDtoNullKeySerializer.java @@ -4,10 +4,20 @@ import java.io.IOException; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.ser.std.StdSerializer; -public class MyDtoNullKeySerializer extends JsonSerializer { +public class MyDtoNullKeySerializer extends StdSerializer { + + private static final long serialVersionUID = -4478531309177369056L; + + public MyDtoNullKeySerializer() { + this(null); + } + + public MyDtoNullKeySerializer(final Class t) { + super(t); + } @Override public void serialize(final Object value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException, JsonProcessingException {