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