title: Jackson Deserialize custom boolean json property
body: <p>I want to deserialize some boolean values that I get in a JSON. The problem is that those values can be null, true, false, "true", false, "Y" or "N".</p>
  
  <p>I've created my own boolean deserializer</p>
  
  <pre><code>public class CustomBooleanDeserializer extends JsonDeserializer&lt;Boolean&gt; {
  
      final protected Class&lt;?&gt; _valueClass = Boolean.class;
  
      @Override
      public Boolean deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException,
              JsonProcessingException {
          return _parseBooleanPrimitive2(jp, ctxt);
      }
  
      protected final boolean _parseBooleanPrimitive2(JsonParser jp, DeserializationContext ctxt)
              throws IOException, JsonProcessingException {
          LogUtils.d("PARSE BOOLEAN");
          JsonToken t = jp.getCurrentToken();
          if (t == JsonToken.VALUE_TRUE) {
              return true;
          }
          if (t == JsonToken.VALUE_FALSE) {
              return false;
          }
          if (t == JsonToken.VALUE_NULL) {
              return false;
          }
          if (t == JsonToken.VALUE_NUMBER_INT) {
              return (jp.getIntValue() != 0);
          }
          if (t == JsonToken.VALUE_STRING) {
              String text = jp.getText().trim();
              if ("true".equals(text)) {
                  return true;
              }
              if ("false".equals(text) || text.length() == 0) {
                  return Boolean.FALSE;
              }
  
              if ("N".equalsIgnoreCase(text) || text.length() == 0) {
                  return Boolean.FALSE;
              }
  
              if ("Y".equalsIgnoreCase(text)) {
                  return Boolean.TRUE;
              }
              throw ctxt.weirdStringException(_valueClass, "only \"true\" or \"false\" recognized");
          }
          // Otherwise, no can do:
          throw ctxt.mappingException(_valueClass);
      }
  </code></pre>
  
  <p>However, this deserializer is never called if I register it as this:</p>
  
  <pre><code>Version version = new Version(1, 0, 0, "SNAPSHOT");
  SimpleModule module = new SimpleModule("MyModuleName", version);
  module = module.addDeserializer(new CustomBooleanDeserializer());
  objectMapper.registerModule(module);
  </code></pre>
  
  <p>If, on the other hand, I use <code>@JsonDeserialize(using = CustomBooleanDeserializer.class)</code> for the boolean fields, it does get called and works great. The only problem is that if the property is null, I get this exception:</p>
  
  <blockquote>
    <p>org.codehaus.jackson.map.JsonMappingException: Problem deserializing
    property 'show_query_cond' (expected type: [simple type, class
    boolean]; actual type: [NULL]), problem: invalid value for field
    (through reference chain: com.csf.model.CSTable["show_query_cond"])</p>
  </blockquote>
  
  <p>So, if the boolean property is null, my deserializer doesn't get a chance to run. Also, I tried using <code>mapper.configure(DeserializationConfig.Feature.FAIL_ON_NULL_FOR_PRIMITIVES, false);</code> but the exception is still thrown if I use the @JsonDeserialize annotation.</p>
  
  <p>Does anyone know how to make this work?</p>