Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7629601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:52:34+00:00 2026-05-31T05:52:34+00:00

I want to deserialize some boolean values that I get in a JSON. The

  • 0

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”.

I’ve created my own boolean deserializer

public class CustomBooleanDeserializer extends JsonDeserializer<Boolean> {

    final protected Class<?> _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);
    }

However, this deserializer is never called if I register it as this:

Version version = new Version(1, 0, 0, "SNAPSHOT");
SimpleModule module = new SimpleModule("MyModuleName", version);
module = module.addDeserializer(new CustomBooleanDeserializer());
objectMapper.registerModule(module);

If, on the other hand, I use @JsonDeserialize(using = CustomBooleanDeserializer.class) 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:

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”])

So, if the boolean property is null, my deserializer doesn’t get a chance to run. Also, I tried using mapper.configure(DeserializationConfig.Feature.FAIL_ON_NULL_FOR_PRIMITIVES, false); but the exception is still thrown if I use the @JsonDeserialize annotation.

Does anyone know how to make this work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T05:52:35+00:00Added an answer on May 31, 2026 at 5:52 am

    As to registration, this is probably due to Java having both primitive boolean and Object wrapper Boolean. So you need to registed it using both java.lang.Boolean and Boolean.TYPE — latter is the placeholder class for primitive type.

    Null handling is different issue: deserialization method is not called for them. However, there is method

    JsonDeserializer.getNullValue()
    

    that should be called; and for primitives you must then just return ‘Boolean.FALSE’, since you can not assign null to a primitive value (it is ok to return wrapped; it gets properly handled).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method to which I want to post some json data, that
I'm trying to use Jackson to deserialize some JSON originally created using Jackson. The
I want to write the code, that can deserialize the class even if the
I really want this so I can serialize / deserialize some complex data objects
I have some types that I want to serialize/deserialize and generate a UI based
I have all this JSON text that I want to deserialize (or something) into
I have several classes that I serialize/deserialize, each with a number of properties, some
I need to deserialize some JavaScript object represented in JSON to an appropriate C#
I have some difficulties with json deserialization using GSon and I hope somebody can
I want to deserialize xml into an object on the android platform.Can you tell

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.