-2

Please help to change date format.

**Source** json file

**Target** Employee Object

I need support for converting file json date to Object. from file

{
 "dateOfBirth": {
    "year": 1980,
    "month": "MAY",
    "monthValue": 5,
    "dayOfMonth": 4,
    "dayOfYear": 124,
    "dayOfWeek": "WEDNESDAY",
    "chronology": {
      "calendarType": "iso8601",
      "id": "ISO"
    },
    "era": "CE",
    "leapYear": false
  }
}

to Object

{"dateOfBirth": "1980-05-04"}

Object

public class Employee {   
    private LocalDate dateOfBirth;
    //setter
    //getter
}

Library "com.fasterxml.jackson.datatype:jackson-datatype-jsr310"

Date : java.time.LocalDate

My goal is to read json data from file and map it to object.

GoutamS
  • 3,535
  • 1
  • 21
  • 25
  • 1
    Could you have the producer of that JSON file create it a reasonable format such as `{"dateOfBirth": "1980-05-04"}`? For example by using the same library that you are yourself using? – Ole V.V. May 30 '22 at 14:06
  • @OleV.V. Those file has already been created. Not able to change. How to change? – GoutamS May 30 '22 at 14:07
  • Does this answer your question? [serialize/deserialize java 8 java.time with Jackson JSON mapper](https://stackoverflow.com/questions/27952472/serialize-deserialize-java-8-java-time-with-jackson-json-mapper) – Valerij Dobler May 30 '22 at 14:29
  • 1
    @ValerijDobler It does not, the date is not a string in some format, it's a json object and JavaTimeModule can't handle it. – Chaosfire May 30 '22 at 15:04

1 Answers1

2

You can use custom deserializer.

public class EmployeeBirthDateDeserializer extends StdDeserializer<LocalDate> {

  public EmployeeBirthDateDeserializer() {
    super(LocalDate.class);
  }

  @Override
  public LocalDate deserialize(JsonParser parser, DeserializationContext context) throws IOException {
    JsonNode root = parser.getCodec().readTree(parser);
    return LocalDate.of(root.get("year").asInt(), root.get("monthValue").asInt(), root.get("dayOfMonth").asInt());
  }
}

Then apply it only to dateOfBirth field in Employee using @JsonDeserialize:

public class Employee {

  @JsonDeserialize(using = EmployeeBirthDateDeserializer.class)
  private LocalDate dateOfBirth;

  //getters and setter
}

Test:

public class Temp {

  public static void main(String[] args) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    Employee employee = mapper.readValue(ClassLoader.getSystemResourceAsStream("strange-date.json"), Employee.class);
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    System.out.println(formatter.format(employee.getDateOfBirth()));
  }
}

Prints: 1980-05-04.

Chaosfire
  • 4,818
  • 4
  • 8
  • 23
  • 2
    There is no need for a custom deserializer, take look at this declarative solution [here](https://stackoverflow.com/a/27952529/7082956). And remember to register the module. – Valerij Dobler May 30 '22 at 14:30
  • 2
    @ValerijDobler The JavaTimeModule can't handle OP's custom date format, custom deserializer is the way in this case. And there is no point to complicate things with registration of module and deserializer, when this format is only used for `Employee` class. Annotation is much cleaner for this particular case. – Chaosfire May 30 '22 at 15:00
  • 1
    @ValerijDobler , your reference and question solution is not helping here. Try all , and last worked solution provied by @Chaosfire `https://stackoverflow.com/users/17795888/chaosfire` – GoutamS Jun 01 '22 at 17:44