resetting Distance class
package com.baeldung.jackson.enums;
import com.baeldung.jackson.serialization.DistanceSerializer;
-import com.fasterxml.jackson.annotation.JsonCreator;
-import com.fasterxml.jackson.annotation.JsonFormat;
-import com.fasterxml.jackson.annotation.JsonProperty;
-import com.fasterxml.jackson.annotation.JsonValue;
-import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
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)
+// @JsonFormat(shape = JsonFormat.Shape.OBJECT)
+@JsonSerialize(using = DistanceSerializer.class)
public enum Distance {
- @JsonProperty("distance-in-km")
- KILOMETER("km", 1000),
- @JsonProperty("distance-in-miles")
- MILE("miles", 1609.34), METER("meters", 1), INCH("inches", 0.0254), CENTIMETER("cm", 0.01),
- MILLIMETER("mm", 0.001);
+ 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;
@@ -60,19 +51,4 @@ public enum Distance {
return distanceInMeters * meters;
}
-// @JsonCreator
- public static Distance forValues(@JsonProperty("unit") String unit, @JsonProperty("meters") double meters) {
-
- for (Distance distance : Distance.values()) {
- if (distance.unit.equals(unit)
- && Double.compare(distance.meters, meters) == 0) {
-
- return distance;
- }
- }
-
- return null;
-
- }
-
}
\ No newline at end of file
