forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BAEL-19673] - Move articles out of jackson part 1
- Loading branch information
1 parent
16de535
commit 5cc2d3d
Showing
97 changed files
with
894 additions
and
210 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<artifactId>jackson-annotations</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>jackson-annotations</name> | ||
|
||
<parent> | ||
<groupId>com.baeldung</groupId> | ||
<artifactId>parent-java</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<relativePath>../parent-java</relativePath> | ||
</parent> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.fasterxml.jackson.core</groupId> | ||
<artifactId>jackson-databind</artifactId> | ||
<version>${jackson.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.fasterxml.jackson.dataformat</groupId> | ||
<artifactId>jackson-dataformat-xml</artifactId> | ||
<version>${jackson.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.fasterxml.jackson.module</groupId> | ||
<artifactId>jackson-module-jsonSchema</artifactId> | ||
<version>${jackson.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.rest-assured</groupId> | ||
<artifactId>json-path</artifactId> | ||
<version>${rest-assured.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.assertj</groupId> | ||
<artifactId>assertj-core</artifactId> | ||
<version>${assertj.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<finalName>jackson-annotations</finalName> | ||
<resources> | ||
<resource> | ||
<directory>src/main/resources</directory> | ||
<filtering>true</filtering> | ||
</resource> | ||
</resources> | ||
</build> | ||
|
||
<properties> | ||
<assertj.version>3.11.0</assertj.version> | ||
<rest-assured.version>3.1.1</rest-assured.version> | ||
</properties> | ||
|
||
</project> |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...jackson/bidirection/ItemWithIdentity.java → ...otation/bidirection/ItemWithIdentity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...g/jackson/bidirection/ItemWithIgnore.java → ...nnotation/bidirection/ItemWithIgnore.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...dung/jackson/bidirection/ItemWithRef.java → ...n/annotation/bidirection/ItemWithRef.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...jackson/bidirection/UserWithIdentity.java → ...otation/bidirection/UserWithIdentity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...g/jackson/bidirection/UserWithIgnore.java → ...nnotation/bidirection/UserWithIgnore.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...dung/jackson/bidirection/UserWithRef.java → ...n/annotation/bidirection/UserWithRef.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.../jackson/date/CustomDateDeserializer.java → ...notation/date/CustomDateDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ng/jackson/date/CustomDateSerializer.java → ...annotation/date/CustomDateSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...aeldung/jackson/date/EventWithFormat.java → ...kson/annotation/date/EventWithFormat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package com.baeldung.jackson.date; | ||
package com.baeldung.jackson.annotation.date; | ||
|
||
import java.util.Date; | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
...ung/jackson/date/EventWithSerializer.java → .../annotation/date/EventWithSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package com.baeldung.jackson.date; | ||
package com.baeldung.jackson.annotation.date; | ||
|
||
import java.util.Date; | ||
|
||
|
41 changes: 41 additions & 0 deletions
41
...rc/main/java/com/baeldung/jackson/annotation/deserialization/ItemDeserializerOnClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<ItemWithSerializer> { | ||
|
||
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)); | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
jackson-annotations/src/main/java/com/baeldung/jackson/annotation/dtos/Item.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
...on-annotations/src/main/java/com/baeldung/jackson/annotation/dtos/ItemWithSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
jackson-annotations/src/main/java/com/baeldung/jackson/annotation/dtos/User.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
...ns/src/main/java/com/baeldung/jackson/annotation/dtos/withEnum/DistanceEnumWithValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
...ldung/jackson/exception/UserWithRoot.java → ...on/annotation/exception/UserWithRoot.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...kson/exception/UserWithRootNamespace.java → ...tion/exception/UserWithRootNamespace.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...a/com/baeldung/jackson/jsonview/Item.java → ...ung/jackson/annotation/jsonview/Item.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.../com/baeldung/jackson/jsonview/Views.java → ...ng/jackson/annotation/jsonview/Views.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...notations/src/main/java/com/baeldung/jackson/annotation/serialization/ItemSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Item> { | ||
|
||
private static final long serialVersionUID = 6739170890621978901L; | ||
|
||
public ItemSerializer() { | ||
this(null); | ||
} | ||
|
||
public ItemSerializer(final Class<Item> 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(); | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
...ns/src/main/java/com/baeldung/jackson/annotation/serialization/ItemSerializerOnClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<ItemWithSerializer> { | ||
|
||
private static final long serialVersionUID = -1760959597313610409L; | ||
|
||
public ItemSerializerOnClass() { | ||
this(null); | ||
} | ||
|
||
public ItemSerializerOnClass(final Class<ItemWithSerializer> 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(); | ||
} | ||
|
||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.