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

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Deserializing XML to Object using Jackson mapping Y/N to true/false boolean without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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;

      }
  }
We are here to answer your question about Deserializing XML to Object using Jackson mapping Y/N to true/false boolean - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji