How to deserialize Jackson Json NULL String today with JsonFormat

I watched a lot, but still have not received an answer, any help is really appreciated!

I have a simple mapping of Stringto fields Dateand try reading the JSON string for a Java object.

@JsonInclude(value=Include.NON_EMPTY)
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="dd-MMM-yyyy", timezone="PST")
protected Date eolAnnounceDate;

However, I get the following exception if the JSON string value is empty. Can you tell me how to get around this? I tried several options, but they are all intended for serialization.

ObjectMapper objectMapper = new ObjectMapper();   
objectMapper.setSerializationInclusion(Include.NON_NULL); 
objectMapper.setSerializationInclusion(Include.NON_EMPTY);

An exception:

java.lang.IllegalArgumentException: could not parse the date value "NULL" (format: "dd-MMM-yyyy"): unpaired date: "NULL" com.fasterxml.jackson.databind.deser.std.DateDeserializers $ DateBasedDeserializer._parseDate ( DateDeserializers.java:180) com.fasterxml.jackson.databind.deser.std.DateDeserializers $ DateDeserializer.deserialize (DateDeserializers.java:279) com.fasterxml.jackson.databind.deser.std.DateDeserializers $ DateDeserializer.deserialize. java: 260) com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize (SettableBeanProperty.java:464) com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet (MethodProperty.java:98) com.fasterx jackson.databind.deser.BeanDeserializer.deserializeFromObject (BeanDeserializer.java:295) com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize (BeanDeserializer.java:121) com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize (CollectionDeserializer.java:230) com.fasterxml.jackson.databind. std.CollectionDeserializer.deserialize (CollectionDeserializer.java:207) com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize (CollectionDeserializer.java:23) com.fasterxml.jackson.databind.izeer.SettableBeanProperty.erial java: 464) com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet (MethodProperty.java:98) com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject (BeanDeserializer.java:ml5 com. jackson.databind.deser.BeanDeserializer.deserialize (BeanDeserializer.java:121) com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose (ObjectMapper.java:2888) com.fasterxml.jackson.databind.ObjectMapper.readValue (ObjectMapper.java:2034) com.cisco.cre.dao.impl.ElasticsearchDAOImpl.getListListlistlter 94)

- Atul

0
1

, JSON . , JSON , "NULL".

, , . , .

1: JSON

- JSON, "NULL" null ( ) .

POJO:

public class DatePojo {
    @JsonInclude(value= JsonInclude.Include.NON_EMPTY)
    @JsonFormat(shape=JsonFormat.Shape.STRING, pattern="dd-MMM-yyyy", timezone="PST")
    @JsonProperty("date")
    private Date date;
}

, , null :

@Test
public void testJson() throws IOException {
    String jsonWithValidDate = "{\"date\":\"12-Jun-1982\"}";
    String jsonWithNoDate = "{}";
    String jsonWithNullDate = "{\"date\":null}";

    ObjectMapper mapper = new ObjectMapper();
    final DatePojo pojoWithValidDate = mapper.readValue(jsonWithValidDate, DatePojo.class);
    final DatePojo pojoWithNoDate = mapper.readValue(jsonWithNoDate, DatePojo.class);
    final DatePojo pojoWithNullDate = mapper.readValue(jsonWithNullDate, DatePojo.class);

    Assert.assertNotNull(pojoWithValidDate.date);
    Assert.assertNull(pojoWithNoDate.date);
    Assert.assertNull(pojoWithNullDate.date);
}

, "NULL", , "NULL" :

@Test(expected = JsonMappingException.class)
public void testInvalidJson() throws IOException {
    String jsonWithNullString = "{\"date\":\"NULL\"}";

    ObjectMapper mapper = new ObjectMapper();
    mapper.readValue(jsonWithNullString, DatePojo.class); // Throws the exception
    Assert.fail();
}

2: , "NULL"

JSON ( 1), .

pojo :

public class DatePojo {
    @JsonProperty("date")
    @JsonDeserialize(converter = MyDateConverter.class)
    private Date date;
}

:

public class MyDateConverter extends StdConverter<String, Date> {
    @Override
    public Date convert(final String value) {
        if (value == null || value.equals("NULL")) {
            return null;
        }
        try {
            return new SimpleDateFormat("dd-MMM-yyyy").parse(value);
        } catch (ParseException e) {
            throw new IllegalStateException("Unable to parse date", e);
        }
    }
}

. :

@Test
public void testJson() throws IOException {
    String jsonWithValidDate = "{\"date\":\"12-Jun-1982\"}";
    String jsonWithNoDate = "{}";
    String jsonWithNullDate = "{\"date\":null}";
    String jsonWithNullString = "{\"date\":\"NULL\"}"; // "NULL"

    ObjectMapper mapper = new ObjectMapper();
    final DatePojo pojoWithValidDate = mapper.readValue(jsonWithValidDate, DatePojo.class);
    final DatePojo pojoWithNoDate = mapper.readValue(jsonWithNoDate, DatePojo.class);
    final DatePojo pojoWithNullDate = mapper.readValue(jsonWithNullDate, DatePojo.class);
    final DatePojo pojoWithNullStr = mapper.readValue(jsonWithNullString, DatePojo.class); // Works

    Assert.assertNotNull(pojoWithValidDate.date);
    Assert.assertNull(pojoWithNoDate.date);
    Assert.assertNull(pojoWithNullDate.date);
    Assert.assertNull(pojoWithNullStr.date); // Works
}

IMO, - 1, JSON.

+10

All Articles