[BAEL-19675] - Move articles out of jackson part 2
This commit is contained in:
@@ -7,6 +7,3 @@ 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:
|
||||
- [Jackson Unmarshalling json with Unknown Properties](https://www.baeldung.com/jackson-deserialize-json-unknown-properties)
|
||||
- [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)
|
||||
|
||||
@@ -1,42 +0,0 @@
|
||||
package com.baeldung.jackson.objectmapper;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.baeldung.jackson.objectmapper.dto.Car;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.ObjectCodec;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
|
||||
|
||||
public class CustomCarDeserializer extends StdDeserializer<Car> {
|
||||
|
||||
private static final long serialVersionUID = -5918629454846356161L;
|
||||
private final Logger Logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
public CustomCarDeserializer() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public CustomCarDeserializer(final Class<?> vc) {
|
||||
super(vc);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Car deserialize(final JsonParser parser, final DeserializationContext deserializer) throws IOException {
|
||||
final Car car = new Car();
|
||||
final ObjectCodec codec = parser.getCodec();
|
||||
final JsonNode node = codec.readTree(parser);
|
||||
try {
|
||||
final JsonNode colorNode = node.get("color");
|
||||
final String color = colorNode.asText();
|
||||
car.setColor(color);
|
||||
} catch (final Exception e) {
|
||||
Logger.debug("101_parse_exeption: unknown json.");
|
||||
}
|
||||
return car;
|
||||
}
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
package com.baeldung.jackson.objectmapper;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.baeldung.jackson.objectmapper.dto.Car;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
|
||||
|
||||
public class CustomCarSerializer extends StdSerializer<Car> {
|
||||
|
||||
private static final long serialVersionUID = 1396140685442227917L;
|
||||
|
||||
public CustomCarSerializer() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public CustomCarSerializer(final Class<Car> t) {
|
||||
super(t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serialize(final Car car, final JsonGenerator jsonGenerator, final SerializerProvider serializer) throws IOException, JsonProcessingException {
|
||||
jsonGenerator.writeStartObject();
|
||||
jsonGenerator.writeStringField("model: ", car.getType());
|
||||
jsonGenerator.writeEndObject();
|
||||
}
|
||||
}
|
||||
@@ -1,113 +0,0 @@
|
||||
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;
|
||||
|
||||
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\" }]";
|
||||
|
||||
@Test
|
||||
public void whenWriteJavaToJson_thanCorrect() throws Exception {
|
||||
final ObjectMapper objectMapper = new ObjectMapper();
|
||||
final Car car = new Car("yellow", "renault");
|
||||
final String carAsString = objectMapper.writeValueAsString(car);
|
||||
assertThat(carAsString, containsString("yellow"));
|
||||
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();
|
||||
final Car car = objectMapper.readValue(EXAMPLE_JSON, Car.class);
|
||||
assertNotNull(car);
|
||||
assertThat(car.getColor(), containsString("Black"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReadJsonToJsonNode_thanCorrect() throws Exception {
|
||||
final ObjectMapper objectMapper = new ObjectMapper();
|
||||
final JsonNode jsonNode = objectMapper.readTree(EXAMPLE_JSON);
|
||||
assertNotNull(jsonNode);
|
||||
assertThat(jsonNode.get("color")
|
||||
.asText(), containsString("Black"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReadJsonToList_thanCorrect() throws Exception {
|
||||
final ObjectMapper objectMapper = new ObjectMapper();
|
||||
final List<Car> listCar = objectMapper.readValue(LOCAL_JSON, new TypeReference<List<Car>>() {
|
||||
|
||||
});
|
||||
for (final Car car : listCar) {
|
||||
assertNotNull(car);
|
||||
assertThat(car.getType(), equalTo("BMW"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReadJsonToMap_thanCorrect() throws Exception {
|
||||
final ObjectMapper objectMapper = new ObjectMapper();
|
||||
final Map<String, Object> map = objectMapper.readValue(EXAMPLE_JSON, new TypeReference<Map<String, Object>>() {
|
||||
});
|
||||
assertNotNull(map);
|
||||
for (final String key : map.keySet()) {
|
||||
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());
|
||||
}
|
||||
}
|
||||
@@ -1,84 +0,0 @@
|
||||
package com.baeldung.jackson.objectmapper;
|
||||
|
||||
import com.baeldung.jackson.objectmapper.dto.Car;
|
||||
import com.baeldung.jackson.objectmapper.dto.Request;
|
||||
import com.fasterxml.jackson.core.Version;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
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 SerializationDeserializationFeatureUnitTest {
|
||||
final String EXAMPLE_JSON = "{ \"color\" : \"Black\", \"type\" : \"BMW\" }";
|
||||
final String JSON_CAR = "{ \"color\" : \"Black\", \"type\" : \"Fiat\", \"year\" : \"1970\" }";
|
||||
final String JSON_ARRAY = "[{ \"color\" : \"Black\", \"type\" : \"BMW\" }, { \"color\" : \"Red\", \"type\" : \"BMW\" }]";
|
||||
|
||||
@Test
|
||||
public void whenFailOnUnkownPropertiesFalse_thanJsonReadCorrectly() throws Exception {
|
||||
|
||||
final ObjectMapper objectMapper = new ObjectMapper();
|
||||
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
final Car car = objectMapper.readValue(JSON_CAR, Car.class);
|
||||
final JsonNode jsonNodeRoot = objectMapper.readTree(JSON_CAR);
|
||||
final JsonNode jsonNodeYear = jsonNodeRoot.get("year");
|
||||
final String year = jsonNodeYear.asText();
|
||||
|
||||
assertNotNull(car);
|
||||
assertThat(car.getColor(), equalTo("Black"));
|
||||
assertThat(year, containsString("1970"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCustomSerializerDeserializer_thanReadWriteCorrect() throws Exception {
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
final SimpleModule serializerModule = new SimpleModule("CustomSerializer", new Version(1, 0, 0, null, null, null));
|
||||
serializerModule.addSerializer(Car.class, new CustomCarSerializer());
|
||||
mapper.registerModule(serializerModule);
|
||||
final Car car = new Car("yellow", "renault");
|
||||
final String carJson = mapper.writeValueAsString(car);
|
||||
assertThat(carJson, containsString("renault"));
|
||||
assertThat(carJson, containsString("model"));
|
||||
|
||||
final SimpleModule deserializerModule = new SimpleModule("CustomCarDeserializer", new Version(1, 0, 0, null, null, null));
|
||||
deserializerModule.addDeserializer(Car.class, new CustomCarDeserializer());
|
||||
mapper.registerModule(deserializerModule);
|
||||
final Car carResult = mapper.readValue(EXAMPLE_JSON, Car.class);
|
||||
assertNotNull(carResult);
|
||||
assertThat(carResult.getColor(), equalTo("Black"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDateFormatSet_thanSerializedAsExpected() throws Exception {
|
||||
final ObjectMapper objectMapper = new ObjectMapper();
|
||||
final Car car = new Car("yellow", "renault");
|
||||
final Request request = new Request();
|
||||
request.setCar(car);
|
||||
request.setDatePurchased(new Date());
|
||||
final DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm a z");
|
||||
objectMapper.setDateFormat(df);
|
||||
final String carAsString = objectMapper.writeValueAsString(request);
|
||||
assertNotNull(carAsString);
|
||||
assertThat(carAsString, containsString("datePurchased"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUseJavaArrayForJsonArrayTrue_thanJsonReadAsArray() throws Exception {
|
||||
final ObjectMapper objectMapper = new ObjectMapper();
|
||||
objectMapper.configure(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY, true);
|
||||
final Car[] cars = objectMapper.readValue(JSON_ARRAY, Car[].class);
|
||||
for (final Car car : cars) {
|
||||
assertNotNull(car);
|
||||
assertThat(car.getType(), equalTo("BMW"));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
package com.baeldung.jackson.objectmapper.dto;
|
||||
|
||||
public class Car {
|
||||
|
||||
private String color;
|
||||
private String type;
|
||||
|
||||
public Car() {
|
||||
}
|
||||
|
||||
public Car(final String color, final String type) {
|
||||
this.color = color;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public String getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
public void setColor(final String color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(final String type) {
|
||||
this.type = type;
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
package com.baeldung.jackson.objectmapper.dto;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class Request {
|
||||
Car car;
|
||||
Date datePurchased;
|
||||
|
||||
public Car getCar() {
|
||||
return car;
|
||||
}
|
||||
|
||||
public void setCar(final Car car) {
|
||||
this.car = car;
|
||||
}
|
||||
|
||||
public Date getDatePurchased() {
|
||||
return datePurchased;
|
||||
}
|
||||
|
||||
public void setDatePurchased(final Date datePurchased) {
|
||||
this.datePurchased = datePurchased;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
{
|
||||
"color": "Black",
|
||||
"type": "BMW"
|
||||
}
|
||||
Reference in New Issue
Block a user