From 99ad241f9d14739a08ee86bd2ab530f9ace5d832 Mon Sep 17 00:00:00 2001 From: Alex Theedom Date: Tue, 6 Dec 2016 13:30:04 +0000 Subject: [PATCH] Add Spring MVC form and binding example --- jackson-annotations/pom.xml | 249 ++++++++++++++++++ .../deserialization/jacksoninject/Author.java | 32 +++ .../deserialization/jacksoninject/Person.java | 49 ++++ .../jsonanysetter/Inventory.java | 38 +++ .../deserialization/jsoncreator/Author.java | 38 +++ .../jsondeserialize/Author.java | 30 +++ .../deserialization/jsondeserialize/Book.java | 53 ++++ .../CustomDateDeserializer.java | 42 +++ .../deserialization/jsondeserialize/Item.java | 61 +++++ .../deserialization/jsonsetter/Author.java | 38 +++ .../jacksonannotation/domain/Author.java | 27 ++ .../jacksonannotation/domain/Book.java | 48 ++++ .../jacksonannotation/domain/Course.java | 71 +++++ .../jacksonannotation/domain/Customer.java | 24 ++ .../jacksonannotation/domain/Inventory.java | 29 ++ .../jacksonannotation/domain/Item.java | 59 +++++ .../jacksonannotation/domain/Order.java | 50 ++++ .../jacksonannotation/domain/Person.java | 44 ++++ .../general/jsonfilter/Author.java | 32 +++ .../general/jsonformat/Book.java | 56 ++++ .../general/jsonidentityinfo/Author.java | 33 +++ .../general/jsonidentityinfo/Course.java | 72 +++++ .../general/jsonidentityinfo/Item.java | 65 +++++ .../general/jsonidentityinfo/Person.java | 44 ++++ .../general/jsonproperty/Author.java | 29 ++ .../general/jsonproperty/Book.java | 61 +++++ .../general/jsonproperty/Item.java | 61 +++++ .../general/jsonunwrapped/Order.java | 54 ++++ .../general/jsonview/Order.java | 57 ++++ .../general/jsonview/Views.java | 15 ++ .../general/reference/Author.java | 30 +++ .../general/reference/Course.java | 72 +++++ .../general/reference/Item.java | 63 +++++ .../general/reference/Person.java | 44 ++++ .../inclusion/jsonautodetect/Order.java | 54 ++++ .../inclusion/jsonignore/Author.java | 29 ++ .../inclusion/jsonignore/Person.java | 47 ++++ .../jsonignoreproperties/Course.java | 76 ++++++ .../inclusion/jsonignoretype/Order.java | 44 ++++ .../inclusion/jsoninclude/Author.java | 34 +++ .../miscellaneous/custom/Course.java | 74 ++++++ .../custom/CustomCourseAnnotation.java | 20 ++ .../miscellaneous/custom/Item.java | 62 +++++ .../miscellaneous/disable/Author.java | 36 +++ .../miscellaneous/mixin/Author.java | 30 +++ .../miscellaneous/mixin/IgnoreListMixIn.java | 13 + .../jacksonannotation/polymorphism/Order.java | 66 +++++ .../jsonanygetter/Inventory.java | 43 +++ .../serialization/jsongetter/Author.java | 33 +++ .../jsonpropertyorder/Author.java | 32 +++ .../jsonpropertyorder/Person.java | 42 +++ .../serialization/jsonrawvalue/Customer.java | 28 ++ .../serialization/jsonrootname/Author.java | 32 +++ .../serialization/jsonserialize/Author.java | 30 +++ .../serialization/jsonserialize/Book.java | 53 ++++ .../jsonserialize/CustomDateSerializer.java | 36 +++ .../serialization/jsonserialize/Item.java | 61 +++++ .../serialization/jsonvalue/Course.java | 76 ++++++ .../jacksoninject/JacksonInjectTest.java | 45 ++++ .../jsonanysetter/JsonAnySetterTest.java | 37 +++ .../jsoncreator/JsonCreatorTest.java | 44 ++++ .../jsondeserialize/JsonDeserializeTest.java | 34 +++ .../jsonsetter/JsonSetterTest.java | 32 +++ .../general/jsonfilter/JsonFilterTest.java | 42 +++ .../general/jsonformat/JsonFormatTest.java | 66 +++++ .../JsonIdentityInfoTest.java | 60 +++++ .../jsonproperty/JsonPropertyTest.java | 73 +++++ .../jsonunwrapped/JsonUnwrappedTest.java | 44 ++++ .../general/jsonview/JsonViewTest.java | 72 +++++ .../general/reference/ReferenceTest.java | 59 +++++ .../jsonautodetect/JsonAutoDetectTest.java | 52 ++++ .../inclusion/jsonignore/JsonIgnoreTest.java | 41 +++ .../JsonIgnorePropertiesTest.java | 52 ++++ .../jsonignoretype/JsonIgnoreTypeTest.java | 40 +++ .../jsoninclude/JsonIncludeTest.java | 41 +++ .../miscellaneous/custom/CustomTest.java | 51 ++++ .../miscellaneous/disable/DisableTest.java | 58 ++++ .../miscellaneous/mixin/MixInTest.java | 58 ++++ .../polymorphism/PolymorphismTest.java | 64 +++++ .../jsonanygetter/JsonAnyGetterTest.java | 49 ++++ .../jsongetter/JsonGetterTest.java | 40 +++ .../JsonPropertyOrderTest.java | 42 +++ .../jsonrawvalue/JsonRawValueTest.java | 41 +++ .../jsonrootname/JsonRootNameTest.java | 46 ++++ .../jsonserialize/JsonSerializeTest.java | 58 ++++ .../jsonvalue/JsonValueTest.java | 27 ++ .../author-jsonpropertyorder-schema.json | 29 ++ pom.xml | 1 + 88 files changed, 4219 insertions(+) create mode 100644 jackson-annotations/pom.xml create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/deserialization/jacksoninject/Author.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/deserialization/jacksoninject/Person.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/deserialization/jsonanysetter/Inventory.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/deserialization/jsoncreator/Author.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/deserialization/jsondeserialize/Author.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/deserialization/jsondeserialize/Book.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/deserialization/jsondeserialize/CustomDateDeserializer.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/deserialization/jsondeserialize/Item.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/deserialization/jsonsetter/Author.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/domain/Author.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/domain/Book.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/domain/Course.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/domain/Customer.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/domain/Inventory.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/domain/Item.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/domain/Order.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/domain/Person.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/jsonfilter/Author.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/jsonformat/Book.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/jsonidentityinfo/Author.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/jsonidentityinfo/Course.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/jsonidentityinfo/Item.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/jsonidentityinfo/Person.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/jsonproperty/Author.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/jsonproperty/Book.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/jsonproperty/Item.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/jsonunwrapped/Order.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/jsonview/Order.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/jsonview/Views.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/reference/Author.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/reference/Course.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/reference/Item.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/reference/Person.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/inclusion/jsonautodetect/Order.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/inclusion/jsonignore/Author.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/inclusion/jsonignore/Person.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/inclusion/jsonignoreproperties/Course.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/inclusion/jsonignoretype/Order.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/inclusion/jsoninclude/Author.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/miscellaneous/custom/Course.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/miscellaneous/custom/CustomCourseAnnotation.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/miscellaneous/custom/Item.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/miscellaneous/disable/Author.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/miscellaneous/mixin/Author.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/miscellaneous/mixin/IgnoreListMixIn.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/polymorphism/Order.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/serialization/jsonanygetter/Inventory.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/serialization/jsongetter/Author.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/serialization/jsonpropertyorder/Author.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/serialization/jsonpropertyorder/Person.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/serialization/jsonrawvalue/Customer.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/serialization/jsonrootname/Author.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/serialization/jsonserialize/Author.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/serialization/jsonserialize/Book.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/serialization/jsonserialize/CustomDateSerializer.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/serialization/jsonserialize/Item.java create mode 100644 jackson-annotations/src/main/java/com/baeldung/jacksonannotation/serialization/jsonvalue/Course.java create mode 100644 jackson-annotations/src/test/java/com/baeldung/jacksonannotation/deserialization/jacksoninject/JacksonInjectTest.java create mode 100644 jackson-annotations/src/test/java/com/baeldung/jacksonannotation/deserialization/jsonanysetter/JsonAnySetterTest.java create mode 100644 jackson-annotations/src/test/java/com/baeldung/jacksonannotation/deserialization/jsoncreator/JsonCreatorTest.java create mode 100644 jackson-annotations/src/test/java/com/baeldung/jacksonannotation/deserialization/jsondeserialize/JsonDeserializeTest.java create mode 100644 jackson-annotations/src/test/java/com/baeldung/jacksonannotation/deserialization/jsonsetter/JsonSetterTest.java create mode 100644 jackson-annotations/src/test/java/com/baeldung/jacksonannotation/general/jsonfilter/JsonFilterTest.java create mode 100644 jackson-annotations/src/test/java/com/baeldung/jacksonannotation/general/jsonformat/JsonFormatTest.java create mode 100644 jackson-annotations/src/test/java/com/baeldung/jacksonannotation/general/jsonidentityinfo/JsonIdentityInfoTest.java create mode 100644 jackson-annotations/src/test/java/com/baeldung/jacksonannotation/general/jsonproperty/JsonPropertyTest.java create mode 100644 jackson-annotations/src/test/java/com/baeldung/jacksonannotation/general/jsonunwrapped/JsonUnwrappedTest.java create mode 100644 jackson-annotations/src/test/java/com/baeldung/jacksonannotation/general/jsonview/JsonViewTest.java create mode 100644 jackson-annotations/src/test/java/com/baeldung/jacksonannotation/general/reference/ReferenceTest.java create mode 100644 jackson-annotations/src/test/java/com/baeldung/jacksonannotation/inclusion/jsonautodetect/JsonAutoDetectTest.java create mode 100644 jackson-annotations/src/test/java/com/baeldung/jacksonannotation/inclusion/jsonignore/JsonIgnoreTest.java create mode 100644 jackson-annotations/src/test/java/com/baeldung/jacksonannotation/inclusion/jsonignoreproperties/JsonIgnorePropertiesTest.java create mode 100644 jackson-annotations/src/test/java/com/baeldung/jacksonannotation/inclusion/jsonignoretype/JsonIgnoreTypeTest.java create mode 100644 jackson-annotations/src/test/java/com/baeldung/jacksonannotation/inclusion/jsoninclude/JsonIncludeTest.java create mode 100644 jackson-annotations/src/test/java/com/baeldung/jacksonannotation/miscellaneous/custom/CustomTest.java create mode 100644 jackson-annotations/src/test/java/com/baeldung/jacksonannotation/miscellaneous/disable/DisableTest.java create mode 100644 jackson-annotations/src/test/java/com/baeldung/jacksonannotation/miscellaneous/mixin/MixInTest.java create mode 100644 jackson-annotations/src/test/java/com/baeldung/jacksonannotation/polymorphism/PolymorphismTest.java create mode 100644 jackson-annotations/src/test/java/com/baeldung/jacksonannotation/serialization/jsonanygetter/JsonAnyGetterTest.java create mode 100644 jackson-annotations/src/test/java/com/baeldung/jacksonannotation/serialization/jsongetter/JsonGetterTest.java create mode 100644 jackson-annotations/src/test/java/com/baeldung/jacksonannotation/serialization/jsonpropertyorder/JsonPropertyOrderTest.java create mode 100644 jackson-annotations/src/test/java/com/baeldung/jacksonannotation/serialization/jsonrawvalue/JsonRawValueTest.java create mode 100644 jackson-annotations/src/test/java/com/baeldung/jacksonannotation/serialization/jsonrootname/JsonRootNameTest.java create mode 100644 jackson-annotations/src/test/java/com/baeldung/jacksonannotation/serialization/jsonserialize/JsonSerializeTest.java create mode 100644 jackson-annotations/src/test/java/com/baeldung/jacksonannotation/serialization/jsonvalue/JsonValueTest.java create mode 100644 jackson-annotations/src/test/resources/author-jsonpropertyorder-schema.json diff --git a/jackson-annotations/pom.xml b/jackson-annotations/pom.xml new file mode 100644 index 0000000000..98d4cc9a87 --- /dev/null +++ b/jackson-annotations/pom.xml @@ -0,0 +1,249 @@ + + 4.0.0 + + com.baeldung + jackson-annotations + 1.0-SNAPSHOT + jar + + jackson-annotations + http://www.baeldung.com + + + + + + + com.google.guava + guava + ${guava.version} + + + + commons-io + commons-io + ${commons-io.version} + + + + + com.fasterxml.jackson.dataformat + jackson-dataformat-xml + ${jackson.version} + + + + org.apache.commons + commons-collections4 + ${commons-collections4.version} + + + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + + + + + + + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + + + + com.fasterxml.jackson.datatype + jackson-datatype-jsr310 + ${jackson.version} + + + + com.fasterxml.jackson.datatype + jackson-datatype-joda + ${jackson.version} + + + + com.fasterxml.jackson.module + jackson-module-jsonSchema + ${jackson.version} + + + + joda-time + joda-time + ${joda-time.version} + + + + com.google.code.gson + gson + ${gson.version} + + + + + + io.rest-assured + rest-assured + 3.0.1 + test + + + + + io.rest-assured + json-schema-validator + 3.0.0 + test + + + + + io.rest-assured + json-path + 3.0.1 + test + + + + com.github.fge + json-schema-validator + 2.2.6 + test + + + + junit + junit + ${junit.version} + test + + + + org.assertj + assertj-core + 3.6.1 + test + + + + + + + + + + + + + ch.qos.logback + logback-classic + ${logback.version} + + + + + org.slf4j + slf4j-api + ${org.slf4j.version} + + + + org.slf4j + jcl-over-slf4j + ${org.slf4j.version} + + + + + org.slf4j + log4j-over-slf4j + ${org.slf4j.version} + + + + + + jackson + + + src/main/resources + true + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + ${maven-compiler-plugin.source} + ${maven-compiler-plugin.source} + + + + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + + + + + + + + 4.3.11.Final + 5.1.38 + + + 2.8.5 + + + 1.7.21 + 1.1.7 + + + 5.1.3.Final + 2.9.6 + 2.8.0 + 4.1 + + + 20.0 + 3.5 + 2.5 + + + 1.3 + 4.12 + 2.2.26 + + 4.4.1 + 4.5 + + 2.9.0 + + + 3.5.1 + 2.6 + 2.19.1 + 2.7 + 1.4.18 + + UTF-8 + 1.8 + + + + diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/deserialization/jacksoninject/Author.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/deserialization/jacksoninject/Author.java new file mode 100644 index 0000000000..2d816282b2 --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/deserialization/jacksoninject/Author.java @@ -0,0 +1,32 @@ +package com.baeldung.jacksonannotation.deserialization.jacksoninject; + +import com.baeldung.jacksonannotation.domain.Item; + +import java.util.ArrayList; +import java.util.List; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class Author extends Person { + + List items = new ArrayList<>(); + + public Author(){ + super(); + } + public Author(String firstName, String lastName) { + super(firstName, lastName); + } + + public List getItems() { + return items; + } + + public void setItems(List items) { + this.items = items; + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/deserialization/jacksoninject/Person.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/deserialization/jacksoninject/Person.java new file mode 100644 index 0000000000..f7e44b3dd4 --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/deserialization/jacksoninject/Person.java @@ -0,0 +1,49 @@ +package com.baeldung.jacksonannotation.deserialization.jacksoninject; + +import com.fasterxml.jackson.annotation.JacksonInject; + +import java.util.UUID; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class Person { + + @JacksonInject + private UUID id; + private String firstName; + private String lastName; + + public Person(){ + + } + + public Person(String firstName, String lastName) { + this.id = UUID.randomUUID(); + this.firstName = firstName; + this.lastName = lastName; + } + + 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 UUID getId() { + return id; + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/deserialization/jsonanysetter/Inventory.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/deserialization/jsonanysetter/Inventory.java new file mode 100644 index 0000000000..22b1540896 --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/deserialization/jsonanysetter/Inventory.java @@ -0,0 +1,38 @@ +package com.baeldung.jacksonannotation.deserialization.jsonanysetter; + +import com.baeldung.jacksonannotation.domain.Author; +import com.baeldung.jacksonannotation.domain.Item; +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonIgnore; + +import java.util.HashMap; +import java.util.Map; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + * @see JsonAnyGetter + */ +public class Inventory { + + private Map stock = new HashMap<>(); + + private Map countryDeliveryCost = new HashMap<>(); + + @JsonIgnore + public Map getStock() { + return stock; + } + + public Map getCountryDeliveryCost() { + return countryDeliveryCost; + } + + @JsonAnySetter + public void addCountryDeliveryCost(String country, Float cost) { + countryDeliveryCost.put(country, cost); + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/deserialization/jsoncreator/Author.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/deserialization/jsoncreator/Author.java new file mode 100644 index 0000000000..c9ed3eaf47 --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/deserialization/jsoncreator/Author.java @@ -0,0 +1,38 @@ +package com.baeldung.jacksonannotation.deserialization.jsoncreator; + + +import com.baeldung.jacksonannotation.domain.Item; +import com.baeldung.jacksonannotation.domain.Person; +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonGetter; +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.util.ArrayList; +import java.util.List; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + * @see JsonGetter + */ +public class Author extends Person { + + List items = new ArrayList<>(); + + @JsonCreator + public Author( + @JsonProperty("christianName") String firstName, + @JsonProperty("surname") String lastName) { + super(firstName, lastName); + } + + public List getItems() { + return items; + } + + public void setItems(List items) { + this.items = items; + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/deserialization/jsondeserialize/Author.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/deserialization/jsondeserialize/Author.java new file mode 100644 index 0000000000..5830d099e1 --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/deserialization/jsondeserialize/Author.java @@ -0,0 +1,30 @@ +package com.baeldung.jacksonannotation.deserialization.jsondeserialize; + +import com.baeldung.jacksonannotation.domain.Item; +import com.baeldung.jacksonannotation.domain.Person; + +import java.util.ArrayList; +import java.util.List; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class Author extends Person { + + List items = new ArrayList<>(); + + public Author(String firstName, String lastName) { + super(firstName, lastName); + } + + public List getItems() { + return items; + } + + public void setItems(List items) { + this.items = items; + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/deserialization/jsondeserialize/Book.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/deserialization/jsondeserialize/Book.java new file mode 100644 index 0000000000..eb2c1c967c --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/deserialization/jsondeserialize/Book.java @@ -0,0 +1,53 @@ +package com.baeldung.jacksonannotation.deserialization.jsondeserialize; + +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; + +import java.math.BigDecimal; +import java.util.Date; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class Book extends Item { + + private String ISBN; + + @JsonDeserialize(using = CustomDateDeserializer.class) + private Date published; + private BigDecimal pages; + + public Book() { + super(); + } + + public Book(String title, Author author) { + super(title, author); + } + + public String getISBN() { + return ISBN; + } + + public void setISBN(String ISBN) { + this.ISBN = ISBN; + } + + public Date getPublished() { + return published; + } + + public void setPublished(Date published) { + this.published = published; + } + + public BigDecimal getPages() { + return pages; + } + + public void setPages(BigDecimal pages) { + this.pages = pages; + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/deserialization/jsondeserialize/CustomDateDeserializer.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/deserialization/jsondeserialize/CustomDateDeserializer.java new file mode 100644 index 0000000000..c8ad9979ab --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/deserialization/jsondeserialize/CustomDateDeserializer.java @@ -0,0 +1,42 @@ +package com.baeldung.jacksonannotation.deserialization.jsondeserialize; + +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; + +import java.io.IOException; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class CustomDateDeserializer extends StdDeserializer { + + private static SimpleDateFormat formatter = + new SimpleDateFormat("dd-MM-yyyy HH:mm:ss"); + + public CustomDateDeserializer() { + this(null); + } + + public CustomDateDeserializer(Class vc) { + super(vc); + } + + @Override + public Date deserialize(JsonParser jsonparser, DeserializationContext context) + throws IOException, JsonProcessingException { + String date = jsonparser.getText(); + try { + return formatter.parse(date); + } catch (ParseException e) { + throw new RuntimeException(e); + } + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/deserialization/jsondeserialize/Item.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/deserialization/jsondeserialize/Item.java new file mode 100644 index 0000000000..3d2fba74e2 --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/deserialization/jsondeserialize/Item.java @@ -0,0 +1,61 @@ +package com.baeldung.jacksonannotation.deserialization.jsondeserialize; + +import com.baeldung.jacksonannotation.domain.Person; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class Item { + + private UUID id; + private String title; + private List authors = new ArrayList<>(); + private float price; + + public Item(){} + + public Item(String title, Author author) { + this.id = UUID.randomUUID(); + this.title = title; + this.authors.add(author); + } + + public UUID getId() { + return id; + } + + public void setId(UUID id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public List getAuthors() { + return authors; + } + + public void setAuthors(List authors) { + this.authors = authors; + } + + public float getPrice() { + return price; + } + + public void setPrice(float price) { + this.price = price; + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/deserialization/jsonsetter/Author.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/deserialization/jsonsetter/Author.java new file mode 100644 index 0000000000..534984f731 --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/deserialization/jsonsetter/Author.java @@ -0,0 +1,38 @@ +package com.baeldung.jacksonannotation.deserialization.jsonsetter; + +import com.baeldung.jacksonannotation.domain.Item; +import com.baeldung.jacksonannotation.domain.Person; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonSetter; + +import java.util.ArrayList; +import java.util.List; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class Author extends Person { + + private List items = new ArrayList<>(); + + public Author(){ + super(); + } + + public Author(String firstName, String lastName) { + super(firstName, lastName); + } + + @JsonIgnore + public List getItems() { + return items; + } + + @JsonSetter("publications") + public void setItems(List items) { + this.items = items; + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/domain/Author.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/domain/Author.java new file mode 100644 index 0000000000..cee1576a79 --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/domain/Author.java @@ -0,0 +1,27 @@ +package com.baeldung.jacksonannotation.domain; + +import java.util.ArrayList; +import java.util.List; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class Author extends Person { + + private List items = new ArrayList<>(); + + public Author(String firstName, String lastName) { + super(firstName, lastName); + } + + public List getItems() { + return items; + } + + public void setItems(List items) { + this.items = items; + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/domain/Book.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/domain/Book.java new file mode 100644 index 0000000000..82980673f1 --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/domain/Book.java @@ -0,0 +1,48 @@ +package com.baeldung.jacksonannotation.domain; + +import java.math.BigDecimal; +import java.util.Date; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class Book extends Item { + + private String ISBN; + private Date published; + private BigDecimal pages; + + public Book(){ + } + + public Book(String title, Author author) { + super(title, author); + } + + public String getISBN() { + return ISBN; + } + + public void setISBN(String ISBN) { + this.ISBN = ISBN; + } + + public Date getPublished() { + return published; + } + + public void setPublished(Date published) { + this.published = published; + } + + public BigDecimal getPages() { + return pages; + } + + public void setPages(BigDecimal pages) { + this.pages = pages; + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/domain/Course.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/domain/Course.java new file mode 100644 index 0000000000..6f0f55dc28 --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/domain/Course.java @@ -0,0 +1,71 @@ +package com.baeldung.jacksonannotation.domain; + +import java.util.List; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class Course extends Item { + + public enum Medium {CLASSROOM, ONLINE} + + public enum Level { + BEGINNER("Beginner", 1), INTERMEDIATE("Intermediate", 2), ADVANCED("Advanced", 3); + + private String name; + private int number; + + Level(String name, int number) { + this.name = name; + this.number = number; + } + + public String getName() { + return name; + } + } + + private float duration; + private Medium medium; + private Level level; + private List prerequisite; + + public Course(String title, Author author) { + super(title, author); + } + + public float getDuration() { + return duration; + } + + public void setDuration(float duration) { + this.duration = duration; + } + + public Medium getMedium() { + return medium; + } + + public void setMedium(Medium medium) { + this.medium = medium; + } + + public Level getLevel() { + return level; + } + + public void setLevel(Level level) { + this.level = level; + } + + public List getPrerequisite() { + return prerequisite; + } + + public void setPrerequisite(List prerequisite) { + this.prerequisite = prerequisite; + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/domain/Customer.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/domain/Customer.java new file mode 100644 index 0000000000..0cd49f7934 --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/domain/Customer.java @@ -0,0 +1,24 @@ +package com.baeldung.jacksonannotation.domain; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class Customer extends Person { + + private String configuration; + + public Customer(String firstName, String lastName) { + super(firstName, lastName); + } + + public String getConfiguration() { + return configuration; + } + + public void setConfiguration(String configuration) { + this.configuration = configuration; + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/domain/Inventory.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/domain/Inventory.java new file mode 100644 index 0000000000..e17d2ab406 --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/domain/Inventory.java @@ -0,0 +1,29 @@ +package com.baeldung.jacksonannotation.domain; + +import java.util.HashMap; +import java.util.Map; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class Inventory { + + private Map stock; + + private Map countryDeliveryCost = new HashMap<>(); + + public Map getStock() { + return stock; + } + + public void setStock(Map stock) { + this.stock = stock; + } + + public Map getCountryDeliveryCost() { + return countryDeliveryCost; + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/domain/Item.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/domain/Item.java new file mode 100644 index 0000000000..a45d99f660 --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/domain/Item.java @@ -0,0 +1,59 @@ +package com.baeldung.jacksonannotation.domain; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class Item { + + private UUID id; + private String title; + private List authors = new ArrayList<>(); + private float price; + + public Item(){} + + public Item(String title, Author author) { + this.id = UUID.randomUUID(); + this.title = title; + this.authors.add(author); + } + + public UUID getId() { + return id; + } + + public void setId(UUID id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public List getAuthors() { + return authors; + } + + public void setAuthors(List authors) { + this.authors = authors; + } + + public float getPrice() { + return price; + } + + public void setPrice(float price) { + this.price = price; + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/domain/Order.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/domain/Order.java new file mode 100644 index 0000000000..614a8921e3 --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/domain/Order.java @@ -0,0 +1,50 @@ +package com.baeldung.jacksonannotation.domain; + +import java.util.UUID; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class Order { + + private UUID id; + private Type type; + private int internalAudit; + + public static class Type { + public long id; + public String name; + } + + public Order() { + this.id = UUID.randomUUID(); + } + + public Order(Type type) { + this(); + this.type = type; + } + + public Order(int internalAudit) { + this(); + this.type = new Type(); + this.type.id = 20; + this.type.name = "Order"; + this.internalAudit = internalAudit; + } + + public UUID getId() { + return id; + } + + public Type getType() { + return type; + } + + public void setType(Type type) { + this.type = type; + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/domain/Person.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/domain/Person.java new file mode 100644 index 0000000000..9189b20662 --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/domain/Person.java @@ -0,0 +1,44 @@ +package com.baeldung.jacksonannotation.domain; + +import java.util.UUID; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class Person { + + private UUID id; + private String firstName; + private String lastName; + + public Person(){} + + public Person(String firstName, String lastName) { + this.id = UUID.randomUUID(); + this.firstName = firstName; + this.lastName = lastName; + } + + 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 UUID getId() { + return id; + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/jsonfilter/Author.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/jsonfilter/Author.java new file mode 100644 index 0000000000..1c45bf02c1 --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/jsonfilter/Author.java @@ -0,0 +1,32 @@ +package com.baeldung.jacksonannotation.general.jsonfilter; + +import com.baeldung.jacksonannotation.domain.Item; +import com.baeldung.jacksonannotation.domain.Person; +import com.fasterxml.jackson.annotation.JsonFilter; + +import java.util.ArrayList; +import java.util.List; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +@JsonFilter("authorFilter") +public class Author extends Person { + + private List items = new ArrayList<>(); + + public Author(String firstName, String lastName) { + super(firstName, lastName); + } + + public List getItems() { + return items; + } + + public void setItems(List items) { + this.items = items; + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/jsonformat/Book.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/jsonformat/Book.java new file mode 100644 index 0000000000..d7f3901be5 --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/jsonformat/Book.java @@ -0,0 +1,56 @@ +package com.baeldung.jacksonannotation.general.jsonformat; + +import com.baeldung.jacksonannotation.domain.Author; +import com.baeldung.jacksonannotation.domain.Item; +import com.fasterxml.jackson.annotation.JsonFormat; + +import java.math.BigDecimal; +import java.util.Date; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class Book extends Item { + + private String ISBN; + + @JsonFormat( + shape = JsonFormat.Shape.STRING, + pattern = "dd-MM-yyyy HH:mm:ss") + private Date published; + private BigDecimal pages; + + public Book() { + } + + public Book(String title, Author author) { + super(title, author); + } + + public String getISBN() { + return ISBN; + } + + public void setISBN(String ISBN) { + this.ISBN = ISBN; + } + + public Date getPublished() { + return published; + } + + public void setPublished(Date published) { + this.published = published; + } + + public BigDecimal getPages() { + return pages; + } + + public void setPages(BigDecimal pages) { + this.pages = pages; + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/jsonidentityinfo/Author.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/jsonidentityinfo/Author.java new file mode 100644 index 0000000000..1a60af54dc --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/jsonidentityinfo/Author.java @@ -0,0 +1,33 @@ +package com.baeldung.jacksonannotation.general.jsonidentityinfo; + +import com.fasterxml.jackson.annotation.JsonIdentityInfo; +import com.fasterxml.jackson.annotation.ObjectIdGenerators; + +import java.util.ArrayList; +import java.util.List; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +@JsonIdentityInfo( + generator = ObjectIdGenerators.PropertyGenerator.class, + property = "id") +public class Author extends Person { + + private List items = new ArrayList<>(); + + public Author(String firstName, String lastName) { + super(firstName, lastName); + } + + public List getItems() { + return items; + } + + public void setItems(List items) { + this.items = items; + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/jsonidentityinfo/Course.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/jsonidentityinfo/Course.java new file mode 100644 index 0000000000..d7195a91d8 --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/jsonidentityinfo/Course.java @@ -0,0 +1,72 @@ +package com.baeldung.jacksonannotation.general.jsonidentityinfo; + + +import java.util.List; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class Course extends Item { + + public enum Medium {CLASSROOM, ONLINE} + + public enum Level { + BEGINNER("Beginner", 1), INTERMEDIATE("Intermediate", 2), ADVANCED("Advanced", 3); + + private String name; + private int number; + + Level(String name, int number) { + this.name = name; + this.number = number; + } + + public String getName() { + return name; + } + } + + private float duration; + private Medium medium; + private Level level; + private List prerequisite; + + public Course(String title, Author author) { + super(title, author); + } + + public float getDuration() { + return duration; + } + + public void setDuration(float duration) { + this.duration = duration; + } + + public Medium getMedium() { + return medium; + } + + public void setMedium(Medium medium) { + this.medium = medium; + } + + public Level getLevel() { + return level; + } + + public void setLevel(Level level) { + this.level = level; + } + + public List getPrerequisite() { + return prerequisite; + } + + public void setPrerequisite(List prerequisite) { + this.prerequisite = prerequisite; + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/jsonidentityinfo/Item.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/jsonidentityinfo/Item.java new file mode 100644 index 0000000000..b5438d4639 --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/jsonidentityinfo/Item.java @@ -0,0 +1,65 @@ +package com.baeldung.jacksonannotation.general.jsonidentityinfo; + +import com.fasterxml.jackson.annotation.JsonIdentityInfo; +import com.fasterxml.jackson.annotation.ObjectIdGenerators; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +@JsonIdentityInfo( + generator = ObjectIdGenerators.PropertyGenerator.class, + property = "id") +public class Item { + + private UUID id; + private String title; + private List authors = new ArrayList<>(); + private float price; + + public Item(){} + + public Item(String title, Author author) { + this.id = UUID.randomUUID(); + this.title = title; + this.authors.add(author); + } + + public UUID getId() { + return id; + } + + public void setId(UUID id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public List getAuthors() { + return authors; + } + + public void setAuthors(List authors) { + this.authors = authors; + } + + public float getPrice() { + return price; + } + + public void setPrice(float price) { + this.price = price; + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/jsonidentityinfo/Person.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/jsonidentityinfo/Person.java new file mode 100644 index 0000000000..1cd3c73b0b --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/jsonidentityinfo/Person.java @@ -0,0 +1,44 @@ +package com.baeldung.jacksonannotation.general.jsonidentityinfo; + +import java.util.UUID; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class Person { + + private UUID id; + private String firstName; + private String lastName; + + public Person(){} + + public Person(String firstName, String lastName) { + this.id = UUID.randomUUID(); + this.firstName = firstName; + this.lastName = lastName; + } + + 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 UUID getId() { + return id; + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/jsonproperty/Author.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/jsonproperty/Author.java new file mode 100644 index 0000000000..7039e60e9b --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/jsonproperty/Author.java @@ -0,0 +1,29 @@ +package com.baeldung.jacksonannotation.general.jsonproperty; + +import com.baeldung.jacksonannotation.domain.Person; + +import java.util.ArrayList; +import java.util.List; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class Author extends Person { + + private List items = new ArrayList<>(); + + public Author(String firstName, String lastName) { + super(firstName, lastName); + } + + public List getItems() { + return items; + } + + public void setItems(List items) { + this.items = items; + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/jsonproperty/Book.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/jsonproperty/Book.java new file mode 100644 index 0000000000..95d3070eba --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/jsonproperty/Book.java @@ -0,0 +1,61 @@ +package com.baeldung.jacksonannotation.general.jsonproperty; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.math.BigDecimal; +import java.util.Date; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class Book extends Item { + + private String ISBN; + private Date published; + private BigDecimal pages; + private String binding; + + public Book() { + } + + public Book(String title, Author author) { + super(title, author); + } + + public String getISBN() { + return ISBN; + } + + public void setISBN(String ISBN) { + this.ISBN = ISBN; + } + + public Date getPublished() { + return published; + } + + public void setPublished(Date published) { + this.published = published; + } + + public BigDecimal getPages() { + return pages; + } + + public void setPages(BigDecimal pages) { + this.pages = pages; + } + + @JsonProperty("binding") + public String coverBinding() { + return binding; + } + + @JsonProperty("binding") + public void configureBinding(String binding) { + this.binding = binding; + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/jsonproperty/Item.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/jsonproperty/Item.java new file mode 100644 index 0000000000..510adaa599 --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/jsonproperty/Item.java @@ -0,0 +1,61 @@ +package com.baeldung.jacksonannotation.general.jsonproperty; + +import com.baeldung.jacksonannotation.domain.Person; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class Item { + + private UUID id; + private String title; + private List authors = new ArrayList<>(); + private float price; + + public Item(){} + + public Item(String title, Author author) { + this.id = UUID.randomUUID(); + this.title = title; + this.authors.add(author); + } + + public UUID getId() { + return id; + } + + public void setId(UUID id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public List getAuthors() { + return authors; + } + + public void setAuthors(List authors) { + this.authors = authors; + } + + public float getPrice() { + return price; + } + + public void setPrice(float price) { + this.price = price; + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/jsonunwrapped/Order.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/jsonunwrapped/Order.java new file mode 100644 index 0000000000..8393473a2c --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/jsonunwrapped/Order.java @@ -0,0 +1,54 @@ +package com.baeldung.jacksonannotation.general.jsonunwrapped; + +import com.fasterxml.jackson.annotation.JsonUnwrapped; + +import java.util.UUID; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class Order { + + private UUID id; + + @JsonUnwrapped + private Type type; + private int internalAudit; + + public static class Type { + public long id; + public String name; + } + + public Order() { + this.id = UUID.randomUUID(); + } + + public Order(Type type) { + this(); + this.type = type; + } + + public Order(int internalAudit) { + this(); + this.type = new Type(); + this.type.id = 20; + this.type.name = "Order"; + this.internalAudit = internalAudit; + } + + public UUID getId() { + return id; + } + + public Type getType() { + return type; + } + + public void setType(Type type) { + this.type = type; + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/jsonview/Order.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/jsonview/Order.java new file mode 100644 index 0000000000..f9aa4fc768 --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/jsonview/Order.java @@ -0,0 +1,57 @@ +package com.baeldung.jacksonannotation.general.jsonview; + +import com.fasterxml.jackson.annotation.JsonView; + +import java.util.UUID; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class Order { + + @JsonView(Views.Public.class) + private UUID id; + + @JsonView(Views.Public.class) + private Type type; + + @JsonView(Views.Internal.class) + private int internalAudit; + + public static class Type { + public long id; + public String name; + } + + public Order() { + this.id = UUID.randomUUID(); + } + + public Order(Type type) { + this(); + this.type = type; + } + + public Order(int internalAudit) { + this(); + this.type = new Type(); + this.type.id = 20; + this.type.name = "Order"; + this.internalAudit = internalAudit; + } + + public UUID getId() { + return id; + } + + public Type getType() { + return type; + } + + public void setType(Type type) { + this.type = type; + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/jsonview/Views.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/jsonview/Views.java new file mode 100644 index 0000000000..116e212295 --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/jsonview/Views.java @@ -0,0 +1,15 @@ +package com.baeldung.jacksonannotation.general.jsonview; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class Views { + public static class Public { + } + + public static class Internal extends Public { + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/reference/Author.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/reference/Author.java new file mode 100644 index 0000000000..7c7c8c0a68 --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/reference/Author.java @@ -0,0 +1,30 @@ +package com.baeldung.jacksonannotation.general.reference; + +import com.fasterxml.jackson.annotation.JsonManagedReference; + +import java.util.ArrayList; +import java.util.List; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class Author extends Person { + + private List items = new ArrayList<>(); + + public Author(String firstName, String lastName) { + super(firstName, lastName); + } + + @JsonManagedReference + public List getItems() { + return items; + } + + public void setItems(List items) { + this.items = items; + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/reference/Course.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/reference/Course.java new file mode 100644 index 0000000000..6fb7251d68 --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/reference/Course.java @@ -0,0 +1,72 @@ +package com.baeldung.jacksonannotation.general.reference; + + +import java.util.List; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class Course extends Item { + + public enum Medium {CLASSROOM, ONLINE} + + public enum Level { + BEGINNER("Beginner", 1), INTERMEDIATE("Intermediate", 2), ADVANCED("Advanced", 3); + + private String name; + private int number; + + Level(String name, int number) { + this.name = name; + this.number = number; + } + + public String getName() { + return name; + } + } + + private float duration; + private Medium medium; + private Level level; + private List prerequisite; + + public Course(String title, Author author) { + super(title, author); + } + + public float getDuration() { + return duration; + } + + public void setDuration(float duration) { + this.duration = duration; + } + + public Medium getMedium() { + return medium; + } + + public void setMedium(Medium medium) { + this.medium = medium; + } + + public Level getLevel() { + return level; + } + + public void setLevel(Level level) { + this.level = level; + } + + public List getPrerequisite() { + return prerequisite; + } + + public void setPrerequisite(List prerequisite) { + this.prerequisite = prerequisite; + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/reference/Item.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/reference/Item.java new file mode 100644 index 0000000000..9123f7f0a9 --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/reference/Item.java @@ -0,0 +1,63 @@ +package com.baeldung.jacksonannotation.general.reference; + +import com.fasterxml.jackson.annotation.JsonBackReference; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class Item { + + private UUID id; + private String title; + + @JsonBackReference + private List authors = new ArrayList<>(); + private float price; + + public Item(){} + + public Item(String title, Author author) { + this.id = UUID.randomUUID(); + this.title = title; + this.authors.add(author); + } + + public UUID getId() { + return id; + } + + public void setId(UUID id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public List getAuthors() { + return authors; + } + + public void setAuthors(List authors) { + this.authors = authors; + } + + public float getPrice() { + return price; + } + + public void setPrice(float price) { + this.price = price; + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/reference/Person.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/reference/Person.java new file mode 100644 index 0000000000..372b18d873 --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/general/reference/Person.java @@ -0,0 +1,44 @@ +package com.baeldung.jacksonannotation.general.reference; + +import java.util.UUID; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class Person { + + private UUID id; + private String firstName; + private String lastName; + + public Person(){} + + public Person(String firstName, String lastName) { + this.id = UUID.randomUUID(); + this.firstName = firstName; + this.lastName = lastName; + } + + 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 UUID getId() { + return id; + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/inclusion/jsonautodetect/Order.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/inclusion/jsonautodetect/Order.java new file mode 100644 index 0000000000..a5bd1b1c7e --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/inclusion/jsonautodetect/Order.java @@ -0,0 +1,54 @@ +package com.baeldung.jacksonannotation.inclusion.jsonautodetect; + +import com.fasterxml.jackson.annotation.JsonAutoDetect; +import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; + +import java.util.UUID; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +@JsonAutoDetect(fieldVisibility = Visibility.ANY) +public class Order { + + private UUID id; + private Type type; + private int internalAudit; + + public static class Type { + public long id; + public String name; + } + + public Order() { + this.id = UUID.randomUUID(); + } + + public Order(Type type) { + this(); + this.type = type; + } + + public Order(int internalAudit) { + this(); + this.type = new Type(); + this.type.id = 20; + this.type.name = "Order"; + this.internalAudit = internalAudit; + } + + public UUID getId() { + return id; + } + + public Type getType() { + return type; + } + + public void setType(Type type) { + this.type = type; + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/inclusion/jsonignore/Author.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/inclusion/jsonignore/Author.java new file mode 100644 index 0000000000..f1f1b07f43 --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/inclusion/jsonignore/Author.java @@ -0,0 +1,29 @@ +package com.baeldung.jacksonannotation.inclusion.jsonignore; + +import com.baeldung.jacksonannotation.domain.Item; + +import java.util.ArrayList; +import java.util.List; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class Author extends Person { + + private List items = new ArrayList<>(); + + public Author(String firstName, String lastName) { + super(firstName, lastName); + } + + public List getItems() { + return items; + } + + public void setItems(List items) { + this.items = items; + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/inclusion/jsonignore/Person.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/inclusion/jsonignore/Person.java new file mode 100644 index 0000000000..8f084e1e99 --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/inclusion/jsonignore/Person.java @@ -0,0 +1,47 @@ +package com.baeldung.jacksonannotation.inclusion.jsonignore; + +import com.fasterxml.jackson.annotation.JsonIgnore; + +import java.util.UUID; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class Person { + + @JsonIgnore + private UUID id; + private String firstName; + private String lastName; + + public Person(){} + + public Person(String firstName, String lastName) { + this.id = UUID.randomUUID(); + this.firstName = firstName; + this.lastName = lastName; + } + + 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 UUID getId() { + return id; + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/inclusion/jsonignoreproperties/Course.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/inclusion/jsonignoreproperties/Course.java new file mode 100644 index 0000000000..6eddad2bda --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/inclusion/jsonignoreproperties/Course.java @@ -0,0 +1,76 @@ +package com.baeldung.jacksonannotation.inclusion.jsonignoreproperties; + +import com.baeldung.jacksonannotation.domain.Author; +import com.baeldung.jacksonannotation.domain.Item; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +import java.util.List; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +@JsonIgnoreProperties({"medium"}) +public class Course extends Item { + + public enum Medium {CLASSROOM, ONLINE} + + public enum Level { + BEGINNER("Beginner", 1), INTERMEDIATE("Intermediate", 2), ADVANCED("Advanced", 3); + + private String name; + private int number; + + Level(String name, int number) { + this.name = name; + this.number = number; + } + + public String getName() { + return name; + } + } + + private float duration; + private Medium medium; + private Level level; + private List prerequisite; + + public Course(String title, Author author) { + super(title, author); + } + + public float getDuration() { + return duration; + } + + public void setDuration(float duration) { + this.duration = duration; + } + + public Medium getMedium() { + return medium; + } + + public void setMedium(Medium medium) { + this.medium = medium; + } + + public Level getLevel() { + return level; + } + + public void setLevel(Level level) { + this.level = level; + } + + public List getPrerequisite() { + return prerequisite; + } + + public void setPrerequisite(List prerequisite) { + this.prerequisite = prerequisite; + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/inclusion/jsonignoretype/Order.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/inclusion/jsonignoretype/Order.java new file mode 100644 index 0000000000..8402f00cfb --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/inclusion/jsonignoretype/Order.java @@ -0,0 +1,44 @@ +package com.baeldung.jacksonannotation.inclusion.jsonignoretype; + +import com.fasterxml.jackson.annotation.JsonIgnoreType; + +import java.util.UUID; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class Order { + + private UUID id; + private Type type; + + @JsonIgnoreType + public static class Type { + public long id; + public String name; + } + + public Order() { + this.id = UUID.randomUUID(); + } + + public Order(Type type) { + this(); + this.type = type; + } + + public UUID getId() { + return id; + } + + public Type getType() { + return type; + } + + public void setType(Type type) { + this.type = type; + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/inclusion/jsoninclude/Author.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/inclusion/jsoninclude/Author.java new file mode 100644 index 0000000000..7ccbf69a79 --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/inclusion/jsoninclude/Author.java @@ -0,0 +1,34 @@ +package com.baeldung.jacksonannotation.inclusion.jsoninclude; + +import com.baeldung.jacksonannotation.domain.Item; +import com.baeldung.jacksonannotation.domain.Person; +import com.fasterxml.jackson.annotation.JsonInclude; + +import java.util.ArrayList; +import java.util.List; + +import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +@JsonInclude(NON_NULL) +public class Author extends Person { + + private List items = new ArrayList<>(); + + public Author(String firstName, String lastName) { + super(firstName, lastName); + } + + public List getItems() { + return items; + } + + public void setItems(List items) { + this.items = items; + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/miscellaneous/custom/Course.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/miscellaneous/custom/Course.java new file mode 100644 index 0000000000..0cfb6a88e3 --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/miscellaneous/custom/Course.java @@ -0,0 +1,74 @@ +package com.baeldung.jacksonannotation.miscellaneous.custom; + +import com.baeldung.jacksonannotation.domain.Author; + +import java.util.List; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +@CustomCourseAnnotation +public class Course extends Item { + + public enum Medium {CLASSROOM, ONLINE} + + public enum Level { + BEGINNER("Beginner", 1), INTERMEDIATE("Intermediate", 2), ADVANCED("Advanced", 3); + + private String name; + private int number; + + Level(String name, int number) { + this.name = name; + this.number = number; + } + + public String getName() { + return name; + } + } + + private float duration; + private Medium medium; + private Level level; + private List prerequisite; + + public Course(String title, Author author) { + super(title, author); + } + + public float getDuration() { + return duration; + } + + public void setDuration(float duration) { + this.duration = duration; + } + + public Medium getMedium() { + return medium; + } + + public void setMedium(Medium medium) { + this.medium = medium; + } + + public Level getLevel() { + return level; + } + + public void setLevel(Level level) { + this.level = level; + } + + public List getPrerequisite() { + return prerequisite; + } + + public void setPrerequisite(List prerequisite) { + this.prerequisite = prerequisite; + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/miscellaneous/custom/CustomCourseAnnotation.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/miscellaneous/custom/CustomCourseAnnotation.java new file mode 100644 index 0000000000..a77cbb722c --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/miscellaneous/custom/CustomCourseAnnotation.java @@ -0,0 +1,20 @@ +package com.baeldung.jacksonannotation.miscellaneous.custom; + +import com.fasterxml.jackson.annotation.*; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +@Retention(RetentionPolicy.RUNTIME) +@JacksonAnnotationsInside +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonPropertyOrder({"title", "price", "id", "duration", "authors", "level"}) +@JsonIgnoreProperties({"prerequisite"}) +public @interface CustomCourseAnnotation { +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/miscellaneous/custom/Item.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/miscellaneous/custom/Item.java new file mode 100644 index 0000000000..3d4411b201 --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/miscellaneous/custom/Item.java @@ -0,0 +1,62 @@ +package com.baeldung.jacksonannotation.miscellaneous.custom; + +import com.baeldung.jacksonannotation.domain.Author; +import com.baeldung.jacksonannotation.domain.Person; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class Item { + + private UUID id; + private String title; + private List authors = new ArrayList<>(); + private float price; + + public Item(){} + + public Item(String title, Author author) { + this.id = UUID.randomUUID(); + this.title = title; + this.authors.add(author); + } + + public UUID getId() { + return id; + } + + public void setId(UUID id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public List getAuthors() { + return authors; + } + + public void setAuthors(List authors) { + this.authors = authors; + } + + public float getPrice() { + return price; + } + + public void setPrice(float price) { + this.price = price; + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/miscellaneous/disable/Author.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/miscellaneous/disable/Author.java new file mode 100644 index 0000000000..f3e0fdc63f --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/miscellaneous/disable/Author.java @@ -0,0 +1,36 @@ +package com.baeldung.jacksonannotation.miscellaneous.disable; + +import com.baeldung.jacksonannotation.domain.Item; +import com.baeldung.jacksonannotation.domain.Person; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; + +import java.util.ArrayList; +import java.util.List; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonPropertyOrder({"lastName", "items", "firstName", "id"}) +public class Author extends Person { + + @JsonIgnore + private List items = new ArrayList<>(); + + public Author(String firstName, String lastName) { + super(firstName, lastName); + } + + public List getItems() { + return items; + } + + public void setItems(List items) { + this.items = items; + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/miscellaneous/mixin/Author.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/miscellaneous/mixin/Author.java new file mode 100644 index 0000000000..155a7f5e5d --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/miscellaneous/mixin/Author.java @@ -0,0 +1,30 @@ +package com.baeldung.jacksonannotation.miscellaneous.mixin; + +import com.baeldung.jacksonannotation.domain.Item; +import com.baeldung.jacksonannotation.domain.Person; + +import java.util.ArrayList; +import java.util.List; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class Author extends Person { + + private List items = new ArrayList<>(); + + public Author(String firstName, String lastName) { + super(firstName, lastName); + } + + public List getItems() { + return items; + } + + public void setItems(List items) { + this.items = items; + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/miscellaneous/mixin/IgnoreListMixIn.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/miscellaneous/mixin/IgnoreListMixIn.java new file mode 100644 index 0000000000..c1aec05041 --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/miscellaneous/mixin/IgnoreListMixIn.java @@ -0,0 +1,13 @@ +package com.baeldung.jacksonannotation.miscellaneous.mixin; + +import com.fasterxml.jackson.annotation.JsonIgnoreType; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +@JsonIgnoreType +public class IgnoreListMixIn { +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/polymorphism/Order.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/polymorphism/Order.java new file mode 100644 index 0000000000..77ac6d0e67 --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/polymorphism/Order.java @@ -0,0 +1,66 @@ +package com.baeldung.jacksonannotation.polymorphism; + +import com.fasterxml.jackson.annotation.JsonSubTypes; +import com.fasterxml.jackson.annotation.JsonTypeInfo; +import com.fasterxml.jackson.annotation.JsonTypeName; + +import java.util.UUID; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class Order { + + private UUID id; + 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") + }) + public static class Type { + public long id; + public String name; + } + + @JsonTypeName("internal") + public static class InternalType extends Type { + public long id; + public String name; + } + + public Order() { + this.id = UUID.randomUUID(); + } + + public Order(Type type) { + this(); + this.type = type; + } + + public Order(int internalAudit) { + this(); + this.type = new Type(); + this.type.id = 20; + this.type.name = "Order"; + this.internalAudit = internalAudit; + } + + public UUID getId() { + return id; + } + + public Type getType() { + return type; + } + + public void setType(Type type) { + this.type = type; + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/serialization/jsonanygetter/Inventory.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/serialization/jsonanygetter/Inventory.java new file mode 100644 index 0000000000..898617b7b2 --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/serialization/jsonanygetter/Inventory.java @@ -0,0 +1,43 @@ +package com.baeldung.jacksonannotation.serialization.jsonanygetter; + +import com.baeldung.jacksonannotation.domain.Author; +import com.baeldung.jacksonannotation.domain.Item; +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonIgnore; + +import java.util.HashMap; +import java.util.Map; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class Inventory { + + private String location; + + private Map stock = new HashMap<>(); + + private Map countryDeliveryCost = new HashMap<>(); + + public String getLocation() { + return location; + } + + public void setLocation(String location) { + this.location = location; + } + + @JsonIgnore + public Map getStock() { + return stock; + } + + @JsonAnyGetter + public Map getCountryDeliveryCost() { + return countryDeliveryCost; + } + +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/serialization/jsongetter/Author.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/serialization/jsongetter/Author.java new file mode 100644 index 0000000000..92283a32a0 --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/serialization/jsongetter/Author.java @@ -0,0 +1,33 @@ +package com.baeldung.jacksonannotation.serialization.jsongetter; + + +import com.baeldung.jacksonannotation.domain.Item; +import com.baeldung.jacksonannotation.domain.Person; +import com.fasterxml.jackson.annotation.JsonGetter; + +import java.util.ArrayList; +import java.util.List; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class Author extends Person { + + List items = new ArrayList<>(); + + public Author(String firstName, String lastName) { + super(firstName, lastName); + } + + @JsonGetter("publications") + public List getItems() { + return items; + } + + public void setItems(List items) { + this.items = items; + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/serialization/jsonpropertyorder/Author.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/serialization/jsonpropertyorder/Author.java new file mode 100644 index 0000000000..ee708e0d80 --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/serialization/jsonpropertyorder/Author.java @@ -0,0 +1,32 @@ +package com.baeldung.jacksonannotation.serialization.jsonpropertyorder; + + +import com.baeldung.jacksonannotation.domain.Item; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; + +import java.util.ArrayList; +import java.util.List; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +@JsonPropertyOrder({"items", "firstName", "lastName", "id"}) +public class Author extends Person { + + List items = new ArrayList<>(); + + public Author(String firstName, String lastName) { + super(firstName, lastName); + } + + public List getItems() { + return items; + } + + public void setItems(List items) { + this.items = items; + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/serialization/jsonpropertyorder/Person.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/serialization/jsonpropertyorder/Person.java new file mode 100644 index 0000000000..96904c2678 --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/serialization/jsonpropertyorder/Person.java @@ -0,0 +1,42 @@ +package com.baeldung.jacksonannotation.serialization.jsonpropertyorder; + +import java.util.UUID; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class Person { + + private UUID id; + private String firstName; + private String lastName; + + public Person(String firstName, String lastName) { + this.id = UUID.randomUUID(); + this.firstName = firstName; + this.lastName = lastName; + } + + 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 UUID getId() { + return id; + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/serialization/jsonrawvalue/Customer.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/serialization/jsonrawvalue/Customer.java new file mode 100644 index 0000000000..ddd0f63a8d --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/serialization/jsonrawvalue/Customer.java @@ -0,0 +1,28 @@ +package com.baeldung.jacksonannotation.serialization.jsonrawvalue; + +import com.baeldung.jacksonannotation.domain.Person; +import com.fasterxml.jackson.annotation.JsonRawValue; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class Customer extends Person { + + @JsonRawValue + private String configuration; + + public Customer(String firstName, String lastName) { + super(firstName, lastName); + } + + public String getConfiguration() { + return configuration; + } + + public void setConfiguration(String configuration) { + this.configuration = configuration; + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/serialization/jsonrootname/Author.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/serialization/jsonrootname/Author.java new file mode 100644 index 0000000000..b33f6fecee --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/serialization/jsonrootname/Author.java @@ -0,0 +1,32 @@ +package com.baeldung.jacksonannotation.serialization.jsonrootname; + +import com.baeldung.jacksonannotation.domain.Item; +import com.baeldung.jacksonannotation.domain.Person; +import com.fasterxml.jackson.annotation.JsonRootName; + +import java.util.ArrayList; +import java.util.List; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +@JsonRootName("writer") +public class Author extends Person { + + List items = new ArrayList<>(); + + public Author(String firstName, String lastName) { + super(firstName, lastName); + } + + public List getItems() { + return items; + } + + public void setItems(List items) { + this.items = items; + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/serialization/jsonserialize/Author.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/serialization/jsonserialize/Author.java new file mode 100644 index 0000000000..5599b0b796 --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/serialization/jsonserialize/Author.java @@ -0,0 +1,30 @@ +package com.baeldung.jacksonannotation.serialization.jsonserialize; + +import com.baeldung.jacksonannotation.domain.Item; +import com.baeldung.jacksonannotation.domain.Person; + +import java.util.ArrayList; +import java.util.List; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class Author extends Person { + + List items = new ArrayList<>(); + + public Author(String firstName, String lastName) { + super(firstName, lastName); + } + + public List getItems() { + return items; + } + + public void setItems(List items) { + this.items = items; + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/serialization/jsonserialize/Book.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/serialization/jsonserialize/Book.java new file mode 100644 index 0000000000..a8a05b093c --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/serialization/jsonserialize/Book.java @@ -0,0 +1,53 @@ +package com.baeldung.jacksonannotation.serialization.jsonserialize; + +import com.fasterxml.jackson.databind.annotation.JsonSerialize; + +import java.math.BigDecimal; +import java.util.Date; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class Book extends Item { + + private String ISBN; + + @JsonSerialize(using = CustomDateSerializer.class) + private Date published; + private BigDecimal pages; + + public Book(){ + super(); + } + + public Book(String title, Author author) { + super(title, author); + } + + public String getISBN() { + return ISBN; + } + + public void setISBN(String ISBN) { + this.ISBN = ISBN; + } + + public Date getPublished() { + return published; + } + + public void setPublished(Date published) { + this.published = published; + } + + public BigDecimal getPages() { + return pages; + } + + public void setPages(BigDecimal pages) { + this.pages = pages; + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/serialization/jsonserialize/CustomDateSerializer.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/serialization/jsonserialize/CustomDateSerializer.java new file mode 100644 index 0000000000..82ad49e9c4 --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/serialization/jsonserialize/CustomDateSerializer.java @@ -0,0 +1,36 @@ +package com.baeldung.jacksonannotation.serialization.jsonserialize; + +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; + +import java.io.IOException; +import java.text.SimpleDateFormat; +import java.util.Date; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class CustomDateSerializer extends StdSerializer { + + private static SimpleDateFormat formatter = + new SimpleDateFormat("dd-MM-yyyy HH:mm:ss"); + + public CustomDateSerializer() { + this(null); + } + + public CustomDateSerializer(Class t) { + super(t); + } + + @Override + public void serialize(Date value, JsonGenerator gen, SerializerProvider arg2) + throws IOException, JsonProcessingException { + gen.writeString(formatter.format(value)); + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/serialization/jsonserialize/Item.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/serialization/jsonserialize/Item.java new file mode 100644 index 0000000000..c56dc9b6ff --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/serialization/jsonserialize/Item.java @@ -0,0 +1,61 @@ +package com.baeldung.jacksonannotation.serialization.jsonserialize; + +import com.baeldung.jacksonannotation.domain.Person; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class Item { + + private UUID id; + private String title; + private List authors = new ArrayList<>(); + private float price; + + public Item(){} + + public Item(String title, Author author) { + this.id = UUID.randomUUID(); + this.title = title; + this.authors.add(author); + } + + public UUID getId() { + return id; + } + + public void setId(UUID id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public List getAuthors() { + return authors; + } + + public void setAuthors(List authors) { + this.authors = authors; + } + + public float getPrice() { + return price; + } + + public void setPrice(float price) { + this.price = price; + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/serialization/jsonvalue/Course.java b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/serialization/jsonvalue/Course.java new file mode 100644 index 0000000000..cb48044ce5 --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/serialization/jsonvalue/Course.java @@ -0,0 +1,76 @@ +package com.baeldung.jacksonannotation.serialization.jsonvalue; + +import com.baeldung.jacksonannotation.domain.Author; +import com.baeldung.jacksonannotation.domain.Item; +import com.fasterxml.jackson.annotation.JsonValue; + +import java.util.List; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class Course extends Item { + + public enum Medium {CLASSROOM, ONLINE} + + public enum Level { + BEGINNER("Beginner", 1), INTERMEDIATE("Intermediate", 2), ADVANCED("Advanced", 3); + + private String name; + private int number; + + Level(String name, int number) { + this.name = name; + this.number = number; + } + + @JsonValue + public String getName() { + return name; + } + } + + private float duration; + private Medium medium; + private Level level; + private List prerequisite; + + public Course(String title, Author author) { + super(title, author); + } + + public float getDuration() { + return duration; + } + + public void setDuration(float duration) { + this.duration = duration; + } + + public Medium getMedium() { + return medium; + } + + public void setMedium(Medium medium) { + this.medium = medium; + } + + public Level getLevel() { + return level; + } + + public void setLevel(Level level) { + this.level = level; + } + + public List getPrerequisite() { + return prerequisite; + } + + public void setPrerequisite(List prerequisite) { + this.prerequisite = prerequisite; + } +} diff --git a/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/deserialization/jacksoninject/JacksonInjectTest.java b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/deserialization/jacksoninject/JacksonInjectTest.java new file mode 100644 index 0000000000..7c9436d711 --- /dev/null +++ b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/deserialization/jacksoninject/JacksonInjectTest.java @@ -0,0 +1,45 @@ +package com.baeldung.jacksonannotation.deserialization.jacksoninject; + +import com.fasterxml.jackson.databind.InjectableValues; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.Test; + +import java.io.IOException; +import java.util.UUID; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class JacksonInjectTest { + + @Test + public void whenDeserializingUsingJacksonInject_thenCorrect() throws IOException { + + UUID id = UUID.fromString("9616dc8c-bad3-11e6-a4a6-cec0c932ce01"); + + // arrange + String authorJson = "{\"firstName\": \"Alex\", \"lastName\": \"Theedom\"}"; + + // act + InjectableValues inject = new InjectableValues.Std().addValue(UUID.class, id); + Author author = new ObjectMapper().reader(inject).forType(Author.class).readValue(authorJson); + + // assert + assertThat(author.getId()).isEqualTo(id); + + + /* + { + "firstName": "Alex", + "lastName": "Theedom", + "publications": [] + } + */ + + } +} \ No newline at end of file diff --git a/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/deserialization/jsonanysetter/JsonAnySetterTest.java b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/deserialization/jsonanysetter/JsonAnySetterTest.java new file mode 100644 index 0000000000..d735f31a0b --- /dev/null +++ b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/deserialization/jsonanysetter/JsonAnySetterTest.java @@ -0,0 +1,37 @@ +package com.baeldung.jacksonannotation.deserialization.jsonanysetter; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.Test; + +import java.io.IOException; + +import static io.restassured.path.json.JsonPath.from; +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class JsonAnySetterTest { + + @Test + public void whenDeserializingUsingJsonAnySetter_thenCorrect() throws IOException { + + // arrange + String json = "{\"USA\":10.00,\"UK\":15.00,\"China\":23.00,\"Brazil\":12.00,\"France\":8.00,\"Russia\":18.00}"; + + // act + Inventory inventory = new ObjectMapper().readerFor(Inventory.class).readValue(json); + + // assert + assertThat(from(json).getMap(".").get("USA")).isEqualTo(inventory.getCountryDeliveryCost().get("USA")); + assertThat(from(json).getMap(".").get("UK")).isEqualTo(inventory.getCountryDeliveryCost().get("UK")); + assertThat(from(json).getMap(".").get("China")).isEqualTo(inventory.getCountryDeliveryCost().get("China")); + assertThat(from(json).getMap(".").get("Brazil")).isEqualTo(inventory.getCountryDeliveryCost().get("Brazil")); + assertThat(from(json).getMap(".").get("France")).isEqualTo(inventory.getCountryDeliveryCost().get("France")); + assertThat(from(json).getMap(".").get("Russia")).isEqualTo(inventory.getCountryDeliveryCost().get("Russia")); + + } +} \ No newline at end of file diff --git a/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/deserialization/jsoncreator/JsonCreatorTest.java b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/deserialization/jsoncreator/JsonCreatorTest.java new file mode 100644 index 0000000000..a2c05811df --- /dev/null +++ b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/deserialization/jsoncreator/JsonCreatorTest.java @@ -0,0 +1,44 @@ +package com.baeldung.jacksonannotation.deserialization.jsoncreator; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.Test; + +import java.io.IOException; + +import static io.restassured.path.json.JsonPath.from; +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class JsonCreatorTest { + + @Test + public void whenDeserializingUsingJsonCreator_thenCorrect() throws IOException { + + // arrange + String authorJson = + "{" + + " \"christianName\": \"Alex\"," + + " \"surname\": \"Theedom\"" + + "}"; + + // act + final Author author = new ObjectMapper().readerFor(Author.class).readValue(authorJson); + + // assert + assertThat(from(authorJson).getString("christianName")).isEqualTo(author.getFirstName()); + assertThat(from(authorJson).getString("surname")).isEqualTo(author.getLastName()); + + /* + { + "christianName": "Alex", + "surname": "Theedom" + } + */ + + } +} \ No newline at end of file diff --git a/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/deserialization/jsondeserialize/JsonDeserializeTest.java b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/deserialization/jsondeserialize/JsonDeserializeTest.java new file mode 100644 index 0000000000..6ad06fd2b9 --- /dev/null +++ b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/deserialization/jsondeserialize/JsonDeserializeTest.java @@ -0,0 +1,34 @@ +package com.baeldung.jacksonannotation.deserialization.jsondeserialize; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.Test; + +import java.io.IOException; +import java.text.SimpleDateFormat; + +import static io.restassured.path.json.JsonPath.from; +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class JsonDeserializeTest { + + @Test + public void whenDeserializingUsingJsonDeserialize_thenCorrect() throws IOException { + + // arrange + String bookJson = "{\"id\":\"957c43f2-fa2e-42f9-bf75-6e3d5bb6960a\",\"title\":\"Effective Java\",\"authors\":[{\"id\":\"9bcd817d-0141-42e6-8f04-e5aaab0980b6\",\"firstName\":\"Joshua\",\"lastName\":\"Bloch\"}],\"price\":0,\"published\":\"25-12-2017 13:30:25\",\"pages\":null,\"isbn\":null}"; + + // act + Book book = new ObjectMapper().readerFor(Book.class).readValue(bookJson); + + // assert + SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss"); + assertThat(from(bookJson).getString("published")).isEqualTo(df.format(book.getPublished())); + + } +} \ No newline at end of file diff --git a/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/deserialization/jsonsetter/JsonSetterTest.java b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/deserialization/jsonsetter/JsonSetterTest.java new file mode 100644 index 0000000000..10b48afc15 --- /dev/null +++ b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/deserialization/jsonsetter/JsonSetterTest.java @@ -0,0 +1,32 @@ +package com.baeldung.jacksonannotation.deserialization.jsonsetter; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.Test; + +import java.io.IOException; + +import static io.restassured.path.json.JsonPath.from; +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class JsonSetterTest { + + @Test + public void whenDeserializingUsingJsonSetter_thenCorrect() throws IOException { + + // arrange + String json = "{\"firstName\":\"Alex\",\"lastName\":\"Theedom\",\"publications\":[{\"title\":\"Professional Java EE Design Patterns\"}]}"; + + // act + Author author = new ObjectMapper().readerFor(Author.class).readValue(json); + + // assert + assertThat(from(json).getList("publications").size()).isEqualTo(author.getItems().size()); + + } +} \ No newline at end of file diff --git a/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/general/jsonfilter/JsonFilterTest.java b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/general/jsonfilter/JsonFilterTest.java new file mode 100644 index 0000000000..fa7f9326a2 --- /dev/null +++ b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/general/jsonfilter/JsonFilterTest.java @@ -0,0 +1,42 @@ +package com.baeldung.jacksonannotation.general.jsonfilter; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ser.FilterProvider; +import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter; +import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider; +import org.junit.Test; + +import static io.restassured.path.json.JsonPath.from; +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class JsonFilterTest { + + @Test + public void whenSerializingUsingJsonFilter_thenCorrect() throws JsonProcessingException { + + // arrange + Author author = new Author("Alex", "Theedom"); + FilterProvider filters = new SimpleFilterProvider() + .addFilter("authorFilter", SimpleBeanPropertyFilter.filterOutAllExcept("lastName")); + + // act + String result = new ObjectMapper().writer(filters).writeValueAsString(author); + + // assert + assertThat(from(result).getList("items")).isNull(); + + /* + { + "lastName": "Theedom" + } + */ + + } +} \ No newline at end of file diff --git a/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/general/jsonformat/JsonFormatTest.java b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/general/jsonformat/JsonFormatTest.java new file mode 100644 index 0000000000..9d61fc409f --- /dev/null +++ b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/general/jsonformat/JsonFormatTest.java @@ -0,0 +1,66 @@ +package com.baeldung.jacksonannotation.general.jsonformat; + +import com.baeldung.jacksonannotation.domain.Author; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.Test; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.TimeZone; + +import static io.restassured.path.json.JsonPath.from; +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class JsonFormatTest { + + @Test + public void whenSerializingUsingJsonFormat_thenCorrect() throws JsonProcessingException, ParseException { + + // arrange + SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss"); + df.setTimeZone(TimeZone.getTimeZone("UTC")); + + String toParse = "20-12-2014 14:30:00"; + Date date = df.parse(toParse); + + Book book = new Book( + "Design Patterns: Elements of Reusable Object-oriented Software", + new Author("The", "GoF") + ); + book.setPublished(date); + + // act + String result = new ObjectMapper().writeValueAsString(book); + + // assert + assertThat(from(result).getString("published")).isEqualTo(toParse); + + /* + { + "id": "762b39be-fd5b-489e-8688-aeb3b9bbf019", + "title": "Design Patterns: Elements of Reusable Object-oriented Software", + "authors": [ + { + "id": "6941b780-0f54-4259-adcb-85523c8f25f4", + "firstName": "The", + "lastName": "GoF", + "items": [] + } + ], + "price": 0, + "published": "20-12-2014 02:30:00", + "pages": null, + "isbn": null + } + */ + + } +} \ No newline at end of file diff --git a/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/general/jsonidentityinfo/JsonIdentityInfoTest.java b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/general/jsonidentityinfo/JsonIdentityInfoTest.java new file mode 100644 index 0000000000..1b08e08c10 --- /dev/null +++ b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/general/jsonidentityinfo/JsonIdentityInfoTest.java @@ -0,0 +1,60 @@ +package com.baeldung.jacksonannotation.general.jsonidentityinfo; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.Test; + +import java.util.Collections; + +import static io.restassured.path.json.JsonPath.from; +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class JsonIdentityInfoTest { + + @Test + public void whenSerializingUsingJsonIdentityInfo_thenCorrect() throws JsonProcessingException { + + // arrange + Author author = new Author("Alex", "Theedom"); + + Course course = new Course("Java EE Introduction", author); + author.setItems(Collections.singletonList(course)); + course.setAuthors(Collections.singletonList(author)); + + // act + String result = new ObjectMapper().writeValueAsString(author); + + // assert + assertThat(from(result).getString("items[0].authors")).isNotNull(); + + /* + Authors are included. + { + "id": "1b408bf9-5946-4a14-a112-fde2953a7fe7", + "firstName": "Alex", + "lastName": "Theedom", + "items": [ + { + "id": "5ed30530-f0a5-42eb-b786-be2c655da968", + "title": "Java EE Introduction", + "authors": [ + "1b408bf9-5946-4a14-a112-fde2953a7fe7" + ], + "price": 0, + "duration": 0, + "medium": null, + "level": null, + "prerequisite": null + } + ] + } + */ + + } +} \ No newline at end of file diff --git a/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/general/jsonproperty/JsonPropertyTest.java b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/general/jsonproperty/JsonPropertyTest.java new file mode 100644 index 0000000000..acfe735dd3 --- /dev/null +++ b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/general/jsonproperty/JsonPropertyTest.java @@ -0,0 +1,73 @@ +package com.baeldung.jacksonannotation.general.jsonproperty; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.Test; + +import java.io.IOException; + +import static io.restassured.path.json.JsonPath.from; +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class JsonPropertyTest { + + @Test + public void whenSerializingUsingJsonProperty_thenCorrect() throws JsonProcessingException { + + // arrange + Book book = new Book( + "Design Patterns: Elements of Reusable Object-oriented Software", + new Author("The", "GoF") + ); + book.configureBinding("Hardback"); + + // act + String result = new ObjectMapper().writeValueAsString(book); + + // assert + assertThat(from(result).getString("binding")).isEqualTo("Hardback"); + + /* + { + "id": "cd941587-d1ae-4c2a-9a36-29533bf50411", + "title": "Design Patterns: Elements of Reusable Object-oriented Software", + "authors": [ + { + "id": "c8e26318-2f5b-4fa2-9fdc-6e99be021fca", + "firstName": "The", + "lastName": "GoF", + "items": [] + } + ], + "price": 0, + "published": null, + "pages": null, + "isbn": null, + "binding": "Hardback" + } + */ + + } + + @Test + public void whenDeserializingUsingJsonProperty_thenCorrect() throws IOException { + + // arrange + String result = "{\"id\":\"cd941587-d1ae-4c2a-9a36-29533bf50411\",\"title\":\"Design Patterns: Elements of Reusable Object-oriented Software\",\"authors\":[{\"id\":\"c8e26318-2f5b-4fa2-9fdc-6e99be021fca\",\"firstName\":\"The\",\"lastName\":\"GoF\"}],\"binding\":\"Hardback\"}"; + + // act + Book book = new ObjectMapper().readerFor(Book.class).readValue(result); + + // assert + assertThat(book.coverBinding()).isEqualTo("Hardback"); + + } + + +} \ No newline at end of file diff --git a/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/general/jsonunwrapped/JsonUnwrappedTest.java b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/general/jsonunwrapped/JsonUnwrappedTest.java new file mode 100644 index 0000000000..63ad4e3c9f --- /dev/null +++ b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/general/jsonunwrapped/JsonUnwrappedTest.java @@ -0,0 +1,44 @@ +package com.baeldung.jacksonannotation.general.jsonunwrapped; + + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.Test; + +import static io.restassured.path.json.JsonPath.from; +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class JsonUnwrappedTest { + + @Test + public void whenSerializingUsingJsonUnwrapped_thenCorrect() throws JsonProcessingException { + + // arrange + Order.Type preorderType = new Order.Type(); + preorderType.id = 10; + preorderType.name = "pre-order"; + + Order order = new Order(preorderType); + + // act + String result = new ObjectMapper().writeValueAsString(order); + + // assert + assertThat(from(result).getInt("id")).isEqualTo(10); + assertThat(from(result).getString("name")).isEqualTo("pre-order"); + + /* + { + "id": 10, + "name": "pre-order" + } + */ + + } +} \ No newline at end of file diff --git a/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/general/jsonview/JsonViewTest.java b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/general/jsonview/JsonViewTest.java new file mode 100644 index 0000000000..e64b815b11 --- /dev/null +++ b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/general/jsonview/JsonViewTest.java @@ -0,0 +1,72 @@ +package com.baeldung.jacksonannotation.general.jsonview; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.Test; + +import static io.restassured.path.json.JsonPath.from; +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class JsonViewTest { + + @Test + public void whenSerializingUsingJsonView_andInternalView_thenCorrect() throws JsonProcessingException { + + // arrange + Order order = new Order(120); + + // act + String result = new ObjectMapper().writerWithView(Views.Internal.class).writeValueAsString(order); + + // assert + assertThat(from(result).getUUID("id")).isNotNull(); + assertThat(from(result).getObject("type", Order.Type.class)).isNotNull(); + assertThat(from(result).getInt("internalAudit")).isEqualTo(120); + + /* + { + "id": "33806388-795b-4812-b90a-60292111bc5c", + "type": { + "id": 20, + "name": "Order" + }, + "internalAudit": 120 + } + */ + + } + + @Test + public void whenSerializingUsingJsonView_andPublicView_thenCorrect() throws JsonProcessingException { + + // arrange + Order order = new Order(120); + + // act + String result = new ObjectMapper().writerWithView(Views.Public.class).writeValueAsString(order); + + // assert + assertThat(from(result).getUUID("id")).isNotNull(); + assertThat(from(result).getObject("type", Order.Type.class)).isNotNull(); + assertThat(result).doesNotContain("internalAudit"); + + /* + { + "id": "5184d5fc-e359-4cdf-93fa-4054025bef4e", + "type": { + "id": 20, + "name": "Order" + } + } + */ + + } + + +} \ No newline at end of file diff --git a/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/general/reference/ReferenceTest.java b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/general/reference/ReferenceTest.java new file mode 100644 index 0000000000..acea159b4f --- /dev/null +++ b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/general/reference/ReferenceTest.java @@ -0,0 +1,59 @@ +package com.baeldung.jacksonannotation.general.reference; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.Test; + +import java.util.Collections; + +import static io.restassured.path.json.JsonPath.from; +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class ReferenceTest { + + @Test + public void whenSerializingUsingReference_thenCorrect() throws JsonProcessingException { + + // arrange + Author author = new Author("Alex", "Theedom"); + + Course course = new Course("Java EE Introduction", author); + author.setItems(Collections.singletonList(course)); + course.setAuthors(Collections.singletonList(author)); + + // act + String result = new ObjectMapper().writeValueAsString(author); + + // assert + assertThat(from(result).getString("items[0].authors")).isNull(); + + /* + Without references defined it throws StackOverflowError. + Authors excluded. + + { + "id": "9c45d9b3-4888-4c24-8b74-65ef35627cd7", + "firstName": "Alex", + "lastName": "Theedom", + "items": [ + { + "id": "f8309629-d178-4d67-93a4-b513ec4a7f47", + "title": "Java EE Introduction", + "price": 0, + "duration": 0, + "medium": null, + "level": null, + "prerequisite": null + } + ] + } + */ + + } +} \ No newline at end of file diff --git a/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/inclusion/jsonautodetect/JsonAutoDetectTest.java b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/inclusion/jsonautodetect/JsonAutoDetectTest.java new file mode 100644 index 0000000000..325a7ad338 --- /dev/null +++ b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/inclusion/jsonautodetect/JsonAutoDetectTest.java @@ -0,0 +1,52 @@ +package com.baeldung.jacksonannotation.inclusion.jsonautodetect; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.Test; + +import static io.restassured.path.json.JsonPath.from; +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class JsonAutoDetectTest { + + @Test + public void whenSerializingUsingJsonAutoDetect_thenCorrect() throws JsonProcessingException { + + // arrange + Order order = new Order(1234567890); + + // act + String result = new ObjectMapper().writeValueAsString(order); + + // assert + assertThat(from(result).getInt("internalAudit")).isEqualTo(1234567890); + + /* + With @JsonAutoDetect + { + "id": "c94774d9-de8f-4244-85d5-624bd3a4567a", + "type": { + "id": 20, + "name": "Order" + }, + "internalAudit": 1234567890 + } + + Without @JsonAutoDetect + { + "id": "c94774d9-de8f-4244-85d5-624bd3a4567a", + "type": { + "id": 20, + "name": "Order" + } + } + */ + + } +} \ No newline at end of file diff --git a/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/inclusion/jsonignore/JsonIgnoreTest.java b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/inclusion/jsonignore/JsonIgnoreTest.java new file mode 100644 index 0000000000..d5476b7442 --- /dev/null +++ b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/inclusion/jsonignore/JsonIgnoreTest.java @@ -0,0 +1,41 @@ +package com.baeldung.jacksonannotation.inclusion.jsonignore; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.Test; + +import static io.restassured.path.json.JsonPath.from; +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class JsonIgnoreTest { + + @Test + public void whenSerializingUsingJsonIgnore_thenCorrect() throws JsonProcessingException { + + // arrange + Author author = new Author("Alex", "Theedom"); + + // act + String result = new ObjectMapper().writeValueAsString(author); + + // assert + assertThat(from(result).getString("firstName")).isEqualTo("Alex"); + assertThat(from(result).getString("lastName")).isEqualTo("Theedom"); + assertThat(from(result).getString("id")).isNull(); + + /* + { + "firstName": "Alex", + "lastName": "Theedom", + "items": [] + } + */ + + } +} \ No newline at end of file diff --git a/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/inclusion/jsonignoreproperties/JsonIgnorePropertiesTest.java b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/inclusion/jsonignoreproperties/JsonIgnorePropertiesTest.java new file mode 100644 index 0000000000..ff9047a98c --- /dev/null +++ b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/inclusion/jsonignoreproperties/JsonIgnorePropertiesTest.java @@ -0,0 +1,52 @@ +package com.baeldung.jacksonannotation.inclusion.jsonignoreproperties; + +import com.baeldung.jacksonannotation.domain.Author; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.Test; + +import static io.restassured.path.json.JsonPath.from; +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class JsonIgnorePropertiesTest { + + @Test + public void whenSerializingUsingJsonIgnoreProperties_thenCorrect() throws JsonProcessingException { + + // arrange + Course course = new Course("Spring Security", new Author("Eugen", "Paraschiv")); + course.setMedium(Course.Medium.ONLINE); + + // act + String result = new ObjectMapper().writeValueAsString(course); + + // assert + assertThat(from(result).getString("medium")).isNull(); + + /* + { + "id": "ef0c8d2b-b088-409e-905c-95ac88dc0ed0", + "title": "Spring Security", + "authors": [ + { + "id": "47a4f498-b0f3-4daf-909f-d2c35a0fe3c2", + "firstName": "Eugen", + "lastName": "Paraschiv", + "items": [] + } + ], + "price": 0, + "duration": 0, + "level": null, + "prerequisite": null + } + */ + + } +} \ No newline at end of file diff --git a/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/inclusion/jsonignoretype/JsonIgnoreTypeTest.java b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/inclusion/jsonignoretype/JsonIgnoreTypeTest.java new file mode 100644 index 0000000000..b80cda826f --- /dev/null +++ b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/inclusion/jsonignoretype/JsonIgnoreTypeTest.java @@ -0,0 +1,40 @@ +package com.baeldung.jacksonannotation.inclusion.jsonignoretype; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.Test; + +import static io.restassured.path.json.JsonPath.from; +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class JsonIgnoreTypeTest { + + @Test + public void whenSerializingUsingJsonIgnoreType_thenCorrect() throws JsonProcessingException { + + // arrange + Order.Type type = new Order.Type(); + type.id = 10; + type.name = "Pre-order"; + + Order order = new Order(type); + + // act + String result = new ObjectMapper().writeValueAsString(order); + + // assert + assertThat(from(result).getString("id")).isNotNull(); + assertThat(from(result).getString("type")).isNull(); + + /* + {"id":"ac2428da-523e-443c-a18a-4ea4d2791fea"} + */ + + } +} \ No newline at end of file diff --git a/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/inclusion/jsoninclude/JsonIncludeTest.java b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/inclusion/jsoninclude/JsonIncludeTest.java new file mode 100644 index 0000000000..6a1201701a --- /dev/null +++ b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/inclusion/jsoninclude/JsonIncludeTest.java @@ -0,0 +1,41 @@ +package com.baeldung.jacksonannotation.inclusion.jsoninclude; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.Test; + +import static io.restassured.path.json.JsonPath.from; +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class JsonIncludeTest { + + @Test + public void whenSerializingUsingJsonInclude_thenCorrect() throws JsonProcessingException { + + // arrange + Author author = new Author("Alex", null); + + // act + String result = new ObjectMapper().writeValueAsString(author); + + // assert + assertThat(from(result).getString("firstName")).isEqualTo("Alex"); + assertThat(result).doesNotContain("lastName"); + + + /* + { + "id": "e8bb4802-6e0c-4fa5-9f68-c233272399cd", + "firstName": "Alex", + "items": [] + } + */ + + } +} \ No newline at end of file diff --git a/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/miscellaneous/custom/CustomTest.java b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/miscellaneous/custom/CustomTest.java new file mode 100644 index 0000000000..8178d12a18 --- /dev/null +++ b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/miscellaneous/custom/CustomTest.java @@ -0,0 +1,51 @@ +package com.baeldung.jacksonannotation.miscellaneous.custom; + +import com.baeldung.jacksonannotation.domain.Author; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.Test; + +import static io.restassured.path.json.JsonPath.from; +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class CustomTest { + + @Test + public void whenSerializingUsingCustom_thenCorrect() throws JsonProcessingException { + + // arrange + Course course = new Course("Spring Security", new Author("Eugen", "Paraschiv")); + course.setMedium(Course.Medium.ONLINE); + + // act + String result = new ObjectMapper().writeValueAsString(course); + + // assert + assertThat(from(result).getString("title")).isEqualTo("Spring Security"); + + /* + { + "title": "Spring Security", + "price": 0, + "id": "7dfd4db9-1175-432f-a53b-687423f7bb9b", + "duration": 0, + "authors": [ + { + "id": "da0738f6-033c-4974-8d87-92820e5ccf27", + "firstName": "Eugen", + "lastName": "Paraschiv", + "items": [] + } + ], + "medium": "ONLINE" + } + */ + + } +} \ No newline at end of file diff --git a/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/miscellaneous/disable/DisableTest.java b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/miscellaneous/disable/DisableTest.java new file mode 100644 index 0000000000..f7bdd31f30 --- /dev/null +++ b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/miscellaneous/disable/DisableTest.java @@ -0,0 +1,58 @@ +package com.baeldung.jacksonannotation.miscellaneous.disable; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.MapperFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.Test; + +import static io.restassured.path.json.JsonPath.from; +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class DisableTest { + + @Test + public void whenSerializingUsingDisable_thenCorrect() throws JsonProcessingException { + + // arrange + Author author = new Author("Alex", "Theedom"); + + // act + ObjectMapper mapper = new ObjectMapper(); + String result = mapper.writeValueAsString(author); + + // assert + assertThat(from(result).getList("items")).isNull(); + + /* + { + "lastName": "Theedom", + "firstName": "Alex", + "id": "de4afbb4-b24d-45c8-bb00-fd6b9acb42f1" + } + */ + + // act + mapper = new ObjectMapper(); + mapper.disable(MapperFeature.USE_ANNOTATIONS); + result = mapper.writeValueAsString(author); + + // assert + assertThat(from(result).getList("items")).isNotNull(); + + /* + { + "id": "81e6ed72-6b27-4fe9-a36f-e3171c5b55ef", + "firstName": "Alex", + "lastName": "Theedom", + "items": [] + } + */ + + } +} \ No newline at end of file diff --git a/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/miscellaneous/mixin/MixInTest.java b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/miscellaneous/mixin/MixInTest.java new file mode 100644 index 0000000000..779031cfa6 --- /dev/null +++ b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/miscellaneous/mixin/MixInTest.java @@ -0,0 +1,58 @@ +package com.baeldung.jacksonannotation.miscellaneous.mixin; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.Test; + +import java.util.List; + +import static io.restassured.path.json.JsonPath.from; +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class MixInTest { + + @Test + public void whenSerializingUsingMixIn_thenCorrect() throws JsonProcessingException { + + // arrange + Author author = new Author("Alex", "Theedom"); + + // act + String result = new ObjectMapper().writeValueAsString(author); + + // assert + assertThat(from(result).getList("items")).isNotNull(); + + /* + { + "id": "f848b076-00a4-444a-a50b-328595dd9bf5", + "firstName": "Alex", + "lastName": "Theedom", + "items": [] + } + */ + + ObjectMapper mapper = new ObjectMapper(); + mapper.addMixIn(List.class, IgnoreListMixIn.class); + + result = mapper.writeValueAsString(author); + + // assert + assertThat(from(result).getList("items")).isNull(); + + /* + { + "id": "9ffefb7d-e56f-447c-9009-e92e142f8347", + "firstName": "Alex", + "lastName": "Theedom" + } + */ + + } +} \ No newline at end of file diff --git a/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/polymorphism/PolymorphismTest.java b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/polymorphism/PolymorphismTest.java new file mode 100644 index 0000000000..ad7aeb937c --- /dev/null +++ b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/polymorphism/PolymorphismTest.java @@ -0,0 +1,64 @@ +package com.baeldung.jacksonannotation.polymorphism; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.Test; + +import java.io.IOException; + +import static io.restassured.path.json.JsonPath.from; +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class PolymorphismTest { + + @Test + public void whenSerializingUsingPolymorphism_thenCorrect() throws JsonProcessingException { + + // arrange + Order.InternalType internalType = new Order.InternalType(); + internalType.id = 250; + internalType.name = "staff"; + + Order order = new Order(internalType); + + // act + String result = new ObjectMapper().writeValueAsString(order); + + // assert + assertThat(from(result).getString("type.ordertype")).isEqualTo("internal"); + + /* + { + "id": "7fc898e3-b4e7-41b0-8ffa-664cf3663f2e", + "type": { + "ordertype": "internal", + "id": 250, + "name": "staff" + } + } + */ + + } + + @Test + public void whenDeserializingPolymorphic_thenCorrect() throws IOException { + + // arrange + String orderJson = "{\"type\":{\"ordertype\":\"internal\",\"id\":100,\"name\":\"directors\"}}"; + + // act + Order order = new ObjectMapper().readerFor(Order.class).readValue(orderJson); + + // assert + assertThat(from(orderJson).getString("type.ordertype")).isEqualTo("internal"); + assertThat(((Order.InternalType) order.getType()).name).isEqualTo("directors"); + assertThat(((Order.InternalType) order.getType()).id).isEqualTo(100); + assertThat(order.getType().getClass()).isEqualTo(Order.InternalType.class); + } +} \ No newline at end of file diff --git a/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/serialization/jsonanygetter/JsonAnyGetterTest.java b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/serialization/jsonanygetter/JsonAnyGetterTest.java new file mode 100644 index 0000000000..de7c9b6d69 --- /dev/null +++ b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/serialization/jsonanygetter/JsonAnyGetterTest.java @@ -0,0 +1,49 @@ +package com.baeldung.jacksonannotation.serialization.jsonanygetter; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.Test; + +import java.util.Map; + +import static io.restassured.path.json.JsonPath.from; +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class JsonAnyGetterTest { + + @Test + public void whenSerializingUsingJsonAnyGetter_thenCorrect() throws JsonProcessingException { + + // arrange + Inventory inventory = new Inventory(); + Map countryDeliveryCost = inventory.getCountryDeliveryCost(); + inventory.setLocation("France"); + + countryDeliveryCost.put("USA", 10.00f); + countryDeliveryCost.put("UK", 15.00f); + + // act + String result = new ObjectMapper().writeValueAsString(inventory); + + // assert + assertThat(from(result).getString("location")).isEqualTo("France"); + assertThat(from(result).getFloat("USA")).isEqualTo(10.00f); + assertThat(from(result).getFloat("UK")).isEqualTo(15.00f); + + /* + { + "location": "France", + "USA": 10, + "UK": 15 + } + */ + + } + +} diff --git a/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/serialization/jsongetter/JsonGetterTest.java b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/serialization/jsongetter/JsonGetterTest.java new file mode 100644 index 0000000000..1eec307afa --- /dev/null +++ b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/serialization/jsongetter/JsonGetterTest.java @@ -0,0 +1,40 @@ +package com.baeldung.jacksonannotation.serialization.jsongetter; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.Test; + +import static io.restassured.path.json.JsonPath.from; +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class JsonGetterTest { + + @Test + public void whenSerializingUsingJsonGetter_thenCorrect() throws JsonProcessingException { + + // arrange + Author author = new Author("Alex", "Theedom"); + + // act + String result = new ObjectMapper().writeValueAsString(author); + + // assert + assertThat(from(result).getList("publications")).isNotNull(); + assertThat(from(result).getList("items")).isNull(); + + /* + { + "firstName": "Alex", + "lastName": "Theedom", + "publications": [] + } + */ + + } +} diff --git a/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/serialization/jsonpropertyorder/JsonPropertyOrderTest.java b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/serialization/jsonpropertyorder/JsonPropertyOrderTest.java new file mode 100644 index 0000000000..864168bdef --- /dev/null +++ b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/serialization/jsonpropertyorder/JsonPropertyOrderTest.java @@ -0,0 +1,42 @@ +package com.baeldung.jacksonannotation.serialization.jsonpropertyorder; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.Test; + +import static io.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchemaInClasspath; +import static org.hamcrest.MatcherAssert.assertThat; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class JsonPropertyOrderTest { + + @Test + public void whenSerializingUsingJsonPropertyOrder_thenCorrect() throws JsonProcessingException { + + // arrange + Author author = new Author("Alex", "Theedom"); + + // act + String result = new ObjectMapper().writeValueAsString(author); + + // assert + assertThat(result, matchesJsonSchemaInClasspath("author-jsonpropertyorder-schema.json")); + + // NOTE: property order is not enforced by the JSON specification. + + /* + { + "items": [], + "firstName": "Alex", + "lastName": "Theedom", + "id": "fd277638-9b6e-49f7-81c1-bc52f165245b" + } + */ + + } +} diff --git a/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/serialization/jsonrawvalue/JsonRawValueTest.java b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/serialization/jsonrawvalue/JsonRawValueTest.java new file mode 100644 index 0000000000..477d33762b --- /dev/null +++ b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/serialization/jsonrawvalue/JsonRawValueTest.java @@ -0,0 +1,41 @@ +package com.baeldung.jacksonannotation.serialization.jsonrawvalue; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class JsonRawValueTest { + + @Test + public void whenSerializingUsingJsonRawValue_thenCorrect() throws JsonProcessingException { + + // arrange + String customerConfig = "{\"colour\":\"red\",\"device\":\"mobile\",\"orientation\":\"landscape\"}"; + Customer customer = new Customer("Alex", "Theedom"); + customer.setConfiguration("{\"colour\":\"red\",\"device\":\"mobile\",\"orientation\":\"landscape\"}"); + + // act + String result = new ObjectMapper().writeValueAsString(customer); + + // assert + assertThat(result.contains(customerConfig)); + + + /* + { + "firstName": "Alex", + "lastName": "Theedom", + "publications": [] + } + */ + + } +} diff --git a/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/serialization/jsonrootname/JsonRootNameTest.java b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/serialization/jsonrootname/JsonRootNameTest.java new file mode 100644 index 0000000000..c73dcd8e62 --- /dev/null +++ b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/serialization/jsonrootname/JsonRootNameTest.java @@ -0,0 +1,46 @@ +package com.baeldung.jacksonannotation.serialization.jsonrootname; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import org.junit.Test; + +import static io.restassured.path.json.JsonPath.from; +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class JsonRootNameTest { + + @Test + public void whenSerializingUsingJsonRootName_thenCorrect() throws JsonProcessingException { + + // arrange + Author author = new Author("Alex", "Theedom"); + + // act + ObjectMapper mapper = new ObjectMapper(); + mapper.enable(SerializationFeature.WRAP_ROOT_VALUE); + String result = mapper.writeValueAsString(author); + + // assert + assertThat(from(result).getString("writer.firstName")).isEqualTo("Alex"); + assertThat(from(result).getString("author.firstName")).isNull(); + + /* + { + "writer": { + "id": "0f50dca6-3dd7-4801-a334-fd1614276389", + "firstName": "Alex", + "lastName": "Theedom", + "items": [] + } + } + */ + + } +} diff --git a/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/serialization/jsonserialize/JsonSerializeTest.java b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/serialization/jsonserialize/JsonSerializeTest.java new file mode 100644 index 0000000000..8fa03418cc --- /dev/null +++ b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/serialization/jsonserialize/JsonSerializeTest.java @@ -0,0 +1,58 @@ +package com.baeldung.jacksonannotation.serialization.jsonserialize; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.Test; + +import java.text.ParseException; +import java.text.SimpleDateFormat; + +import static io.restassured.path.json.JsonPath.from; +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class JsonSerializeTest { + + @Test + public void whenSerializingUsingJsonSerialize_thenCorrect() throws JsonProcessingException, ParseException { + + // arrange + Author joshuaBloch = new Author("Joshua", "Bloch"); + Book book = new Book("Effective Java", joshuaBloch); + + SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss"); + String toParse = "25-12-2017 13:30:25"; + book.setPublished(df.parse(toParse)); + + // act + String result = new ObjectMapper().writeValueAsString(book); + + // assert + assertThat(from(result).getString("published")).isEqualTo(toParse); + + /* + { + "id": "957c43f2-fa2e-42f9-bf75-6e3d5bb6960a", + "title": "Effective Java", + "authors": [ + { + "id": "9bcd817d-0141-42e6-8f04-e5aaab0980b6", + "firstName": "Joshua", + "lastName": "Bloch", + "items": [] + } + ], + "price": 0, + "published": "25-12-2017 13:30:25", + "pages": null, + "isbn": null + } + */ + + } +} diff --git a/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/serialization/jsonvalue/JsonValueTest.java b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/serialization/jsonvalue/JsonValueTest.java new file mode 100644 index 0000000000..1dceb3bfea --- /dev/null +++ b/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/serialization/jsonvalue/JsonValueTest.java @@ -0,0 +1,27 @@ +package com.baeldung.jacksonannotation.serialization.jsonvalue; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Source code github.com/readlearncode + * + * @author Alex Theedom www.readlearncode.com + * @version 1.0 + */ +public class JsonValueTest { + + @Test + public void whenSerializingUsingJsonValue_thenCorrect() throws JsonProcessingException { + + // act + String result = new ObjectMapper().writeValueAsString(Course.Level.ADVANCED); + + // assert + assertThat(result).isEqualTo("\"Advanced\""); + + } +} \ No newline at end of file diff --git a/jackson-annotations/src/test/resources/author-jsonpropertyorder-schema.json b/jackson-annotations/src/test/resources/author-jsonpropertyorder-schema.json new file mode 100644 index 0000000000..8e7a85372c --- /dev/null +++ b/jackson-annotations/src/test/resources/author-jsonpropertyorder-schema.json @@ -0,0 +1,29 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "Author", + "description": "An author", + "type": "object", + "properties": { + "items": { + "type": "array", + "items": { + "type": "object" + } + }, + "firstName": { + "type": "string" + }, + "lastName": { + "type": "string" + }, + "id": { + "type": "string" + } + }, + "required": [ + "items", + "firstName", + "lastName", + "id" + ] +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index 77cf615a98..db62629081 100644 --- a/pom.xml +++ b/pom.xml @@ -47,6 +47,7 @@ immutables jackson + jackson-annotations java-cassandra javaxval jee7