001package com.identityworksllc.iiq.common.vo;
002
003import com.fasterxml.jackson.core.JsonGenerator;
004import com.fasterxml.jackson.databind.SerializerProvider;
005import com.fasterxml.jackson.databind.ser.std.StdSerializer;
006
007import java.io.IOException;
008import java.util.Map;
009
010/**
011 * Custom serializer for StampedMessage outputs, to avoid having to let Jackson
012 * figure out how to serialize the Throwable object.
013 */
014public class StampedMessageSerializer extends StdSerializer<StampedMessage> {
015
016    /**
017     * Constructs a new StampedMessageSerializer
018     */
019    public StampedMessageSerializer() {
020        this(null);
021    }
022
023    /**
024     * Constructs a new serializer
025     * @param t The input type
026     */
027    public StampedMessageSerializer(Class<StampedMessage> t) {
028        super(t);
029    }
030
031    @Override
032    public void serialize(StampedMessage value, JsonGenerator gen, SerializerProvider provider) throws IOException {
033        Map<String, String> map = value.toMap();
034        gen.writeStartObject();
035        gen.writeStringField("timestamp", map.get("timestamp"));
036        gen.writeStringField("formattedDate", map.get("formattedDate"));
037        gen.writeStringField("level", map.get("level"));
038        gen.writeStringField("message", map.get("message"));
039
040        if (map.containsKey("exception")) {
041            gen.writeStringField("exception", map.get("exception"));
042        }
043
044        gen.writeEndObject();
045    }
046}