BAEL-17 - reformatting code
This commit is contained in:
parent
53c32286be
commit
c44644b6d8
|
@ -13,26 +13,22 @@ import com.fasterxml.jackson.databind.DeserializationContext;
|
||||||
import com.fasterxml.jackson.databind.JsonDeserializer;
|
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
|
|
||||||
public class CustomCarDeserializer extends JsonDeserializer<Car>
|
public class CustomCarDeserializer extends JsonDeserializer<Car> {
|
||||||
{
|
|
||||||
protected final Logger Logger = LoggerFactory.getLogger(getClass());
|
protected final Logger Logger = LoggerFactory.getLogger(getClass());
|
||||||
|
|
||||||
public CustomCarDeserializer() { }
|
public CustomCarDeserializer() {
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Car deserialize(final JsonParser parser, final DeserializationContext deserializer) throws IOException, JsonProcessingException
|
public Car deserialize(final JsonParser parser, final DeserializationContext deserializer) throws IOException, JsonProcessingException {
|
||||||
{
|
|
||||||
final Car car = new Car();
|
final Car car = new Car();
|
||||||
final ObjectCodec codec = parser.getCodec();
|
final ObjectCodec codec = parser.getCodec();
|
||||||
final JsonNode node = codec.readTree(parser);
|
final JsonNode node = codec.readTree(parser);
|
||||||
try
|
try {
|
||||||
{
|
|
||||||
final JsonNode colorNode = node.get("color");
|
final JsonNode colorNode = node.get("color");
|
||||||
final String color = colorNode.asText();
|
final String color = colorNode.asText();
|
||||||
car.setColor(color);
|
car.setColor(color);
|
||||||
}
|
} catch (final Exception e) {
|
||||||
catch(final Exception e)
|
|
||||||
{
|
|
||||||
Logger.debug("101_parse_exeption: unknown json.");
|
Logger.debug("101_parse_exeption: unknown json.");
|
||||||
}
|
}
|
||||||
return car;
|
return car;
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
package com.baeldung.jackson.objectmapper;
|
package com.baeldung.jackson.objectmapper;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
import com.baeldung.jackson.objectmapper.dto.Car;
|
import com.baeldung.jackson.objectmapper.dto.Car;
|
||||||
import com.fasterxml.jackson.core.JsonGenerator;
|
import com.fasterxml.jackson.core.JsonGenerator;
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
public class CustomCarSerializer extends JsonSerializer<Car>
|
public class CustomCarSerializer extends JsonSerializer<Car>
|
||||||
{
|
{
|
||||||
public CustomCarSerializer() { }
|
public CustomCarSerializer() { }
|
||||||
|
|
|
@ -1,10 +1,9 @@
|
||||||
package com.baeldung.jackson.objectmapper;
|
package com.baeldung.jackson.objectmapper;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import com.baeldung.jackson.objectmapper.dto.Car;
|
import com.baeldung.jackson.objectmapper.dto.Car;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
public class JavaToJsonExample extends Example
|
public class JavaToJsonExample extends Example
|
||||||
{
|
{
|
||||||
|
|
|
@ -8,27 +8,24 @@ import com.fasterxml.jackson.core.Version;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.fasterxml.jackson.databind.module.SimpleModule;
|
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||||
|
|
||||||
public class JsonAdvancedCustomSerializeExample extends Example
|
public class JsonAdvancedCustomSerializeExample extends Example {
|
||||||
{
|
|
||||||
|
|
||||||
protected final Logger Logger = LoggerFactory.getLogger(getClass());
|
protected final Logger Logger = LoggerFactory.getLogger(getClass());
|
||||||
|
|
||||||
public JsonAdvancedCustomSerializeExample() { }
|
public JsonAdvancedCustomSerializeExample() {
|
||||||
|
}
|
||||||
|
|
||||||
String json = "{ \"color\" : \"Black\", \"type\" : \"BMW\" }";
|
String json = "{ \"color\" : \"Black\", \"type\" : \"BMW\" }";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String name()
|
public String name() {
|
||||||
{
|
|
||||||
return this.getClass().getName();
|
return this.getClass().getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void execute()
|
public void execute() {
|
||||||
{
|
Logger.debug("Executing: " + name());
|
||||||
Logger.debug("Executing: "+name());
|
try {
|
||||||
try
|
|
||||||
{
|
|
||||||
ObjectMapper mapper = new ObjectMapper();
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
final SimpleModule module = new SimpleModule("CustomSerializer", new Version(1, 0, 0, null, null, null));
|
final SimpleModule module = new SimpleModule("CustomSerializer", new Version(1, 0, 0, null, null, null));
|
||||||
module.addSerializer(Car.class, new CustomCarSerializer());
|
module.addSerializer(Car.class, new CustomCarSerializer());
|
||||||
|
@ -37,13 +34,10 @@ public class JsonAdvancedCustomSerializeExample extends Example
|
||||||
final Car car = new Car("yellow", "renault");
|
final Car car = new Car("yellow", "renault");
|
||||||
final String carJson = mapper.writeValueAsString(car);
|
final String carJson = mapper.writeValueAsString(car);
|
||||||
Logger.debug("car as json = " + carJson);
|
Logger.debug("car as json = " + carJson);
|
||||||
}
|
} catch (final Exception e) {
|
||||||
catch (final Exception e)
|
|
||||||
{
|
|
||||||
Logger.error(e.toString());
|
Logger.error(e.toString());
|
||||||
}
|
}
|
||||||
try
|
try {
|
||||||
{
|
|
||||||
ObjectMapper mapper = new ObjectMapper();
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
final SimpleModule module = new SimpleModule("CustomCarDeserializer", new Version(1, 0, 0, null, null, null));
|
final SimpleModule module = new SimpleModule("CustomCarDeserializer", new Version(1, 0, 0, null, null, null));
|
||||||
module.addDeserializer(Car.class, new CustomCarDeserializer());
|
module.addDeserializer(Car.class, new CustomCarDeserializer());
|
||||||
|
@ -52,9 +46,7 @@ public class JsonAdvancedCustomSerializeExample extends Example
|
||||||
final Car car = mapper.readValue(json, Car.class);
|
final Car car = mapper.readValue(json, Car.class);
|
||||||
Logger.debug("car type = " + car.getType());
|
Logger.debug("car type = " + car.getType());
|
||||||
Logger.debug("car color = " + car.getColor());
|
Logger.debug("car color = " + car.getColor());
|
||||||
}
|
} catch (final Exception e) {
|
||||||
catch (final Exception e)
|
|
||||||
{
|
|
||||||
Logger.error(e.toString());
|
Logger.error(e.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,27 +11,24 @@ import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||||
|
|
||||||
public class JsonAdvancedJsonNodeExample extends Example
|
public class JsonAdvancedJsonNodeExample extends Example {
|
||||||
{
|
|
||||||
|
|
||||||
protected final Logger Logger = LoggerFactory.getLogger(getClass());
|
protected final Logger Logger = LoggerFactory.getLogger(getClass());
|
||||||
|
|
||||||
public JsonAdvancedJsonNodeExample() { }
|
public JsonAdvancedJsonNodeExample() {
|
||||||
|
}
|
||||||
|
|
||||||
String jsonString = "{ \"color\" : \"Black\", \"type\" : \"Fiat\", \"year\" : \"1970\" }";
|
String jsonString = "{ \"color\" : \"Black\", \"type\" : \"Fiat\", \"year\" : \"1970\" }";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String name()
|
public String name() {
|
||||||
{
|
|
||||||
return this.getClass().getName();
|
return this.getClass().getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void execute()
|
public void execute() {
|
||||||
{
|
Logger.debug("Executing: " + name());
|
||||||
Logger.debug("Executing: "+name());
|
try {
|
||||||
try
|
|
||||||
{
|
|
||||||
final ObjectMapper objectMapper = new ObjectMapper();
|
final ObjectMapper objectMapper = new ObjectMapper();
|
||||||
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||||
final Car car = objectMapper.readValue(jsonString, Car.class);
|
final Car car = objectMapper.readValue(jsonString, Car.class);
|
||||||
|
@ -45,10 +42,8 @@ public class JsonAdvancedJsonNodeExample extends Example
|
||||||
objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
|
objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
|
||||||
final StringWriter string = new StringWriter();
|
final StringWriter string = new StringWriter();
|
||||||
objectMapper.writeValue(string, car);
|
objectMapper.writeValue(string, car);
|
||||||
Logger.debug("Car JSON is:"+string);
|
Logger.debug("Car JSON is:" + string);
|
||||||
}
|
} catch (final Exception e) {
|
||||||
catch (final Exception e)
|
|
||||||
{
|
|
||||||
Logger.error(e.toString());
|
Logger.error(e.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,50 +14,42 @@ public class JsonArrayExample extends Example {
|
||||||
|
|
||||||
protected final Logger Logger = LoggerFactory.getLogger(getClass());
|
protected final Logger Logger = LoggerFactory.getLogger(getClass());
|
||||||
|
|
||||||
public JsonArrayExample() { }
|
public JsonArrayExample() {
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String name()
|
public String name() {
|
||||||
{
|
|
||||||
return this.getClass().getName();
|
return this.getClass().getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void execute()
|
public void execute() {
|
||||||
{
|
Logger.debug("Executing: " + name());
|
||||||
Logger.debug("Executing: "+name());
|
try {
|
||||||
try
|
|
||||||
{
|
|
||||||
final ObjectMapper objectMapper = new ObjectMapper();
|
final ObjectMapper objectMapper = new ObjectMapper();
|
||||||
objectMapper.configure(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY, true);
|
objectMapper.configure(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY, true);
|
||||||
|
|
||||||
final String jsonCarArray = "[{ \"color\" : \"Black\", \"type\" : \"BMW\" }, { \"color\" : \"Red\", \"type\" : \"FIAT\" }]";
|
final String jsonCarArray = "[{ \"color\" : \"Black\", \"type\" : \"BMW\" }, { \"color\" : \"Red\", \"type\" : \"FIAT\" }]";
|
||||||
final Car[] cars = objectMapper.readValue(jsonCarArray, Car[].class);
|
final Car[] cars = objectMapper.readValue(jsonCarArray, Car[].class);
|
||||||
for(final Car car : cars)
|
for (final Car car : cars) {
|
||||||
{
|
|
||||||
Logger.debug("Color = " + car.getColor());
|
Logger.debug("Color = " + car.getColor());
|
||||||
Logger.debug("Type = " + car.getType());
|
Logger.debug("Type = " + car.getType());
|
||||||
}
|
}
|
||||||
}
|
} catch (final Exception e) {
|
||||||
catch (final Exception e)
|
|
||||||
{
|
|
||||||
Logger.error(e.toString());
|
Logger.error(e.toString());
|
||||||
}
|
}
|
||||||
try
|
try {
|
||||||
{
|
|
||||||
final ObjectMapper objectMapper = new ObjectMapper();
|
final ObjectMapper objectMapper = new ObjectMapper();
|
||||||
objectMapper.configure(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY, true);
|
objectMapper.configure(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY, true);
|
||||||
|
|
||||||
final String jsonCarArray = "[{ \"color\" : \"Black\", \"type\" : \"BMW\" }, { \"color\" : \"Red\", \"type\" : \"FIAT\" }]";
|
final String jsonCarArray = "[{ \"color\" : \"Black\", \"type\" : \"BMW\" }, { \"color\" : \"Red\", \"type\" : \"FIAT\" }]";
|
||||||
final List<Car> listCar = objectMapper.readValue(jsonCarArray, new TypeReference<List<Car>>(){});
|
final List<Car> listCar = objectMapper.readValue(jsonCarArray, new TypeReference<List<Car>>() {
|
||||||
for(final Car car : listCar)
|
});
|
||||||
{
|
for (final Car car : listCar) {
|
||||||
Logger.debug("Color = " + car.getColor());
|
Logger.debug("Color = " + car.getColor());
|
||||||
Logger.debug("Type = " + car.getType());
|
Logger.debug("Type = " + car.getType());
|
||||||
}
|
}
|
||||||
}
|
} catch (final Exception e) {
|
||||||
catch (final Exception e)
|
|
||||||
{
|
|
||||||
Logger.error(e.toString());
|
Logger.error(e.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,9 +39,11 @@ public class JsonDateExample extends Example {
|
||||||
Logger.error(e.toString());
|
Logger.error(e.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Request {
|
class Request {
|
||||||
Car car;
|
Car car;
|
||||||
Date datePurchased;
|
Date datePurchased;
|
||||||
|
|
||||||
public Car getCar() {
|
public Car getCar() {
|
||||||
return car;
|
return car;
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,29 +12,25 @@ public class JsonMapExample extends Example {
|
||||||
|
|
||||||
protected final Logger Logger = LoggerFactory.getLogger(getClass());
|
protected final Logger Logger = LoggerFactory.getLogger(getClass());
|
||||||
|
|
||||||
public JsonMapExample() { }
|
public JsonMapExample() {
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String name()
|
public String name() {
|
||||||
{
|
|
||||||
return this.getClass().getName();
|
return this.getClass().getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void execute()
|
public void execute() {
|
||||||
{
|
|
||||||
final ObjectMapper objectMapper = new ObjectMapper();
|
final ObjectMapper objectMapper = new ObjectMapper();
|
||||||
final String json = "{ \"color\" : \"Black\", \"type\" : \"BMW\" }";
|
final String json = "{ \"color\" : \"Black\", \"type\" : \"BMW\" }";
|
||||||
try
|
try {
|
||||||
{
|
final Map<String, Object> map = objectMapper.readValue(json, new TypeReference<Map<String, Object>>() {
|
||||||
final Map<String, Object> map = objectMapper.readValue(json, new TypeReference<Map<String,Object>>(){});
|
});
|
||||||
for(final String key : map.keySet())
|
for (final String key : map.keySet()) {
|
||||||
{
|
|
||||||
Logger.debug("key = " + key + " | value = " + map.get(key));
|
Logger.debug("key = " + key + " | value = " + map.get(key));
|
||||||
}
|
}
|
||||||
}
|
} catch (final Exception e) {
|
||||||
catch (final Exception e)
|
|
||||||
{
|
|
||||||
Logger.error(e.toString());
|
Logger.error(e.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,47 +12,42 @@ public class JsonParserExample extends Example {
|
||||||
|
|
||||||
protected final Logger Logger = LoggerFactory.getLogger(getClass());
|
protected final Logger Logger = LoggerFactory.getLogger(getClass());
|
||||||
|
|
||||||
public JsonParserExample() { }
|
public JsonParserExample() {
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String name()
|
public String name() {
|
||||||
{
|
|
||||||
return this.getClass().getName();
|
return this.getClass().getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void execute()
|
public void execute() {
|
||||||
{
|
Logger.debug("Executing: " + name());
|
||||||
Logger.debug("Executing: "+name());
|
|
||||||
final String carJson = "{ \"color\" : \"Black\", \"type\" : \"BMW\" }";
|
final String carJson = "{ \"color\" : \"Black\", \"type\" : \"BMW\" }";
|
||||||
final JsonFactory factory = new JsonFactory();
|
final JsonFactory factory = new JsonFactory();
|
||||||
JsonParser parser;
|
JsonParser parser;
|
||||||
try
|
try {
|
||||||
{
|
|
||||||
final Car car = new Car();
|
final Car car = new Car();
|
||||||
parser = factory.createParser(carJson);
|
parser = factory.createParser(carJson);
|
||||||
while(!parser.isClosed())
|
while (!parser.isClosed()) {
|
||||||
{
|
|
||||||
JsonToken jsonToken = parser.nextToken();
|
JsonToken jsonToken = parser.nextToken();
|
||||||
Logger.debug("jsonToken = " + jsonToken);
|
Logger.debug("jsonToken = " + jsonToken);
|
||||||
|
|
||||||
if(JsonToken.FIELD_NAME.equals(jsonToken)){
|
if (JsonToken.FIELD_NAME.equals(jsonToken)) {
|
||||||
final String fieldName = parser.getCurrentName();
|
final String fieldName = parser.getCurrentName();
|
||||||
System.out.println(fieldName);
|
System.out.println(fieldName);
|
||||||
|
|
||||||
jsonToken = parser.nextToken();
|
jsonToken = parser.nextToken();
|
||||||
|
|
||||||
if("color".equals(fieldName)){
|
if ("color".equals(fieldName)) {
|
||||||
car.setColor(parser.getValueAsString());
|
car.setColor(parser.getValueAsString());
|
||||||
} else if ("type".equals(fieldName)){
|
} else if ("type".equals(fieldName)) {
|
||||||
car.setType(parser.getValueAsString());
|
car.setType(parser.getValueAsString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Logger.debug("car:"+car.getColor());
|
Logger.debug("car:" + car.getColor());
|
||||||
}
|
} catch (final Exception e) {
|
||||||
catch (final Exception e)
|
|
||||||
{
|
|
||||||
Logger.error(e.toString());
|
Logger.error(e.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,48 +7,40 @@ import com.baeldung.jackson.objectmapper.dto.Car;
|
||||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
public class JsonToJavaExample extends Example
|
public class JsonToJavaExample extends Example {
|
||||||
{
|
|
||||||
protected final Logger Logger = LoggerFactory.getLogger(getClass());
|
protected final Logger Logger = LoggerFactory.getLogger(getClass());
|
||||||
|
|
||||||
public JsonToJavaExample() { }
|
public JsonToJavaExample() {
|
||||||
|
}
|
||||||
|
|
||||||
String json = "{ \"color\" : \"Black\", \"type\" : \"BMW\" }";
|
String json = "{ \"color\" : \"Black\", \"type\" : \"BMW\" }";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String name()
|
public String name() {
|
||||||
{
|
|
||||||
return this.getClass().getName();
|
return this.getClass().getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void execute()
|
public void execute() {
|
||||||
{
|
Logger.debug("Executing: " + name());
|
||||||
Logger.debug("Executing: "+name());
|
try {
|
||||||
try
|
|
||||||
{
|
|
||||||
final ObjectMapper objectMapper = new ObjectMapper();
|
final ObjectMapper objectMapper = new ObjectMapper();
|
||||||
final Car car = objectMapper.readValue(json, Car.class);
|
final Car car = objectMapper.readValue(json, Car.class);
|
||||||
Logger.debug("Color = " + car.getColor());
|
Logger.debug("Color = " + car.getColor());
|
||||||
Logger.debug("Type = " + car.getType());
|
Logger.debug("Type = " + car.getType());
|
||||||
}
|
} catch (final Exception e) {
|
||||||
catch (final Exception e)
|
|
||||||
{
|
|
||||||
Logger.error(e.toString());
|
Logger.error(e.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
try {
|
||||||
{
|
|
||||||
final ObjectMapper objectMapper = new ObjectMapper();
|
final ObjectMapper objectMapper = new ObjectMapper();
|
||||||
objectMapper.configure(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY, true);
|
objectMapper.configure(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY, true);
|
||||||
|
|
||||||
final String jsonCar = "\"car\" : { \"color\" : \"Red\", \"type\" : \"FIAT\" }";
|
final String jsonCar = "\"car\" : { \"color\" : \"Red\", \"type\" : \"FIAT\" }";
|
||||||
final Response response = objectMapper.readValue(jsonCar, Response.class);
|
final Response response = objectMapper.readValue(jsonCar, Response.class);
|
||||||
|
|
||||||
Logger.debug("response: "+response);
|
Logger.debug("response: " + response);
|
||||||
}
|
} catch (final Exception e) {
|
||||||
catch (final Exception e)
|
|
||||||
{
|
|
||||||
Logger.error(e.toString());
|
Logger.error(e.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,32 +6,27 @@ import org.slf4j.LoggerFactory;
|
||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
public class JsonToJsonNode extends Example
|
public class JsonToJsonNode extends Example {
|
||||||
{
|
|
||||||
protected final Logger Logger = LoggerFactory.getLogger(getClass());
|
protected final Logger Logger = LoggerFactory.getLogger(getClass());
|
||||||
|
|
||||||
public JsonToJsonNode() { }
|
public JsonToJsonNode() {
|
||||||
|
}
|
||||||
|
|
||||||
String jsonString = "{ \"color\" : \"Black\", \"type\" : \"BMW\" }";
|
String jsonString = "{ \"color\" : \"Black\", \"type\" : \"BMW\" }";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String name()
|
public String name() {
|
||||||
{
|
|
||||||
return this.getClass().getName();
|
return this.getClass().getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void execute()
|
public void execute() {
|
||||||
{
|
Logger.debug("Executing: " + name());
|
||||||
Logger.debug("Executing: "+name());
|
try {
|
||||||
try
|
|
||||||
{
|
|
||||||
final ObjectMapper objectMapper = new ObjectMapper();
|
final ObjectMapper objectMapper = new ObjectMapper();
|
||||||
final JsonNode jsonNode = objectMapper.readTree(jsonString);
|
final JsonNode jsonNode = objectMapper.readTree(jsonString);
|
||||||
Logger.debug(jsonNode.get("color").asText());
|
Logger.debug(jsonNode.get("color").asText());
|
||||||
}
|
} catch (final Exception e) {
|
||||||
catch (final Exception e)
|
|
||||||
{
|
|
||||||
Logger.error(e.toString());
|
Logger.error(e.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,8 @@ public class Car {
|
||||||
private String color;
|
private String color;
|
||||||
private String type;
|
private String type;
|
||||||
|
|
||||||
public Car() { }
|
public Car() {
|
||||||
|
}
|
||||||
|
|
||||||
public Car(final String color, final String type) {
|
public Car(final String color, final String type) {
|
||||||
this.color = color;
|
this.color = color;
|
||||||
|
|
Loading…
Reference in New Issue