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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:00:49+00:00 2026-06-13T17:00:49+00:00

I have the following json { "id":null, "name":"Myapp", "description":"application", "myListA":["java.util.ArrayList",[{ "id":50, "name":"nameA1", "myListB":{ "id":48,

  • 0

I have the following json

{
"id":null,
"name":"Myapp",
"description":"application",
"myListA":["java.util.ArrayList",[{
    "id":50,
    "name":"nameA1",
    "myListB":{
        "id":48,
        "name":"nameB1",
        "myListC":["java.util.ArrayList",[{
            "id":1250,
            "name":"nameC1",
            "description":"nameC1_desc",
            "myReferenceObject":{
                "code":"someCodeA"
            }
        },{
            "id":1251,
            "name":"nameC2",
            "description":"nameC1_desc",
            "myReferenceObject":{
                "code":"someCodeB"
            }

and so on.

I i wish to replace myReferenceObject with an item from persistence layer.

I followed JacksonHowToCustomDeserializers

My deserializer is as follows:

public class MyReferenceObjectCodeDeserializer extends JsonDeserializer<MyReferenceObjectBean> {

    @Override
    public ColumnReferenceBean deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {

        while (jp.nextToken() != JsonToken.END_OBJECT) {
            String fieldname = jp.getCurrentName();
            jp.nextToken();
            if ("code".equalsIgnoreCase(fieldname)) {
                MyReferenceObjectBean b = MyReferenceObjectServiceImpl.retrieveByCode(jp.getText());
                logger.info("returning " +b.toString());
                return b;
            }
        }
        logger.info("returning null");
        return null;
    }
}

And I attache the module like so:

ObjectMapper mapper = new ObjectMapper();
    mapper.enableDefaultTyping();
    SimpleModule module = new SimpleModule("myModule", new Version(1, 0, 0, null));
    module.addDeserializer(MyReferenceObjectBean.class, new MyReferenceObjectCodeDeserializer());
    
    mapper.registerModule(module);
    try {
        return mapper.readValue(serializedJsonString, MyMainObjectBean.class);
    } catch (IOException e) {
        logger.error("Unable to parse=" + serializedJsonString, e);
    }

everything debugs correctly however the resulting myListC list has double the amount of objects with even numbers holding the correct objects along with the correct myReferenceObject out of persistence (deserialized correctly using my module) and the odd elements holding empty Pojos, that is an object with null values for all variables.

Through debug, It never reaches return null in my custom deserializer, for it works properly each time its executed. The issue seems to be further up stream where it inserts blank myListC objects.

Any help would greeatly be 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-06-13T17:00:50+00:00Added an answer on June 13, 2026 at 5:00 pm

    There is a logic problem in your code.
    You want to loop until you reach the end of the object but break your loop with return b (if block). This means that you will not read the object stream until its end.

    Try something like this (didn’t try it but should work).

    public class MyReferenceObjectCodeDeserializer extends JsonDeserializer<MyReferenceObjectBean> {
    
    @Override
    public ColumnReferenceBean deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        MyReferenceObjectBean b = null;
        while (jp.nextToken() != JsonToken.END_OBJECT) {
            String fieldname = jp.getCurrentName();
            jp.nextToken();
            if ("code".equalsIgnoreCase(fieldname)) {
              b = MyReferenceObjectServiceImpl.retrieveByCode(jp.getText());
              logger.info("returning " +b.toString());
            }
        }
        if (b==null) logger.info("returning null");
        return b;
    }
    }
    

    You can also have a look at Genson http://code.google.com/p/genson/ if you can change from jackson. In addition of some other features it is supposed to be easier to use. Here is how you can solve your problem with genson (for this example it is quite similar to jackson):

    public class MyReferenceObjectCodeDeserializer implements Deserializer<MyReferenceObjectBean> {
    
    public MyReferenceObjectBeandeserialize(ObjectReader reader, Context ctx) throws TransformationException, IOException {
            MyReferenceObjectBean b = null;
            reader.beginObject();
            while (reader.hasNext()) {
                reader.next();
                if ("code".equalsIgnoreCase(reader.name()))
                    b = MyReferenceObjectServiceImpl.retrieveByCode(reader.valueAsString());
            }
            reader.endObject();
            return b;
        }
    }
    
    // register it
    Genson genson = new Genson.Builder().withDeserializers(new MyReferenceObjectCodeDeserializer()).create();
    MyClass myClass = genson.deserialize(json, MyClass.class);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following JSON String { name:Product, properties: { id: { type:number, description:Product
I have following JSON object. {feed:[ {news: {adopted_from:null,user_id:null,description:this is test,id:2} }, {news: {adopted_from:null,user_id:null,description:like unlike
I have the following JSON string (from wikipedia http://en.wikipedia.org/wiki/JSON ) { name:Product, properties: {
I have the following json output when i call a sample webservice <string>[{Name:Ajay Singh,Company:Birlasoft
I have the following json : { "users": [{ "user": { "user_id" :"a11uhk22hsd3jbskj", "username"
I have the following JSON string coming from external input source: {value: "82363549923gnyh49c9djl239pjm01223", id:
I have the following JSON: { registration: { name: Vik Kumar, first_name: Vik, last_name:
I have the following javascript: $.ajax({ type: "POST", dataType: "json", url: "/Home/Submit", data: {
I have a JSON file which looks like the following: {"posts":[{"Latitude":"53.38246685","lontitude":"-6.41501535"}, {"Latitude":"53.4062787","lontitude":"-6.3767205"}]} and I
I have following JSON for sending request on server { name:Home, latitude:45.5037078163837, longitude:-122.622699737549, radius:240

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.