Add video tutorial module and Jackson annotation code
This commit is contained in:
+45
@@ -0,0 +1,45 @@
|
||||
package com.baeldung.jacksonannotation.deserialization.jacksoninject;
|
||||
|
||||
import com.fasterxml.jackson.databind.InjectableValues;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Source code github.com/eugenp/tutorials
|
||||
*
|
||||
* @author Alex Theedom www.baeldung.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class JacksonInjectTest {
|
||||
|
||||
@Test
|
||||
public void whenDeserializingUsingJacksonInject_thenCorrect() throws IOException {
|
||||
|
||||
UUID id = UUID.fromString("9616dc8c-bad3-11e6-a4a6-cec0c932ce01");
|
||||
|
||||
// arrange
|
||||
String authorJson = "{\"firstName\": \"Alex\", \"lastName\": \"Theedom\"}";
|
||||
|
||||
// act
|
||||
InjectableValues inject = new InjectableValues.Std().addValue(UUID.class, id);
|
||||
Author author = new ObjectMapper().reader(inject).forType(Author.class).readValue(authorJson);
|
||||
|
||||
// assert
|
||||
assertThat(author.getId()).isEqualTo(id);
|
||||
|
||||
|
||||
/*
|
||||
{
|
||||
"firstName": "Alex",
|
||||
"lastName": "Theedom",
|
||||
"publications": []
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package com.baeldung.jacksonannotation.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/eugenp/tutorials
|
||||
*
|
||||
* @author Alex Theedom www.baeldung.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class JsonAnySetterTest {
|
||||
|
||||
@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"));
|
||||
|
||||
}
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
package com.baeldung.jacksonannotation.deserialization.jsoncreator;
|
||||
|
||||
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/eugenp/tutorials
|
||||
*
|
||||
* @author Alex Theedom www.baeldung.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class JsonCreatorTest {
|
||||
|
||||
@Test
|
||||
public void whenDeserializingUsingJsonCreator_thenCorrect() throws IOException {
|
||||
|
||||
// arrange
|
||||
String authorJson =
|
||||
"{" +
|
||||
" \"christianName\": \"Alex\"," +
|
||||
" \"surname\": \"Theedom\"" +
|
||||
"}";
|
||||
|
||||
// act
|
||||
final Author author = new ObjectMapper().readerFor(Author.class).readValue(authorJson);
|
||||
|
||||
// assert
|
||||
assertThat(from(authorJson).getString("christianName")).isEqualTo(author.getFirstName());
|
||||
assertThat(from(authorJson).getString("surname")).isEqualTo(author.getLastName());
|
||||
|
||||
/*
|
||||
{
|
||||
"christianName": "Alex",
|
||||
"surname": "Theedom"
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.jacksonannotation.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/eugenp/tutorials
|
||||
*
|
||||
* @author Alex Theedom www.baeldung.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class JsonDeserializeTest {
|
||||
|
||||
@Test
|
||||
public void whenDeserializingUsingJsonDeserialize_thenCorrect() throws IOException {
|
||||
|
||||
// arrange
|
||||
String bookJson = "{\"id\":\"957c43f2-fa2e-42f9-bf75-6e3d5bb6960a\",\"title\":\"Effective Java\",\"authors\":[{\"id\":\"9bcd817d-0141-42e6-8f04-e5aaab0980b6\",\"firstName\":\"Joshua\",\"lastName\":\"Bloch\"}],\"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()));
|
||||
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.jacksonannotation.deserialization.jsonsetter;
|
||||
|
||||
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/eugenp/tutorials
|
||||
*
|
||||
* @author Alex Theedom www.baeldung.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class JsonSetterTest {
|
||||
|
||||
@Test
|
||||
public void whenDeserializingUsingJsonSetter_thenCorrect() throws IOException {
|
||||
|
||||
// arrange
|
||||
String json = "{\"firstName\":\"Alex\",\"lastName\":\"Theedom\",\"publications\":[{\"title\":\"Professional Java EE Design Patterns\"}]}";
|
||||
|
||||
// act
|
||||
Author author = new ObjectMapper().readerFor(Author.class).readValue(json);
|
||||
|
||||
// assert
|
||||
assertThat(from(json).getList("publications").size()).isEqualTo(author.getItems().size());
|
||||
|
||||
}
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package com.baeldung.jacksonannotation.general.jsonfilter;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.ser.FilterProvider;
|
||||
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
|
||||
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
|
||||
import org.junit.Test;
|
||||
|
||||
import static io.restassured.path.json.JsonPath.from;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Source code github.com/eugenp/tutorials
|
||||
*
|
||||
* @author Alex Theedom www.baeldung.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class JsonFilterTest {
|
||||
|
||||
@Test
|
||||
public void whenSerializingUsingJsonFilter_thenCorrect() throws JsonProcessingException {
|
||||
|
||||
// arrange
|
||||
Author author = new Author("Alex", "Theedom");
|
||||
FilterProvider filters = new SimpleFilterProvider()
|
||||
.addFilter("authorFilter", SimpleBeanPropertyFilter.filterOutAllExcept("lastName"));
|
||||
|
||||
// act
|
||||
String result = new ObjectMapper().writer(filters).writeValueAsString(author);
|
||||
|
||||
// assert
|
||||
assertThat(from(result).getList("items")).isNull();
|
||||
|
||||
/*
|
||||
{
|
||||
"lastName": "Theedom"
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
}
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
package com.baeldung.jacksonannotation.general.jsonformat;
|
||||
|
||||
import com.baeldung.jacksonannotation.domain.Author;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import static io.restassured.path.json.JsonPath.from;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Source code github.com/eugenp/tutorials
|
||||
*
|
||||
* @author Alex Theedom www.baeldung.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class JsonFormatTest {
|
||||
|
||||
@Test
|
||||
public void whenSerializingUsingJsonFormat_thenCorrect() throws JsonProcessingException, ParseException {
|
||||
|
||||
// arrange
|
||||
SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
|
||||
df.setTimeZone(TimeZone.getTimeZone("UTC"));
|
||||
|
||||
String toParse = "20-12-2014 14:30:00";
|
||||
Date date = df.parse(toParse);
|
||||
|
||||
Book book = new Book(
|
||||
"Design Patterns: Elements of Reusable Object-oriented Software",
|
||||
new Author("The", "GoF")
|
||||
);
|
||||
book.setPublished(date);
|
||||
|
||||
// act
|
||||
String result = new ObjectMapper().writeValueAsString(book);
|
||||
|
||||
// assert
|
||||
assertThat(from(result).getString("published")).isEqualTo(toParse);
|
||||
|
||||
/*
|
||||
{
|
||||
"id": "762b39be-fd5b-489e-8688-aeb3b9bbf019",
|
||||
"title": "Design Patterns: Elements of Reusable Object-oriented Software",
|
||||
"authors": [
|
||||
{
|
||||
"id": "6941b780-0f54-4259-adcb-85523c8f25f4",
|
||||
"firstName": "The",
|
||||
"lastName": "GoF",
|
||||
"items": []
|
||||
}
|
||||
],
|
||||
"price": 0,
|
||||
"published": "20-12-2014 02:30:00",
|
||||
"pages": null,
|
||||
"isbn": null
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
}
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
package com.baeldung.jacksonannotation.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/eugenp/tutorials
|
||||
*
|
||||
* @author Alex Theedom www.baeldung.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class JsonIdentityInfoTest {
|
||||
|
||||
@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
|
||||
}
|
||||
]
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
}
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
package com.baeldung.jacksonannotation.general.jsonproperty;
|
||||
|
||||
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/eugenp/tutorials
|
||||
*
|
||||
* @author Alex Theedom www.baeldung.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class JsonPropertyTest {
|
||||
|
||||
@Test
|
||||
public void whenSerializingUsingJsonProperty_thenCorrect() throws JsonProcessingException {
|
||||
|
||||
// arrange
|
||||
Book book = new Book(
|
||||
"Design Patterns: Elements of Reusable Object-oriented Software",
|
||||
new Author("The", "GoF")
|
||||
);
|
||||
book.configureBinding("Hardback");
|
||||
|
||||
// act
|
||||
String result = new ObjectMapper().writeValueAsString(book);
|
||||
|
||||
// assert
|
||||
assertThat(from(result).getString("binding")).isEqualTo("Hardback");
|
||||
|
||||
/*
|
||||
{
|
||||
"id": "cd941587-d1ae-4c2a-9a36-29533bf50411",
|
||||
"title": "Design Patterns: Elements of Reusable Object-oriented Software",
|
||||
"authors": [
|
||||
{
|
||||
"id": "c8e26318-2f5b-4fa2-9fdc-6e99be021fca",
|
||||
"firstName": "The",
|
||||
"lastName": "GoF",
|
||||
"items": []
|
||||
}
|
||||
],
|
||||
"price": 0,
|
||||
"published": null,
|
||||
"pages": null,
|
||||
"isbn": null,
|
||||
"binding": "Hardback"
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDeserializingUsingJsonProperty_thenCorrect() throws IOException {
|
||||
|
||||
// arrange
|
||||
String result = "{\"id\":\"cd941587-d1ae-4c2a-9a36-29533bf50411\",\"title\":\"Design Patterns: Elements of Reusable Object-oriented Software\",\"authors\":[{\"id\":\"c8e26318-2f5b-4fa2-9fdc-6e99be021fca\",\"firstName\":\"The\",\"lastName\":\"GoF\"}],\"binding\":\"Hardback\"}";
|
||||
|
||||
// act
|
||||
Book book = new ObjectMapper().readerFor(Book.class).readValue(result);
|
||||
|
||||
// assert
|
||||
assertThat(book.coverBinding()).isEqualTo("Hardback");
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
package com.baeldung.jacksonannotation.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/eugenp/tutorials
|
||||
*
|
||||
* @author Alex Theedom www.baeldung.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class JsonUnwrappedTest {
|
||||
|
||||
@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"
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
}
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
package com.baeldung.jacksonannotation.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/eugenp/tutorials
|
||||
*
|
||||
* @author Alex Theedom www.baeldung.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class JsonViewTest {
|
||||
|
||||
@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"
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
package com.baeldung.jacksonannotation.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/eugenp/tutorials
|
||||
*
|
||||
* @author Alex Theedom www.baeldung.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class ReferenceTest {
|
||||
|
||||
@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
|
||||
}
|
||||
]
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
package com.baeldung.jacksonannotation.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/eugenp/tutorials
|
||||
*
|
||||
* @author Alex Theedom www.baeldung.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class JsonAutoDetectTest {
|
||||
|
||||
@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"
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.jacksonannotation.inclusion.jsonignore;
|
||||
|
||||
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/eugenp/tutorials
|
||||
*
|
||||
* @author Alex Theedom www.baeldung.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class JsonIgnoreTest {
|
||||
|
||||
@Test
|
||||
public void whenSerializingUsingJsonIgnore_thenCorrect() throws JsonProcessingException {
|
||||
|
||||
// arrange
|
||||
Author author = new Author("Alex", "Theedom");
|
||||
|
||||
// act
|
||||
String result = new ObjectMapper().writeValueAsString(author);
|
||||
|
||||
// assert
|
||||
assertThat(from(result).getString("firstName")).isEqualTo("Alex");
|
||||
assertThat(from(result).getString("lastName")).isEqualTo("Theedom");
|
||||
assertThat(from(result).getString("id")).isNull();
|
||||
|
||||
/*
|
||||
{
|
||||
"firstName": "Alex",
|
||||
"lastName": "Theedom",
|
||||
"items": []
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
package com.baeldung.jacksonannotation.inclusion.jsonignoreproperties;
|
||||
|
||||
import com.baeldung.jacksonannotation.domain.Author;
|
||||
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/eugenp/tutorials
|
||||
*
|
||||
* @author Alex Theedom www.baeldung.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class JsonIgnorePropertiesTest {
|
||||
|
||||
@Test
|
||||
public void whenSerializingUsingJsonIgnoreProperties_thenCorrect() throws JsonProcessingException {
|
||||
|
||||
// arrange
|
||||
Course course = new Course("Spring Security", new Author("Eugen", "Paraschiv"));
|
||||
course.setMedium(Course.Medium.ONLINE);
|
||||
|
||||
// act
|
||||
String result = new ObjectMapper().writeValueAsString(course);
|
||||
|
||||
// assert
|
||||
assertThat(from(result).getString("medium")).isNull();
|
||||
|
||||
/*
|
||||
{
|
||||
"id": "ef0c8d2b-b088-409e-905c-95ac88dc0ed0",
|
||||
"title": "Spring Security",
|
||||
"authors": [
|
||||
{
|
||||
"id": "47a4f498-b0f3-4daf-909f-d2c35a0fe3c2",
|
||||
"firstName": "Eugen",
|
||||
"lastName": "Paraschiv",
|
||||
"items": []
|
||||
}
|
||||
],
|
||||
"price": 0,
|
||||
"duration": 0,
|
||||
"level": null,
|
||||
"prerequisite": null
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package com.baeldung.jacksonannotation.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/eugenp/tutorials
|
||||
*
|
||||
* @author Alex Theedom www.baeldung.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class JsonIgnoreTypeTest {
|
||||
|
||||
@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"}
|
||||
*/
|
||||
|
||||
}
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.jacksonannotation.inclusion.jsoninclude;
|
||||
|
||||
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/eugenp/tutorials
|
||||
*
|
||||
* @author Alex Theedom www.baeldung.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class JsonIncludeTest {
|
||||
|
||||
@Test
|
||||
public void whenSerializingUsingJsonInclude_thenCorrect() throws JsonProcessingException {
|
||||
|
||||
// arrange
|
||||
Author author = new Author("Alex", null);
|
||||
|
||||
// act
|
||||
String result = new ObjectMapper().writeValueAsString(author);
|
||||
|
||||
// assert
|
||||
assertThat(from(result).getString("firstName")).isEqualTo("Alex");
|
||||
assertThat(result).doesNotContain("lastName");
|
||||
|
||||
|
||||
/*
|
||||
{
|
||||
"id": "e8bb4802-6e0c-4fa5-9f68-c233272399cd",
|
||||
"firstName": "Alex",
|
||||
"items": []
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
package com.baeldung.jacksonannotation.miscellaneous.custom;
|
||||
|
||||
import com.baeldung.jacksonannotation.domain.Author;
|
||||
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/eugenp/tutorials
|
||||
*
|
||||
* @author Alex Theedom www.baeldung.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class CustomTest {
|
||||
|
||||
@Test
|
||||
public void whenSerializingUsingCustom_thenCorrect() throws JsonProcessingException {
|
||||
|
||||
// arrange
|
||||
Course course = new Course("Spring Security", new Author("Eugen", "Paraschiv"));
|
||||
course.setMedium(Course.Medium.ONLINE);
|
||||
|
||||
// act
|
||||
String result = new ObjectMapper().writeValueAsString(course);
|
||||
|
||||
// assert
|
||||
assertThat(from(result).getString("title")).isEqualTo("Spring Security");
|
||||
|
||||
/*
|
||||
{
|
||||
"title": "Spring Security",
|
||||
"price": 0,
|
||||
"id": "7dfd4db9-1175-432f-a53b-687423f7bb9b",
|
||||
"duration": 0,
|
||||
"authors": [
|
||||
{
|
||||
"id": "da0738f6-033c-4974-8d87-92820e5ccf27",
|
||||
"firstName": "Eugen",
|
||||
"lastName": "Paraschiv",
|
||||
"items": []
|
||||
}
|
||||
],
|
||||
"medium": "ONLINE"
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
}
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
package com.baeldung.jacksonannotation.miscellaneous.disable;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.MapperFeature;
|
||||
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/eugenp/tutorials
|
||||
*
|
||||
* @author Alex Theedom www.baeldung.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class DisableTest {
|
||||
|
||||
@Test
|
||||
public void whenSerializingUsingDisable_thenCorrect() throws JsonProcessingException {
|
||||
|
||||
// arrange
|
||||
Author author = new Author("Alex", "Theedom");
|
||||
|
||||
// act
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
String result = mapper.writeValueAsString(author);
|
||||
|
||||
// assert
|
||||
assertThat(from(result).getList("items")).isNull();
|
||||
|
||||
/*
|
||||
{
|
||||
"lastName": "Theedom",
|
||||
"firstName": "Alex",
|
||||
"id": "de4afbb4-b24d-45c8-bb00-fd6b9acb42f1"
|
||||
}
|
||||
*/
|
||||
|
||||
// act
|
||||
mapper = new ObjectMapper();
|
||||
mapper.disable(MapperFeature.USE_ANNOTATIONS);
|
||||
result = mapper.writeValueAsString(author);
|
||||
|
||||
// assert
|
||||
assertThat(from(result).getList("items")).isNotNull();
|
||||
|
||||
/*
|
||||
{
|
||||
"id": "81e6ed72-6b27-4fe9-a36f-e3171c5b55ef",
|
||||
"firstName": "Alex",
|
||||
"lastName": "Theedom",
|
||||
"items": []
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
}
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
package com.baeldung.jacksonannotation.miscellaneous.mixin;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static io.restassured.path.json.JsonPath.from;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Source code github.com/eugenp/tutorials
|
||||
*
|
||||
* @author Alex Theedom www.baeldung.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class MixInTest {
|
||||
|
||||
@Test
|
||||
public void whenSerializingUsingMixIn_thenCorrect() throws JsonProcessingException {
|
||||
|
||||
// arrange
|
||||
Author author = new Author("Alex", "Theedom");
|
||||
|
||||
// act
|
||||
String result = new ObjectMapper().writeValueAsString(author);
|
||||
|
||||
// assert
|
||||
assertThat(from(result).getList("items")).isNotNull();
|
||||
|
||||
/*
|
||||
{
|
||||
"id": "f848b076-00a4-444a-a50b-328595dd9bf5",
|
||||
"firstName": "Alex",
|
||||
"lastName": "Theedom",
|
||||
"items": []
|
||||
}
|
||||
*/
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.addMixIn(List.class, IgnoreListMixIn.class);
|
||||
|
||||
result = mapper.writeValueAsString(author);
|
||||
|
||||
// assert
|
||||
assertThat(from(result).getList("items")).isNull();
|
||||
|
||||
/*
|
||||
{
|
||||
"id": "9ffefb7d-e56f-447c-9009-e92e142f8347",
|
||||
"firstName": "Alex",
|
||||
"lastName": "Theedom"
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
}
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
package com.baeldung.jacksonannotation.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/eugenp/tutorials
|
||||
*
|
||||
* @author Alex Theedom www.baeldung.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class PolymorphismTest {
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
package com.baeldung.jacksonannotation.serialization.jsonanygetter;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static io.restassured.path.json.JsonPath.from;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Source code github.com/eugenp/tutorials
|
||||
*
|
||||
* @author Alex Theedom www.baeldung.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class JsonAnyGetterTest {
|
||||
|
||||
@Test
|
||||
public void whenSerializingUsingJsonAnyGetter_thenCorrect() throws JsonProcessingException {
|
||||
|
||||
// arrange
|
||||
Inventory inventory = new Inventory();
|
||||
Map<String, Float> countryDeliveryCost = inventory.getCountryDeliveryCost();
|
||||
inventory.setLocation("France");
|
||||
|
||||
countryDeliveryCost.put("USA", 10.00f);
|
||||
countryDeliveryCost.put("UK", 15.00f);
|
||||
|
||||
// act
|
||||
String result = new ObjectMapper().writeValueAsString(inventory);
|
||||
|
||||
// assert
|
||||
assertThat(from(result).getString("location")).isEqualTo("France");
|
||||
assertThat(from(result).getFloat("USA")).isEqualTo(10.00f);
|
||||
assertThat(from(result).getFloat("UK")).isEqualTo(15.00f);
|
||||
|
||||
/*
|
||||
{
|
||||
"location": "France",
|
||||
"USA": 10,
|
||||
"UK": 15
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package com.baeldung.jacksonannotation.serialization.jsongetter;
|
||||
|
||||
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/eugenp/tutorials
|
||||
*
|
||||
* @author Alex Theedom www.baeldung.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class JsonGetterTest {
|
||||
|
||||
@Test
|
||||
public void whenSerializingUsingJsonGetter_thenCorrect() throws JsonProcessingException {
|
||||
|
||||
// arrange
|
||||
Author author = new Author("Alex", "Theedom");
|
||||
|
||||
// act
|
||||
String result = new ObjectMapper().writeValueAsString(author);
|
||||
|
||||
// assert
|
||||
assertThat(from(result).getList("publications")).isNotNull();
|
||||
assertThat(from(result).getList("items")).isNull();
|
||||
|
||||
/*
|
||||
{
|
||||
"firstName": "Alex",
|
||||
"lastName": "Theedom",
|
||||
"publications": []
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.jacksonannotation.serialization.jsonpropertyorder;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.Test;
|
||||
|
||||
import static io.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchemaInClasspath;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
|
||||
/**
|
||||
* Source code github.com/eugenp/tutorials
|
||||
*
|
||||
* @author Alex Theedom www.baeldung.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class JsonPropertyOrderTest {
|
||||
|
||||
@Test
|
||||
public void whenSerializingUsingJsonPropertyOrder_thenCorrect() throws JsonProcessingException {
|
||||
|
||||
// arrange
|
||||
Author author = new Author("Alex", "Theedom");
|
||||
author.setzIndex("z123");
|
||||
author.setAlphaIndex("z123");
|
||||
|
||||
// act
|
||||
String result = new ObjectMapper().writeValueAsString(author);
|
||||
|
||||
// assert
|
||||
assertThat(result, matchesJsonSchemaInClasspath("author-jsonpropertyorder-schema.json"));
|
||||
|
||||
// NOTE: property order is not enforced by the JSON specification.
|
||||
|
||||
/*
|
||||
{
|
||||
"items": [],
|
||||
"firstName": "Alex",
|
||||
"lastName": "Theedom",
|
||||
"id": "31ca2af9-df0a-4d49-a74c-86c0a3f944a2",
|
||||
"alphaIndex": "z123",
|
||||
"zIndex": "z123"
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.jacksonannotation.serialization.jsonrawvalue;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Source code github.com/eugenp/tutorials
|
||||
*
|
||||
* @author Alex Theedom www.baeldung.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class JsonRawValueTest {
|
||||
|
||||
@Test
|
||||
public void whenSerializingUsingJsonRawValue_thenCorrect() throws JsonProcessingException {
|
||||
|
||||
// arrange
|
||||
String customerConfig = "{\"colour\":\"red\",\"device\":\"mobile\",\"orientation\":\"landscape\"}";
|
||||
Customer customer = new Customer("Alex", "Theedom");
|
||||
customer.setConfiguration("{\"colour\":\"red\",\"device\":\"mobile\",\"orientation\":\"landscape\"}");
|
||||
|
||||
// act
|
||||
String result = new ObjectMapper().writeValueAsString(customer);
|
||||
|
||||
// assert
|
||||
assertThat(result.contains(customerConfig));
|
||||
|
||||
|
||||
/*
|
||||
{
|
||||
"id": "7674fbec-527f-4008-a619-f9967cd0cbe0",
|
||||
"firstName": "Alex",
|
||||
"lastName": "Theedom",
|
||||
"configuration": {
|
||||
"colour": "red",
|
||||
"device": "mobile",
|
||||
"orientation": "landscape"
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.jacksonannotation.serialization.jsonrootname;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import org.junit.Test;
|
||||
|
||||
import static io.restassured.path.json.JsonPath.from;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Source code github.com/eugenp/tutorials
|
||||
*
|
||||
* @author Alex Theedom www.baeldung.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class JsonRootNameTest {
|
||||
|
||||
@Test
|
||||
public void whenSerializingUsingJsonRootName_thenCorrect() throws JsonProcessingException {
|
||||
|
||||
// arrange
|
||||
Author author = new Author("Alex", "Theedom");
|
||||
|
||||
// act
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
|
||||
String result = mapper.writeValueAsString(author);
|
||||
|
||||
// assert
|
||||
assertThat(from(result).getString("writer.firstName")).isEqualTo("Alex");
|
||||
assertThat(from(result).getString("author.firstName")).isNull();
|
||||
|
||||
/*
|
||||
{
|
||||
"writer": {
|
||||
"id": "0f50dca6-3dd7-4801-a334-fd1614276389",
|
||||
"firstName": "Alex",
|
||||
"lastName": "Theedom",
|
||||
"items": []
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
}
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
package com.baeldung.jacksonannotation.serialization.jsonserialize;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
import static io.restassured.path.json.JsonPath.from;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Source code github.com/eugenp/tutorials
|
||||
*
|
||||
* @author Alex Theedom www.baeldung.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class JsonSerializeTest {
|
||||
|
||||
@Test
|
||||
public void whenSerializingUsingJsonSerialize_thenCorrect() throws JsonProcessingException, ParseException {
|
||||
|
||||
// arrange
|
||||
Author joshuaBloch = new Author("Joshua", "Bloch");
|
||||
Book book = new Book("Effective Java", joshuaBloch);
|
||||
|
||||
SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
|
||||
String toParse = "25-12-2017 13:30:25";
|
||||
book.setPublished(df.parse(toParse));
|
||||
|
||||
// act
|
||||
String result = new ObjectMapper().writeValueAsString(book);
|
||||
|
||||
// assert
|
||||
assertThat(from(result).getString("published")).isEqualTo(toParse);
|
||||
|
||||
/*
|
||||
{
|
||||
"id": "957c43f2-fa2e-42f9-bf75-6e3d5bb6960a",
|
||||
"title": "Effective Java",
|
||||
"authors": [
|
||||
{
|
||||
"id": "9bcd817d-0141-42e6-8f04-e5aaab0980b6",
|
||||
"firstName": "Joshua",
|
||||
"lastName": "Bloch",
|
||||
"items": []
|
||||
}
|
||||
],
|
||||
"price": 0,
|
||||
"published": "25-12-2017 13:30:25",
|
||||
"pages": null,
|
||||
"isbn": null
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
}
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
package com.baeldung.jacksonannotation.serialization.jsonvalue;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Source code github.com/eugenp/tutorials
|
||||
*
|
||||
* @author Alex Theedom www.baeldung.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class JsonValueTest {
|
||||
|
||||
@Test
|
||||
public void whenSerializingUsingJsonValue_thenCorrect() throws JsonProcessingException {
|
||||
|
||||
// act
|
||||
String result = new ObjectMapper().writeValueAsString(Course.Level.ADVANCED);
|
||||
|
||||
// assert
|
||||
assertThat(result).isEqualTo("\"Advanced\"");
|
||||
|
||||
/*
|
||||
|
||||
"Advanced"
|
||||
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSerializingAuthorUsingJsonValue_thenFirstNameAndLastNameAreConcatenated() throws JsonProcessingException {
|
||||
|
||||
// arrange
|
||||
Author me = new Author("Alex", "Theedom");
|
||||
|
||||
// act
|
||||
String result = new ObjectMapper().writeValueAsString(me);
|
||||
|
||||
// assert
|
||||
assertThat(result).contains("Alex Theedom");
|
||||
|
||||
/*
|
||||
{
|
||||
"name": "Alex Theedom"
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
}
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
{
|
||||
"$schema": "http://json-schema.org/draft-04/schema#",
|
||||
"title": "Author",
|
||||
"description": "An author",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"items": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "object"
|
||||
}
|
||||
},
|
||||
"firstName": {
|
||||
"type": "string"
|
||||
},
|
||||
"lastName": {
|
||||
"type": "string"
|
||||
},
|
||||
"id": {
|
||||
"type": "string"
|
||||
},
|
||||
"zIndex": {
|
||||
"type": "string"
|
||||
},
|
||||
"alphaIndex": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"items",
|
||||
"firstName",
|
||||
"lastName",
|
||||
"id",
|
||||
"alphaIndex",
|
||||
"zIndex"
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user