-1

I need to deserialize a Json response

response: {
 speed: 40,
 distance: 20,
 time: 3
}

into 3 different objects, that is

Class Speed implements Base{
  int speed;
}

Class Distance implements Base{
  int distance;
}

Class Time implements Base{
  int time;
}

This is what I have so far,

@jsondeserialize(using = customdeserializer.class)
Class Response implements Base{
  ...
}

Class customdeserializer extends JsonDeserializer<List<Base>> {

@Override
public List<Base> deserialize(...) {
 //read Jsonparser
 // construct objects for Speed, Time and Distance
  //return 
}
}

Class Context {

public List<Base> converter(..) {
   ObjectMappper mapper = new ObjectMapper()
   List<Base> params = objectMapper.readValue(jsonValue, new TypeReference<List<Response>>() { }); 
   return params;

}

}

ISSUE: My customserializer is never getting called. I verified this by having a debug point

I am using FasterXML Jackson

Any idea how to do this using custom deserializer.

Jhanvi
  • 5,069
  • 8
  • 32
  • 41

1 Answers1

0

response in your case should be like follwing

response: [
 speed: 40,
 distance: 20,
 time: 3
]

when you pass the JSON the way you passed(in {}), it will be considered as an Object, not list of Objects, in JSON to denote list you need to specify them in [] brackets

dev2d
  • 4,245
  • 3
  • 31
  • 54