Deserializing XML to Object using Jackson mapping Y/N to true/false boolean

I need to map elements in an xml that are listed as Y/N to a boolean (true/false).

One of the elements in the xml is:

<parent_node>
  <due_override_flag>N</due_override_flag>
  ...more elements
</parent_node>

I need to map the ‘N’/’Y’ to a boolean false/true This is the class I want to element to be mapped to:

@JsonRootName("trailer_standard_loads")
@JsonIgnoreProperties(ignoreUnknown=true)
public class StandardLoad {
    @JsonProperty("trailer_load_seq")
    private Integer trailerLoadSeq;
    private String createdBy;
    private ZonedDateTime createdDt;
    private String updatedBy;
    private ZonedDateTime updatedDt;
    @JsonProperty("due_override_flag")
    private Boolean dueOverrideFlag;
}

I have standard getter and setters. I don’t understand from the documentation how to map Y = true; N = false;

Answer

Add that logic in setter method

@JsonRootName("trailer_standard_loads")
@JsonIgnoreProperties(ignoreUnknown=true)     
  public class StandardLoad {
  @JsonProperty("trailer_load_seq")
  private Integer trailerLoadSeq;
  private String createdBy;
  private ZonedDateTime createdDt;
  private String updatedBy;
  private ZonedDateTime updatedDt;

  private Boolean dueOverrideFlag;

    @JsonProperty("due_override_flag")
    public void setDueOverrideFlag(String value)  {

     this.dueOverrideFlag = value.equalsIgnoreCase("Y) ? true : false;

      }

     public Boolean getDueOverrideFlag() {

      return this.dueOverrideFlag;

      }
  }

Leave a Reply

Your email address will not be published. Required fields are marked *