1private Car parseCar(JsonNode node) {
2 Car car;
3 ObjectMapper mapper = new ObjectMapper();
4 SimpleModule module = new SimpleModule().addDeserializer(Car.class, new CarDeserializer());
5 mapper.registerModule(module);
6 organization = mapper.convertValue(node, Car.class);
7 return car;
8}
9
10//deserializer class
11public class CarDeserializer extends StdDeserializer<Car> {
12
13 public CarDeserializer() {
14 super(Car.class);
15 }
16
17 @Override
18 public Car deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
19 ObjectMapper objectMapper = new ObjectMapper();
20 objectMapper.registerModule(new JodaModule());
21
22 Car car = new Car();
23 car.setName(getValueAsText(root, "carName"));
24 car.setDoorCount(getValueAsInt(root,"doorCount"));
25 car.setColor(getValueAsText(root,"color"));
26 car.setType(getValueAsText(root,"type"));
27
28 return car;
29 }
30
31}
1package net.sghill.example;
2
3import net.sghill.example.User;
4import org.codehaus.jackson.JsonNode;
5import org.codehaus.jackson.JsonParser;
6import org.codehaus.jackson.ObjectCodec;
7import org.codehaus.jackson.map.DeserializationContext;
8import org.codehaus.jackson.map.JsonDeserializer;
9
10import java.io.IOException;
11
12public class UserDeserializer extends JsonDeserializer<User> {
13
14 @Override
15 public User deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
16 ObjectCodec oc = jsonParser.getCodec();
17 JsonNode node = oc.readTree(jsonParser);
18 return new User(null, node.get("username").getTextValue(), node.get("password").getTextValue());
19 }
20}