minor formatting cleanup
This commit is contained in:
+2
-1
@@ -9,7 +9,8 @@ public class Author extends Person {
|
||||
|
||||
List<Item> items = new ArrayList<>();
|
||||
|
||||
public Author(){}
|
||||
public Author() {
|
||||
}
|
||||
|
||||
public Author(String firstName, String lastName) {
|
||||
super(firstName, lastName);
|
||||
|
||||
+1
-1
@@ -11,7 +11,7 @@ public class Person {
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
|
||||
public Person(){
|
||||
public Person() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.baeldung.jackson.deserialization.jsoncreator;
|
||||
|
||||
|
||||
import com.baeldung.jackson.domain.Person;
|
||||
import com.baeldung.jackson.domain.Item;
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
@@ -14,9 +13,7 @@ public class Author extends Person {
|
||||
List<Item> items = new ArrayList<>();
|
||||
|
||||
@JsonCreator
|
||||
public Author(
|
||||
@JsonProperty("christianName") String firstName,
|
||||
@JsonProperty("surname") String lastName) {
|
||||
public Author(@JsonProperty("christianName") String firstName, @JsonProperty("surname") String lastName) {
|
||||
super(firstName, lastName);
|
||||
}
|
||||
|
||||
|
||||
+2
-1
@@ -13,7 +13,8 @@ public class Book extends Item {
|
||||
private Date published;
|
||||
private BigDecimal pages;
|
||||
|
||||
public Book() {}
|
||||
public Book() {
|
||||
}
|
||||
|
||||
public Book(String title, Author author) {
|
||||
super(title, author);
|
||||
|
||||
+2
-4
@@ -11,8 +11,7 @@ import java.util.Date;
|
||||
|
||||
public class CustomDateDeserializer extends StdDeserializer<Date> {
|
||||
|
||||
private static SimpleDateFormat formatter =
|
||||
new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
|
||||
private static SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
|
||||
|
||||
public CustomDateDeserializer() {
|
||||
this(null);
|
||||
@@ -23,8 +22,7 @@ public class CustomDateDeserializer extends StdDeserializer<Date> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Date deserialize(JsonParser jsonparser, DeserializationContext context)
|
||||
throws IOException {
|
||||
public Date deserialize(JsonParser jsonparser, DeserializationContext context) throws IOException {
|
||||
String date = jsonparser.getText();
|
||||
try {
|
||||
return formatter.parse(date);
|
||||
|
||||
+2
-1
@@ -19,7 +19,8 @@ public class Item {
|
||||
private List<Person> authors = new ArrayList<>();
|
||||
private float price;
|
||||
|
||||
public Item(){}
|
||||
public Item() {
|
||||
}
|
||||
|
||||
public Item(String title, Author author) {
|
||||
this.id = UUID.randomUUID();
|
||||
|
||||
@@ -18,7 +18,8 @@ public class Author extends Person {
|
||||
|
||||
private List<Item> items = new ArrayList<>();
|
||||
|
||||
public Author(){}
|
||||
public Author() {
|
||||
}
|
||||
|
||||
public Author(String firstName, String lastName) {
|
||||
super(firstName, lastName);
|
||||
|
||||
@@ -15,7 +15,7 @@ public class Book extends Item {
|
||||
private Date published;
|
||||
private BigDecimal pages;
|
||||
|
||||
public Book(){
|
||||
public Book() {
|
||||
}
|
||||
|
||||
public Book(String title, Author author) {
|
||||
|
||||
@@ -10,7 +10,9 @@ import java.util.List;
|
||||
*/
|
||||
public class Course extends Item {
|
||||
|
||||
public enum Medium {CLASSROOM, ONLINE}
|
||||
public enum Medium {
|
||||
CLASSROOM, ONLINE
|
||||
}
|
||||
|
||||
public enum Level {
|
||||
BEGINNER("Beginner", 1), INTERMEDIATE("Intermediate", 2), ADVANCED("Advanced", 3);
|
||||
|
||||
@@ -17,7 +17,8 @@ public class Item {
|
||||
private List<Person> authors = new ArrayList<>();
|
||||
private float price;
|
||||
|
||||
public Item(){}
|
||||
public Item() {
|
||||
}
|
||||
|
||||
public Item(String title, Author author) {
|
||||
this.id = UUID.randomUUID();
|
||||
|
||||
@@ -14,7 +14,8 @@ public class Person {
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
|
||||
public Person(){}
|
||||
public Person() {
|
||||
}
|
||||
|
||||
public Person(String firstName, String lastName) {
|
||||
this.id = UUID.randomUUID();
|
||||
|
||||
@@ -9,16 +9,16 @@ import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||
|
||||
public class ClassWithAMap {
|
||||
|
||||
@JsonProperty("map")
|
||||
@JsonDeserialize(keyUsing = MyPairDeserializer.class)
|
||||
private final Map<MyPair, String> map;
|
||||
@JsonProperty("map")
|
||||
@JsonDeserialize(keyUsing = MyPairDeserializer.class)
|
||||
private final Map<MyPair, String> map;
|
||||
|
||||
@JsonCreator
|
||||
public ClassWithAMap(Map<MyPair, String> map) {
|
||||
this.map = map;
|
||||
}
|
||||
@JsonCreator
|
||||
public ClassWithAMap(Map<MyPair, String> map) {
|
||||
this.map = map;
|
||||
}
|
||||
|
||||
public Map<MyPair, String> getMap() {
|
||||
return map;
|
||||
}
|
||||
public Map<MyPair, String> getMap() {
|
||||
return map;
|
||||
}
|
||||
}
|
||||
@@ -4,77 +4,77 @@ import com.fasterxml.jackson.annotation.JsonValue;
|
||||
|
||||
public class MyPair {
|
||||
|
||||
private String first;
|
||||
private String second;
|
||||
private String first;
|
||||
private String second;
|
||||
|
||||
public MyPair(String first, String second) {
|
||||
this.first = first;
|
||||
this.second = 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();
|
||||
}
|
||||
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
|
||||
@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 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;
|
||||
}
|
||||
@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 String getFirst() {
|
||||
return first;
|
||||
}
|
||||
|
||||
public void setFirst(String first) {
|
||||
this.first = first;
|
||||
}
|
||||
public void setFirst(String first) {
|
||||
this.first = first;
|
||||
}
|
||||
|
||||
public String getSecond() {
|
||||
return second;
|
||||
}
|
||||
public String getSecond() {
|
||||
return second;
|
||||
}
|
||||
|
||||
public void setSecond(String second) {
|
||||
this.second = second;
|
||||
}
|
||||
public void setSecond(String second) {
|
||||
this.second = second;
|
||||
}
|
||||
}
|
||||
@@ -13,28 +13,25 @@ public class User extends Person {
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
|
||||
@JsonFormat(shape = JsonFormat.Shape.STRING,
|
||||
pattern = "yyyy-MM-dd@HH:mm:ss.SSSZ")
|
||||
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd@HH:mm:ss.SSSZ")
|
||||
private Date createdDate;
|
||||
|
||||
public User(String firstName,String lastName) {
|
||||
super(firstName, lastName);
|
||||
this.createdDate = new Date();
|
||||
public User(String firstName, String lastName) {
|
||||
super(firstName, lastName);
|
||||
this.createdDate = new Date();
|
||||
}
|
||||
|
||||
public Date getCreatedDate() {
|
||||
return createdDate;
|
||||
}
|
||||
|
||||
@JsonFormat(shape = JsonFormat.Shape.STRING,
|
||||
pattern = "yyyy-MM-dd@HH:mm:ss.SSSZ",
|
||||
locale = "en_GB")
|
||||
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd@HH:mm:ss.SSSZ", locale = "en_GB")
|
||||
public Date getCurrentDate() {
|
||||
return new Date();
|
||||
return new Date();
|
||||
}
|
||||
|
||||
@JsonFormat(shape = JsonFormat.Shape.NUMBER)
|
||||
public Date getDateNum() {
|
||||
return new Date();
|
||||
return new Date();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,8 @@ public class Person {
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
|
||||
public Person(){}
|
||||
public Person() {
|
||||
}
|
||||
|
||||
public Person(String firstName, String lastName) {
|
||||
this.id = UUID.randomUUID();
|
||||
|
||||
+4
-2
@@ -12,10 +12,12 @@ import java.util.List;
|
||||
* @author Alex Theedom www.readlearncode.com
|
||||
* @version 1.0
|
||||
*/
|
||||
@JsonIgnoreProperties({"medium"})
|
||||
@JsonIgnoreProperties({ "medium" })
|
||||
public class Course extends Item {
|
||||
|
||||
public enum Medium {CLASSROOM, ONLINE}
|
||||
public enum Medium {
|
||||
CLASSROOM, ONLINE
|
||||
}
|
||||
|
||||
public enum Level {
|
||||
BEGINNER("Beginner", 1), INTERMEDIATE("Intermediate", 2), ADVANCED("Advanced", 3);
|
||||
|
||||
@@ -12,10 +12,7 @@ public class ItemIdAddedToUser extends Event {
|
||||
private final Long quantity;
|
||||
|
||||
@JsonCreator
|
||||
public ItemIdAddedToUser(@JsonProperty("id") String id,
|
||||
@JsonProperty("timestamp") Long timestamp,
|
||||
@JsonProperty("itemId") String itemId,
|
||||
@JsonProperty("quantity") Long quantity) {
|
||||
public ItemIdAddedToUser(@JsonProperty("id") String id, @JsonProperty("timestamp") Long timestamp, @JsonProperty("itemId") String itemId, @JsonProperty("quantity") Long quantity) {
|
||||
super(id, timestamp);
|
||||
this.itemId = itemId;
|
||||
this.quantity = quantity;
|
||||
|
||||
@@ -10,10 +10,7 @@ public class ItemIdRemovedFromUser extends Event {
|
||||
private final Long quantity;
|
||||
|
||||
@JsonCreator
|
||||
public ItemIdRemovedFromUser(@JsonProperty("id") String id,
|
||||
@JsonProperty("timestamp") Long timestamp,
|
||||
@JsonProperty("itemId") String itemId,
|
||||
@JsonProperty("quantity") Long quantity) {
|
||||
public ItemIdRemovedFromUser(@JsonProperty("id") String id, @JsonProperty("timestamp") Long timestamp, @JsonProperty("itemId") String itemId, @JsonProperty("quantity") Long quantity) {
|
||||
super(id, timestamp);
|
||||
this.itemId = itemId;
|
||||
this.quantity = quantity;
|
||||
|
||||
@@ -13,7 +13,9 @@ import java.util.List;
|
||||
@CustomCourseAnnotation
|
||||
public class Course extends Item {
|
||||
|
||||
public enum Medium {CLASSROOM, ONLINE}
|
||||
public enum Medium {
|
||||
CLASSROOM, ONLINE
|
||||
}
|
||||
|
||||
public enum Level {
|
||||
BEGINNER("Beginner", 1), INTERMEDIATE("Intermediate", 2), ADVANCED("Advanced", 3);
|
||||
|
||||
+2
-2
@@ -14,7 +14,7 @@ import java.lang.annotation.RetentionPolicy;
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@JacksonAnnotationsInside
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
@JsonPropertyOrder({"title", "price", "id", "duration", "authors", "level"})
|
||||
@JsonIgnoreProperties({"prerequisite"})
|
||||
@JsonPropertyOrder({ "title", "price", "id", "duration", "authors", "level" })
|
||||
@JsonIgnoreProperties({ "prerequisite" })
|
||||
public @interface CustomCourseAnnotation {
|
||||
}
|
||||
|
||||
@@ -20,7 +20,8 @@ public class Item {
|
||||
private List<Person> authors = new ArrayList<>();
|
||||
private float price;
|
||||
|
||||
public Item(){}
|
||||
public Item() {
|
||||
}
|
||||
|
||||
public Item(String title, Author author) {
|
||||
this.id = UUID.randomUUID();
|
||||
|
||||
@@ -16,7 +16,7 @@ import java.util.List;
|
||||
* @version 1.0
|
||||
*/
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
@JsonPropertyOrder({"lastName", "items", "firstName", "id"})
|
||||
@JsonPropertyOrder({ "lastName", "items", "firstName", "id" })
|
||||
public class Author extends Person {
|
||||
|
||||
@JsonIgnore
|
||||
|
||||
@@ -18,12 +18,8 @@ public class Order {
|
||||
private Type type;
|
||||
private int internalAudit;
|
||||
|
||||
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME,
|
||||
include = JsonTypeInfo.As.PROPERTY,
|
||||
property = "ordertype")
|
||||
@JsonSubTypes({
|
||||
@JsonSubTypes.Type(value = InternalType.class, name = "internal")
|
||||
})
|
||||
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "ordertype")
|
||||
@JsonSubTypes({ @JsonSubTypes.Type(value = InternalType.class, name = "internal") })
|
||||
public static class Type {
|
||||
public long id;
|
||||
public String name;
|
||||
|
||||
+5
-2
@@ -24,8 +24,11 @@ public class ActorJacksonSerializer extends StdSerializer<ActorJackson> {
|
||||
jsonGenerator.writeStartObject();
|
||||
jsonGenerator.writeStringField("imdbId", actor.getImdbId());
|
||||
jsonGenerator.writeObjectField("dateOfBirth", actor.getDateOfBirth() != null ? sdf.format(actor.getDateOfBirth()) : null);
|
||||
jsonGenerator.writeNumberField("N° Film: ", actor.getFilmography() != null ? actor.getFilmography().size() : null);
|
||||
jsonGenerator.writeStringField("filmography", actor.getFilmography().stream().collect(Collectors.joining("-")));
|
||||
jsonGenerator.writeNumberField("N° Film: ", actor.getFilmography() != null ? actor.getFilmography()
|
||||
.size() : null);
|
||||
jsonGenerator.writeStringField("filmography", actor.getFilmography()
|
||||
.stream()
|
||||
.collect(Collectors.joining("-")));
|
||||
jsonGenerator.writeEndObject();
|
||||
}
|
||||
}
|
||||
@@ -9,10 +9,9 @@ import com.fasterxml.jackson.databind.KeyDeserializer;
|
||||
|
||||
public class MyPairDeserializer extends KeyDeserializer {
|
||||
|
||||
@Override
|
||||
public MyPair deserializeKey(String key, DeserializationContext ctxt)
|
||||
throws IOException, JsonProcessingException {
|
||||
@Override
|
||||
public MyPair deserializeKey(String key, DeserializationContext ctxt) throws IOException, JsonProcessingException {
|
||||
|
||||
return new MyPair(key);
|
||||
}
|
||||
return new MyPair(key);
|
||||
}
|
||||
}
|
||||
@@ -12,14 +12,12 @@ import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
|
||||
public class MyPairSerializer extends JsonSerializer<MyPair> {
|
||||
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
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());
|
||||
}
|
||||
@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());
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.baeldung.jackson.serialization.jsongetter;
|
||||
|
||||
|
||||
import com.baeldung.jackson.domain.Item;
|
||||
import com.baeldung.jackson.domain.Person;
|
||||
import com.fasterxml.jackson.annotation.JsonGetter;
|
||||
|
||||
+1
-2
@@ -1,6 +1,5 @@
|
||||
package com.baeldung.jackson.serialization.jsonpropertyorder;
|
||||
|
||||
|
||||
import com.baeldung.jackson.domain.Item;
|
||||
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
|
||||
|
||||
@@ -13,7 +12,7 @@ import java.util.List;
|
||||
* @author Alex Theedom www.readlearncode.com
|
||||
* @version 1.0
|
||||
*/
|
||||
@JsonPropertyOrder({"items", "firstName", "lastName", "id"})
|
||||
@JsonPropertyOrder({ "items", "firstName", "lastName", "id" })
|
||||
public class Author extends Person {
|
||||
|
||||
List<Item> items = new ArrayList<>();
|
||||
|
||||
@@ -19,7 +19,8 @@ public class Book extends Item {
|
||||
private Date published;
|
||||
private BigDecimal pages;
|
||||
|
||||
public Book(){}
|
||||
public Book() {
|
||||
}
|
||||
|
||||
public Book(String title, Author author) {
|
||||
super(title, author);
|
||||
|
||||
+2
-4
@@ -17,8 +17,7 @@ import java.util.Date;
|
||||
*/
|
||||
public class CustomDateSerializer extends StdSerializer<Date> {
|
||||
|
||||
private static SimpleDateFormat formatter =
|
||||
new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
|
||||
private static SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
|
||||
|
||||
public CustomDateSerializer() {
|
||||
this(null);
|
||||
@@ -29,8 +28,7 @@ public class CustomDateSerializer extends StdSerializer<Date> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serialize(Date value, JsonGenerator gen, SerializerProvider arg2)
|
||||
throws IOException, JsonProcessingException {
|
||||
public void serialize(Date value, JsonGenerator gen, SerializerProvider arg2) throws IOException, JsonProcessingException {
|
||||
gen.writeString(formatter.format(value));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,8 @@ public class Item {
|
||||
private List<Person> authors = new ArrayList<>();
|
||||
private float price;
|
||||
|
||||
public Item(){}
|
||||
public Item() {
|
||||
}
|
||||
|
||||
public Item(String title, Author author) {
|
||||
this.id = UUID.randomUUID();
|
||||
|
||||
@@ -14,7 +14,9 @@ import java.util.List;
|
||||
*/
|
||||
public class Course extends Item {
|
||||
|
||||
public enum Medium {CLASSROOM, ONLINE}
|
||||
public enum Medium {
|
||||
CLASSROOM, ONLINE
|
||||
}
|
||||
|
||||
public enum Level {
|
||||
BEGINNER("Beginner", 1), INTERMEDIATE("Intermediate", 2), ADVANCED("Advanced", 3);
|
||||
|
||||
Reference in New Issue
Block a user