Refs #BAEL-17 : Test objectMapper.

This commit is contained in:
Ashanka Das 2016-07-17 01:52:45 +02:00
parent f14f2af421
commit eaa32bdb41
9 changed files with 9 additions and 208 deletions

View File

@ -8,8 +8,6 @@ public abstract class Example {
public abstract String name();
public abstract void execute();
@Test
public abstract void test() throws Exception;
public abstract void testExample() throws Exception;
}

View File

@ -3,7 +3,6 @@ package com.baeldung.jackson.objectmapper;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertThat;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -24,24 +23,7 @@ public class JavaToJsonExample extends Example
}
@Override
public void execute()
{
try
{
final ObjectMapper objectMapper = new ObjectMapper();
final Car car = new Car("yellow", "renault");
final String carAsString = objectMapper.writeValueAsString(car);
Logger.debug(carAsString);
}
catch(final Exception e)
{
Logger.error(e.toString());
}
}
@Override
@Test
public void test() throws Exception {
public void testExample() throws Exception {
final ObjectMapper objectMapper = new ObjectMapper();
final Car car = new Car("yellow", "renault");
final String carAsString = objectMapper.writeValueAsString(car);

View File

@ -5,7 +5,6 @@ import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -28,44 +27,7 @@ public class JsonAdvancedCustomSerializeExample extends Example
}
@Override
public void execute()
{
Logger.debug("Executing: "+name());
try
{
ObjectMapper mapper = new ObjectMapper();
final SimpleModule module = new SimpleModule("CustomSerializer", new Version(1, 0, 0, null, null, null));
module.addSerializer(Car.class, new CustomCarSerializer());
mapper = new ObjectMapper();
mapper.registerModule(module);
final Car car = new Car("yellow", "renault");
final String carJson = mapper.writeValueAsString(car);
Logger.debug("car as json = " + carJson);
}
catch (final Exception e)
{
Logger.error(e.toString());
}
try
{
ObjectMapper mapper = new ObjectMapper();
final SimpleModule module = new SimpleModule("CustomCarDeserializer", new Version(1, 0, 0, null, null, null));
module.addDeserializer(Car.class, new CustomCarDeserializer());
mapper = new ObjectMapper();
mapper.registerModule(module);
final Car car = mapper.readValue(EXAMPLE_JSON, Car.class);
Logger.debug("car type = " + car.getType());
Logger.debug("car color = " + car.getColor());
}
catch (final Exception e)
{
Logger.error(e.toString());
}
}
@Override
@Test
public void test() throws Exception {
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());

View File

@ -5,9 +5,6 @@ import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import java.io.StringWriter;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -15,7 +12,6 @@ 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;
import com.fasterxml.jackson.databind.SerializationFeature;
public class JsonAdvancedJsonNodeExample extends Example
{
@ -33,35 +29,7 @@ public class JsonAdvancedJsonNodeExample extends Example
}
@Override
public void execute()
{
Logger.debug("Executing: "+name());
try
{
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();
Logger.debug("Year = " + year);
Logger.debug("Color = " + car.getColor());
Logger.debug("Type = " + car.getType());
objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
final StringWriter string = new StringWriter();
objectMapper.writeValue(string, car);
Logger.debug("Car JSON is:"+string);
}
catch (final Exception e)
{
Logger.error(e.toString());
}
}
@Override
@Test
public void test() throws Exception {
public void testExample() throws Exception {
final ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

View File

@ -27,46 +27,6 @@ public class JsonArrayExample extends Example {
return this.getClass().getName();
}
@Override
public void execute()
{
Logger.debug("Executing: "+name());
try
{
final ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY, true);
final String jsonCarArray = "[{ \"color\" : \"Black\", \"type\" : \"BMW\" }, { \"color\" : \"Red\", \"type\" : \"FIAT\" }]";
final Car[] cars = objectMapper.readValue(jsonCarArray, Car[].class);
for(final Car car : cars)
{
Logger.debug("Color = " + car.getColor());
Logger.debug("Type = " + car.getType());
}
}
catch (final Exception e)
{
Logger.error(e.toString());
}
try
{
final ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY, true);
final String jsonCarArray = "[{ \"color\" : \"Black\", \"type\" : \"BMW\" }, { \"color\" : \"Red\", \"type\" : \"FIAT\" }]";
final List<Car> listCar = objectMapper.readValue(jsonCarArray, new TypeReference<List<Car>>(){});
for(final Car car : listCar)
{
Logger.debug("Color = " + car.getColor());
Logger.debug("Type = " + car.getType());
}
}
catch (final Exception e)
{
Logger.error(e.toString());
}
}
class Response {
public Response(final List<Car> cars) {
@ -89,7 +49,7 @@ public class JsonArrayExample extends Example {
@Override
@Test
public void test() throws Exception {
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);

View File

@ -26,23 +26,6 @@ public class JsonDateExample extends Example {
return this.getClass().getName();
}
@Override
public void execute() {
Logger.debug("Executing: " + name());
try {
final Car car = new Car("yellow", "renault");
final Request request = new Request();
request.setCar(car);
request.setDatePurchased(new Date());
final ObjectMapper objectMapper = new ObjectMapper();
final DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm a z");
objectMapper.setDateFormat(df);
final String carAsString = objectMapper.writeValueAsString(request);
Logger.debug(carAsString);
} catch (final Exception e) {
Logger.error(e.toString());
}
}
class Request {
Car car;
Date datePurchased;
@ -64,7 +47,7 @@ public class JsonDateExample extends Example {
}
@Override
public void test() throws Exception {
public void testExample() throws Exception {
final ObjectMapper objectMapper = new ObjectMapper();
final Car car = new Car("yellow", "renault");
final Request request = new Request();

View File

@ -23,28 +23,9 @@ public class JsonMapExample extends Example {
return this.getClass().getName();
}
@Override
public void execute()
{
final ObjectMapper objectMapper = new ObjectMapper();
try
{
final Map<String, Object> map = objectMapper.readValue(EXAMPLE_JSON, new TypeReference<Map<String, Object>>() {
});
for(final String key : map.keySet())
{
Logger.debug("key = " + key + " | value = " + map.get(key));
}
}
catch (final Exception e)
{
Logger.error(e.toString());
}
}
@Override
@Test
public void test() throws Exception {
public void testExample() throws Exception {
final ObjectMapper objectMapper = new ObjectMapper();
final Map<String, Object> map = objectMapper.readValue(EXAMPLE_JSON, new TypeReference<Map<String, Object>>() {
});

View File

@ -23,26 +23,9 @@ public class JsonToJavaExample extends Example
return this.getClass().getName();
}
@Override
public void execute()
{
Logger.debug("Executing: "+name());
try
{
final ObjectMapper objectMapper = new ObjectMapper();
final Car car = objectMapper.readValue(EXAMPLE_JSON, Car.class);
Logger.debug("Color = " + car.getColor());
Logger.debug("Type = " + car.getType());
}
catch (final Exception e)
{
Logger.error(e.toString());
}
}
@Override
@Test
public void test() throws Exception {
public void testExample() throws Exception {
final ObjectMapper objectMapper = new ObjectMapper();
final Car car = objectMapper.readValue(EXAMPLE_JSON, Car.class);
assertNotNull(car);

View File

@ -23,25 +23,9 @@ public class JsonToJsonNode extends Example
return this.getClass().getName();
}
@Override
public void execute()
{
Logger.debug("Executing: "+name());
try
{
final ObjectMapper objectMapper = new ObjectMapper();
final JsonNode jsonNode = objectMapper.readTree(EXAMPLE_JSON);
Logger.debug(jsonNode.get("color").asText());
}
catch (final Exception e)
{
Logger.error(e.toString());
}
}
@Override
@Test
public void test() throws Exception {
public void testExample() throws Exception {
final ObjectMapper objectMapper = new ObjectMapper();
final JsonNode jsonNode = objectMapper.readTree(EXAMPLE_JSON);
assertNotNull(jsonNode);