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 6864565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:55:39+00:00 2026-05-27T02:55:39+00:00

I have a Jackson Question. Is there a way to deserialize a property that

  • 0

I have a Jackson Question.

Is there a way to deserialize a property that may have two types, for some objects it appears like this

"someObj" : { "obj1" : 5, etc....}

then for others it appears as an empty array, i.e.

"someObj" : []

Any help is appreciated!

Thanks!

  • 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-27T02:55:40+00:00Added an answer on May 27, 2026 at 2:55 am

    Jackson doesn’t currently have a built-in configuration to automatically handle this particular case, so custom deserialization processing is necessary.

    Following is an example of what such custom deserialization might look like.

    import java.io.IOException;
    
    import org.codehaus.jackson.JsonNode;
    import org.codehaus.jackson.JsonParser;
    import org.codehaus.jackson.JsonProcessingException;
    import org.codehaus.jackson.Version;
    import org.codehaus.jackson.annotate.JsonAutoDetect.Visibility;
    import org.codehaus.jackson.annotate.JsonMethod;
    import org.codehaus.jackson.map.DeserializationContext;
    import org.codehaus.jackson.map.JsonDeserializer;
    import org.codehaus.jackson.map.ObjectMapper;
    import org.codehaus.jackson.map.module.SimpleModule;
    
    public class JacksonFoo
    {
      public static void main(String[] args) throws Exception
      {
        // {"property1":{"property2":42}}
        String json1 = "{\"property1\":{\"property2\":42}}";
    
        // {"property1":[]}
        String json2 = "{\"property1\":[]}";
    
        SimpleModule module = new SimpleModule("", Version.unknownVersion());
        module.addDeserializer(Thing2.class, new ArrayAsNullDeserializer());
    
        ObjectMapper mapper = new ObjectMapper().setVisibility(JsonMethod.FIELD, Visibility.ANY).withModule(module);
    
        Thing1 firstThing = mapper.readValue(json1, Thing1.class);
        System.out.println(firstThing);
        // output:
        // Thing1: property1=Thing2: property2=42
    
        Thing1 secondThing = mapper.readValue(json2, Thing1.class);
        System.out.println(secondThing);
        // output: 
        // Thing1: property1=null
      }
    }
    
    class Thing1
    {
      Thing2 property1;
    
      @Override
      public String toString()
      {
        return String.format("Thing1: property1=%s", property1);
      }
    }
    
    class Thing2
    {
      int property2;
    
      @Override
      public String toString()
      {
        return String.format("Thing2: property2=%d", property2);
      }
    }
    
    class ArrayAsNullDeserializer extends JsonDeserializer<Thing2>
    {
      @Override
      public Thing2 deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException
      {
        JsonNode node = jp.readValueAsTree();
        if (node.isObject())
          return new ObjectMapper().setVisibility(JsonMethod.FIELD, Visibility.ANY).readValue(node, Thing2.class);
        return null;
      }
    }
    

    (You could make use of DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY to force the input to always bind to a collection, but that’s probably not the approach I’d take given how the problem is currently described.)

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

Sidebar

Related Questions

I have a problem when converting hibernate objects to JSON with Jackson, because some
I have question related to bean to json serialziation/deserialization using Jackson. Previously I have
I have successfully set up a quick test of creating a REST-like service that
I have some errors from Date and Long types and Overlay Types. Both uses
I have a question which is similar to some questions at stackoverflow but none
i have something like below : $array1 = array('first_name'=>'tom','last_name'=>'jackson','city'=>'london'); $array2 = array('last_name'=>'jackson','city'=>'london','first_name'=>'tom'); $array3 =
I have a class that needs to be deserialized from JSON using Jackson. The
I have a program that returns something like this: status: playing artURL: http://beta.grooveshark.com/static/amazonart/m3510922.jpg estimateDuration:
Have a SomeLib.pro file that contains: CONFIG += debug TEMPLATE = lib TARGET =
Have a matrix report now that has Position, Hours and Wages for a location

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.