0

So a very basic example of what I'm trying to do: I have a Parent class and a Child class. When deserializing the object, I want to have the option on how to deserialize it, either passing in the primitive long value or the actual object itself. Is there a way to do this? Right now, passing the child object as a long value works. However, I can't pass it in as an object. When I do so, it gives me an error saying that it can not deserialize instance of long out of START_OBJECT token.

Checked the error out and a lot of the answers seems to suggest that there's an issue with the object itself. Or implementing custom serialization (which seems overly complicated to me at the moment).

Can not deserialize instance of java.lang.String out of START_OBJECT token

JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token 1

However, when I comment out the setChild function that takes in a long value, it works fine. So it got me wondering if it's even possible to deserialize the object in multiple ways. Some additional information just in case it's helpful: I'm using the Jackson library and I'm passing the object through REST.

public class Parent {
    private long parentId;
    private Child childObj;

    public void setChild(Child childObj) {
        this.childObj = childObj;
    }

    public void setChild(long childId) {
        this.childObj = getChildObj(childId);
    }
}

public class Child {
    private long childId;
}

Just a note: if I'm just passing the Child object itself (without the Parent object), passing the entire object is fine.

Karen
  • 1
  • Please show the JSON strings which work and don't work, and the full stack trace of the error. – tgdavies Dec 12 '20 at 04:30
  • I was only using an example so I don't have the actual JSON string. It works when the setter for the ID is removed so it's not the JSON string that's causing the error. And this is what I get from the ajax call. "Can not deserialize instance of long out of START_OBJECT token at [Source: org.glassfish.jersey.message.internal.EntityInputStream@50870a5b; line: 1, column: 1765] (through reference chain: ..." – Karen Dec 13 '20 at 23:53
  • Can you create an example which illustrates the problem and show us the JSON? – tgdavies Dec 14 '20 at 21:15

1 Answers1

0

Try to use com.fasterxml.jackson.annotation.JsonCreator annotation in Child class:

class Child {

    private long childId;

    @JsonCreator
    public Child() {
    }

    @JsonCreator
    public Child(long childId) {
        this.childId = childId;
    }

    //getters, setters
}

Child() will be used in case of JSON Object and Child(long childId) constructor will be used in case of NUMBER in JSON.

Michał Ziober
  • 37,175
  • 18
  • 99
  • 146
  • I tried it and got the same error. I think what's happening is that the program is somehow getting confused when deserializing the object, and for some reason, using the wrong setter function. Which would explain why using @JsonCreator doesn't work. Because it's still trying to use the setter that takes in a long instead of an object. – Karen Dec 14 '20 at 00:53
  • @Karen, you should not use setter method with `long` parameter. Try to remove it and test deserialisation process only with annotated constructors. – Michał Ziober Dec 14 '20 at 12:16
  • Is there a reason why I can't use a setter method with a long parameter? If I remove it, then passing in an object will work. I won't be able to pass in a long though. – Karen Dec 16 '20 at 21:25
  • @Karen, try to ignore this extra setter with `@JsonIgnore` annotation. You could use it in your code and it will not break a deserialisation process. – Michał Ziober Dec 16 '20 at 23:18
  • 1
    Tried it and it didn't work. Actually, my question ended up being answered here: https://stackoverflow.com/questions/51538990/deserializing-to-object-with-overloaded-setter-with-fasterxml-jackson – Karen Jan 21 '21 at 17:34