Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert a Boolean to String using Dozer?

Tags:

dozer

I am new to Dozer and I am trying to map a String to a Boolean and vica versa. Can anyone tell me does Dozer support this or do I have to create a custom converter. The string will contain true or false so will map directly. Also I am using the Dozer API and not the XML config. Thanks for your help

like image 202
irishguy Avatar asked Feb 12 '26 20:02

irishguy


1 Answers

I don't think dozer supports this out of the box, you can use a custom converter to do this work for you. In fact the help page on custom converters uses this case as example:

public class NewDozerConverter extends DozerConverter<String, Boolean> {

  public NewDozerConverter() {
    super(String.class, Boolean.class);
  }

  public Boolean convertTo(String source, Boolean destination) {
    if ("true".equals(source)) {
      return Boolean.TRUE;
    } else if ("false".equals(source)) {
      return Boolean.FALSE;
    }
    throw new IllegalStateException("Unknown value!");
  }

  public String convertFrom(Boolean source, String destination) {
    if (Boolean.TRUE.equals(source)) {
      return "true";
    } else if (Boolean.FALSE.equals(source)) {
      return "false";
    }
    throw new IllegalStateException("Unknown value!");
  }

}  
like image 105
mark-cs Avatar answered Feb 17 '26 04:02

mark-cs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!