BAEL-17 - reorganizing objectmapper tests

This commit is contained in:
Slavisa Baeldung 2016-07-18 13:16:52 +02:00
parent a727caec3a
commit d577830362
13 changed files with 178 additions and 375 deletions

View File

@ -14,13 +14,13 @@ import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
public class CustomCarDeserializer extends JsonDeserializer<Car> {
protected final Logger Logger = LoggerFactory.getLogger(getClass());
private final Logger Logger = LoggerFactory.getLogger(getClass());
public CustomCarDeserializer() {
}
@Override
public Car deserialize(final JsonParser parser, final DeserializationContext deserializer) throws IOException, JsonProcessingException {
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);

View File

@ -1,13 +0,0 @@
package com.baeldung.jackson.objectmapper;
import org.junit.Test;
public abstract class Example {
String EXAMPLE_JSON = "{ \"color\" : \"Black\", \"type\" : \"BMW\" }";
public abstract String name();
@Test
public abstract void testExample() throws Exception;
}

View File

@ -1,33 +0,0 @@
package com.baeldung.jackson.objectmapper;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertThat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.baeldung.jackson.objectmapper.dto.Car;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JavaToJsonExample extends Example
{
protected final Logger Logger = LoggerFactory.getLogger(getClass());
public JavaToJsonExample() { }
@Override
public String name()
{
return this.getClass().getName();
}
@Override
public void testExample() 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"));
}
}

View File

@ -1,47 +0,0 @@
package com.baeldung.jackson.objectmapper;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.baeldung.jackson.objectmapper.dto.Car;
import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
public class JsonAdvancedCustomSerializeExample extends Example
{
protected final Logger Logger = LoggerFactory.getLogger(getClass());
public JsonAdvancedCustomSerializeExample() { }
@Override
public String name()
{
return this.getClass().getName();
}
@Override
public void testExample() 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"));
}
}

View File

@ -1,45 +0,0 @@
package com.baeldung.jackson.objectmapper;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.baeldung.jackson.objectmapper.dto.Car;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonAdvancedJsonNodeExample extends Example
{
protected final Logger Logger = LoggerFactory.getLogger(getClass());
public JsonAdvancedJsonNodeExample() { }
String LOCAL_JSON = "{ \"color\" : \"Black\", \"type\" : \"Fiat\", \"year\" : \"1970\" }";
@Override
public String name()
{
return this.getClass().getName();
}
@Override
public void testExample() throws Exception {
final ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
final Car car = objectMapper.readValue(LOCAL_JSON, Car.class);
final JsonNode jsonNodeRoot = objectMapper.readTree(LOCAL_JSON);
final JsonNode jsonNodeYear = jsonNodeRoot.get("year");
final String year = jsonNodeYear.asText();
assertNotNull(car);
assertThat(car.getColor(), equalTo("Black"));
assertThat(year, containsString("1970"));
}
}

View File

@ -1,68 +0,0 @@
package com.baeldung.jackson.objectmapper;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import java.util.List;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.baeldung.jackson.objectmapper.dto.Car;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonArrayExample extends Example {
protected final Logger Logger = LoggerFactory.getLogger(getClass());
public JsonArrayExample() { }
@Override
public String name()
{
return this.getClass().getName();
}
class Response {
public Response(final List<Car> cars) {
this.cars = cars;
}
List<Car> cars;
public List<Car> getCars() {
return cars;
}
public void setCars(final List<Car> cars) {
this.cars = cars;
}
}
final String LOCAL_JSON = "[{ \"color\" : \"Black\", \"type\" : \"BMW\" }, { \"color\" : \"Red\", \"type\" : \"BMW\" }]";
@Override
@Test
public void testExample() throws Exception {
final ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY, true);
final Car[] cars = objectMapper.readValue(LOCAL_JSON, Car[].class);
for (final Car car : cars) {
assertNotNull(car);
assertThat(car.getType(), equalTo("BMW"));
}
final List<Car> listCar = objectMapper.readValue(LOCAL_JSON, new TypeReference<List<Car>>() {
});
for (final Car car : listCar) {
assertNotNull(car);
assertThat(car.getType(), equalTo("BMW"));
}
}
}

View File

@ -1,62 +0,0 @@
package com.baeldung.jackson.objectmapper;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.baeldung.jackson.objectmapper.dto.Car;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonDateExample extends Example {
protected final Logger Logger = LoggerFactory.getLogger(getClass());
public JsonDateExample() {
}
@Override
public String name() {
return this.getClass().getName();
}
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;
}
}
@Override
public void testExample() 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"));
}
}

View File

@ -1,37 +0,0 @@
package com.baeldung.jackson.objectmapper;
import static org.junit.Assert.assertNotNull;
import java.util.Map;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonMapExample extends Example {
protected final Logger Logger = LoggerFactory.getLogger(getClass());
public JsonMapExample() { }
@Override
public String name()
{
return this.getClass().getName();
}
@Override
@Test
public void testExample() 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);
}
}
}

View File

@ -1,34 +0,0 @@
package com.baeldung.jackson.objectmapper;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.baeldung.jackson.objectmapper.dto.Car;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonToJavaExample extends Example
{
protected final Logger Logger = LoggerFactory.getLogger(getClass());
public JsonToJavaExample() { }
@Override
public String name()
{
return this.getClass().getName();
}
@Override
@Test
public void testExample() throws Exception {
final ObjectMapper objectMapper = new ObjectMapper();
final Car car = objectMapper.readValue(EXAMPLE_JSON, Car.class);
assertNotNull(car);
assertThat(car.getColor(), containsString("Black"));
}
}

View File

@ -1,34 +0,0 @@
package com.baeldung.jackson.objectmapper;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonToJsonNode extends Example
{
protected final Logger Logger = LoggerFactory.getLogger(getClass());
public JsonToJsonNode() { }
@Override
public String name()
{
return this.getClass().getName();
}
@Override
@Test
public void testExample() throws Exception {
final ObjectMapper objectMapper = new ObjectMapper();
final JsonNode jsonNode = objectMapper.readTree(EXAMPLE_JSON);
assertNotNull(jsonNode);
assertThat(jsonNode.get("color").asText(), containsString("Black"));
}
}

View File

@ -0,0 +1,68 @@
package com.baeldung.jackson.objectmapper;
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 TestJavaReadWriteJsonExample {
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 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);
}
}
}

View File

@ -0,0 +1,84 @@
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 TestSerializationDeserializationFeature {
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"));
}
}
}

View File

@ -0,0 +1,24 @@
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;
}
}