diff --git a/jackson-annotations/README.md b/jackson-annotations/README.md new file mode 100644 index 0000000000..d979ec0e0e --- /dev/null +++ b/jackson-annotations/README.md @@ -0,0 +1,11 @@ +## Jackson Annotations + +This module contains articles about Jackson annotations. + +### Relevant Articles: +- [Guide to @JsonFormat in Jackson](https://www.baeldung.com/jackson-jsonformat) +- [More Jackson Annotations](https://www.baeldung.com/jackson-advanced-annotations) +- [Jackson Annotation Examples](https://www.baeldung.com/jackson-annotations) +- [Jackson – Bidirectional Relationships](https://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion) +- [Jackson – Change Name of Field](https://www.baeldung.com/jackson-name-of-property) +- [Jackson Ignore Properties on Marshalling](https://www.baeldung.com/jackson-ignore-properties-on-serialization) diff --git a/jackson-annotations/pom.xml b/jackson-annotations/pom.xml new file mode 100644 index 0000000000..b3efc51e62 --- /dev/null +++ b/jackson-annotations/pom.xml @@ -0,0 +1,60 @@ + + 4.0.0 + jackson-annotations + 0.0.1-SNAPSHOT + jackson-annotations + + + com.baeldung + parent-java + 0.0.1-SNAPSHOT + ../parent-java + + + + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + + + com.fasterxml.jackson.dataformat + jackson-dataformat-xml + ${jackson.version} + + + com.fasterxml.jackson.module + jackson-module-jsonSchema + ${jackson.version} + + + io.rest-assured + json-path + ${rest-assured.version} + test + + + org.assertj + assertj-core + ${assertj.version} + test + + + + + jackson-annotations + + + src/main/resources + true + + + + + + 3.11.0 + 3.1.1 + + + diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/annotation/AliasBean.java b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/AliasBean.java similarity index 100% rename from jackson-simple/src/test/java/com/baeldung/jackson/annotation/AliasBean.java rename to jackson-annotations/src/main/java/com/baeldung/jackson/annotation/AliasBean.java diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/annotation/BeanWithCreator.java b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/BeanWithCreator.java similarity index 100% rename from jackson-simple/src/test/java/com/baeldung/jackson/annotation/BeanWithCreator.java rename to jackson-annotations/src/main/java/com/baeldung/jackson/annotation/BeanWithCreator.java diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/annotation/BeanWithCustomAnnotation.java b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/BeanWithCustomAnnotation.java similarity index 100% rename from jackson-simple/src/test/java/com/baeldung/jackson/annotation/BeanWithCustomAnnotation.java rename to jackson-annotations/src/main/java/com/baeldung/jackson/annotation/BeanWithCustomAnnotation.java diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/annotation/BeanWithFilter.java b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/BeanWithFilter.java similarity index 100% rename from jackson-simple/src/test/java/com/baeldung/jackson/annotation/BeanWithFilter.java rename to jackson-annotations/src/main/java/com/baeldung/jackson/annotation/BeanWithFilter.java diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/annotation/BeanWithGetter.java b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/BeanWithGetter.java similarity index 100% rename from jackson-simple/src/test/java/com/baeldung/jackson/annotation/BeanWithGetter.java rename to jackson-annotations/src/main/java/com/baeldung/jackson/annotation/BeanWithGetter.java diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/annotation/BeanWithIgnore.java b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/BeanWithIgnore.java similarity index 100% rename from jackson-simple/src/test/java/com/baeldung/jackson/annotation/BeanWithIgnore.java rename to jackson-annotations/src/main/java/com/baeldung/jackson/annotation/BeanWithIgnore.java diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/annotation/BeanWithInject.java b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/BeanWithInject.java similarity index 100% rename from jackson-simple/src/test/java/com/baeldung/jackson/annotation/BeanWithInject.java rename to jackson-annotations/src/main/java/com/baeldung/jackson/annotation/BeanWithInject.java diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/annotation/ExtendableBean.java b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/ExtendableBean.java similarity index 100% rename from jackson-simple/src/test/java/com/baeldung/jackson/annotation/ExtendableBean.java rename to jackson-annotations/src/main/java/com/baeldung/jackson/annotation/ExtendableBean.java diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/annotation/MyBean.java b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/MyBean.java similarity index 100% rename from jackson-simple/src/test/java/com/baeldung/jackson/annotation/MyBean.java rename to jackson-annotations/src/main/java/com/baeldung/jackson/annotation/MyBean.java diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/annotation/PrivateBean.java b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/PrivateBean.java similarity index 100% rename from jackson-simple/src/test/java/com/baeldung/jackson/annotation/PrivateBean.java rename to jackson-annotations/src/main/java/com/baeldung/jackson/annotation/PrivateBean.java diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/annotation/RawBean.java b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/RawBean.java similarity index 100% rename from jackson-simple/src/test/java/com/baeldung/jackson/annotation/RawBean.java rename to jackson-annotations/src/main/java/com/baeldung/jackson/annotation/RawBean.java diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/annotation/UnwrappedUser.java b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/UnwrappedUser.java similarity index 100% rename from jackson-simple/src/test/java/com/baeldung/jackson/annotation/UnwrappedUser.java rename to jackson-annotations/src/main/java/com/baeldung/jackson/annotation/UnwrappedUser.java diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/annotation/UserWithIgnoreType.java b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/UserWithIgnoreType.java similarity index 100% rename from jackson-simple/src/test/java/com/baeldung/jackson/annotation/UserWithIgnoreType.java rename to jackson-annotations/src/main/java/com/baeldung/jackson/annotation/UserWithIgnoreType.java diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/annotation/Zoo.java b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/Zoo.java similarity index 100% rename from jackson-simple/src/test/java/com/baeldung/jackson/annotation/Zoo.java rename to jackson-annotations/src/main/java/com/baeldung/jackson/annotation/Zoo.java diff --git a/jackson/src/test/java/com/baeldung/jackson/bidirection/ItemWithIdentity.java b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/bidirection/ItemWithIdentity.java similarity index 91% rename from jackson/src/test/java/com/baeldung/jackson/bidirection/ItemWithIdentity.java rename to jackson-annotations/src/main/java/com/baeldung/jackson/annotation/bidirection/ItemWithIdentity.java index 25de4a8f7a..acd68e9e4a 100644 --- a/jackson/src/test/java/com/baeldung/jackson/bidirection/ItemWithIdentity.java +++ b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/bidirection/ItemWithIdentity.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.bidirection; +package com.baeldung.jackson.annotation.bidirection; import com.fasterxml.jackson.annotation.JsonIdentityInfo; import com.fasterxml.jackson.annotation.ObjectIdGenerators; diff --git a/jackson/src/test/java/com/baeldung/jackson/bidirection/ItemWithIgnore.java b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/bidirection/ItemWithIgnore.java similarity index 86% rename from jackson/src/test/java/com/baeldung/jackson/bidirection/ItemWithIgnore.java rename to jackson-annotations/src/main/java/com/baeldung/jackson/annotation/bidirection/ItemWithIgnore.java index 910ccec174..a71bdd57f8 100644 --- a/jackson/src/test/java/com/baeldung/jackson/bidirection/ItemWithIgnore.java +++ b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/bidirection/ItemWithIgnore.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.bidirection; +package com.baeldung.jackson.annotation.bidirection; public class ItemWithIgnore { public int id; diff --git a/jackson/src/test/java/com/baeldung/jackson/bidirection/ItemWithRef.java b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/bidirection/ItemWithRef.java similarity index 88% rename from jackson/src/test/java/com/baeldung/jackson/bidirection/ItemWithRef.java rename to jackson-annotations/src/main/java/com/baeldung/jackson/annotation/bidirection/ItemWithRef.java index 0ca8d721e8..3b9bb44c37 100644 --- a/jackson/src/test/java/com/baeldung/jackson/bidirection/ItemWithRef.java +++ b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/bidirection/ItemWithRef.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.bidirection; +package com.baeldung.jackson.annotation.bidirection; import com.fasterxml.jackson.annotation.JsonManagedReference; diff --git a/jackson/src/test/java/com/baeldung/jackson/bidirection/UserWithIdentity.java b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/bidirection/UserWithIdentity.java similarity index 93% rename from jackson/src/test/java/com/baeldung/jackson/bidirection/UserWithIdentity.java rename to jackson-annotations/src/main/java/com/baeldung/jackson/annotation/bidirection/UserWithIdentity.java index db83a09389..9703b27f3e 100644 --- a/jackson/src/test/java/com/baeldung/jackson/bidirection/UserWithIdentity.java +++ b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/bidirection/UserWithIdentity.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.bidirection; +package com.baeldung.jackson.annotation.bidirection; import java.util.ArrayList; import java.util.List; diff --git a/jackson/src/test/java/com/baeldung/jackson/bidirection/UserWithIgnore.java b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/bidirection/UserWithIgnore.java similarity index 91% rename from jackson/src/test/java/com/baeldung/jackson/bidirection/UserWithIgnore.java rename to jackson-annotations/src/main/java/com/baeldung/jackson/annotation/bidirection/UserWithIgnore.java index 857a373cc5..19598a4c17 100644 --- a/jackson/src/test/java/com/baeldung/jackson/bidirection/UserWithIgnore.java +++ b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/bidirection/UserWithIgnore.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.bidirection; +package com.baeldung.jackson.annotation.bidirection; import java.util.ArrayList; import java.util.List; diff --git a/jackson/src/test/java/com/baeldung/jackson/bidirection/UserWithRef.java b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/bidirection/UserWithRef.java similarity index 91% rename from jackson/src/test/java/com/baeldung/jackson/bidirection/UserWithRef.java rename to jackson-annotations/src/main/java/com/baeldung/jackson/annotation/bidirection/UserWithRef.java index 3de03fc651..3d844ea6c0 100644 --- a/jackson/src/test/java/com/baeldung/jackson/bidirection/UserWithRef.java +++ b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/bidirection/UserWithRef.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.bidirection; +package com.baeldung.jackson.annotation.bidirection; import java.util.ArrayList; import java.util.List; diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/date/CustomDateDeserializer.java b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/date/CustomDateDeserializer.java similarity index 95% rename from jackson-simple/src/test/java/com/baeldung/jackson/date/CustomDateDeserializer.java rename to jackson-annotations/src/main/java/com/baeldung/jackson/annotation/date/CustomDateDeserializer.java index 90c7d9fbac..f17c4dc0dd 100644 --- a/jackson-simple/src/test/java/com/baeldung/jackson/date/CustomDateDeserializer.java +++ b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/date/CustomDateDeserializer.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.date; +package com.baeldung.jackson.annotation.date; import java.io.IOException; import java.text.ParseException; diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/date/CustomDateSerializer.java b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/date/CustomDateSerializer.java similarity index 95% rename from jackson-simple/src/test/java/com/baeldung/jackson/date/CustomDateSerializer.java rename to jackson-annotations/src/main/java/com/baeldung/jackson/annotation/date/CustomDateSerializer.java index d840e1940f..1e869d876a 100644 --- a/jackson-simple/src/test/java/com/baeldung/jackson/date/CustomDateSerializer.java +++ b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/date/CustomDateSerializer.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.date; +package com.baeldung.jackson.annotation.date; import java.io.IOException; import java.text.SimpleDateFormat; diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/date/EventWithFormat.java b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/date/EventWithFormat.java similarity index 92% rename from jackson-simple/src/test/java/com/baeldung/jackson/date/EventWithFormat.java rename to jackson-annotations/src/main/java/com/baeldung/jackson/annotation/date/EventWithFormat.java index 607e694cef..423b927976 100644 --- a/jackson-simple/src/test/java/com/baeldung/jackson/date/EventWithFormat.java +++ b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/date/EventWithFormat.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.date; +package com.baeldung.jackson.annotation.date; import java.util.Date; diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/date/EventWithSerializer.java b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/date/EventWithSerializer.java similarity index 93% rename from jackson-simple/src/test/java/com/baeldung/jackson/date/EventWithSerializer.java rename to jackson-annotations/src/main/java/com/baeldung/jackson/annotation/date/EventWithSerializer.java index c359b5c846..292cb9129d 100644 --- a/jackson-simple/src/test/java/com/baeldung/jackson/date/EventWithSerializer.java +++ b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/date/EventWithSerializer.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.date; +package com.baeldung.jackson.annotation.date; import java.util.Date; diff --git a/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/deserialization/ItemDeserializerOnClass.java b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/deserialization/ItemDeserializerOnClass.java new file mode 100644 index 0000000000..fe86d48f6a --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/deserialization/ItemDeserializerOnClass.java @@ -0,0 +1,41 @@ +package com.baeldung.jackson.annotation.deserialization; + +import java.io.IOException; + +import com.baeldung.jackson.annotation.dtos.ItemWithSerializer; +import com.baeldung.jackson.annotation.dtos.User; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.deser.std.StdDeserializer; +import com.fasterxml.jackson.databind.node.IntNode; + +public class ItemDeserializerOnClass extends StdDeserializer { + + private static final long serialVersionUID = 5579141241817332594L; + + public ItemDeserializerOnClass() { + this(null); + } + + public ItemDeserializerOnClass(final Class vc) { + super(vc); + } + + /** + * {"id":1,"itemNr":"theItem","owner":2} + */ + @Override + public ItemWithSerializer deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException, JsonProcessingException { + final JsonNode node = jp.getCodec() + .readTree(jp); + final int id = (Integer) ((IntNode) node.get("id")).numberValue(); + final String itemName = node.get("itemName") + .asText(); + final int userId = (Integer) ((IntNode) node.get("owner")).numberValue(); + + return new ItemWithSerializer(id, itemName, new User(userId, null)); + } + +} \ No newline at end of file diff --git a/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/dtos/Item.java b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/dtos/Item.java new file mode 100644 index 0000000000..009789bbf6 --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/dtos/Item.java @@ -0,0 +1,32 @@ +package com.baeldung.jackson.annotation.dtos; + +public class Item { + public int id; + public String itemName; + public User owner; + + public Item() { + super(); + } + + public Item(final int id, final String itemName, final User owner) { + this.id = id; + this.itemName = itemName; + this.owner = owner; + } + + // API + + public int getId() { + return id; + } + + public String getItemName() { + return itemName; + } + + public User getOwner() { + return owner; + } + +} \ No newline at end of file diff --git a/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/dtos/ItemWithSerializer.java b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/dtos/ItemWithSerializer.java new file mode 100644 index 0000000000..a4cd7d78ad --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/dtos/ItemWithSerializer.java @@ -0,0 +1,36 @@ +package com.baeldung.jackson.annotation.dtos; + +import com.baeldung.jackson.annotation.deserialization.ItemDeserializerOnClass; +import com.baeldung.jackson.annotation.serialization.ItemSerializerOnClass; + +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; + +@JsonSerialize(using = ItemSerializerOnClass.class) +@JsonDeserialize(using = ItemDeserializerOnClass.class) +public class ItemWithSerializer { + public final int id; + public final String itemName; + public final User owner; + + public ItemWithSerializer(final int id, final String itemName, final User owner) { + this.id = id; + this.itemName = itemName; + this.owner = owner; + } + + // API + + public int getId() { + return id; + } + + public String getItemName() { + return itemName; + } + + public User getOwner() { + return owner; + } + +} \ No newline at end of file diff --git a/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/dtos/User.java b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/dtos/User.java new file mode 100644 index 0000000000..9a4ec08ec3 --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/dtos/User.java @@ -0,0 +1,26 @@ +package com.baeldung.jackson.annotation.dtos; + +public class User { + public int id; + public String name; + + public User() { + super(); + } + + public User(final int id, final String name) { + this.id = id; + this.name = name; + } + + // API + + public int getId() { + return id; + } + + public String getName() { + return name; + } + +} \ No newline at end of file diff --git a/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/dtos/withEnum/DistanceEnumWithValue.java b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/dtos/withEnum/DistanceEnumWithValue.java new file mode 100644 index 0000000000..574908523a --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/dtos/withEnum/DistanceEnumWithValue.java @@ -0,0 +1,29 @@ +package com.baeldung.jackson.annotation.dtos.withEnum; + +import com.fasterxml.jackson.annotation.JsonValue; + +public enum DistanceEnumWithValue { + KILOMETER("km", 1000), MILE("miles", 1609.34), METER("meters", 1), INCH("inches", 0.0254), CENTIMETER("cm", 0.01), MILLIMETER("mm", 0.001); + + private String unit; + private final double meters; + + private DistanceEnumWithValue(String unit, double meters) { + this.unit = unit; + this.meters = meters; + } + + @JsonValue + public double getMeters() { + return meters; + } + + public String getUnit() { + return unit; + } + + public void setUnit(String unit) { + this.unit = unit; + } + +} \ No newline at end of file diff --git a/jackson/src/test/java/com/baeldung/jackson/exception/UserWithRoot.java b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/exception/UserWithRoot.java similarity index 86% rename from jackson/src/test/java/com/baeldung/jackson/exception/UserWithRoot.java rename to jackson-annotations/src/main/java/com/baeldung/jackson/annotation/exception/UserWithRoot.java index d879c16e6a..629f444356 100644 --- a/jackson/src/test/java/com/baeldung/jackson/exception/UserWithRoot.java +++ b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/exception/UserWithRoot.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.exception; +package com.baeldung.jackson.annotation.exception; import com.fasterxml.jackson.annotation.JsonRootName; diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/exception/UserWithRootNamespace.java b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/exception/UserWithRootNamespace.java similarity index 87% rename from jackson-simple/src/test/java/com/baeldung/jackson/exception/UserWithRootNamespace.java rename to jackson-annotations/src/main/java/com/baeldung/jackson/annotation/exception/UserWithRootNamespace.java index bf8c85efba..8f863168af 100644 --- a/jackson-simple/src/test/java/com/baeldung/jackson/exception/UserWithRootNamespace.java +++ b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/exception/UserWithRootNamespace.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.exception; +package com.baeldung.jackson.annotation.exception; import com.fasterxml.jackson.annotation.JsonRootName; diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/jsonview/Item.java b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/jsonview/Item.java similarity index 92% rename from jackson-simple/src/test/java/com/baeldung/jackson/jsonview/Item.java rename to jackson-annotations/src/main/java/com/baeldung/jackson/annotation/jsonview/Item.java index 26d20d4847..3a2681e0f0 100644 --- a/jackson-simple/src/test/java/com/baeldung/jackson/jsonview/Item.java +++ b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/jsonview/Item.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.jsonview; +package com.baeldung.jackson.annotation.jsonview; import com.fasterxml.jackson.annotation.JsonView; diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/jsonview/Views.java b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/jsonview/Views.java similarity index 70% rename from jackson-simple/src/test/java/com/baeldung/jackson/jsonview/Views.java rename to jackson-annotations/src/main/java/com/baeldung/jackson/annotation/jsonview/Views.java index 65950b7f9f..567592608a 100644 --- a/jackson-simple/src/test/java/com/baeldung/jackson/jsonview/Views.java +++ b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/jsonview/Views.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.jsonview; +package com.baeldung.jackson.annotation.jsonview; public class Views { public static class Public { diff --git a/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/serialization/ItemSerializer.java b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/serialization/ItemSerializer.java new file mode 100644 index 0000000000..cc17228de2 --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/serialization/ItemSerializer.java @@ -0,0 +1,32 @@ +package com.baeldung.jackson.annotation.serialization; + +import java.io.IOException; + +import com.baeldung.jackson.annotation.dtos.Item; +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.ser.std.StdSerializer; + +public class ItemSerializer extends StdSerializer { + + private static final long serialVersionUID = 6739170890621978901L; + + public ItemSerializer() { + this(null); + } + + public ItemSerializer(final Class t) { + super(t); + } + + @Override + public final void serialize(final Item value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException, JsonProcessingException { + jgen.writeStartObject(); + jgen.writeNumberField("id", value.id); + jgen.writeStringField("itemName", value.itemName); + jgen.writeNumberField("owner", value.owner.id); + jgen.writeEndObject(); + } + +} \ No newline at end of file diff --git a/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/serialization/ItemSerializerOnClass.java b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/serialization/ItemSerializerOnClass.java new file mode 100644 index 0000000000..bad44c01e7 --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jackson/annotation/serialization/ItemSerializerOnClass.java @@ -0,0 +1,32 @@ +package com.baeldung.jackson.annotation.serialization; + +import java.io.IOException; + +import com.baeldung.jackson.annotation.dtos.ItemWithSerializer; +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.ser.std.StdSerializer; + +public class ItemSerializerOnClass extends StdSerializer { + + private static final long serialVersionUID = -1760959597313610409L; + + public ItemSerializerOnClass() { + this(null); + } + + public ItemSerializerOnClass(final Class t) { + super(t); + } + + @Override + public final void serialize(final ItemWithSerializer value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException, JsonProcessingException { + jgen.writeStartObject(); + jgen.writeNumberField("id", value.id); + jgen.writeStringField("itemName", value.itemName); + jgen.writeNumberField("owner", value.owner.id); + jgen.writeEndObject(); + } + +} \ No newline at end of file diff --git a/jackson/src/test/java/com/baeldung/jackson/bidirection/CustomListDeserializer.java b/jackson-annotations/src/main/java/com/baeldung/jackson/bidirection/CustomListDeserializer.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/bidirection/CustomListDeserializer.java rename to jackson-annotations/src/main/java/com/baeldung/jackson/bidirection/CustomListDeserializer.java diff --git a/jackson/src/test/java/com/baeldung/jackson/bidirection/CustomListSerializer.java b/jackson-annotations/src/main/java/com/baeldung/jackson/bidirection/CustomListSerializer.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/bidirection/CustomListSerializer.java rename to jackson-annotations/src/main/java/com/baeldung/jackson/bidirection/CustomListSerializer.java diff --git a/jackson/src/test/java/com/baeldung/jackson/bidirection/Item.java b/jackson-annotations/src/main/java/com/baeldung/jackson/bidirection/Item.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/bidirection/Item.java rename to jackson-annotations/src/main/java/com/baeldung/jackson/bidirection/Item.java diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/bidirection/ItemWithIdentity.java b/jackson-annotations/src/main/java/com/baeldung/jackson/bidirection/ItemWithIdentity.java similarity index 100% rename from jackson-simple/src/test/java/com/baeldung/jackson/bidirection/ItemWithIdentity.java rename to jackson-annotations/src/main/java/com/baeldung/jackson/bidirection/ItemWithIdentity.java diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/bidirection/ItemWithIgnore.java b/jackson-annotations/src/main/java/com/baeldung/jackson/bidirection/ItemWithIgnore.java similarity index 100% rename from jackson-simple/src/test/java/com/baeldung/jackson/bidirection/ItemWithIgnore.java rename to jackson-annotations/src/main/java/com/baeldung/jackson/bidirection/ItemWithIgnore.java diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/bidirection/ItemWithRef.java b/jackson-annotations/src/main/java/com/baeldung/jackson/bidirection/ItemWithRef.java similarity index 100% rename from jackson-simple/src/test/java/com/baeldung/jackson/bidirection/ItemWithRef.java rename to jackson-annotations/src/main/java/com/baeldung/jackson/bidirection/ItemWithRef.java diff --git a/jackson/src/test/java/com/baeldung/jackson/bidirection/ItemWithSerializer.java b/jackson-annotations/src/main/java/com/baeldung/jackson/bidirection/ItemWithSerializer.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/bidirection/ItemWithSerializer.java rename to jackson-annotations/src/main/java/com/baeldung/jackson/bidirection/ItemWithSerializer.java diff --git a/jackson/src/test/java/com/baeldung/jackson/bidirection/ItemWithView.java b/jackson-annotations/src/main/java/com/baeldung/jackson/bidirection/ItemWithView.java similarity index 90% rename from jackson/src/test/java/com/baeldung/jackson/bidirection/ItemWithView.java rename to jackson-annotations/src/main/java/com/baeldung/jackson/bidirection/ItemWithView.java index ffa19fbad2..5cf9cd9981 100644 --- a/jackson/src/test/java/com/baeldung/jackson/bidirection/ItemWithView.java +++ b/jackson-annotations/src/main/java/com/baeldung/jackson/bidirection/ItemWithView.java @@ -1,6 +1,6 @@ package com.baeldung.jackson.bidirection; -import com.baeldung.jackson.jsonview.Views; +import com.baeldung.jackson.bidirection.jsonview.Views; import com.fasterxml.jackson.annotation.JsonView; diff --git a/jackson/src/test/java/com/baeldung/jackson/bidirection/User.java b/jackson-annotations/src/main/java/com/baeldung/jackson/bidirection/User.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/bidirection/User.java rename to jackson-annotations/src/main/java/com/baeldung/jackson/bidirection/User.java diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/bidirection/UserWithIdentity.java b/jackson-annotations/src/main/java/com/baeldung/jackson/bidirection/UserWithIdentity.java similarity index 100% rename from jackson-simple/src/test/java/com/baeldung/jackson/bidirection/UserWithIdentity.java rename to jackson-annotations/src/main/java/com/baeldung/jackson/bidirection/UserWithIdentity.java diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/bidirection/UserWithIgnore.java b/jackson-annotations/src/main/java/com/baeldung/jackson/bidirection/UserWithIgnore.java similarity index 100% rename from jackson-simple/src/test/java/com/baeldung/jackson/bidirection/UserWithIgnore.java rename to jackson-annotations/src/main/java/com/baeldung/jackson/bidirection/UserWithIgnore.java diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/bidirection/UserWithRef.java b/jackson-annotations/src/main/java/com/baeldung/jackson/bidirection/UserWithRef.java similarity index 100% rename from jackson-simple/src/test/java/com/baeldung/jackson/bidirection/UserWithRef.java rename to jackson-annotations/src/main/java/com/baeldung/jackson/bidirection/UserWithRef.java diff --git a/jackson/src/test/java/com/baeldung/jackson/bidirection/UserWithSerializer.java b/jackson-annotations/src/main/java/com/baeldung/jackson/bidirection/UserWithSerializer.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/bidirection/UserWithSerializer.java rename to jackson-annotations/src/main/java/com/baeldung/jackson/bidirection/UserWithSerializer.java diff --git a/jackson/src/test/java/com/baeldung/jackson/bidirection/UserWithView.java b/jackson-annotations/src/main/java/com/baeldung/jackson/bidirection/UserWithView.java similarity index 92% rename from jackson/src/test/java/com/baeldung/jackson/bidirection/UserWithView.java rename to jackson-annotations/src/main/java/com/baeldung/jackson/bidirection/UserWithView.java index d92d67b3a1..ad73eb8f92 100644 --- a/jackson/src/test/java/com/baeldung/jackson/bidirection/UserWithView.java +++ b/jackson-annotations/src/main/java/com/baeldung/jackson/bidirection/UserWithView.java @@ -3,7 +3,7 @@ package com.baeldung.jackson.bidirection; import java.util.ArrayList; import java.util.List; -import com.baeldung.jackson.jsonview.Views; +import com.baeldung.jackson.bidirection.jsonview.Views; import com.fasterxml.jackson.annotation.JsonView; diff --git a/jackson-annotations/src/main/java/com/baeldung/jackson/bidirection/jsonview/Views.java b/jackson-annotations/src/main/java/com/baeldung/jackson/bidirection/jsonview/Views.java new file mode 100644 index 0000000000..0a13d55a5b --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jackson/bidirection/jsonview/Views.java @@ -0,0 +1,9 @@ +package com.baeldung.jackson.bidirection.jsonview; + +public class Views { + public static class Public { + } + + public static class Internal extends Public { + } +} diff --git a/jackson-annotations/src/main/java/com/baeldung/jackson/domain/Person.java b/jackson-annotations/src/main/java/com/baeldung/jackson/domain/Person.java new file mode 100644 index 0000000000..f11ba41113 --- /dev/null +++ b/jackson-annotations/src/main/java/com/baeldung/jackson/domain/Person.java @@ -0,0 +1,30 @@ +package com.baeldung.jackson.domain; + +public class Person { + + private String firstName; + private String lastName; + + public Person(String firstName, String lastName) { + super(); + 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; + } +} + diff --git a/jackson/src/main/java/com/baeldung/jackson/format/User.java b/jackson-annotations/src/main/java/com/baeldung/jackson/format/User.java old mode 100755 new mode 100644 similarity index 100% rename from jackson/src/main/java/com/baeldung/jackson/format/User.java rename to jackson-annotations/src/main/java/com/baeldung/jackson/format/User.java diff --git a/jackson/src/test/java/com/baeldung/jackson/annotation/extra/ExtraAnnotationUnitTest.java b/jackson-annotations/src/test/java/com/baeldung/jackson/advancedannotations/AdvancedAnnotationsUnitTest.java similarity index 92% rename from jackson/src/test/java/com/baeldung/jackson/annotation/extra/ExtraAnnotationUnitTest.java rename to jackson-annotations/src/test/java/com/baeldung/jackson/advancedannotations/AdvancedAnnotationsUnitTest.java index 647c451659..c0eb917292 100644 --- a/jackson/src/test/java/com/baeldung/jackson/annotation/extra/ExtraAnnotationUnitTest.java +++ b/jackson-annotations/src/test/java/com/baeldung/jackson/advancedannotations/AdvancedAnnotationsUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.annotation.extra; +package com.baeldung.jackson.advancedannotations; import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.instanceOf; @@ -12,10 +12,10 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; -import com.baeldung.jackson.annotation.extra.AppendBeans.BeanWithAppend; -import com.baeldung.jackson.annotation.extra.AppendBeans.BeanWithoutAppend; -import com.baeldung.jackson.annotation.extra.IdentityReferenceBeans.BeanWithIdentityReference; -import com.baeldung.jackson.annotation.extra.IdentityReferenceBeans.BeanWithoutIdentityReference; +import com.baeldung.jackson.advancedannotations.AppendBeans.BeanWithAppend; +import com.baeldung.jackson.advancedannotations.AppendBeans.BeanWithoutAppend; +import com.baeldung.jackson.advancedannotations.IdentityReferenceBeans.BeanWithIdentityReference; +import com.baeldung.jackson.advancedannotations.IdentityReferenceBeans.BeanWithoutIdentityReference; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper.DefaultTyping; @@ -23,7 +23,7 @@ import com.fasterxml.jackson.databind.ObjectWriter; import com.fasterxml.jackson.module.jsonSchema.JsonSchema; import com.fasterxml.jackson.module.jsonSchema.factories.SchemaFactoryWrapper; -public class ExtraAnnotationUnitTest { +public class AdvancedAnnotationsUnitTest { @Test public void whenNotUsingJsonIdentityReferenceAnnotation_thenCorrect() throws JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); diff --git a/jackson/src/test/java/com/baeldung/jackson/annotation/extra/AppendBeans.java b/jackson-annotations/src/test/java/com/baeldung/jackson/advancedannotations/AppendBeans.java similarity index 95% rename from jackson/src/test/java/com/baeldung/jackson/annotation/extra/AppendBeans.java rename to jackson-annotations/src/test/java/com/baeldung/jackson/advancedannotations/AppendBeans.java index 7b75c205c9..ab045dd0c6 100644 --- a/jackson/src/test/java/com/baeldung/jackson/annotation/extra/AppendBeans.java +++ b/jackson-annotations/src/test/java/com/baeldung/jackson/advancedannotations/AppendBeans.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.annotation.extra; +package com.baeldung.jackson.advancedannotations; import com.fasterxml.jackson.databind.annotation.JsonAppend; diff --git a/jackson/src/test/java/com/baeldung/jackson/annotation/extra/IdentityReferenceBeans.java b/jackson-annotations/src/test/java/com/baeldung/jackson/advancedannotations/IdentityReferenceBeans.java similarity index 96% rename from jackson/src/test/java/com/baeldung/jackson/annotation/extra/IdentityReferenceBeans.java rename to jackson-annotations/src/test/java/com/baeldung/jackson/advancedannotations/IdentityReferenceBeans.java index 0a8736d9a5..6e723c317a 100644 --- a/jackson/src/test/java/com/baeldung/jackson/annotation/extra/IdentityReferenceBeans.java +++ b/jackson-annotations/src/test/java/com/baeldung/jackson/advancedannotations/IdentityReferenceBeans.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.annotation.extra; +package com.baeldung.jackson.advancedannotations; import com.fasterxml.jackson.annotation.JsonIdentityInfo; import com.fasterxml.jackson.annotation.JsonIdentityReference; diff --git a/jackson/src/test/java/com/baeldung/jackson/annotation/extra/NamingBean.java b/jackson-annotations/src/test/java/com/baeldung/jackson/advancedannotations/NamingBean.java similarity index 92% rename from jackson/src/test/java/com/baeldung/jackson/annotation/extra/NamingBean.java rename to jackson-annotations/src/test/java/com/baeldung/jackson/advancedannotations/NamingBean.java index efd26ab9ae..323df49c14 100644 --- a/jackson/src/test/java/com/baeldung/jackson/annotation/extra/NamingBean.java +++ b/jackson-annotations/src/test/java/com/baeldung/jackson/advancedannotations/NamingBean.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.annotation.extra; +package com.baeldung.jackson.advancedannotations; import com.fasterxml.jackson.databind.PropertyNamingStrategy; import com.fasterxml.jackson.databind.annotation.JsonNaming; diff --git a/jackson/src/test/java/com/baeldung/jackson/annotation/extra/POJOBuilderBean.java b/jackson-annotations/src/test/java/com/baeldung/jackson/advancedannotations/POJOBuilderBean.java similarity index 96% rename from jackson/src/test/java/com/baeldung/jackson/annotation/extra/POJOBuilderBean.java rename to jackson-annotations/src/test/java/com/baeldung/jackson/advancedannotations/POJOBuilderBean.java index e0a89c6903..97b747fe40 100644 --- a/jackson/src/test/java/com/baeldung/jackson/annotation/extra/POJOBuilderBean.java +++ b/jackson-annotations/src/test/java/com/baeldung/jackson/advancedannotations/POJOBuilderBean.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.annotation.extra; +package com.baeldung.jackson.advancedannotations; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder; diff --git a/jackson/src/test/java/com/baeldung/jackson/annotation/extra/PropertyDescriptionBean.java b/jackson-annotations/src/test/java/com/baeldung/jackson/advancedannotations/PropertyDescriptionBean.java similarity index 90% rename from jackson/src/test/java/com/baeldung/jackson/annotation/extra/PropertyDescriptionBean.java rename to jackson-annotations/src/test/java/com/baeldung/jackson/advancedannotations/PropertyDescriptionBean.java index 1563cddb83..dcc0c67054 100644 --- a/jackson/src/test/java/com/baeldung/jackson/annotation/extra/PropertyDescriptionBean.java +++ b/jackson-annotations/src/test/java/com/baeldung/jackson/advancedannotations/PropertyDescriptionBean.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.annotation.extra; +package com.baeldung.jackson.advancedannotations; import com.fasterxml.jackson.annotation.JsonPropertyDescription; diff --git a/jackson/src/test/java/com/baeldung/jackson/annotation/extra/TypeIdBean.java b/jackson-annotations/src/test/java/com/baeldung/jackson/advancedannotations/TypeIdBean.java similarity index 90% rename from jackson/src/test/java/com/baeldung/jackson/annotation/extra/TypeIdBean.java rename to jackson-annotations/src/test/java/com/baeldung/jackson/advancedannotations/TypeIdBean.java index 32a6d5a1d5..1b25529743 100644 --- a/jackson/src/test/java/com/baeldung/jackson/annotation/extra/TypeIdBean.java +++ b/jackson-annotations/src/test/java/com/baeldung/jackson/advancedannotations/TypeIdBean.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.annotation.extra; +package com.baeldung.jackson.advancedannotations; import com.fasterxml.jackson.annotation.JsonTypeId; diff --git a/jackson/src/test/java/com/baeldung/jackson/annotation/extra/TypeIdResolverStructure.java b/jackson-annotations/src/test/java/com/baeldung/jackson/advancedannotations/TypeIdResolverStructure.java similarity index 98% rename from jackson/src/test/java/com/baeldung/jackson/annotation/extra/TypeIdResolverStructure.java rename to jackson-annotations/src/test/java/com/baeldung/jackson/advancedannotations/TypeIdResolverStructure.java index 9056023c69..f9ddd8eea9 100644 --- a/jackson/src/test/java/com/baeldung/jackson/annotation/extra/TypeIdResolverStructure.java +++ b/jackson-annotations/src/test/java/com/baeldung/jackson/advancedannotations/TypeIdResolverStructure.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.annotation.extra; +package com.baeldung.jackson.advancedannotations; import java.util.List; diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/test/JacksonAnnotationUnitTest.java b/jackson-annotations/src/test/java/com/baeldung/jackson/annotation/JacksonAnnotationUnitTest.java similarity index 90% rename from jackson-simple/src/test/java/com/baeldung/jackson/test/JacksonAnnotationUnitTest.java rename to jackson-annotations/src/test/java/com/baeldung/jackson/annotation/JacksonAnnotationUnitTest.java index ee11f8f20e..462b65787a 100644 --- a/jackson-simple/src/test/java/com/baeldung/jackson/test/JacksonAnnotationUnitTest.java +++ b/jackson-annotations/src/test/java/com/baeldung/jackson/annotation/JacksonAnnotationUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.test; +package com.baeldung.jackson.annotation; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.is; @@ -14,32 +14,18 @@ import java.util.TimeZone; import org.junit.Test; -import com.baeldung.jackson.annotation.AliasBean; -import com.baeldung.jackson.annotation.BeanWithCreator; -import com.baeldung.jackson.annotation.BeanWithCustomAnnotation; -import com.baeldung.jackson.annotation.BeanWithFilter; -import com.baeldung.jackson.annotation.BeanWithGetter; -import com.baeldung.jackson.annotation.BeanWithIgnore; -import com.baeldung.jackson.annotation.BeanWithInject; -import com.baeldung.jackson.annotation.ExtendableBean; -import com.baeldung.jackson.annotation.MyBean; -import com.baeldung.jackson.annotation.PrivateBean; -import com.baeldung.jackson.annotation.RawBean; -import com.baeldung.jackson.annotation.UnwrappedUser; -import com.baeldung.jackson.annotation.UserWithIgnoreType; -import com.baeldung.jackson.annotation.Zoo; -import com.baeldung.jackson.bidirection.ItemWithIdentity; -import com.baeldung.jackson.bidirection.ItemWithRef; -import com.baeldung.jackson.bidirection.UserWithIdentity; -import com.baeldung.jackson.bidirection.UserWithRef; -import com.baeldung.jackson.date.EventWithFormat; -import com.baeldung.jackson.date.EventWithSerializer; -import com.baeldung.jackson.dtos.MyMixInForIgnoreType; -import com.baeldung.jackson.dtos.withEnum.DistanceEnumWithValue; -import com.baeldung.jackson.exception.UserWithRoot; -import com.baeldung.jackson.exception.UserWithRootNamespace; -import com.baeldung.jackson.jsonview.Item; -import com.baeldung.jackson.jsonview.Views; +import com.baeldung.jackson.annotation.bidirection.ItemWithIdentity; +import com.baeldung.jackson.annotation.bidirection.ItemWithRef; +import com.baeldung.jackson.annotation.bidirection.UserWithIdentity; +import com.baeldung.jackson.annotation.bidirection.UserWithRef; +import com.baeldung.jackson.annotation.date.EventWithFormat; +import com.baeldung.jackson.annotation.date.EventWithSerializer; +import com.baeldung.jackson.ignore.dtos.MyMixInForIgnoreType; +import com.baeldung.jackson.annotation.dtos.withEnum.DistanceEnumWithValue; +import com.baeldung.jackson.annotation.exception.UserWithRoot; +import com.baeldung.jackson.annotation.exception.UserWithRootNamespace; +import com.baeldung.jackson.annotation.jsonview.Item; +import com.baeldung.jackson.annotation.jsonview.Views; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.InjectableValues; import com.fasterxml.jackson.databind.MapperFeature; @@ -352,13 +338,13 @@ public class JacksonAnnotationUnitTest { // @Ignore("Jackson 2.7.1-1 seems to have changed the API regarding mixins") @Test public void whenSerializingUsingMixInAnnotation_thenCorrect() throws JsonProcessingException { - final com.baeldung.jackson.dtos.Item item = new com.baeldung.jackson.dtos.Item(1, "book", null); + final com.baeldung.jackson.annotation.dtos.Item item = new com.baeldung.jackson.annotation.dtos.Item(1, "book", null); String result = new ObjectMapper().writeValueAsString(item); assertThat(result, containsString("owner")); final ObjectMapper mapper = new ObjectMapper(); - mapper.addMixIn(com.baeldung.jackson.dtos.User.class, MyMixInForIgnoreType.class); + mapper.addMixIn(com.baeldung.jackson.annotation.dtos.User.class, MyMixInForIgnoreType.class); result = mapper.writeValueAsString(item); assertThat(result, not(containsString("owner"))); diff --git a/jackson/src/test/java/com/baeldung/jackson/test/JacksonBidirectionRelationUnitTest.java b/jackson-annotations/src/test/java/com/baeldung/jackson/bidirection/JacksonBidirectionRelationUnitTest.java similarity index 87% rename from jackson/src/test/java/com/baeldung/jackson/test/JacksonBidirectionRelationUnitTest.java rename to jackson-annotations/src/test/java/com/baeldung/jackson/bidirection/JacksonBidirectionRelationUnitTest.java index e55ca55ac9..d0c5209891 100644 --- a/jackson/src/test/java/com/baeldung/jackson/test/JacksonBidirectionRelationUnitTest.java +++ b/jackson-annotations/src/test/java/com/baeldung/jackson/bidirection/JacksonBidirectionRelationUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.test; +package com.baeldung.jackson.bidirection; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.not; @@ -9,19 +9,7 @@ import java.io.IOException; import org.junit.Test; -import com.baeldung.jackson.bidirection.Item; -import com.baeldung.jackson.bidirection.ItemWithIdentity; -import com.baeldung.jackson.bidirection.ItemWithIgnore; -import com.baeldung.jackson.bidirection.ItemWithRef; -import com.baeldung.jackson.bidirection.ItemWithSerializer; -import com.baeldung.jackson.bidirection.ItemWithView; -import com.baeldung.jackson.bidirection.User; -import com.baeldung.jackson.bidirection.UserWithIdentity; -import com.baeldung.jackson.bidirection.UserWithIgnore; -import com.baeldung.jackson.bidirection.UserWithRef; -import com.baeldung.jackson.bidirection.UserWithSerializer; -import com.baeldung.jackson.bidirection.UserWithView; -import com.baeldung.jackson.jsonview.Views; +import com.baeldung.jackson.bidirection.jsonview.Views; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; diff --git a/jackson/src/test/java/com/baeldung/jackson/format/JsonFormatUnitTest.java b/jackson-annotations/src/test/java/com/baeldung/jackson/format/JsonFormatUnitTest.java old mode 100755 new mode 100644 similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/format/JsonFormatUnitTest.java rename to jackson-annotations/src/test/java/com/baeldung/jackson/format/JsonFormatUnitTest.java diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/ignore/JacksonSerializationIgnoreUnitTest.java b/jackson-annotations/src/test/java/com/baeldung/jackson/ignore/JacksonSerializationIgnoreUnitTest.java similarity index 96% rename from jackson-simple/src/test/java/com/baeldung/jackson/ignore/JacksonSerializationIgnoreUnitTest.java rename to jackson-annotations/src/test/java/com/baeldung/jackson/ignore/JacksonSerializationIgnoreUnitTest.java index da8b464d03..ab51fc11e7 100644 --- a/jackson-simple/src/test/java/com/baeldung/jackson/ignore/JacksonSerializationIgnoreUnitTest.java +++ b/jackson-annotations/src/test/java/com/baeldung/jackson/ignore/JacksonSerializationIgnoreUnitTest.java @@ -10,15 +10,15 @@ import java.util.Map; import org.junit.Test; -import com.baeldung.jackson.dtos.MyDto; -import com.baeldung.jackson.dtos.MyDtoIncludeNonDefault; -import com.baeldung.jackson.dtos.MyDtoWithFilter; -import com.baeldung.jackson.dtos.MyDtoWithSpecialField; -import com.baeldung.jackson.dtos.MyMixInForIgnoreType; +import com.baeldung.jackson.ignore.dtos.MyDto; +import com.baeldung.jackson.ignore.dtos.MyDtoIncludeNonDefault; +import com.baeldung.jackson.ignore.dtos.MyDtoWithFilter; +import com.baeldung.jackson.ignore.dtos.MyDtoWithSpecialField; +import com.baeldung.jackson.ignore.dtos.MyMixInForIgnoreType; import com.baeldung.jackson.ignore.dtos.MyDtoIgnoreField; import com.baeldung.jackson.ignore.dtos.MyDtoIgnoreFieldByName; import com.baeldung.jackson.ignore.dtos.MyDtoIgnoreNull; -import com.baeldung.jackson.serialization.MyDtoNullKeySerializer; +import com.baeldung.jackson.ignore.dtos.MyDtoNullKeySerializer; import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonParseException; diff --git a/jackson-annotations/src/test/java/com/baeldung/jackson/ignore/dtos/MyDto.java b/jackson-annotations/src/test/java/com/baeldung/jackson/ignore/dtos/MyDto.java new file mode 100644 index 0000000000..3357c041c0 --- /dev/null +++ b/jackson-annotations/src/test/java/com/baeldung/jackson/ignore/dtos/MyDto.java @@ -0,0 +1,54 @@ +package com.baeldung.jackson.ignore.dtos; + +public class MyDto { + + private String stringValue; + private int intValue; + private boolean booleanValue; + + public MyDto() { + super(); + } + + public MyDto(final String stringValue, final int intValue, final boolean booleanValue) { + super(); + + this.stringValue = stringValue; + this.intValue = intValue; + this.booleanValue = booleanValue; + } + + // API + + public String getStringValue() { + return stringValue; + } + + public void setStringValue(final String stringValue) { + this.stringValue = stringValue; + } + + public int getIntValue() { + return intValue; + } + + public void setIntValue(final int intValue) { + this.intValue = intValue; + } + + public boolean isBooleanValue() { + return booleanValue; + } + + public void setBooleanValue(final boolean booleanValue) { + this.booleanValue = booleanValue; + } + + // + + @Override + public String toString() { + return "MyDto [stringValue=" + stringValue + ", intValue=" + intValue + ", booleanValue=" + booleanValue + "]"; + } + +} diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/ignore/dtos/MyDtoIgnoreField.java b/jackson-annotations/src/test/java/com/baeldung/jackson/ignore/dtos/MyDtoIgnoreField.java similarity index 100% rename from jackson-simple/src/test/java/com/baeldung/jackson/ignore/dtos/MyDtoIgnoreField.java rename to jackson-annotations/src/test/java/com/baeldung/jackson/ignore/dtos/MyDtoIgnoreField.java diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/ignore/dtos/MyDtoIgnoreFieldByName.java b/jackson-annotations/src/test/java/com/baeldung/jackson/ignore/dtos/MyDtoIgnoreFieldByName.java similarity index 100% rename from jackson-simple/src/test/java/com/baeldung/jackson/ignore/dtos/MyDtoIgnoreFieldByName.java rename to jackson-annotations/src/test/java/com/baeldung/jackson/ignore/dtos/MyDtoIgnoreFieldByName.java diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/ignore/dtos/MyDtoIgnoreNull.java b/jackson-annotations/src/test/java/com/baeldung/jackson/ignore/dtos/MyDtoIgnoreNull.java similarity index 100% rename from jackson-simple/src/test/java/com/baeldung/jackson/ignore/dtos/MyDtoIgnoreNull.java rename to jackson-annotations/src/test/java/com/baeldung/jackson/ignore/dtos/MyDtoIgnoreNull.java diff --git a/jackson-annotations/src/test/java/com/baeldung/jackson/ignore/dtos/MyDtoIncludeNonDefault.java b/jackson-annotations/src/test/java/com/baeldung/jackson/ignore/dtos/MyDtoIncludeNonDefault.java new file mode 100644 index 0000000000..cf5d1682db --- /dev/null +++ b/jackson-annotations/src/test/java/com/baeldung/jackson/ignore/dtos/MyDtoIncludeNonDefault.java @@ -0,0 +1,43 @@ +package com.baeldung.jackson.ignore.dtos; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; + +@JsonInclude(Include.NON_DEFAULT) +public class MyDtoIncludeNonDefault { + + private String stringValue; + private int intValue; + private boolean booleanValue; + + public MyDtoIncludeNonDefault() { + super(); + } + + // API + + public String getStringValue() { + return stringValue; + } + + public void setStringValue(final String stringValue) { + this.stringValue = stringValue; + } + + public int getIntValue() { + return intValue; + } + + public void setIntValue(final int intValue) { + this.intValue = intValue; + } + + public boolean isBooleanValue() { + return booleanValue; + } + + public void setBooleanValue(final boolean booleanValue) { + this.booleanValue = booleanValue; + } + +} diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/serialization/MyDtoNullKeySerializer.java b/jackson-annotations/src/test/java/com/baeldung/jackson/ignore/dtos/MyDtoNullKeySerializer.java similarity index 91% rename from jackson-simple/src/test/java/com/baeldung/jackson/serialization/MyDtoNullKeySerializer.java rename to jackson-annotations/src/test/java/com/baeldung/jackson/ignore/dtos/MyDtoNullKeySerializer.java index d0b2d7f5e9..dcd4070dba 100644 --- a/jackson-simple/src/test/java/com/baeldung/jackson/serialization/MyDtoNullKeySerializer.java +++ b/jackson-annotations/src/test/java/com/baeldung/jackson/ignore/dtos/MyDtoNullKeySerializer.java @@ -1,27 +1,27 @@ -package com.baeldung.jackson.serialization; - -import java.io.IOException; - -import com.fasterxml.jackson.core.JsonGenerator; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.SerializerProvider; -import com.fasterxml.jackson.databind.ser.std.StdSerializer; - -public class MyDtoNullKeySerializer extends StdSerializer { - - private static final long serialVersionUID = -4478531309177369056L; - - public MyDtoNullKeySerializer() { - this(null); - } - - public MyDtoNullKeySerializer(final Class t) { - super(t); - } - - @Override - public void serialize(final Object value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException, JsonProcessingException { - jgen.writeFieldName(""); - } - -} +package com.baeldung.jackson.ignore.dtos; + +import java.io.IOException; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.ser.std.StdSerializer; + +public class MyDtoNullKeySerializer extends StdSerializer { + + private static final long serialVersionUID = -4478531309177369056L; + + public MyDtoNullKeySerializer() { + this(null); + } + + public MyDtoNullKeySerializer(final Class t) { + super(t); + } + + @Override + public void serialize(final Object value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException, JsonProcessingException { + jgen.writeFieldName(""); + } + +} diff --git a/jackson-annotations/src/test/java/com/baeldung/jackson/ignore/dtos/MyDtoWithFilter.java b/jackson-annotations/src/test/java/com/baeldung/jackson/ignore/dtos/MyDtoWithFilter.java new file mode 100644 index 0000000000..256b7d206d --- /dev/null +++ b/jackson-annotations/src/test/java/com/baeldung/jackson/ignore/dtos/MyDtoWithFilter.java @@ -0,0 +1,50 @@ +package com.baeldung.jackson.ignore.dtos; + +import com.fasterxml.jackson.annotation.JsonFilter; + +@JsonFilter("myFilter") +public class MyDtoWithFilter { + + private String stringValue; + private int intValue; + private boolean booleanValue; + + public MyDtoWithFilter() { + super(); + } + + public MyDtoWithFilter(final String stringValue, final int intValue, final boolean booleanValue) { + super(); + + this.stringValue = stringValue; + this.intValue = intValue; + this.booleanValue = booleanValue; + } + + // API + + public String getStringValue() { + return stringValue; + } + + public void setStringValue(final String stringValue) { + this.stringValue = stringValue; + } + + public int getIntValue() { + return intValue; + } + + public void setIntValue(final int intValue) { + this.intValue = intValue; + } + + public boolean isBooleanValue() { + return booleanValue; + } + + public void setBooleanValue(final boolean booleanValue) { + this.booleanValue = booleanValue; + } + +} diff --git a/jackson-annotations/src/test/java/com/baeldung/jackson/ignore/dtos/MyDtoWithSpecialField.java b/jackson-annotations/src/test/java/com/baeldung/jackson/ignore/dtos/MyDtoWithSpecialField.java new file mode 100644 index 0000000000..edb0dab13b --- /dev/null +++ b/jackson-annotations/src/test/java/com/baeldung/jackson/ignore/dtos/MyDtoWithSpecialField.java @@ -0,0 +1,54 @@ +package com.baeldung.jackson.ignore.dtos; + +public class MyDtoWithSpecialField { + + private String[] stringValue; + private int intValue; + private boolean booleanValue; + + public MyDtoWithSpecialField() { + super(); + } + + public MyDtoWithSpecialField(final String[] stringValue, final int intValue, final boolean booleanValue) { + super(); + + this.stringValue = stringValue; + this.intValue = intValue; + this.booleanValue = booleanValue; + } + + // API + + public String[] getStringValue() { + return stringValue; + } + + public void setStringValue(final String[] stringValue) { + this.stringValue = stringValue; + } + + public int getIntValue() { + return intValue; + } + + public void setIntValue(final int intValue) { + this.intValue = intValue; + } + + public boolean isBooleanValue() { + return booleanValue; + } + + public void setBooleanValue(final boolean booleanValue) { + this.booleanValue = booleanValue; + } + + // + + @Override + public String toString() { + return "MyDto [stringValue=" + stringValue + ", intValue=" + intValue + ", booleanValue=" + booleanValue + "]"; + } + +} diff --git a/jackson-annotations/src/test/java/com/baeldung/jackson/ignore/dtos/MyMixInForIgnoreType.java b/jackson-annotations/src/test/java/com/baeldung/jackson/ignore/dtos/MyMixInForIgnoreType.java new file mode 100644 index 0000000000..ce97d35c83 --- /dev/null +++ b/jackson-annotations/src/test/java/com/baeldung/jackson/ignore/dtos/MyMixInForIgnoreType.java @@ -0,0 +1,8 @@ +package com.baeldung.jackson.ignore.dtos; + +import com.fasterxml.jackson.annotation.JsonIgnoreType; + +@JsonIgnoreType +public class MyMixInForIgnoreType { + // +} diff --git a/jackson-annotations/src/test/java/com/baeldung/jackson/jsonproperty/JsonPropertyUnitTest.java b/jackson-annotations/src/test/java/com/baeldung/jackson/jsonproperty/JsonPropertyUnitTest.java new file mode 100644 index 0000000000..59622ff8b8 --- /dev/null +++ b/jackson-annotations/src/test/java/com/baeldung/jackson/jsonproperty/JsonPropertyUnitTest.java @@ -0,0 +1,44 @@ +package com.baeldung.jackson.jsonproperty; + +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.not; +import static org.junit.Assert.assertThat; + +import java.io.IOException; +import java.util.List; +import org.junit.Test; + +import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; +import com.fasterxml.jackson.annotation.PropertyAccessor; +import com.fasterxml.jackson.core.JsonGenerationException; +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.module.SimpleModule; + +public class JsonPropertyUnitTest { + + @Test + public final void whenSerializing_thenCorrect() throws JsonParseException, IOException { + final ObjectMapper mapper = new ObjectMapper(); + final String dtoAsString = mapper.writeValueAsString(new MyDto()); + + assertThat(dtoAsString, containsString("intValue")); + assertThat(dtoAsString, containsString("stringValue")); + assertThat(dtoAsString, containsString("booleanValue")); + } + + @Test + public final void givenNameOfFieldIsChangedViaAnnotationOnGetter_whenSerializing_thenCorrect() throws JsonParseException, IOException { + final ObjectMapper mapper = new ObjectMapper(); + final MyDtoFieldNameChanged dtoObject = new MyDtoFieldNameChanged(); + dtoObject.setStringValue("a"); + + final String dtoAsString = mapper.writeValueAsString(dtoObject); + + assertThat(dtoAsString, not(containsString("stringValue"))); + assertThat(dtoAsString, containsString("strVal")); + System.out.println(dtoAsString); + } + +} diff --git a/jackson-annotations/src/test/java/com/baeldung/jackson/jsonproperty/MyDto.java b/jackson-annotations/src/test/java/com/baeldung/jackson/jsonproperty/MyDto.java new file mode 100644 index 0000000000..30ea8b4a56 --- /dev/null +++ b/jackson-annotations/src/test/java/com/baeldung/jackson/jsonproperty/MyDto.java @@ -0,0 +1,54 @@ +package com.baeldung.jackson.jsonproperty; + +public class MyDto { + + private String stringValue; + private int intValue; + private boolean booleanValue; + + public MyDto() { + super(); + } + + public MyDto(final String stringValue, final int intValue, final boolean booleanValue) { + super(); + + this.stringValue = stringValue; + this.intValue = intValue; + this.booleanValue = booleanValue; + } + + // API + + public String getStringValue() { + return stringValue; + } + + public void setStringValue(final String stringValue) { + this.stringValue = stringValue; + } + + public int getIntValue() { + return intValue; + } + + public void setIntValue(final int intValue) { + this.intValue = intValue; + } + + public boolean isBooleanValue() { + return booleanValue; + } + + public void setBooleanValue(final boolean booleanValue) { + this.booleanValue = booleanValue; + } + + // + + @Override + public String toString() { + return "MyDto [stringValue=" + stringValue + ", intValue=" + intValue + ", booleanValue=" + booleanValue + "]"; + } + +} diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/dtos/MyDtoFieldNameChanged.java b/jackson-annotations/src/test/java/com/baeldung/jackson/jsonproperty/MyDtoFieldNameChanged.java similarity index 96% rename from jackson-simple/src/test/java/com/baeldung/jackson/dtos/MyDtoFieldNameChanged.java rename to jackson-annotations/src/test/java/com/baeldung/jackson/jsonproperty/MyDtoFieldNameChanged.java index ce7086f1fe..be0a25a240 100644 --- a/jackson-simple/src/test/java/com/baeldung/jackson/dtos/MyDtoFieldNameChanged.java +++ b/jackson-annotations/src/test/java/com/baeldung/jackson/jsonproperty/MyDtoFieldNameChanged.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.dtos; +package com.baeldung.jackson.jsonproperty; import com.fasterxml.jackson.annotation.JsonProperty; diff --git a/jackson-exceptions/README.md b/jackson-exceptions/README.md new file mode 100644 index 0000000000..6f082aaaa5 --- /dev/null +++ b/jackson-exceptions/README.md @@ -0,0 +1,7 @@ +## Jackson Exceptions + +This module contains articles about Jackson exceptions. + +### Relevant Articles: +- [Jackson Exceptions – Problems and Solutions](https://www.baeldung.com/jackson-exception) +- [Jackson – JsonMappingException (No serializer found for class)](https://www.baeldung.com/jackson-jsonmappingexception) diff --git a/jackson-exceptions/pom.xml b/jackson-exceptions/pom.xml new file mode 100644 index 0000000000..ce580996de --- /dev/null +++ b/jackson-exceptions/pom.xml @@ -0,0 +1,33 @@ + + 4.0.0 + jackson-exceptions + 0.0.1-SNAPSHOT + jackson-exceptions + + + com.baeldung + parent-java + 0.0.1-SNAPSHOT + ../parent-java + + + + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + + + + + jackson-exceptions + + + src/main/resources + true + + + + + diff --git a/jackson-exceptions/src/main/java/com/baeldung/exceptions/User.java b/jackson-exceptions/src/main/java/com/baeldung/exceptions/User.java new file mode 100644 index 0000000000..e0e77c658e --- /dev/null +++ b/jackson-exceptions/src/main/java/com/baeldung/exceptions/User.java @@ -0,0 +1,26 @@ +package com.baeldung.exceptions; + +public class User { + public int id; + public String name; + + public User() { + super(); + } + + public User(final int id, final String name) { + this.id = id; + this.name = name; + } + + // API + + public int getId() { + return id; + } + + public String getName() { + return name; + } + +} \ No newline at end of file diff --git a/jackson/src/test/java/com/baeldung/jackson/exception/UserWithConflict.java b/jackson-exceptions/src/main/java/com/baeldung/exceptions/UserWithConflict.java similarity index 91% rename from jackson/src/test/java/com/baeldung/jackson/exception/UserWithConflict.java rename to jackson-exceptions/src/main/java/com/baeldung/exceptions/UserWithConflict.java index 01ff695475..5adbb7b01e 100644 --- a/jackson/src/test/java/com/baeldung/jackson/exception/UserWithConflict.java +++ b/jackson-exceptions/src/main/java/com/baeldung/exceptions/UserWithConflict.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.exception; +package com.baeldung.exceptions; public class UserWithConflict { public int id; diff --git a/jackson-exceptions/src/main/java/com/baeldung/exceptions/UserWithNoDefaultConstructor.java b/jackson-exceptions/src/main/java/com/baeldung/exceptions/UserWithNoDefaultConstructor.java new file mode 100644 index 0000000000..c75b06ed6a --- /dev/null +++ b/jackson-exceptions/src/main/java/com/baeldung/exceptions/UserWithNoDefaultConstructor.java @@ -0,0 +1,21 @@ +package com.baeldung.exceptions; + +public class UserWithNoDefaultConstructor { + + private int id; + private String name; + + public UserWithNoDefaultConstructor(final int id, final String name) { + this.id = id; + this.name = name; + } + + public int getId() { + return id; + } + + public String getName() { + return name; + } + +} \ No newline at end of file diff --git a/jackson/src/test/java/com/baeldung/jackson/exception/UserWithPrivateFields.java b/jackson-exceptions/src/main/java/com/baeldung/exceptions/UserWithPrivateFields.java similarity index 86% rename from jackson/src/test/java/com/baeldung/jackson/exception/UserWithPrivateFields.java rename to jackson-exceptions/src/main/java/com/baeldung/exceptions/UserWithPrivateFields.java index f627975184..c10020bd85 100644 --- a/jackson/src/test/java/com/baeldung/jackson/exception/UserWithPrivateFields.java +++ b/jackson-exceptions/src/main/java/com/baeldung/exceptions/UserWithPrivateFields.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.exception; +package com.baeldung.exceptions; public class UserWithPrivateFields { int id; diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/exception/UserWithRoot.java b/jackson-exceptions/src/main/java/com/baeldung/exceptions/UserWithRoot.java similarity index 89% rename from jackson-simple/src/test/java/com/baeldung/jackson/exception/UserWithRoot.java rename to jackson-exceptions/src/main/java/com/baeldung/exceptions/UserWithRoot.java index d879c16e6a..cadcd43a72 100644 --- a/jackson-simple/src/test/java/com/baeldung/jackson/exception/UserWithRoot.java +++ b/jackson-exceptions/src/main/java/com/baeldung/exceptions/UserWithRoot.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.exception; +package com.baeldung.exceptions; import com.fasterxml.jackson.annotation.JsonRootName; diff --git a/jackson/src/test/java/com/baeldung/jackson/exception/Zoo.java b/jackson-exceptions/src/main/java/com/baeldung/exceptions/Zoo.java similarity index 85% rename from jackson/src/test/java/com/baeldung/jackson/exception/Zoo.java rename to jackson-exceptions/src/main/java/com/baeldung/exceptions/Zoo.java index 647b5955c0..3e729a1d2f 100644 --- a/jackson/src/test/java/com/baeldung/jackson/exception/Zoo.java +++ b/jackson-exceptions/src/main/java/com/baeldung/exceptions/Zoo.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.exception; +package com.baeldung.exceptions; public class Zoo { public Animal animal; diff --git a/jackson/src/test/java/com/baeldung/jackson/exception/ZooConfigured.java b/jackson-exceptions/src/main/java/com/baeldung/exceptions/ZooConfigured.java similarity index 91% rename from jackson/src/test/java/com/baeldung/jackson/exception/ZooConfigured.java rename to jackson-exceptions/src/main/java/com/baeldung/exceptions/ZooConfigured.java index f51b1e150a..31c2ce4d4c 100644 --- a/jackson/src/test/java/com/baeldung/jackson/exception/ZooConfigured.java +++ b/jackson-exceptions/src/main/java/com/baeldung/exceptions/ZooConfigured.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.exception; +package com.baeldung.exceptions; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/dtos/MyDtoNoAccessors.java b/jackson-exceptions/src/main/java/com/baeldung/mappingexception/MyDtoNoAccessors.java similarity index 91% rename from jackson-simple/src/test/java/com/baeldung/jackson/dtos/MyDtoNoAccessors.java rename to jackson-exceptions/src/main/java/com/baeldung/mappingexception/MyDtoNoAccessors.java index 6e9abc90ae..17bc5257fb 100644 --- a/jackson-simple/src/test/java/com/baeldung/jackson/dtos/MyDtoNoAccessors.java +++ b/jackson-exceptions/src/main/java/com/baeldung/mappingexception/MyDtoNoAccessors.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.dtos; +package com.baeldung.mappingexception; public class MyDtoNoAccessors { diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/dtos/MyDtoNoAccessorsAndFieldVisibility.java b/jackson-exceptions/src/main/java/com/baeldung/mappingexception/MyDtoNoAccessorsAndFieldVisibility.java similarity index 94% rename from jackson-simple/src/test/java/com/baeldung/jackson/dtos/MyDtoNoAccessorsAndFieldVisibility.java rename to jackson-exceptions/src/main/java/com/baeldung/mappingexception/MyDtoNoAccessorsAndFieldVisibility.java index 149969f769..cf5d5ca500 100644 --- a/jackson-simple/src/test/java/com/baeldung/jackson/dtos/MyDtoNoAccessorsAndFieldVisibility.java +++ b/jackson-exceptions/src/main/java/com/baeldung/mappingexception/MyDtoNoAccessorsAndFieldVisibility.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.dtos; +package com.baeldung.mappingexception; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; diff --git a/jackson/src/test/java/com/baeldung/jackson/exception/test/JacksonExceptionsUnitTest.java b/jackson-exceptions/src/test/java/com/baeldung/exceptions/JacksonExceptionsUnitTest.java similarity index 86% rename from jackson/src/test/java/com/baeldung/jackson/exception/test/JacksonExceptionsUnitTest.java rename to jackson-exceptions/src/test/java/com/baeldung/exceptions/JacksonExceptionsUnitTest.java index dd0fe8dc56..38ef3f9390 100644 --- a/jackson/src/test/java/com/baeldung/jackson/exception/test/JacksonExceptionsUnitTest.java +++ b/jackson-exceptions/src/test/java/com/baeldung/exceptions/JacksonExceptionsUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.exception.test; +package com.baeldung.exceptions; import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.assertEquals; @@ -9,11 +9,11 @@ import java.util.List; import org.junit.Test; -import com.baeldung.jackson.exception.User; -import com.baeldung.jackson.exception.UserWithPrivateFields; -import com.baeldung.jackson.exception.UserWithRoot; -import com.baeldung.jackson.exception.Zoo; -import com.baeldung.jackson.exception.ZooConfigured; +import com.baeldung.exceptions.User; +import com.baeldung.exceptions.UserWithPrivateFields; +import com.baeldung.exceptions.UserWithRoot; +import com.baeldung.exceptions.Zoo; +import com.baeldung.exceptions.ZooConfigured; import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.core.JsonFactory; @@ -78,7 +78,7 @@ public class JacksonExceptionsUnitTest { final ObjectMapper mapper = new ObjectMapper(); mapper.reader() - .forType(User.class) + .forType(UserWithNoDefaultConstructor.class) .readValue(json); } @@ -87,8 +87,8 @@ public class JacksonExceptionsUnitTest { final String json = "{\"id\":1,\"name\":\"John\"}"; final ObjectMapper mapper = new ObjectMapper(); - final com.baeldung.jackson.dtos.User user = mapper.reader() - .forType(com.baeldung.jackson.dtos.User.class) + final User user = mapper.reader() + .forType(User.class) .readValue(json); assertEquals("John", user.name); } @@ -102,7 +102,7 @@ public class JacksonExceptionsUnitTest { mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE); mapper.reader() - .forType(com.baeldung.jackson.dtos.User.class) + .forType(User.class) .readValue(json); } @@ -126,7 +126,7 @@ public class JacksonExceptionsUnitTest { final ObjectMapper mapper = new ObjectMapper(); mapper.reader() - .forType(com.baeldung.jackson.dtos.User.class) + .forType(User.class) .readValue(json); } @@ -135,8 +135,8 @@ public class JacksonExceptionsUnitTest { final String json = "[{\"id\":1,\"name\":\"John\"},{\"id\":2,\"name\":\"Adam\"}]"; final ObjectMapper mapper = new ObjectMapper(); - final List users = mapper.reader() - .forType(new TypeReference>() { + final List users = mapper.reader() + .forType(new TypeReference>() { }) .readValue(json); @@ -150,7 +150,7 @@ public class JacksonExceptionsUnitTest { final ObjectMapper mapper = new ObjectMapper(); mapper.reader() - .forType(com.baeldung.jackson.dtos.User.class) + .forType(User.class) .readValue(json); } @@ -161,8 +161,8 @@ public class JacksonExceptionsUnitTest { final ObjectMapper mapper = new ObjectMapper(); mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); - final com.baeldung.jackson.dtos.User user = mapper.reader() - .forType(com.baeldung.jackson.dtos.User.class) + final User user = mapper.reader() + .forType(User.class) .readValue(json); assertEquals("John", user.name); } @@ -174,7 +174,7 @@ public class JacksonExceptionsUnitTest { final ObjectMapper mapper = new ObjectMapper(); mapper.reader() - .forType(com.baeldung.jackson.dtos.User.class) + .forType(User.class) .readValue(json); } @@ -186,8 +186,8 @@ public class JacksonExceptionsUnitTest { factory.enable(JsonParser.Feature.ALLOW_SINGLE_QUOTES); final ObjectMapper mapper = new ObjectMapper(factory); - final com.baeldung.jackson.dtos.User user = mapper.reader() - .forType(com.baeldung.jackson.dtos.User.class) + final User user = mapper.reader() + .forType(User.class) .readValue(json); assertEquals("John", user.name); } diff --git a/jackson-exceptions/src/test/java/com/baeldung/mappingexception/JacksonMappingExceptionUnitTest.java b/jackson-exceptions/src/test/java/com/baeldung/mappingexception/JacksonMappingExceptionUnitTest.java new file mode 100644 index 0000000000..df35626828 --- /dev/null +++ b/jackson-exceptions/src/test/java/com/baeldung/mappingexception/JacksonMappingExceptionUnitTest.java @@ -0,0 +1,51 @@ +package com.baeldung.mappingexception; + +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.notNullValue; +import static org.junit.Assert.assertThat; + +import java.io.IOException; +import java.util.List; +import org.junit.Test; + +import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; +import com.fasterxml.jackson.annotation.PropertyAccessor; +import com.fasterxml.jackson.core.JsonGenerationException; +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.module.SimpleModule; +import com.google.common.collect.Lists; + +public class JacksonMappingExceptionUnitTest { + + @Test(expected = JsonMappingException.class) + public final void givenObjectHasNoAccessors_whenSerializing_thenException() throws JsonParseException, IOException { + final String dtoAsString = new ObjectMapper().writeValueAsString(new MyDtoNoAccessors()); + + assertThat(dtoAsString, notNullValue()); + } + + @Test + public final void givenObjectHasNoAccessors_whenSerializingWithPrivateFieldsVisibility_thenNoException() throws JsonParseException, IOException { + final ObjectMapper objectMapper = new ObjectMapper(); + objectMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY); + final String dtoAsString = objectMapper.writeValueAsString(new MyDtoNoAccessors()); + + assertThat(dtoAsString, containsString("intValue")); + assertThat(dtoAsString, containsString("stringValue")); + assertThat(dtoAsString, containsString("booleanValue")); + } + + @Test + public final void givenObjectHasNoAccessorsButHasVisibleFields_whenSerializing_thenNoException() throws JsonParseException, IOException { + final ObjectMapper objectMapper = new ObjectMapper(); + final String dtoAsString = objectMapper.writeValueAsString(new MyDtoNoAccessorsAndFieldVisibility()); + + assertThat(dtoAsString, containsString("intValue")); + assertThat(dtoAsString, containsString("stringValue")); + assertThat(dtoAsString, containsString("booleanValue")); + } + +} diff --git a/jackson-simple/README.md b/jackson-simple/README.md index 9d24a20e4a..8a37dd073f 100644 --- a/jackson-simple/README.md +++ b/jackson-simple/README.md @@ -7,9 +7,6 @@ This module contains articles about Jackson that are also part of the Jackson Eb The "REST With Spring" Classes: http://bit.ly/restwithspring ### Relevant Articles: -- [Jackson Ignore Properties on Marshalling](https://www.baeldung.com/jackson-ignore-properties-on-serialization) - [Jackson Unmarshalling json with Unknown Properties](https://www.baeldung.com/jackson-deserialize-json-unknown-properties) -- [Jackson Annotation Examples](https://www.baeldung.com/jackson-annotations) - [Intro to the Jackson ObjectMapper](https://www.baeldung.com/jackson-object-mapper-tutorial) - [Ignore Null Fields with Jackson](https://www.baeldung.com/jackson-ignore-null-fields) -- [Jackson – Change Name of Field](https://www.baeldung.com/jackson-name-of-property) diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/dtos/withEnum/DistanceEnumWithValue.java b/jackson-simple/src/test/java/com/baeldung/jackson/dtos/withEnum/DistanceEnumWithValue.java index 69c476d8a5..574908523a 100644 --- a/jackson-simple/src/test/java/com/baeldung/jackson/dtos/withEnum/DistanceEnumWithValue.java +++ b/jackson-simple/src/test/java/com/baeldung/jackson/dtos/withEnum/DistanceEnumWithValue.java @@ -1,4 +1,4 @@ -package com.baeldung.jackson.dtos.withEnum; +package com.baeldung.jackson.annotation.dtos.withEnum; import com.fasterxml.jackson.annotation.JsonValue; diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/test/JacksonSerializationUnitTest.java b/jackson-simple/src/test/java/com/baeldung/jackson/test/JacksonSerializationUnitTest.java index 563dda9eb6..93dd708b18 100644 --- a/jackson-simple/src/test/java/com/baeldung/jackson/test/JacksonSerializationUnitTest.java +++ b/jackson-simple/src/test/java/com/baeldung/jackson/test/JacksonSerializationUnitTest.java @@ -8,13 +8,10 @@ import static org.junit.Assert.assertThat; import java.io.IOException; import java.util.List; -import com.baeldung.jackson.dtos.MyDtoFieldNameChanged; import com.baeldung.jackson.dtos.User; import com.baeldung.jackson.dtos.Item; import com.baeldung.jackson.dtos.ItemWithSerializer; import com.baeldung.jackson.dtos.MyDto; -import com.baeldung.jackson.dtos.MyDtoNoAccessors; -import com.baeldung.jackson.dtos.MyDtoNoAccessorsAndFieldVisibility; import com.baeldung.jackson.serialization.ItemSerializer; import org.junit.Test; @@ -29,61 +26,6 @@ import com.google.common.collect.Lists; public class JacksonSerializationUnitTest { - // tests - single entity to json - - @Test - public final void whenSerializing_thenCorrect() throws JsonParseException, IOException { - final ObjectMapper mapper = new ObjectMapper(); - final String dtoAsString = mapper.writeValueAsString(new MyDto()); - - assertThat(dtoAsString, containsString("intValue")); - assertThat(dtoAsString, containsString("stringValue")); - assertThat(dtoAsString, containsString("booleanValue")); - } - - @Test - public final void givenNameOfFieldIsChangedViaAnnotationOnGetter_whenSerializing_thenCorrect() throws JsonParseException, IOException { - final ObjectMapper mapper = new ObjectMapper(); - final MyDtoFieldNameChanged dtoObject = new MyDtoFieldNameChanged(); - dtoObject.setStringValue("a"); - - final String dtoAsString = mapper.writeValueAsString(dtoObject); - - assertThat(dtoAsString, not(containsString("stringValue"))); - assertThat(dtoAsString, containsString("strVal")); - System.out.println(dtoAsString); - } - - // tests - serialize via accessors/fields - - @Test(expected = JsonMappingException.class) - public final void givenObjectHasNoAccessors_whenSerializing_thenException() throws JsonParseException, IOException { - final String dtoAsString = new ObjectMapper().writeValueAsString(new MyDtoNoAccessors()); - - assertThat(dtoAsString, notNullValue()); - } - - @Test - public final void givenObjectHasNoAccessors_whenSerializingWithPrivateFieldsVisibility_thenNoException() throws JsonParseException, IOException { - final ObjectMapper objectMapper = new ObjectMapper(); - objectMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY); - final String dtoAsString = objectMapper.writeValueAsString(new MyDtoNoAccessors()); - - assertThat(dtoAsString, containsString("intValue")); - assertThat(dtoAsString, containsString("stringValue")); - assertThat(dtoAsString, containsString("booleanValue")); - } - - @Test - public final void givenObjectHasNoAccessorsButHasVisibleFields_whenSerializing_thenNoException() throws JsonParseException, IOException { - final ObjectMapper objectMapper = new ObjectMapper(); - final String dtoAsString = objectMapper.writeValueAsString(new MyDtoNoAccessorsAndFieldVisibility()); - - assertThat(dtoAsString, containsString("intValue")); - assertThat(dtoAsString, containsString("stringValue")); - assertThat(dtoAsString, containsString("booleanValue")); - } - // tests - multiple entities to json @Test diff --git a/jackson/README.md b/jackson/README.md index a62a788c41..f2aed46145 100644 --- a/jackson/README.md +++ b/jackson/README.md @@ -11,21 +11,16 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [Jackson – Unmarshall to Collection/Array](https://www.baeldung.com/jackson-collection-array) - [Jackson – Custom Serializer](https://www.baeldung.com/jackson-custom-serialization) - [Getting Started with Custom Deserialization in Jackson](https://www.baeldung.com/jackson-deserialization) -- [Jackson Exceptions – Problems and Solutions](https://www.baeldung.com/jackson-exception) - [Jackson Date](https://www.baeldung.com/jackson-serialize-dates) -- [Jackson – Bidirectional Relationships](https://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion) - [Jackson JSON Tutorial](https://www.baeldung.com/jackson) - [Jackson – Working with Maps and nulls](https://www.baeldung.com/jackson-map-null-values-or-null-key) - [Jackson – Decide What Fields Get Serialized/Deserialized](https://www.baeldung.com/jackson-field-serializable-deserializable-or-not) - [Jackson vs Gson](https://www.baeldung.com/jackson-vs-gson) - [XML Serialization and Deserialization with Jackson](https://www.baeldung.com/jackson-xml-serialization-and-deserialization) -- [More Jackson Annotations](https://www.baeldung.com/jackson-advanced-annotations) - [Inheritance with Jackson](https://www.baeldung.com/jackson-inheritance) -- [Guide to @JsonFormat in Jackson](https://www.baeldung.com/jackson-jsonformat) - [Using Optional with Jackson](https://www.baeldung.com/jackson-optional) - [Map Serialization and Deserialization with Jackson](https://www.baeldung.com/jackson-map) - [Jackson Streaming API](https://www.baeldung.com/jackson-streaming-api) -- [Jackson – JsonMappingException (No serializer found for class)](https://www.baeldung.com/jackson-jsonmappingexception) - [How To Serialize and Deserialize Enums with Jackson](https://www.baeldung.com/jackson-serialize-enums) - [Jackson – Marshall String to JsonNode](https://www.baeldung.com/jackson-json-to-jsonnode) - [Jackson – Unmarshall to Collection/Array](https://www.baeldung.com/jackson-collection-array) diff --git a/jackson/src/test/java/com/baeldung/jackson/exception/User.java b/jackson/src/test/java/com/baeldung/jackson/exception/User.java deleted file mode 100644 index 1d78e82bec..0000000000 --- a/jackson/src/test/java/com/baeldung/jackson/exception/User.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.baeldung.jackson.exception; - -public class User { - public int id; - public String name; - - public User(final int id, final String name) { - this.id = id; - this.name = name; - } -} diff --git a/pom.xml b/pom.xml index 10a1ce2aeb..781c4ab84a 100644 --- a/pom.xml +++ b/pom.xml @@ -499,6 +499,8 @@ jackson jackson-2 jackson-simple + jackson-annotations + jackson-exceptions java-collections-conversions java-collections-conversions-2 java-collections-maps @@ -1266,6 +1268,8 @@ jackson jackson-2 jackson-simple + jackson-annotations + jackson-exceptions java-collections-conversions java-collections-conversions-2 java-collections-maps