Deserialize an Object of Another

Asked

Viewed 35 times

-1

I’m trying to deserialize three DTOS classes to export to a JSON file, I was able to do much of the process but when I try to export one class inside another I can’t.

follows the following scenarios:

public class TakeOffPoliciesDto {

private List<ExportTakeOffDto> exportTakeOffDto;

public List<ExportTakeOffDto> getExportTakeOffDto() {
    return exportTakeOffDto;
}

public void setExportTakeOffDto(List<ExportTakeOffDto> exportTakeOffDto) {
    this.exportTakeOffDto = exportTakeOffDto;
}

public class ExportTakeOffDto {


private List<FamilyTypeDto> familyTypeDtos;
private List<AirportPolicieDto> airportPolicieDtos;

public List<FamilyTypeDto> getFamilyTypeDtos() {
    return familyTypeDtos;
}

public void setFamilyTypeDtos(List<FamilyTypeDto> familyTypeDtos) {
    this.familyTypeDtos = familyTypeDtos;
}

public List<AirportPolicieDto> getAirportPolicieDtos() {
    return airportPolicieDtos;
}

public void setAirportPolicieDtos(List<AirportPolicieDto> airportPolicieDtos) {
    this.airportPolicieDtos = airportPolicieDtos;
}



private void exportToJSonFile() {

    ObjectMapper objectMapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);

    TakeOffPoliciesDto takeOffPoliciesDto = new TakeOffPoliciesDto();

    takeOffPoliciesDto.setExportTakeOffDto(Arrays.asList("1111", "9998887654", "1234567890"));

    ExportTakeOffDto exportDto = new ExportTakeOffDto();

    exportDto.setFamilyTypeDtos( Arrays.asList("8095185442", "9998887654", "1234567890"));
    exportDto.setAirportPolicieDtos( Arrays.asList("8095185442", "9998887654", "1234567890"));

    try {
    objectMapper.writeValue(new File(file.getAbsolutePath(), "teste.json"), takeOffPoliciesDto);


    } catch (JsonGenerationException e) {
        e.printStackTrace();
    } catch (JsonMappingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
  • What’s the matter?

  • When I try to deserialize one object within another it comes null ,

  • And where is the Json of deserialization?

  • I’m trying to get this values straight from the Bank, I have a mode class, and I created a specific DTO for them

  • And what is the bank’s data?

1 answer

0

I managed using Modelmapper a library that copies the data of my models and transfers to the DTOS, was like this :

     private void exportToJSonFile() {

      ObjectMapper objectMapper = new 
     ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);

    ExportAllDTo exportAllDTo = new ExportAllDTo();

    List<FamilyPoliciesEntity> familyPoliciesEntities = 
    familyPoliciesService.findAll();
    List<FamilyPolicieDto> familyPoliciesDtoList = new ArrayList<>();

    ModelMapper mapper = new ModelMapper();
  for (FamilyPoliciesEntity familyPoliciesEntity : familyPoliciesEntities) {

        try {

            FamilyPolicieDto familyPoliciesDto = mapper.map(familyPoliciesEntity, 
            FamilyPolicieDto.class);

            TakeoffConfigurationEntity takeoffConfigurationEntity = 
           takeoffConfigurationService
                    .recoverByPolicy(familyPoliciesEntity);

    try {

        exportAllDTo.setTakeoff(familyPoliciesDtoList);
        objectMapper.writeValue(new File(file.getAbsolutePath(), "teste.json"), 
        exportAllDTo);
        String jsonString = objectMapper.writeValueAsString(exportAllDTo);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.