3

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

irishguy
  • 671
  • 1
  • 9
  • 24

1 Answers1

3

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!");
  }

}  
mark-cs
  • 4,677
  • 24
  • 32