From 9abb6ff1f0ce22726ecdf0c763ac5cbddcc53e78 Mon Sep 17 00:00:00 2001 From: Pekka Helenius Date: Thu, 24 Sep 2020 10:50:52 +0300 Subject: [PATCH] Implement custom JSON serializer for book category Signed-off-by: Pekka Helenius --- .../model/CategoryJsonSerializer.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 bookstore/src/main/java/com/fjordtek/bookstore/model/CategoryJsonSerializer.java diff --git a/bookstore/src/main/java/com/fjordtek/bookstore/model/CategoryJsonSerializer.java b/bookstore/src/main/java/com/fjordtek/bookstore/model/CategoryJsonSerializer.java new file mode 100644 index 0000000..7b44f61 --- /dev/null +++ b/bookstore/src/main/java/com/fjordtek/bookstore/model/CategoryJsonSerializer.java @@ -0,0 +1,30 @@ +package com.fjordtek.bookstore.model; + +import java.io.IOException; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.ser.std.StdSerializer; + +public class CategoryJsonSerializer extends StdSerializer { + private static final long serialVersionUID = 6376700470881235634L; + + public CategoryJsonSerializer() { + this(null); + } + + public CategoryJsonSerializer(Class jd) { + super(jd); + } + + @Override + public void serialize(Category category, JsonGenerator gen, SerializerProvider provider) + throws IOException { + gen.writeStartObject(); + // Category class Id has JsonIgnore annotation + //gen.writeFieldId(category.getId()); + gen.writeStringField("name", category.getName()); + gen.writeEndObject(); + } + +} \ No newline at end of file