[BAEL-19676] - Move articles out of jackson part 3
This commit is contained in:
parent
9b801298ca
commit
01c2fc6e3a
|
@ -1,16 +0,0 @@
|
|||
*.class
|
||||
|
||||
#folders#
|
||||
/target
|
||||
/neoDb*
|
||||
/data
|
||||
/src/main/webapp/WEB-INF/classes
|
||||
*/META-INF/*
|
||||
|
||||
# Packaged files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
||||
|
||||
# Files
|
||||
/src/main/resources/orderOutput.yaml
|
|
@ -1,12 +0,0 @@
|
|||
## Jackson Cookbooks and Examples
|
||||
|
||||
This module contains articles about Jackson.
|
||||
|
||||
### The Course
|
||||
The "REST With Spring" Classes: http://bit.ly/restwithspring
|
||||
|
||||
### Relevant Articles:
|
||||
- [Working with Tree Model Nodes in Jackson](https://www.baeldung.com/jackson-json-node-tree-model)
|
||||
- [Compare Two JSON Objects with Jackson](https://www.baeldung.com/jackson-compare-two-json-objects)
|
||||
- [Calling Default Serializer from Custom Serializer in Jackson](https://www.baeldung.com/jackson-call-default-serializer-from-custom-serializer)
|
||||
- More articles: [[<-- prev]](/../jackson)
|
|
@ -1,13 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
|
@ -5,7 +5,5 @@ 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)
|
||||
- [Jackson JSON Views](https://www.baeldung.com/jackson-json-view-annotation)
|
||||
|
|
|
@ -1,54 +0,0 @@
|
|||
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 + "]";
|
||||
}
|
||||
|
||||
}
|
|
@ -1,43 +0,0 @@
|
|||
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;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,54 +0,0 @@
|
|||
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 + "]";
|
||||
}
|
||||
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.jackson.test;
|
||||
package com.baeldung.jackson.jsonview;
|
||||
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.not;
|
|
@ -4,8 +4,6 @@ This module contains articles about Jackson conversions.
|
|||
|
||||
### Relevant Articles:
|
||||
- [Mapping a Dynamic JSON Object with Jackson](https://www.baeldung.com/jackson-mapping-dynamic-object)
|
||||
- [Jackson Unmarshalling JSON with Unknown Properties](https://www.baeldung.com/jackson-deserialize-json-unknown-properties)
|
||||
- [Ignore Null Fields with Jackson](https://www.baeldung.com/jackson-ignore-null-fields)
|
||||
- [Mapping Multiple JSON Fields to a Single Java Field](https://www.baeldung.com/json-multiple-fields-single-java-field)
|
||||
- [Convert XML to JSON Using Jackson](https://www.baeldung.com/jackson-convert-xml-json)
|
||||
- [Converting JSON to CSV in Java](https://www.baeldung.com/java-converting-json-to-csv)
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
## Jackson Custom Conversions
|
||||
|
||||
This module contains articles about Jackson custom conversions.
|
||||
|
||||
### Relevant Articles:
|
||||
- [Jackson – Custom Serializer](https://www.baeldung.com/jackson-custom-serialization)
|
||||
- [Getting Started with Custom Deserialization in Jackson](https://www.baeldung.com/jackson-deserialization)
|
||||
- [Serialize Only Fields that meet a Custom Criteria with Jackson](https://www.baeldung.com/jackson-serialize-field-custom-criteria)
|
||||
- [Calling Default Serializer from Custom Serializer in Jackson](https://www.baeldung.com/jackson-call-default-serializer-from-custom-serializer)
|
|
@ -1,9 +1,9 @@
|
|||
<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-2</artifactId>
|
||||
<version>0.1-SNAPSHOT</version>
|
||||
<name>jackson-2</name>
|
||||
<artifactId>jackson-custom-conversions</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>jackson-custom-conversions</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
@ -13,27 +13,25 @@
|
|||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!-- Allow use of LocalDate -->
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.datatype</groupId>
|
||||
<artifactId>jackson-datatype-jsr310</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- test scoped -->
|
||||
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
<groupId>com.fasterxml.jackson.datatype</groupId>
|
||||
<artifactId>jackson-datatype-joda</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>jackson-2</finalName>
|
||||
<finalName>jackson-custom-conversions</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
|
@ -42,10 +40,4 @@
|
|||
</resources>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
|
||||
<!-- testing -->
|
||||
<assertj.version>3.11.0</assertj.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.jackson.entities;
|
||||
package com.baeldung.defaultserializercustomserializer;
|
||||
|
||||
public class File {
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.jackson.entities;
|
||||
package com.baeldung.defaultserializercustomserializer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
|
@ -1,6 +1,5 @@
|
|||
package com.baeldung.jackson.serialization.custom.serializer;
|
||||
package com.baeldung.defaultserializercustomserializer;
|
||||
|
||||
import com.baeldung.jackson.entities.Folder;
|
||||
import com.fasterxml.jackson.databind.BeanDescription;
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
import com.fasterxml.jackson.databind.SerializationConfig;
|
|
@ -1,9 +1,7 @@
|
|||
package com.baeldung.jackson.serialization.custom.serializer;
|
||||
package com.baeldung.defaultserializercustomserializer;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.baeldung.jackson.entities.File;
|
||||
import com.baeldung.jackson.entities.Folder;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
|
|
@ -1,8 +1,7 @@
|
|||
package com.baeldung.jackson.serialization.custom.serializer;
|
||||
package com.baeldung.defaultserializercustomserializer;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.baeldung.jackson.entities.Folder;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
|
|
@ -1,8 +1,7 @@
|
|||
package com.baeldung.jackson.serialization.custom.serializer;
|
||||
package com.baeldung.defaultserializercustomserializer;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.baeldung.jackson.entities.Folder;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
|
@ -1,8 +1,7 @@
|
|||
package com.baeldung.jackson.serialization.custom.serializer;
|
||||
package com.baeldung.defaultserializercustomserializer;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.baeldung.jackson.entities.Folder;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
|
@ -1,8 +1,7 @@
|
|||
package com.baeldung.jackson.serialization.custom.serializer;
|
||||
package com.baeldung.defaultserializercustomserializer;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.baeldung.jackson.entities.Folder;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.jackson.entities;
|
||||
package com.baeldung.deserialization;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.jackson.dtos;
|
||||
package com.baeldung.deserialization;
|
||||
|
||||
public class Item {
|
||||
public int id;
|
|
@ -1,9 +1,7 @@
|
|||
package com.baeldung.jackson.deserialization;
|
||||
package com.baeldung.deserialization;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.baeldung.jackson.dtos.Item;
|
||||
import com.baeldung.jackson.dtos.User;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
|
@ -1,9 +1,7 @@
|
|||
package com.baeldung.jackson.deserialization;
|
||||
package com.baeldung.deserialization;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.baeldung.jackson.dtos.ItemWithSerializer;
|
||||
import com.baeldung.jackson.dtos.User;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
|
@ -11,7 +9,7 @@ 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> {
|
||||
public class ItemDeserializerOnClass extends StdDeserializer<ItemWithDeserializer> {
|
||||
|
||||
private static final long serialVersionUID = 5579141241817332594L;
|
||||
|
||||
|
@ -27,7 +25,7 @@ public class ItemDeserializerOnClass extends StdDeserializer<ItemWithSerializer>
|
|||
* {"id":1,"itemNr":"theItem","owner":2}
|
||||
*/
|
||||
@Override
|
||||
public ItemWithSerializer deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException, JsonProcessingException {
|
||||
public ItemWithDeserializer 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();
|
||||
|
@ -35,7 +33,7 @@ public class ItemDeserializerOnClass extends StdDeserializer<ItemWithSerializer>
|
|||
.asText();
|
||||
final int userId = (Integer) ((IntNode) node.get("owner")).numberValue();
|
||||
|
||||
return new ItemWithSerializer(id, itemName, new User(userId, null));
|
||||
return new ItemWithDeserializer(id, itemName, new User(userId, null));
|
||||
}
|
||||
|
||||
}
|
|
@ -1,19 +1,14 @@
|
|||
package com.baeldung.jackson.dtos;
|
||||
|
||||
import com.baeldung.jackson.deserialization.ItemDeserializerOnClass;
|
||||
import com.baeldung.jackson.serialization.ItemSerializerOnClass;
|
||||
package com.baeldung.deserialization;
|
||||
|
||||
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 class ItemWithDeserializer {
|
||||
public final int id;
|
||||
public final String itemName;
|
||||
public final User owner;
|
||||
|
||||
public ItemWithSerializer(final int id, final String itemName, final User owner) {
|
||||
public ItemWithDeserializer(final int id, final String itemName, final User owner) {
|
||||
this.id = id;
|
||||
this.itemName = itemName;
|
||||
this.owner = owner;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.jackson.entities;
|
||||
package com.baeldung.deserialization;
|
||||
|
||||
import java.util.List;
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
package com.baeldung.deserialization;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.jackson.dtos;
|
||||
package com.baeldung.serialization;
|
||||
|
||||
public class Item {
|
||||
public int id;
|
|
@ -1,8 +1,7 @@
|
|||
package com.baeldung.jackson.serialization;
|
||||
package com.baeldung.serialization;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.baeldung.jackson.dtos.Item;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
|
@ -1,8 +1,7 @@
|
|||
package com.baeldung.jackson.serialization;
|
||||
package com.baeldung.serialization;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.baeldung.jackson.dtos.ItemWithSerializer;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
|
@ -1,13 +1,9 @@
|
|||
package com.baeldung.jackson.dtos;
|
||||
|
||||
import com.baeldung.jackson.deserialization.ItemDeserializerOnClass;
|
||||
import com.baeldung.jackson.serialization.ItemSerializerOnClass;
|
||||
package com.baeldung.serialization;
|
||||
|
||||
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;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.jackson.dtos;
|
||||
package com.baeldung.serialization;
|
||||
|
||||
public class User {
|
||||
public int id;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.jackson.dynamicIgnore;
|
||||
package com.baeldung.skipfields;
|
||||
|
||||
public class Address implements Hidable {
|
||||
private String city;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.jackson.dynamicIgnore;
|
||||
package com.baeldung.skipfields;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.jackson.dynamicIgnore;
|
||||
package com.baeldung.skipfields;
|
||||
|
||||
import java.io.IOException;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.jackson.dtos;
|
||||
package com.baeldung.skipfields;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFilter;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.jackson.dynamicIgnore;
|
||||
package com.baeldung.skipfields;
|
||||
|
||||
public class Person implements Hidable {
|
||||
private String name;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.jackson.serialization.custom.serializer;
|
||||
package com.baeldung.defaultserializercustomserializer;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
@ -14,8 +14,6 @@ import java.util.Map;
|
|||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.jackson.entities.File;
|
||||
import com.baeldung.jackson.entities.Folder;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.module.SimpleModule;
|
|
@ -1,4 +1,16 @@
|
|||
package com.baeldung.jackson.test;
|
||||
package com.baeldung.deserialization;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.CoreMatchers.not;
|
||||
|
@ -7,53 +19,7 @@ import static org.hamcrest.Matchers.notNullValue;
|
|||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.jackson.deserialization.ItemDeserializer;
|
||||
import com.baeldung.jackson.dtos.Item;
|
||||
import com.baeldung.jackson.dtos.ItemWithSerializer;
|
||||
import com.baeldung.jackson.dtos.MyDto;
|
||||
import com.fasterxml.jackson.core.JsonFactory;
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
|
||||
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||
|
||||
public class JacksonDeserializationUnitTest {
|
||||
|
||||
@Test
|
||||
public final void whenDeserializingAJsonToAClass_thenCorrect() throws JsonParseException, JsonMappingException, IOException {
|
||||
final String jsonAsString = "{\"stringValue\":\"a\",\"intValue\":1,\"booleanValue\":true}";
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
final MyDto readValue = mapper.readValue(jsonAsString, MyDto.class);
|
||||
|
||||
assertNotNull(readValue);
|
||||
assertThat(readValue.getStringValue(), equalTo("a"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenNotAllFieldsHaveValuesInJson_whenDeserializingAJsonToAClass_thenCorrect() throws JsonParseException, JsonMappingException, IOException {
|
||||
final String jsonAsString = "{\"stringValue\":\"a\",\"booleanValue\":true}";
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
final MyDto readValue = mapper.readValue(jsonAsString, MyDto.class);
|
||||
|
||||
assertNotNull(readValue);
|
||||
assertThat(readValue.getStringValue(), equalTo("a"));
|
||||
assertThat(readValue.isBooleanValue(), equalTo(true));
|
||||
}
|
||||
|
||||
// custom deserialization
|
||||
public class CustomDeserializationUnitTest {
|
||||
|
||||
@Test
|
||||
public final void whenDeserializingTheStandardRepresentation_thenCorrect() throws JsonParseException, JsonMappingException, IOException {
|
||||
|
@ -80,7 +46,7 @@ public class JacksonDeserializationUnitTest {
|
|||
public final void givenDeserializerIsOnClass_whenDeserializingCustomRepresentation_thenCorrect() throws JsonParseException, JsonMappingException, IOException {
|
||||
final String json = "{\"id\":1,\"itemName\":\"theItem\",\"owner\":2}";
|
||||
|
||||
final ItemWithSerializer readValue = new ObjectMapper().readValue(json, ItemWithSerializer.class);
|
||||
final ItemWithDeserializer readValue = new ObjectMapper().readValue(json, ItemWithDeserializer.class);
|
||||
assertThat(readValue, notNullValue());
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.jackson.test;
|
||||
package com.baeldung.serialization;
|
||||
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
|
@ -7,12 +7,6 @@ import static org.junit.Assert.assertThat;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
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.serialization.ItemSerializer;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
|
||||
|
@ -24,21 +18,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
public class JacksonSerializationUnitTest {
|
||||
|
||||
// tests - multiple entities to json
|
||||
|
||||
@Test
|
||||
public final void whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException {
|
||||
final List<MyDto> listOfDtos = Lists.newArrayList(new MyDto("a", 1, true), new MyDto("bc", 3, false));
|
||||
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
final String dtosAsString = mapper.writeValueAsString(listOfDtos);
|
||||
|
||||
System.out.println(dtosAsString);
|
||||
}
|
||||
|
||||
// tests - custom serializer
|
||||
public class CustomSerializationUnitTest {
|
||||
|
||||
@Test
|
||||
public final void whenSerializing_thenNoExceptions() throws JsonGenerationException, JsonMappingException, IOException {
|
|
@ -0,0 +1,89 @@
|
|||
package com.baeldung.skipfields;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
|
||||
import com.fasterxml.jackson.databind.ser.FilterProvider;
|
||||
import com.fasterxml.jackson.databind.ser.PropertyFilter;
|
||||
import com.fasterxml.jackson.databind.ser.PropertyWriter;
|
||||
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
|
||||
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
public class IgnoreFieldsWithFilterUnitTest {
|
||||
|
||||
@Test
|
||||
public final void givenTypeHasFilterThatIgnoresFieldByName_whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException {
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
final SimpleBeanPropertyFilter theFilter = SimpleBeanPropertyFilter.serializeAllExcept("intValue");
|
||||
final FilterProvider filters = new SimpleFilterProvider().addFilter("myFilter", theFilter);
|
||||
|
||||
final MyDtoWithFilter dtoObject = new MyDtoWithFilter();
|
||||
dtoObject.setIntValue(12);
|
||||
|
||||
final String dtoAsString = mapper.writer(filters)
|
||||
.writeValueAsString(dtoObject);
|
||||
|
||||
assertThat(dtoAsString, not(containsString("intValue")));
|
||||
assertThat(dtoAsString, containsString("booleanValue"));
|
||||
assertThat(dtoAsString, containsString("stringValue"));
|
||||
System.out.println(dtoAsString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenTypeHasFilterThatIgnoresNegativeInt_whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException {
|
||||
final PropertyFilter theFilter = new SimpleBeanPropertyFilter() {
|
||||
@Override
|
||||
public final void serializeAsField(final Object pojo, final JsonGenerator jgen, final SerializerProvider provider, final PropertyWriter writer) throws Exception {
|
||||
if (include(writer)) {
|
||||
if (!writer.getName()
|
||||
.equals("intValue")) {
|
||||
writer.serializeAsField(pojo, jgen, provider);
|
||||
return;
|
||||
}
|
||||
|
||||
final int intValue = ((MyDtoWithFilter) pojo).getIntValue();
|
||||
if (intValue >= 0) {
|
||||
writer.serializeAsField(pojo, jgen, provider);
|
||||
}
|
||||
} else if (!jgen.canOmitFields()) { // since 2.3
|
||||
writer.serializeAsOmittedField(pojo, jgen, provider);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final boolean include(final BeanPropertyWriter writer) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final boolean include(final PropertyWriter writer) {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
final FilterProvider filters = new SimpleFilterProvider().addFilter("myFilter", theFilter);
|
||||
|
||||
final MyDtoWithFilter dtoObject = new MyDtoWithFilter();
|
||||
dtoObject.setIntValue(-1);
|
||||
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
final String dtoAsString = mapper.writer(filters)
|
||||
.writeValueAsString(dtoObject);
|
||||
|
||||
assertThat(dtoAsString, not(containsString("intValue")));
|
||||
assertThat(dtoAsString, containsString("booleanValue"));
|
||||
assertThat(dtoAsString, containsString("stringValue"));
|
||||
System.out.println(dtoAsString);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,14 +1,10 @@
|
|||
package com.baeldung.jackson.test;
|
||||
package com.baeldung.skipfields;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import com.baeldung.jackson.dynamicIgnore.Address;
|
||||
import com.baeldung.jackson.dynamicIgnore.HidableSerializer;
|
||||
import com.baeldung.jackson.dynamicIgnore.Person;
|
||||
import com.baeldung.jackson.dynamicIgnore.Hidable;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
|
@ -6,4 +6,14 @@ 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:
|
||||
### Relevant Articles:
|
||||
- [Jackson Annotation Examples](https://www.baeldung.com/jackson-annotations)
|
||||
- [Intro to the Jackson ObjectMapper](https://www.baeldung.com/jackson-object-mapper-tutorial)
|
||||
- [Jackson Ignore Properties on Marshalling](https://www.baeldung.com/jackson-ignore-properties-on-serialization)
|
||||
- [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)
|
||||
- [Jackson Unmarshalling JSON with Unknown Properties](https://www.baeldung.com/jackson-deserialize-json-unknown-properties)
|
||||
|
||||
### NOTE:
|
||||
|
||||
Since this is a module tied to an e-book, it should **not** be used to store the code for any further article.
|
||||
|
|
|
@ -21,7 +21,6 @@
|
|||
</dependency>
|
||||
|
||||
<!-- test scoped -->
|
||||
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.jackson.ignore.dtos;
|
||||
package com.baeldung.jackson.annotation.ignore;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreType;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.jackson.dtos;
|
||||
package com.baeldung.jackson.ignore;
|
||||
|
||||
public class MyDto {
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.jackson.ignore.dtos;
|
||||
package com.baeldung.jackson.ignore;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.jackson.ignore.dtos;
|
||||
package com.baeldung.jackson.ignore;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.jackson.ignore.dtos;
|
||||
package com.baeldung.jackson.ignore;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.jackson.dtos;
|
||||
package com.baeldung.jackson.ignore;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.jackson.ignore.dtos;
|
||||
package com.baeldung.jackson.ignore;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFilter;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.jackson.dtos;
|
||||
package com.baeldung.jackson.ignore;
|
||||
|
||||
public class MyDtoWithSpecialField {
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.jackson.dtos;
|
||||
package com.baeldung.jackson.ignore;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreType;
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue