Created
August 2, 2016 07:41
-
-
Save gurdotan/4e4291f376870f7e321750971cad7135 to your computer and use it in GitHub Desktop.
Jackson Custom JSON Serializer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 java.io.IOException; | |
public class UiNotificationSerializer extends JsonSerializer<MyDto> { | |
@Override public void serialize(MyDto dto, JsonGenerator generator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException { | |
generator.writeStartObject(); | |
generator.writeNumberField("id", dto.getId()); | |
Integer brandId = dto.getBrandId(); | |
if (brandId != null) { | |
generator.writeNumberField("brandId", dto.getBrandId()); | |
} else { | |
generator.writeNullField("brandId"); | |
} | |
String message = dto.getMessage(); | |
if (message != null) { | |
generator.writeStringField("message", message); | |
} else { | |
generator.writeNullField("message"); | |
} | |
generator.writeEndObject(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment