BAEL-245: Add Enum serialization example (#970)

* Add NDC and JBoss Logging to the demo application

* NDC for Log4j, Log4j2 and JBoss Logging

* Simplify NDC example by making it a single operation instead of two

* Make NDC example as RestController, Use JBoss Logging only as a logging bridge

* Fix merge conflicts in pull request - log-mdc pom.xml updated

* BAEL-445 Update to Spring security SpEL example

* BAEL-445: Change tabs to spaces in the updated code

* BAEL-245: Add Enum Serialization exmaple

* BAEL-245: Remove the folder jackson/src/test/java/com/baeldung/jackson/dtos/withEnum as the example is not used anymore
This commit is contained in:
Sunil Mogadati 2017-01-08 07:13:39 -07:00 committed by Eugen
parent e8a9b60862
commit 82fc8cf8fc
10 changed files with 109 additions and 284 deletions

View File

@ -0,0 +1,54 @@
package com.baeldung.jackson.enums;
import com.baeldung.jackson.serialization.DistanceSerializer;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
/**
* Use @JsonFormat to handle representation of Enum as JSON (available since Jackson 2.1.2)
* Use @JsonSerialize to configure a custom Jackson serializer
*/
// @JsonFormat(shape = JsonFormat.Shape.OBJECT)
@JsonSerialize(using = DistanceSerializer.class)
public enum Distance {
KILOMETER("km", 1000), MILE("miles", 1609.34), METER("meters", 1), INCH("inches", 0.0254), CENTIMETER("cm", 0.01), MILLIMETER("mm", 0.001);
private String unit;
private final double meters;
private Distance(String unit, double meters) {
this.unit = unit;
this.meters = meters;
}
/**
* Use @JsonValue to control marshalling output for an enum
*/
// @JsonValue
public double getMeters() {
return meters;
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
/**
* Usage example: Distance.MILE.convertFromMeters(1205.5);
*/
public double convertFromMeters(double distanceInMeters) {
return distanceInMeters / meters;
}
/**
* Usage example: Distance.MILE.convertToMeters(0.5);
*/
public double convertToMeters(double distanceInMeters) {
return distanceInMeters * meters;
}
}

View File

@ -0,0 +1,33 @@
package com.baeldung.jackson.serialization;
import java.io.IOException;
import com.baeldung.jackson.enums.Distance;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
public class DistanceSerializer extends StdSerializer<Distance> {
private static final long serialVersionUID = 1376504304439963619L;
public DistanceSerializer() {
super(Distance.class);
}
public DistanceSerializer(Class<Distance> t) {
super(t);
}
public void serialize(Distance distance, JsonGenerator generator, SerializerProvider provider) throws IOException, JsonProcessingException {
generator.writeStartObject();
generator.writeFieldName("name");
generator.writeNumber(distance.name());
generator.writeFieldName("unit");
generator.writeString(distance.getUnit());
generator.writeFieldName("meters");
generator.writeNumber(distance.getMeters());
generator.writeEndObject();
}
}

View File

@ -1,57 +0,0 @@
package com.baeldung.jackson.dtos.withEnum;
public class MyDtoWithEnum {
private String stringValue;
private int intValue;
private boolean booleanValue;
private TypeEnum type;
public MyDtoWithEnum() {
super();
}
public MyDtoWithEnum(final String stringValue, final int intValue, final boolean booleanValue, final TypeEnum type) {
super();
this.stringValue = stringValue;
this.intValue = intValue;
this.booleanValue = booleanValue;
this.type = type;
}
// API
public String getStringValue() {
return stringValue;
}
public void setStringValue(final String stringValue) {
this.stringValue = stringValue;
}
public int getIntValue() {
return intValue;
}
public void setIntValue(final int intValue) {
this.intValue = intValue;
}
public boolean isBooleanValue() {
return booleanValue;
}
public void setBooleanValue(final boolean booleanValue) {
this.booleanValue = booleanValue;
}
public TypeEnum getType() {
return type;
}
public void setType(final TypeEnum type) {
this.type = type;
}
}

View File

@ -1,57 +0,0 @@
package com.baeldung.jackson.dtos.withEnum;
public class MyDtoWithEnumCustom {
private String stringValue;
private int intValue;
private boolean booleanValue;
private TypeEnumWithCustomSerializer type;
public MyDtoWithEnumCustom() {
super();
}
public MyDtoWithEnumCustom(final String stringValue, final int intValue, final boolean booleanValue, final TypeEnumWithCustomSerializer type) {
super();
this.stringValue = stringValue;
this.intValue = intValue;
this.booleanValue = booleanValue;
this.type = type;
}
// API
public String getStringValue() {
return stringValue;
}
public void setStringValue(final String stringValue) {
this.stringValue = stringValue;
}
public int getIntValue() {
return intValue;
}
public void setIntValue(final int intValue) {
this.intValue = intValue;
}
public boolean isBooleanValue() {
return booleanValue;
}
public void setBooleanValue(final boolean booleanValue) {
this.booleanValue = booleanValue;
}
public TypeEnumWithCustomSerializer getType() {
return type;
}
public void setType(final TypeEnumWithCustomSerializer type) {
this.type = type;
}
}

View File

@ -1,35 +0,0 @@
package com.baeldung.jackson.dtos.withEnum;
import com.fasterxml.jackson.annotation.JsonFormat;
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public enum TypeEnum {
TYPE1(1, "Type A"), TYPE2(2, "Type 2");
private Integer id;
private String name;
private TypeEnum(final Integer id, final String name) {
this.id = id;
this.name = name;
}
// API
public Integer getId() {
return id;
}
public void setId(final Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
}

View File

@ -1,32 +0,0 @@
package com.baeldung.jackson.dtos.withEnum;
public enum TypeEnumSimple {
TYPE1(1, "Type A"), TYPE2(2, "Type 2");
private Integer id;
private String name;
private TypeEnumSimple(final Integer id, final String name) {
this.id = id;
this.name = name;
}
// API
public Integer getId() {
return id;
}
public void setId(final Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
}

View File

@ -1,35 +0,0 @@
package com.baeldung.jackson.dtos.withEnum;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
@JsonSerialize(using = TypeSerializer.class)
public enum TypeEnumWithCustomSerializer {
TYPE1(1, "Type A"), TYPE2(2, "Type 2");
private Integer id;
private String name;
private TypeEnumWithCustomSerializer(final Integer id, final String name) {
this.id = id;
this.name = name;
}
// API
public Integer getId() {
return id;
}
public void setId(final Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
}

View File

@ -1,36 +0,0 @@
package com.baeldung.jackson.dtos.withEnum;
import com.fasterxml.jackson.annotation.JsonValue;
public enum TypeEnumWithValue {
TYPE1(1, "Type A"), TYPE2(2, "Type 2");
private Integer id;
private String name;
private TypeEnumWithValue(final Integer id, final String name) {
this.id = id;
this.name = name;
}
// API
public Integer getId() {
return id;
}
public void setId(final Integer id) {
this.id = id;
}
@JsonValue
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
}

View File

@ -1,32 +0,0 @@
package com.baeldung.jackson.dtos.withEnum;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
public class TypeSerializer extends StdSerializer<TypeEnumWithCustomSerializer> {
private static final long serialVersionUID = -7650668914169390772L;
public TypeSerializer() {
this(null);
}
public TypeSerializer(final Class<TypeEnumWithCustomSerializer> t) {
super(t);
}
@Override
public void serialize(final TypeEnumWithCustomSerializer value, final JsonGenerator generator, final SerializerProvider provider) throws IOException, JsonProcessingException {
generator.writeStartObject();
generator.writeFieldName("id");
generator.writeNumber(value.getId());
generator.writeFieldName("name");
generator.writeString(value.getName());
generator.writeEndObject();
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.jackson.enums;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertThat;
import java.io.IOException;
import org.junit.Test;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JacksonEnumSerializationTest {
@Test
public final void givenEnum_whenSerializingJson_thenCorrectRepresentation() throws JsonParseException, IOException {
final String dtoAsString = new ObjectMapper().writeValueAsString(Distance.MILE);
assertThat(dtoAsString, containsString("1609.34"));
}
}