Merge branch 'master' of https://github.com/eugenp/tutorials into BAEL-14274-8
This commit is contained in:
@@ -1,13 +1,15 @@
|
||||
=========
|
||||
### Jackson Articles that are also part of the e-book
|
||||
|
||||
###The Course
|
||||
This module contains articles about Jackson that are also part of the Jackson Ebook.
|
||||
|
||||
### The Course
|
||||
|
||||
The "REST With Spring" Classes: http://bit.ly/restwithspring
|
||||
|
||||
### Relevant Articles:
|
||||
- [Jackson Ignore Properties on Marshalling](http://www.baeldung.com/jackson-ignore-properties-on-serialization)
|
||||
- [Jackson Unmarshalling json with Unknown Properties](http://www.baeldung.com/jackson-deserialize-json-unknown-properties)
|
||||
- [Jackson Annotation Examples](http://www.baeldung.com/jackson-annotations)
|
||||
- [Intro to the Jackson ObjectMapper](http://www.baeldung.com/jackson-object-mapper-tutorial)
|
||||
- [Ignore Null Fields with Jackson](http://www.baeldung.com/jackson-ignore-null-fields)
|
||||
- [Jackson – Change Name of Field](http://www.baeldung.com/jackson-name-of-property)
|
||||
- [Jackson Ignore Properties on Marshalling](https://www.baeldung.com/jackson-ignore-properties-on-serialization)
|
||||
- [Jackson Unmarshalling json with Unknown Properties](https://www.baeldung.com/jackson-deserialize-json-unknown-properties)
|
||||
- [Jackson Annotation Examples](https://www.baeldung.com/jackson-annotations)
|
||||
- [Intro to the Jackson ObjectMapper](https://www.baeldung.com/jackson-object-mapper-tutorial)
|
||||
- [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)
|
||||
|
||||
@@ -118,7 +118,6 @@
|
||||
|
||||
<properties>
|
||||
<!-- util -->
|
||||
<commons-lang3.version>3.8</commons-lang3.version>
|
||||
<joda-time.version>2.10</joda-time.version>
|
||||
<gson.version>2.8.5</gson.version>
|
||||
<commons-collections4.version>4.2</commons-collections4.version>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.jackson.test;
|
||||
package com.baeldung.jackson.ignore;
|
||||
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
@@ -15,9 +15,9 @@ import com.baeldung.jackson.dtos.MyDtoIncludeNonDefault;
|
||||
import com.baeldung.jackson.dtos.MyDtoWithFilter;
|
||||
import com.baeldung.jackson.dtos.MyDtoWithSpecialField;
|
||||
import com.baeldung.jackson.dtos.MyMixInForIgnoreType;
|
||||
import com.baeldung.jackson.dtos.ignore.MyDtoIgnoreField;
|
||||
import com.baeldung.jackson.dtos.ignore.MyDtoIgnoreFieldByName;
|
||||
import com.baeldung.jackson.dtos.ignore.MyDtoIgnoreNull;
|
||||
import com.baeldung.jackson.ignore.dtos.MyDtoIgnoreField;
|
||||
import com.baeldung.jackson.ignore.dtos.MyDtoIgnoreFieldByName;
|
||||
import com.baeldung.jackson.ignore.dtos.MyDtoIgnoreNull;
|
||||
import com.baeldung.jackson.serialization.MyDtoNullKeySerializer;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
@@ -165,7 +165,7 @@ public class JacksonSerializationIgnoreUnitTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenIgnoringNullFieldsOnClass_whenWritingObjectWithNullField_thenFieldIsIgnored() throws JsonProcessingException {
|
||||
public final void givenNullsIgnoredOnClass_whenWritingObjectWithNullField_thenIgnored() throws JsonProcessingException {
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
final MyDtoIgnoreNull dtoObject = new MyDtoIgnoreNull();
|
||||
|
||||
@@ -178,7 +178,7 @@ public class JacksonSerializationIgnoreUnitTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenIgnoringNullFieldsGlobally_whenWritingObjectWithNullField_thenIgnored() throws JsonProcessingException {
|
||||
public final void givenNullsIgnoredGlobally_whenWritingObjectWithNullField_thenIgnored() throws JsonProcessingException {
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.setSerializationInclusion(Include.NON_NULL);
|
||||
final MyDto dtoObject = new MyDto();
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.jackson.dtos.ignore;
|
||||
package com.baeldung.jackson.ignore.dtos;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.jackson.dtos.ignore;
|
||||
package com.baeldung.jackson.ignore.dtos;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.jackson.dtos.ignore;
|
||||
package com.baeldung.jackson.ignore.dtos;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
||||
@@ -1,20 +1,29 @@
|
||||
package com.baeldung.jackson.objectmapper;
|
||||
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.URL;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
|
||||
import com.baeldung.jackson.objectmapper.dto.Car;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
public class JavaReadWriteJsonExampleUnitTest {
|
||||
@Rule
|
||||
public TemporaryFolder folder = new TemporaryFolder();
|
||||
|
||||
final String EXAMPLE_JSON = "{ \"color\" : \"Black\", \"type\" : \"BMW\" }";
|
||||
final String LOCAL_JSON = "[{ \"color\" : \"Black\", \"type\" : \"BMW\" }, { \"color\" : \"Red\", \"type\" : \"BMW\" }]";
|
||||
|
||||
@@ -27,6 +36,19 @@ public class JavaReadWriteJsonExampleUnitTest {
|
||||
assertThat(carAsString, containsString("renault"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenWriteToFile_thanCorrect() throws Exception {
|
||||
File resultFile = folder.newFile("car.json");
|
||||
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
Car car = new Car("yellow", "renault");
|
||||
objectMapper.writeValue(resultFile, car);
|
||||
|
||||
Car fromFile = objectMapper.readValue(resultFile, Car.class);
|
||||
assertEquals(car.getType(), fromFile.getType());
|
||||
assertEquals(car.getColor(), fromFile.getColor());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReadJsonToJava_thanCorrect() throws Exception {
|
||||
final ObjectMapper objectMapper = new ObjectMapper();
|
||||
@@ -66,4 +88,26 @@ public class JavaReadWriteJsonExampleUnitTest {
|
||||
assertNotNull(key);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void wheReadFromFile_thanCorrect() throws Exception {
|
||||
File resource = new File("src/test/resources/json_car.json");
|
||||
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
Car fromFile = objectMapper.readValue(resource, Car.class);
|
||||
|
||||
assertEquals("BMW", fromFile.getType());
|
||||
assertEquals("Black", fromFile.getColor());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void wheReadFromUrl_thanCorrect() throws Exception {
|
||||
URL resource = new URL("file:src/test/resources/json_car.json");
|
||||
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
Car fromFile = objectMapper.readValue(resource, Car.class);
|
||||
|
||||
assertEquals("BMW", fromFile.getType());
|
||||
assertEquals("Black", fromFile.getColor());
|
||||
}
|
||||
}
|
||||
|
||||
13
jackson-simple/src/test/resources/.gitignore
vendored
13
jackson-simple/src/test/resources/.gitignore
vendored
@@ -1,13 +0,0 @@
|
||||
*.class
|
||||
|
||||
#folders#
|
||||
/target
|
||||
/neoDb*
|
||||
/data
|
||||
/src/main/webapp/WEB-INF/classes
|
||||
*/META-INF/*
|
||||
|
||||
# Packaged files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
||||
4
jackson-simple/src/test/resources/json_car.json
Normal file
4
jackson-simple/src/test/resources/json_car.json
Normal file
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"color": "Black",
|
||||
"type": "BMW"
|
||||
}
|
||||
Reference in New Issue
Block a user