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.