Skip to content

Commit

Permalink
[BAEL-19673] - Move articles out of jackson part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
catalin-burcea committed Nov 22, 2019
1 parent 16de535 commit 5cc2d3d
Show file tree
Hide file tree
Showing 97 changed files with 894 additions and 210 deletions.
11 changes: 11 additions & 0 deletions jackson-annotations/README.md
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)
60 changes: 60 additions & 0 deletions jackson-annotations/pom.xml
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>
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baeldung.jackson.bidirection;
package com.baeldung.jackson.annotation.bidirection;

public class ItemWithIgnore {
public int id;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baeldung.jackson.bidirection;
package com.baeldung.jackson.annotation.bidirection;

import com.fasterxml.jackson.annotation.JsonManagedReference;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baeldung.jackson.bidirection;
package com.baeldung.jackson.annotation.bidirection;

import java.util.ArrayList;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baeldung.jackson.bidirection;
package com.baeldung.jackson.annotation.bidirection;

import java.util.ArrayList;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baeldung.jackson.bidirection;
package com.baeldung.jackson.annotation.bidirection;

import java.util.ArrayList;
import java.util.List;
Expand Down
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.io.IOException;
import java.text.ParseException;
Expand Down
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.io.IOException;
import java.text.SimpleDateFormat;
Expand Down
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;

Expand Down
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;

Expand Down
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));
}

}
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;
}

}
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;
}

}
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;
}

}
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;
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baeldung.jackson.exception;
package com.baeldung.jackson.annotation.exception;

import com.fasterxml.jackson.annotation.JsonRootName;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baeldung.jackson.exception;
package com.baeldung.jackson.annotation.exception;

import com.fasterxml.jackson.annotation.JsonRootName;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baeldung.jackson.jsonview;
package com.baeldung.jackson.annotation.jsonview;

import com.fasterxml.jackson.annotation.JsonView;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baeldung.jackson.jsonview;
package com.baeldung.jackson.annotation.jsonview;

public class Views {
public static class Public {
Expand Down
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();
}

}
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();
}

}
Loading

0 comments on commit 5cc2d3d

Please sign in to comment.