[BAEL-12185] - Removed copied classes from other repository - part 2
This commit is contained in:
parent
3d5e52cde0
commit
92e54a9231
|
@ -1,54 +0,0 @@
|
|||
package com.baeldung.jackson.inclusion.jsonautodetect;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
||||
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Source code github.com/readlearncode
|
||||
*
|
||||
* @author Alex Theedom www.readlearncode.com
|
||||
* @version 1.0
|
||||
*/
|
||||
@JsonAutoDetect(fieldVisibility = Visibility.ANY)
|
||||
public class Order {
|
||||
|
||||
private UUID id;
|
||||
private Type type;
|
||||
private int internalAudit;
|
||||
|
||||
public static class Type {
|
||||
public long id;
|
||||
public String name;
|
||||
}
|
||||
|
||||
public Order() {
|
||||
this.id = UUID.randomUUID();
|
||||
}
|
||||
|
||||
public Order(Type type) {
|
||||
this();
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public Order(int internalAudit) {
|
||||
this();
|
||||
this.type = new Type();
|
||||
this.type.id = 20;
|
||||
this.type.name = "Order";
|
||||
this.internalAudit = internalAudit;
|
||||
}
|
||||
|
||||
public UUID getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public Type getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(Type type) {
|
||||
this.type = type;
|
||||
}
|
||||
}
|
|
@ -1,44 +0,0 @@
|
|||
package com.baeldung.jackson.inclusion.jsonignoretype;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreType;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Source code github.com/readlearncode
|
||||
*
|
||||
* @author Alex Theedom www.readlearncode.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class Order {
|
||||
|
||||
private UUID id;
|
||||
private Type type;
|
||||
|
||||
@JsonIgnoreType
|
||||
public static class Type {
|
||||
public long id;
|
||||
public String name;
|
||||
}
|
||||
|
||||
public Order() {
|
||||
this.id = UUID.randomUUID();
|
||||
}
|
||||
|
||||
public Order(Type type) {
|
||||
this();
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public UUID getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public Type getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(Type type) {
|
||||
this.type = type;
|
||||
}
|
||||
}
|
|
@ -1,62 +0,0 @@
|
|||
package com.baeldung.jackson.polymorphism;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonSubTypes;
|
||||
import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
||||
import com.fasterxml.jackson.annotation.JsonTypeName;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Source code github.com/readlearncode
|
||||
*
|
||||
* @author Alex Theedom www.readlearncode.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class Order {
|
||||
|
||||
private UUID id;
|
||||
private Type type;
|
||||
private int internalAudit;
|
||||
|
||||
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "ordertype")
|
||||
@JsonSubTypes({ @JsonSubTypes.Type(value = InternalType.class, name = "internal") })
|
||||
public static class Type {
|
||||
public long id;
|
||||
public String name;
|
||||
}
|
||||
|
||||
@JsonTypeName("internal")
|
||||
public static class InternalType extends Type {
|
||||
public long id;
|
||||
public String name;
|
||||
}
|
||||
|
||||
public Order() {
|
||||
this.id = UUID.randomUUID();
|
||||
}
|
||||
|
||||
public Order(Type type) {
|
||||
this();
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public Order(int internalAudit) {
|
||||
this();
|
||||
this.type = new Type();
|
||||
this.type.id = 20;
|
||||
this.type.name = "Order";
|
||||
this.internalAudit = internalAudit;
|
||||
}
|
||||
|
||||
public UUID getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public Type getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(Type type) {
|
||||
this.type = type;
|
||||
}
|
||||
}
|
|
@ -1,50 +0,0 @@
|
|||
package com.baeldung.jackson.deserialization.jsonanysetter;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static io.restassured.path.json.JsonPath.from;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Source code github.com/readlearncode
|
||||
*
|
||||
* @author Alex Theedom www.readlearncode.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class JsonAnySetterUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenDeserializingUsingJsonAnySetter_thenCorrect() throws IOException {
|
||||
|
||||
// arrange
|
||||
String json = "{\"USA\":10.00,\"UK\":15.00,\"China\":23.00,\"Brazil\":12.00,\"France\":8.00,\"Russia\":18.00}";
|
||||
|
||||
// act
|
||||
Inventory inventory = new ObjectMapper().readerFor(Inventory.class)
|
||||
.readValue(json);
|
||||
|
||||
// assert
|
||||
assertThat(from(json).getMap(".")
|
||||
.get("USA")).isEqualTo(inventory.getCountryDeliveryCost()
|
||||
.get("USA"));
|
||||
assertThat(from(json).getMap(".")
|
||||
.get("UK")).isEqualTo(inventory.getCountryDeliveryCost()
|
||||
.get("UK"));
|
||||
assertThat(from(json).getMap(".")
|
||||
.get("China")).isEqualTo(inventory.getCountryDeliveryCost()
|
||||
.get("China"));
|
||||
assertThat(from(json).getMap(".")
|
||||
.get("Brazil")).isEqualTo(inventory.getCountryDeliveryCost()
|
||||
.get("Brazil"));
|
||||
assertThat(from(json).getMap(".")
|
||||
.get("France")).isEqualTo(inventory.getCountryDeliveryCost()
|
||||
.get("France"));
|
||||
assertThat(from(json).getMap(".")
|
||||
.get("Russia")).isEqualTo(inventory.getCountryDeliveryCost()
|
||||
.get("Russia"));
|
||||
|
||||
}
|
||||
}
|
|
@ -1,35 +0,0 @@
|
|||
package com.baeldung.jackson.deserialization.jsondeserialize;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
import static io.restassured.path.json.JsonPath.from;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Source code github.com/readlearncode
|
||||
*
|
||||
* @author Alex Theedom www.readlearncode.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class JsonDeserializeUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenDeserializingUsingJsonDeserialize_thenCorrect() throws IOException {
|
||||
|
||||
// arrange
|
||||
String bookJson = "{\"id\":\"957c43f2-fa2e-42f9-bf75-6e3d5bb6960a\",\"title\":\"Effective Java\",\"price\":0,\"published\":\"25-12-2017 13:30:25\",\"pages\":null,\"isbn\":null}";
|
||||
|
||||
// act
|
||||
Book book = new ObjectMapper().readerFor(Book.class)
|
||||
.readValue(bookJson);
|
||||
|
||||
// assert
|
||||
SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
|
||||
assertThat(from(bookJson).getString("published")).isEqualTo(df.format(book.getPublished()));
|
||||
|
||||
}
|
||||
}
|
|
@ -1,31 +0,0 @@
|
|||
package com.baeldung.jackson.general.jsonidentityinfo;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
|
||||
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Source code github.com/readlearncode
|
||||
*
|
||||
* @author Alex Theedom www.readlearncode.com
|
||||
* @version 1.0
|
||||
*/
|
||||
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
|
||||
public class Author extends Person {
|
||||
|
||||
private List<Item> items = new ArrayList<>();
|
||||
|
||||
public Author(String firstName, String lastName) {
|
||||
super(firstName, lastName);
|
||||
}
|
||||
|
||||
public List<Item> getItems() {
|
||||
return items;
|
||||
}
|
||||
|
||||
public void setItems(List<Item> items) {
|
||||
this.items = items;
|
||||
}
|
||||
}
|
|
@ -1,73 +0,0 @@
|
|||
package com.baeldung.jackson.general.jsonidentityinfo;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Source code github.com/readlearncode
|
||||
*
|
||||
* @author Alex Theedom www.readlearncode.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class Course extends Item {
|
||||
|
||||
public enum Medium {
|
||||
CLASSROOM, ONLINE
|
||||
}
|
||||
|
||||
public enum Level {
|
||||
BEGINNER("Beginner", 1), INTERMEDIATE("Intermediate", 2), ADVANCED("Advanced", 3);
|
||||
|
||||
private String name;
|
||||
private int number;
|
||||
|
||||
Level(String name, int number) {
|
||||
this.name = name;
|
||||
this.number = number;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
|
||||
private float duration;
|
||||
private Medium medium;
|
||||
private Level level;
|
||||
private List<Course> prerequisite;
|
||||
|
||||
public Course(String title, Author author) {
|
||||
super(title, author);
|
||||
}
|
||||
|
||||
public float getDuration() {
|
||||
return duration;
|
||||
}
|
||||
|
||||
public void setDuration(float duration) {
|
||||
this.duration = duration;
|
||||
}
|
||||
|
||||
public Medium getMedium() {
|
||||
return medium;
|
||||
}
|
||||
|
||||
public void setMedium(Medium medium) {
|
||||
this.medium = medium;
|
||||
}
|
||||
|
||||
public Level getLevel() {
|
||||
return level;
|
||||
}
|
||||
|
||||
public void setLevel(Level level) {
|
||||
this.level = level;
|
||||
}
|
||||
|
||||
public List<Course> getPrerequisite() {
|
||||
return prerequisite;
|
||||
}
|
||||
|
||||
public void setPrerequisite(List<Course> prerequisite) {
|
||||
this.prerequisite = prerequisite;
|
||||
}
|
||||
}
|
|
@ -1,64 +0,0 @@
|
|||
package com.baeldung.jackson.general.jsonidentityinfo;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
|
||||
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Source code github.com/readlearncode
|
||||
*
|
||||
* @author Alex Theedom www.readlearncode.com
|
||||
* @version 1.0
|
||||
*/
|
||||
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
|
||||
public class Item {
|
||||
|
||||
private UUID id;
|
||||
private String title;
|
||||
private List<Person> authors = new ArrayList<>();
|
||||
private float price;
|
||||
|
||||
public Item() {
|
||||
}
|
||||
|
||||
public Item(String title, Author author) {
|
||||
this.id = UUID.randomUUID();
|
||||
this.title = title;
|
||||
this.authors.add(author);
|
||||
}
|
||||
|
||||
public UUID getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(UUID id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public List<Person> getAuthors() {
|
||||
return authors;
|
||||
}
|
||||
|
||||
public void setAuthors(List<Person> authors) {
|
||||
this.authors = authors;
|
||||
}
|
||||
|
||||
public float getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(float price) {
|
||||
this.price = price;
|
||||
}
|
||||
}
|
|
@ -1,60 +0,0 @@
|
|||
package com.baeldung.jackson.general.jsonidentityinfo;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import static io.restassured.path.json.JsonPath.from;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Source code github.com/readlearncode
|
||||
*
|
||||
* @author Alex Theedom www.readlearncode.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class JsonIdentityInfoUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenSerializingUsingJsonIdentityInfo_thenCorrect() throws JsonProcessingException {
|
||||
|
||||
// arrange
|
||||
Author author = new Author("Alex", "Theedom");
|
||||
|
||||
Course course = new Course("Java EE Introduction", author);
|
||||
author.setItems(Collections.singletonList(course));
|
||||
course.setAuthors(Collections.singletonList(author));
|
||||
|
||||
// act
|
||||
String result = new ObjectMapper().writeValueAsString(author);
|
||||
|
||||
// assert
|
||||
assertThat(from(result).getString("items[0].authors")).isNotNull();
|
||||
|
||||
/*
|
||||
Authors are included.
|
||||
{
|
||||
"id": "1b408bf9-5946-4a14-a112-fde2953a7fe7",
|
||||
"firstName": "Alex",
|
||||
"lastName": "Theedom",
|
||||
"items": [
|
||||
{
|
||||
"id": "5ed30530-f0a5-42eb-b786-be2c655da968",
|
||||
"title": "Java EE Introduction",
|
||||
"authors": [
|
||||
"1b408bf9-5946-4a14-a112-fde2953a7fe7"
|
||||
],
|
||||
"price": 0,
|
||||
"duration": 0,
|
||||
"medium": null,
|
||||
"level": null,
|
||||
"prerequisite": null
|
||||
}
|
||||
]
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
}
|
|
@ -1,45 +0,0 @@
|
|||
package com.baeldung.jackson.general.jsonidentityinfo;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Source code github.com/readlearncode
|
||||
*
|
||||
* @author Alex Theedom www.readlearncode.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class Person {
|
||||
|
||||
private UUID id;
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
|
||||
public Person() {
|
||||
}
|
||||
|
||||
public Person(String firstName, String lastName) {
|
||||
this.id = UUID.randomUUID();
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public UUID getId() {
|
||||
return id;
|
||||
}
|
||||
}
|
|
@ -1,43 +0,0 @@
|
|||
package com.baeldung.jackson.general.jsonunwrapped;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.Test;
|
||||
|
||||
import static io.restassured.path.json.JsonPath.from;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Source code github.com/readlearncode
|
||||
*
|
||||
* @author Alex Theedom www.readlearncode.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class JsonUnwrappedUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenSerializingUsingJsonUnwrapped_thenCorrect() throws JsonProcessingException {
|
||||
|
||||
// arrange
|
||||
Order.Type preorderType = new Order.Type();
|
||||
preorderType.id = 10;
|
||||
preorderType.name = "pre-order";
|
||||
|
||||
Order order = new Order(preorderType);
|
||||
|
||||
// act
|
||||
String result = new ObjectMapper().writeValueAsString(order);
|
||||
|
||||
// assert
|
||||
assertThat(from(result).getInt("id")).isEqualTo(10);
|
||||
assertThat(from(result).getString("name")).isEqualTo("pre-order");
|
||||
|
||||
/*
|
||||
{
|
||||
"id": 10,
|
||||
"name": "pre-order"
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
}
|
|
@ -1,54 +0,0 @@
|
|||
package com.baeldung.jackson.general.jsonunwrapped;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonUnwrapped;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Source code github.com/readlearncode
|
||||
*
|
||||
* @author Alex Theedom www.readlearncode.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class Order {
|
||||
|
||||
private UUID id;
|
||||
|
||||
@JsonUnwrapped
|
||||
private Type type;
|
||||
private int internalAudit;
|
||||
|
||||
public static class Type {
|
||||
public long id;
|
||||
public String name;
|
||||
}
|
||||
|
||||
public Order() {
|
||||
this.id = UUID.randomUUID();
|
||||
}
|
||||
|
||||
public Order(Type type) {
|
||||
this();
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public Order(int internalAudit) {
|
||||
this();
|
||||
this.type = new Type();
|
||||
this.type.id = 20;
|
||||
this.type.name = "Order";
|
||||
this.internalAudit = internalAudit;
|
||||
}
|
||||
|
||||
public UUID getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public Type getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(Type type) {
|
||||
this.type = type;
|
||||
}
|
||||
}
|
|
@ -1,73 +0,0 @@
|
|||
package com.baeldung.jackson.general.jsonview;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.Test;
|
||||
|
||||
import static io.restassured.path.json.JsonPath.from;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Source code github.com/readlearncode
|
||||
*
|
||||
* @author Alex Theedom www.readlearncode.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class JsonViewUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenSerializingUsingJsonView_andInternalView_thenCorrect() throws JsonProcessingException {
|
||||
|
||||
// arrange
|
||||
Order order = new Order(120);
|
||||
|
||||
// act
|
||||
String result = new ObjectMapper().writerWithView(Views.Internal.class)
|
||||
.writeValueAsString(order);
|
||||
|
||||
// assert
|
||||
assertThat(from(result).getUUID("id")).isNotNull();
|
||||
assertThat(from(result).getObject("type", Order.Type.class)).isNotNull();
|
||||
assertThat(from(result).getInt("internalAudit")).isEqualTo(120);
|
||||
|
||||
/*
|
||||
{
|
||||
"id": "33806388-795b-4812-b90a-60292111bc5c",
|
||||
"type": {
|
||||
"id": 20,
|
||||
"name": "Order"
|
||||
},
|
||||
"internalAudit": 120
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSerializingUsingJsonView_andPublicView_thenCorrect() throws JsonProcessingException {
|
||||
|
||||
// arrange
|
||||
Order order = new Order(120);
|
||||
|
||||
// act
|
||||
String result = new ObjectMapper().writerWithView(Views.Public.class)
|
||||
.writeValueAsString(order);
|
||||
|
||||
// assert
|
||||
assertThat(from(result).getUUID("id")).isNotNull();
|
||||
assertThat(from(result).getObject("type", Order.Type.class)).isNotNull();
|
||||
assertThat(result).doesNotContain("internalAudit");
|
||||
|
||||
/*
|
||||
{
|
||||
"id": "5184d5fc-e359-4cdf-93fa-4054025bef4e",
|
||||
"type": {
|
||||
"id": 20,
|
||||
"name": "Order"
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -1,57 +0,0 @@
|
|||
package com.baeldung.jackson.general.jsonview;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonView;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Source code github.com/readlearncode
|
||||
*
|
||||
* @author Alex Theedom www.readlearncode.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class Order {
|
||||
|
||||
@JsonView(Views.Public.class)
|
||||
private UUID id;
|
||||
|
||||
@JsonView(Views.Public.class)
|
||||
private Type type;
|
||||
|
||||
@JsonView(Views.Internal.class)
|
||||
private int internalAudit;
|
||||
|
||||
public static class Type {
|
||||
public long id;
|
||||
public String name;
|
||||
}
|
||||
|
||||
public Order() {
|
||||
this.id = UUID.randomUUID();
|
||||
}
|
||||
|
||||
public Order(Type type) {
|
||||
this();
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public Order(int internalAudit) {
|
||||
this();
|
||||
this.type = new Type();
|
||||
this.type.id = 20;
|
||||
this.type.name = "Order";
|
||||
this.internalAudit = internalAudit;
|
||||
}
|
||||
|
||||
public UUID getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public Type getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(Type type) {
|
||||
this.type = type;
|
||||
}
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
package com.baeldung.jackson.general.jsonview;
|
||||
|
||||
/**
|
||||
* Source code github.com/readlearncode
|
||||
*
|
||||
* @author Alex Theedom www.readlearncode.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class Views {
|
||||
public static class Public {
|
||||
}
|
||||
|
||||
public static class Internal extends Public {
|
||||
}
|
||||
}
|
|
@ -1,30 +0,0 @@
|
|||
package com.baeldung.jackson.general.reference;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonManagedReference;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Source code github.com/readlearncode
|
||||
*
|
||||
* @author Alex Theedom www.readlearncode.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class Author extends Person {
|
||||
|
||||
private List<Item> items = new ArrayList<>();
|
||||
|
||||
public Author(String firstName, String lastName) {
|
||||
super(firstName, lastName);
|
||||
}
|
||||
|
||||
@JsonManagedReference
|
||||
public List<Item> getItems() {
|
||||
return items;
|
||||
}
|
||||
|
||||
public void setItems(List<Item> items) {
|
||||
this.items = items;
|
||||
}
|
||||
}
|
|
@ -1,73 +0,0 @@
|
|||
package com.baeldung.jackson.general.reference;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Source code github.com/readlearncode
|
||||
*
|
||||
* @author Alex Theedom www.readlearncode.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class Course extends Item {
|
||||
|
||||
public enum Medium {
|
||||
CLASSROOM, ONLINE
|
||||
}
|
||||
|
||||
public enum Level {
|
||||
BEGINNER("Beginner", 1), INTERMEDIATE("Intermediate", 2), ADVANCED("Advanced", 3);
|
||||
|
||||
private String name;
|
||||
private int number;
|
||||
|
||||
Level(String name, int number) {
|
||||
this.name = name;
|
||||
this.number = number;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
|
||||
private float duration;
|
||||
private Medium medium;
|
||||
private Level level;
|
||||
private List<Course> prerequisite;
|
||||
|
||||
public Course(String title, Author author) {
|
||||
super(title, author);
|
||||
}
|
||||
|
||||
public float getDuration() {
|
||||
return duration;
|
||||
}
|
||||
|
||||
public void setDuration(float duration) {
|
||||
this.duration = duration;
|
||||
}
|
||||
|
||||
public Medium getMedium() {
|
||||
return medium;
|
||||
}
|
||||
|
||||
public void setMedium(Medium medium) {
|
||||
this.medium = medium;
|
||||
}
|
||||
|
||||
public Level getLevel() {
|
||||
return level;
|
||||
}
|
||||
|
||||
public void setLevel(Level level) {
|
||||
this.level = level;
|
||||
}
|
||||
|
||||
public List<Course> getPrerequisite() {
|
||||
return prerequisite;
|
||||
}
|
||||
|
||||
public void setPrerequisite(List<Course> prerequisite) {
|
||||
this.prerequisite = prerequisite;
|
||||
}
|
||||
}
|
|
@ -1,64 +0,0 @@
|
|||
package com.baeldung.jackson.general.reference;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonBackReference;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Source code github.com/readlearncode
|
||||
*
|
||||
* @author Alex Theedom www.readlearncode.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class Item {
|
||||
|
||||
private UUID id;
|
||||
private String title;
|
||||
|
||||
@JsonBackReference
|
||||
private List<Person> authors = new ArrayList<>();
|
||||
private float price;
|
||||
|
||||
public Item() {
|
||||
}
|
||||
|
||||
public Item(String title, Author author) {
|
||||
this.id = UUID.randomUUID();
|
||||
this.title = title;
|
||||
this.authors.add(author);
|
||||
}
|
||||
|
||||
public UUID getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(UUID id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public List<Person> getAuthors() {
|
||||
return authors;
|
||||
}
|
||||
|
||||
public void setAuthors(List<Person> authors) {
|
||||
this.authors = authors;
|
||||
}
|
||||
|
||||
public float getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(float price) {
|
||||
this.price = price;
|
||||
}
|
||||
}
|
|
@ -1,45 +0,0 @@
|
|||
package com.baeldung.jackson.general.reference;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Source code github.com/readlearncode
|
||||
*
|
||||
* @author Alex Theedom www.readlearncode.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class Person {
|
||||
|
||||
private UUID id;
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
|
||||
public Person() {
|
||||
}
|
||||
|
||||
public Person(String firstName, String lastName) {
|
||||
this.id = UUID.randomUUID();
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public UUID getId() {
|
||||
return id;
|
||||
}
|
||||
}
|
|
@ -1,59 +0,0 @@
|
|||
package com.baeldung.jackson.general.reference;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import static io.restassured.path.json.JsonPath.from;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Source code github.com/readlearncode
|
||||
*
|
||||
* @author Alex Theedom www.readlearncode.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class ReferenceUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenSerializingUsingReference_thenCorrect() throws JsonProcessingException {
|
||||
|
||||
// arrange
|
||||
Author author = new Author("Alex", "Theedom");
|
||||
|
||||
Course course = new Course("Java EE Introduction", author);
|
||||
author.setItems(Collections.singletonList(course));
|
||||
course.setAuthors(Collections.singletonList(author));
|
||||
|
||||
// act
|
||||
String result = new ObjectMapper().writeValueAsString(author);
|
||||
|
||||
// assert
|
||||
assertThat(from(result).getString("items[0].authors")).isNull();
|
||||
|
||||
/*
|
||||
Without references defined it throws StackOverflowError.
|
||||
Authors excluded.
|
||||
|
||||
{
|
||||
"id": "9c45d9b3-4888-4c24-8b74-65ef35627cd7",
|
||||
"firstName": "Alex",
|
||||
"lastName": "Theedom",
|
||||
"items": [
|
||||
{
|
||||
"id": "f8309629-d178-4d67-93a4-b513ec4a7f47",
|
||||
"title": "Java EE Introduction",
|
||||
"price": 0,
|
||||
"duration": 0,
|
||||
"medium": null,
|
||||
"level": null,
|
||||
"prerequisite": null
|
||||
}
|
||||
]
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
}
|
|
@ -1,52 +0,0 @@
|
|||
package com.baeldung.jackson.inclusion.jsonautodetect;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.Test;
|
||||
|
||||
import static io.restassured.path.json.JsonPath.from;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Source code github.com/readlearncode
|
||||
*
|
||||
* @author Alex Theedom www.readlearncode.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class JsonAutoDetectUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenSerializingUsingJsonAutoDetect_thenCorrect() throws JsonProcessingException {
|
||||
|
||||
// arrange
|
||||
Order order = new Order(1234567890);
|
||||
|
||||
// act
|
||||
String result = new ObjectMapper().writeValueAsString(order);
|
||||
|
||||
// assert
|
||||
assertThat(from(result).getInt("internalAudit")).isEqualTo(1234567890);
|
||||
|
||||
/*
|
||||
With @JsonAutoDetect
|
||||
{
|
||||
"id": "c94774d9-de8f-4244-85d5-624bd3a4567a",
|
||||
"type": {
|
||||
"id": 20,
|
||||
"name": "Order"
|
||||
},
|
||||
"internalAudit": 1234567890
|
||||
}
|
||||
|
||||
Without @JsonAutoDetect
|
||||
{
|
||||
"id": "c94774d9-de8f-4244-85d5-624bd3a4567a",
|
||||
"type": {
|
||||
"id": 20,
|
||||
"name": "Order"
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
}
|
|
@ -1,40 +0,0 @@
|
|||
package com.baeldung.jackson.inclusion.jsonignoretype;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.Test;
|
||||
|
||||
import static io.restassured.path.json.JsonPath.from;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Source code github.com/readlearncode
|
||||
*
|
||||
* @author Alex Theedom www.readlearncode.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class JsonIgnoreTypeUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenSerializingUsingJsonIgnoreType_thenCorrect() throws JsonProcessingException {
|
||||
|
||||
// arrange
|
||||
Order.Type type = new Order.Type();
|
||||
type.id = 10;
|
||||
type.name = "Pre-order";
|
||||
|
||||
Order order = new Order(type);
|
||||
|
||||
// act
|
||||
String result = new ObjectMapper().writeValueAsString(order);
|
||||
|
||||
// assert
|
||||
assertThat(from(result).getString("id")).isNotNull();
|
||||
assertThat(from(result).getString("type")).isNull();
|
||||
|
||||
/*
|
||||
{"id":"ac2428da-523e-443c-a18a-4ea4d2791fea"}
|
||||
*/
|
||||
|
||||
}
|
||||
}
|
|
@ -1,66 +0,0 @@
|
|||
package com.baeldung.jackson.polymorphism;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static io.restassured.path.json.JsonPath.from;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Source code github.com/readlearncode
|
||||
*
|
||||
* @author Alex Theedom www.readlearncode.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class PolymorphismUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenSerializingUsingPolymorphism_thenCorrect() throws JsonProcessingException {
|
||||
|
||||
// arrange
|
||||
Order.InternalType internalType = new Order.InternalType();
|
||||
internalType.id = 250;
|
||||
internalType.name = "staff";
|
||||
|
||||
Order order = new Order(internalType);
|
||||
|
||||
// act
|
||||
String result = new ObjectMapper().writeValueAsString(order);
|
||||
|
||||
// assert
|
||||
assertThat(from(result).getString("type.ordertype")).isEqualTo("internal");
|
||||
|
||||
/*
|
||||
{
|
||||
"id": "7fc898e3-b4e7-41b0-8ffa-664cf3663f2e",
|
||||
"type": {
|
||||
"ordertype": "internal",
|
||||
"id": 250,
|
||||
"name": "staff"
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDeserializingPolymorphic_thenCorrect() throws IOException {
|
||||
|
||||
// arrange
|
||||
String orderJson = "{\"type\":{\"ordertype\":\"internal\",\"id\":100,\"name\":\"directors\"}}";
|
||||
|
||||
// act
|
||||
Order order = new ObjectMapper().readerFor(Order.class)
|
||||
.readValue(orderJson);
|
||||
|
||||
// assert
|
||||
assertThat(from(orderJson).getString("type.ordertype")).isEqualTo("internal");
|
||||
assertThat(((Order.InternalType) order.getType()).name).isEqualTo("directors");
|
||||
assertThat(((Order.InternalType) order.getType()).id).isEqualTo(100);
|
||||
assertThat(order.getType()
|
||||
.getClass()).isEqualTo(Order.InternalType.class);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue