[BAEL-13511] - Create jackson-modules parent for all related modules

This commit is contained in:
catalin-burcea
2019-12-19 13:34:50 +02:00
parent 93b79e272d
commit c8f16ee116
291 changed files with 52 additions and 103 deletions
@@ -0,0 +1,36 @@
package com.baeldung.jackson.date;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
public class CustomDateDeserializer extends StdDeserializer<Date> {
private static final long serialVersionUID = -5451717385630622729L;
private SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
public CustomDateDeserializer() {
this(null);
}
public CustomDateDeserializer(final Class<?> vc) {
super(vc);
}
@Override
public Date deserialize(final JsonParser jsonparser, final DeserializationContext context) throws IOException, JsonProcessingException {
final String date = jsonparser.getText();
try {
return formatter.parse(date);
} catch (final ParseException e) {
throw new RuntimeException(e);
}
}
}
@@ -0,0 +1,29 @@
package com.baeldung.jackson.date;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
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 CustomDateSerializer extends StdSerializer<Date> {
private static final long serialVersionUID = -2894356342227378312L;
private SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
public CustomDateSerializer() {
this(null);
}
public CustomDateSerializer(final Class<Date> t) {
super(t);
}
@Override
public void serialize(final Date value, final JsonGenerator gen, final SerializerProvider arg2) throws IOException, JsonProcessingException {
gen.writeString(formatter.format(value));
}
}
@@ -0,0 +1,32 @@
package com.baeldung.jackson.date;
import java.io.IOException;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
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 CustomDateTimeSerializer extends StdSerializer<DateTime> {
private static final long serialVersionUID = -3927232057990121460L;
public CustomDateTimeSerializer() {
this(null);
}
public CustomDateTimeSerializer(final Class<DateTime> t) {
super(t);
}
private static DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm");
@Override
public void serialize(final DateTime value, final JsonGenerator gen, final SerializerProvider arg2) throws IOException, JsonProcessingException {
gen.writeString(formatter.print(value));
}
}
@@ -0,0 +1,29 @@
package com.baeldung.jackson.date;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
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 CustomLocalDateTimeSerializer extends StdSerializer<LocalDateTime> {
private static final long serialVersionUID = -7449444168934819290L;
private static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
public CustomLocalDateTimeSerializer() {
this(null);
}
public CustomLocalDateTimeSerializer(final Class<LocalDateTime> t) {
super(t);
}
@Override
public void serialize(final LocalDateTime value, final JsonGenerator gen, final SerializerProvider arg2) throws IOException, JsonProcessingException {
gen.writeString(formatter.format(value));
}
}
@@ -0,0 +1,25 @@
package com.baeldung.jackson.date;
import java.util.Date;
public class Event {
public String name;
public Date eventDate;
public Event() {
super();
}
public Event(final String name, final Date eventDate) {
this.name = name;
this.eventDate = eventDate;
}
public Date getEventDate() {
return eventDate;
}
public String getName() {
return name;
}
}
@@ -0,0 +1,29 @@
package com.baeldung.jackson.date;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat;
public class EventWithFormat {
public String name;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy hh:mm:ss")
public Date eventDate;
public EventWithFormat() {
super();
}
public EventWithFormat(final String name, final Date eventDate) {
this.name = name;
this.eventDate = eventDate;
}
public Date getEventDate() {
return eventDate;
}
public String getName() {
return name;
}
}
@@ -0,0 +1,29 @@
package com.baeldung.jackson.date;
import org.joda.time.DateTime;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
public class EventWithJodaTime {
public String name;
@JsonSerialize(using = CustomDateTimeSerializer.class)
public DateTime eventDate;
public EventWithJodaTime() {
super();
}
public EventWithJodaTime(final String name, final DateTime eventDate) {
this.name = name;
this.eventDate = eventDate;
}
public DateTime getEventDate() {
return eventDate;
}
public String getName() {
return name;
}
}
@@ -0,0 +1,29 @@
package com.baeldung.jackson.date;
import java.time.LocalDateTime;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
public class EventWithLocalDateTime {
public String name;
@JsonSerialize(using = CustomLocalDateTimeSerializer.class)
public LocalDateTime eventDate;
public EventWithLocalDateTime() {
super();
}
public EventWithLocalDateTime(final String name, final LocalDateTime eventDate) {
this.name = name;
this.eventDate = eventDate;
}
public LocalDateTime getEventDate() {
return eventDate;
}
public String getName() {
return name;
}
}
@@ -0,0 +1,31 @@
package com.baeldung.jackson.date;
import java.util.Date;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
public class EventWithSerializer {
public String name;
@JsonDeserialize(using = CustomDateDeserializer.class)
@JsonSerialize(using = CustomDateSerializer.class)
public Date eventDate;
public EventWithSerializer() {
super();
}
public EventWithSerializer(final String name, final Date eventDate) {
this.name = name;
this.eventDate = eventDate;
}
public Date getEventDate() {
return eventDate;
}
public String getName() {
return name;
}
}
@@ -0,0 +1,15 @@
package com.baeldung.jackson.enums.deserialization;
public class City {
private Distance distance;
public Distance getDistance() {
return distance;
}
public void setDistance(Distance distance) {
this.distance = distance;
}
}
@@ -0,0 +1,31 @@
package com.baeldung.jackson.enums.deserialization;
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 double meters;
private Distance(String unit, double meters) {
this.unit = unit;
this.meters = meters;
}
public void setMeters(double meters) {
this.meters = meters;
}
public double getMeters() {
return meters;
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
}
@@ -0,0 +1,15 @@
package com.baeldung.jackson.enums.deserialization.customdeserializer;
public class City {
private Distance distance;
public Distance getDistance() {
return distance;
}
public void setDistance(Distance distance) {
this.distance = distance;
}
}
@@ -0,0 +1,42 @@
package com.baeldung.jackson.enums.deserialization.customdeserializer;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
public class CustomEnumDeserializer extends StdDeserializer<Distance> {
private static final long serialVersionUID = -1166032307856492833L;
public CustomEnumDeserializer() {
this(null);
}
public CustomEnumDeserializer(Class<?> c) {
super(c);
}
@Override
public Distance deserialize(JsonParser jsonParser, DeserializationContext ctxt) throws IOException, JsonProcessingException {
JsonNode node = jsonParser.getCodec().readTree(jsonParser);
String unit = node.get("unit").asText();
double meters = node.get("meters").asDouble();
for (Distance distance : Distance.values()) {
if (distance.getUnit().equals(unit) &&
Double.compare(distance.getMeters(), meters) == 0) {
return distance;
}
}
return null;
}
}
@@ -0,0 +1,33 @@
package com.baeldung.jackson.enums.deserialization.customdeserializer;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
@JsonDeserialize(using = CustomEnumDeserializer.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 double meters;
private Distance(String unit, double meters) {
this.unit = unit;
this.meters = meters;
}
public double getMeters() {
return meters;
}
public void setMeters(double meters) {
this.meters = meters;
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
}
@@ -0,0 +1,15 @@
package com.baeldung.jackson.enums.deserialization.jsoncreator;
public class City {
private Distance distance;
public Distance getDistance() {
return distance;
}
public void setDistance(Distance distance) {
this.distance = distance;
}
}
@@ -0,0 +1,48 @@
package com.baeldung.jackson.enums.deserialization.jsoncreator;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
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 double meters;
private Distance(String unit, double meters) {
this.unit = unit;
this.meters = meters;
}
public void setMeters(double meters) {
this.meters = meters;
}
public double getMeters() {
return meters;
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
@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;
}
}
@@ -0,0 +1,15 @@
package com.baeldung.jackson.enums.deserialization.jsonproperty;
public class City {
private Distance distance;
public Distance getDistance() {
return distance;
}
public void setDistance(Distance distance) {
this.distance = distance;
}
}
@@ -0,0 +1,51 @@
package com.baeldung.jackson.enums.deserialization.jsonproperty;
import com.fasterxml.jackson.annotation.JsonProperty;
public enum Distance {
@JsonProperty("distance-in-km")
KILOMETER("km", 1000),
@JsonProperty("distance-in-miles")
MILE("miles", 1609.34),
@JsonProperty("distance-in-meters")
METER("meters", 1),
@JsonProperty("distance-in-inches")
INCH("inches", 0.0254),
@JsonProperty("distance-in-cm")
CENTIMETER("cm", 0.01),
@JsonProperty("distance-in-mm")
MILLIMETER("mm", 0.001);
private String unit;
private double meters;
private Distance(String unit, double meters) {
this.unit = unit;
this.meters = meters;
}
public void setMeters(double meters) {
this.meters = meters;
}
public double getMeters() {
return meters;
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
}
@@ -0,0 +1,15 @@
package com.baeldung.jackson.enums.deserialization.jsonvalue;
public class City {
private Distance distance;
public Distance getDistance() {
return distance;
}
public void setDistance(Distance distance) {
this.distance = distance;
}
}
@@ -0,0 +1,35 @@
package com.baeldung.jackson.enums.deserialization.jsonvalue;
import com.fasterxml.jackson.annotation.JsonValue;
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 double meters;
private Distance(String unit, double meters) {
this.unit = unit;
this.meters = meters;
}
public void setMeters(double meters) {
this.meters = meters;
}
@JsonValue
public double getMeters() {
return meters;
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
}
@@ -0,0 +1,54 @@
package com.baeldung.jackson.enums.serialization;
import com.baeldung.jackson.enums.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;
}
}
@@ -0,0 +1,32 @@
package com.baeldung.jackson.enums.serialization;
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 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.writeString(distance.name());
generator.writeFieldName("unit");
generator.writeString(distance.getUnit());
generator.writeFieldName("meters");
generator.writeNumber(distance.getMeters());
generator.writeEndObject();
}
}
@@ -0,0 +1,26 @@
package com.baeldung.jackson.enums.withEnum;
public enum DistanceEnumSimple {
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 DistanceEnumSimple(String unit, double meters) {
this.unit = unit;
this.meters = meters;
}
public double getMeters() {
return meters;
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
}
@@ -0,0 +1,29 @@
package com.baeldung.jackson.enums.withEnum;
import com.fasterxml.jackson.annotation.JsonFormat;
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public enum DistanceEnumWithJsonFormat {
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 DistanceEnumWithJsonFormat(String unit, double meters) {
this.unit = unit;
this.meters = meters;
}
public double getMeters() {
return meters;
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
}
@@ -0,0 +1,29 @@
package com.baeldung.jackson.enums.withEnum;
import com.fasterxml.jackson.annotation.JsonValue;
public enum DistanceEnumWithValue {
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 DistanceEnumWithValue(String unit, double meters) {
this.unit = unit;
this.meters = meters;
}
@JsonValue
public double getMeters() {
return meters;
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
}
@@ -0,0 +1,59 @@
package com.baeldung.jackson.enums.withEnum;
import com.baeldung.jackson.enums.serialization.Distance;
public class MyDtoWithEnumCustom {
private String stringValue;
private int intValue;
private boolean booleanValue;
private Distance type;
public MyDtoWithEnumCustom() {
super();
}
public MyDtoWithEnumCustom(final String stringValue, final int intValue, final boolean booleanValue, final Distance 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 Distance getType() {
return type;
}
public void setType(final Distance type) {
this.type = type;
}
}
@@ -0,0 +1,57 @@
package com.baeldung.jackson.enums.withEnum;
public class MyDtoWithEnumJsonFormat {
private String stringValue;
private int intValue;
private boolean booleanValue;
private DistanceEnumWithJsonFormat distanceType;
public MyDtoWithEnumJsonFormat() {
super();
}
public MyDtoWithEnumJsonFormat(final String stringValue, final int intValue, final boolean booleanValue, final DistanceEnumWithJsonFormat type) {
super();
this.stringValue = stringValue;
this.intValue = intValue;
this.booleanValue = booleanValue;
this.distanceType = 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 DistanceEnumWithJsonFormat getDistanceType() {
return distanceType;
}
public void setDistanceType(final DistanceEnumWithJsonFormat type) {
this.distanceType = type;
}
}
@@ -0,0 +1,47 @@
package com.baeldung.jackson.field;
public class MyDto {
private String stringValue;
private int intValue;
private boolean booleanValue;
public MyDto() {
super();
}
public MyDto(final String stringValue, final int intValue, final boolean booleanValue) {
super();
this.stringValue = stringValue;
this.intValue = intValue;
this.booleanValue = booleanValue;
}
// 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 getBooleanValue() {
return booleanValue;
}
public void setBooleanValue(final boolean booleanValue) {
this.booleanValue = booleanValue;
}
}
@@ -0,0 +1,23 @@
package com.baeldung.jackson.field;
public class MyDtoAccessLevel {
private String stringValue;
int intValue;
protected float floatValue;
public boolean booleanValue;
public MyDtoAccessLevel() {
super();
}
public MyDtoAccessLevel(final String stringValue, final int intValue, final float floatValue, final boolean booleanValue) {
super();
this.stringValue = stringValue;
this.intValue = intValue;
this.floatValue = floatValue;
this.booleanValue = booleanValue;
}
}
@@ -0,0 +1,25 @@
package com.baeldung.jackson.field;
public class MyDtoWithGetter {
private String stringValue;
private int intValue;
public MyDtoWithGetter() {
super();
}
public MyDtoWithGetter(final String stringValue, final int intValue) {
super();
this.stringValue = stringValue;
this.intValue = intValue;
}
// API
public String getStringValue() {
return stringValue;
}
}
@@ -0,0 +1,29 @@
package com.baeldung.jackson.field;
public class MyDtoWithSetter {
private int intValue;
public boolean booleanValue;
public MyDtoWithSetter() {
super();
}
public MyDtoWithSetter(final int intValue, final boolean booleanValue) {
super();
this.intValue = intValue;
this.booleanValue = booleanValue;
}
// API
public void setIntValue(final int intValue) {
this.intValue = intValue;
}
public int accessIntValue() {
return intValue;
}
}
@@ -0,0 +1,24 @@
package com.baeldung.jackson.immutable;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class Employee {
private final long id;
private final String name;
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
public Employee(@JsonProperty("id") long id, @JsonProperty("name") String name) {
this.id = id;
this.name = name;
}
public long getId() {
return id;
}
public String getName() {
return name;
}
}
@@ -0,0 +1,44 @@
package com.baeldung.jackson.immutable;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
@JsonDeserialize(builder = Person.Builder.class)
public class Person {
private final String name;
private final Integer age;
private Person(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public Integer getAge() {
return age;
}
@JsonPOJOBuilder
static class Builder {
String name;
Integer age;
Builder withName(String name) {
this.name = name;
return this;
}
Builder withAge(Integer age) {
this.age = age;
return this;
}
Person build() {
return new Person(name, age);
}
}
}
@@ -0,0 +1,23 @@
package com.baeldung.jackson.map;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
public class ClassWithAMap {
@JsonProperty("map")
@JsonDeserialize(keyUsing = MyPairDeserializer.class)
private final Map<MyPair, String> map;
@JsonCreator
public ClassWithAMap(Map<MyPair, String> map) {
this.map = map;
}
public Map<MyPair, String> getMap() {
return map;
}
}
@@ -0,0 +1,80 @@
package com.baeldung.jackson.map;
import com.fasterxml.jackson.annotation.JsonValue;
public class MyPair {
private String first;
private String second;
public MyPair(String first, String second) {
this.first = first;
this.second = second;
}
public MyPair(String both) {
String[] pairs = both.split("and");
this.first = pairs[0].trim();
this.second = pairs[1].trim();
}
@Override
@JsonValue
public String toString() {
return first + " and " + second;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((first == null) ? 0 : first.hashCode());
result = prime * result + ((second == null) ? 0 : second.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof MyPair)) {
return false;
}
MyPair other = (MyPair) obj;
if (first == null) {
if (other.first != null) {
return false;
}
} else if (!first.equals(other.first)) {
return false;
}
if (second == null) {
if (other.second != null) {
return false;
}
} else if (!second.equals(other.second)) {
return false;
}
return true;
}
public String getFirst() {
return first;
}
public void setFirst(String first) {
this.first = first;
}
public String getSecond() {
return second;
}
public void setSecond(String second) {
this.second = second;
}
}
@@ -0,0 +1,16 @@
package com.baeldung.jackson.map;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.KeyDeserializer;
public class MyPairDeserializer extends KeyDeserializer {
@Override
public MyPair deserializeKey(String key, DeserializationContext ctxt) throws IOException, JsonProcessingException {
return new MyPair(key);
}
}
@@ -0,0 +1,22 @@
package com.baeldung.jackson.map;
import java.io.IOException;
import java.io.StringWriter;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
public class MyPairSerializer extends JsonSerializer<MyPair> {
private final ObjectMapper mapper = new ObjectMapper();
@Override
public void serialize(MyPair value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
StringWriter writer = new StringWriter();
mapper.writeValue(writer, value);
gen.writeFieldName(writer.toString());
}
}
@@ -0,0 +1,54 @@
package com.baeldung.jackson.mapnull;
public class MyDto {
private String stringValue;
private int intValue;
private boolean booleanValue;
public MyDto() {
super();
}
public MyDto(final String stringValue, final int intValue, final boolean booleanValue) {
super();
this.stringValue = stringValue;
this.intValue = intValue;
this.booleanValue = booleanValue;
}
// 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;
}
//
@Override
public String toString() {
return "MyDto [stringValue=" + stringValue + ", intValue=" + intValue + ", booleanValue=" + booleanValue + "]";
}
}
@@ -0,0 +1,27 @@
package com.baeldung.jackson.mapnull;
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 MyDtoNullKeySerializer extends StdSerializer<Object> {
private static final long serialVersionUID = -4478531309177369056L;
public MyDtoNullKeySerializer() {
this(null);
}
public MyDtoNullKeySerializer(final Class<Object> t) {
super(t);
}
@Override
public void serialize(final Object value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException, JsonProcessingException {
jgen.writeFieldName("");
}
}
@@ -0,0 +1,54 @@
package com.baeldung.jackson.tocollection;
public class MyDto {
private String stringValue;
private int intValue;
private boolean booleanValue;
public MyDto() {
super();
}
public MyDto(final String stringValue, final int intValue, final boolean booleanValue) {
super();
this.stringValue = stringValue;
this.intValue = intValue;
this.booleanValue = booleanValue;
}
// 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;
}
//
@Override
public String toString() {
return "MyDto [stringValue=" + stringValue + ", intValue=" + intValue + ", booleanValue=" + booleanValue + "]";
}
}
@@ -0,0 +1,33 @@
package com.baeldung.jackson.xml;
public class Address {
String streetNumber;
String streetName;
String city;
public String getStreetNumber() {
return streetNumber;
}
public void setStreetNumber(String streetNumber) {
this.streetNumber = streetNumber;
}
public String getStreetName() {
return streetName;
}
public void setStreetName(String streetName) {
this.streetName = streetName;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
@@ -0,0 +1,47 @@
package com.baeldung.jackson.xml;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import java.util.ArrayList;
import java.util.List;
@JacksonXmlRootElement(localName = "person")
public final class Person {
private String firstName;
private String lastName;
private List<String> phoneNumbers = new ArrayList<>();
private List<Address> address = new ArrayList<>();
public List<Address> getAddress() {
return address;
}
public void setAddress(List<Address> address) {
this.address = address;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public List<String> getPhoneNumbers() {
return phoneNumbers;
}
public void setPhoneNumbers(List<String> phoneNumbers) {
this.phoneNumbers = phoneNumbers;
}
}