Skip to content
Advertisement

Custom jackson deserializer only for nested class

I have a vehicle Java class defined like this:

JavaScript

TimeWindow is defined like this:

JavaScript

Now, the JSON I am getting, does not define latitude and longitude for the location (start and end); this information is encoded as just an array, see e.g.:

JavaScript

How can I write a custom deserializer for just Location (same problem with TimeWindow) in that case? If possible, I do not want to write a custom deserializer for the whole Vehicle class.

I tried this:

JavaScript
JavaScript

It seems to me, that I am getting the whole Vehicle passed into my deserialize method, not just the Location part. Am I doing something wrong here? Is this approach feasible using Jackson?

Advertisement

Answer

You can add a constructor to Vehicle class using @JsonCreator tag and a Double[] for parameters start and end. You also need to add @JsonProperty tag to each parameter.

Here’s an example, for simplicity I didn’t include parameters time_window and breaks.

JavaScript

Test using the json in the question without parameters time_window and breaks:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement