|
1 | 1 | package com.baeldung.jackson.objectmapper; |
2 | 2 |
|
| 3 | +import static org.hamcrest.Matchers.containsString; |
| 4 | +import static org.hamcrest.Matchers.equalTo; |
| 5 | +import static org.junit.Assert.assertNotNull; |
| 6 | +import static org.junit.Assert.assertThat; |
| 7 | + |
3 | 8 | import org.slf4j.Logger; |
4 | 9 | import org.slf4j.LoggerFactory; |
5 | 10 |
|
|
8 | 13 | import com.fasterxml.jackson.databind.ObjectMapper; |
9 | 14 | import com.fasterxml.jackson.databind.module.SimpleModule; |
10 | 15 |
|
11 | | -public class JsonAdvancedCustomSerializeExample extends Example { |
| 16 | +public class JsonAdvancedCustomSerializeExample extends Example |
| 17 | +{ |
12 | 18 |
|
13 | 19 | protected final Logger Logger = LoggerFactory.getLogger(getClass()); |
14 | 20 |
|
15 | | - public JsonAdvancedCustomSerializeExample() { |
16 | | - } |
17 | | - |
18 | | - String json = "{ \"color\" : \"Black\", \"type\" : \"BMW\" }"; |
| 21 | + public JsonAdvancedCustomSerializeExample() { } |
19 | 22 |
|
20 | 23 | @Override |
21 | | - public String name() { |
| 24 | + public String name() |
| 25 | + { |
22 | 26 | return this.getClass().getName(); |
23 | 27 | } |
24 | 28 |
|
25 | 29 | @Override |
26 | | - public void execute() { |
27 | | - Logger.debug("Executing: " + name()); |
28 | | - try { |
29 | | - ObjectMapper mapper = new ObjectMapper(); |
30 | | - final SimpleModule module = new SimpleModule("CustomSerializer", new Version(1, 0, 0, null, null, null)); |
31 | | - module.addSerializer(Car.class, new CustomCarSerializer()); |
32 | | - mapper = new ObjectMapper(); |
33 | | - mapper.registerModule(module); |
34 | | - final Car car = new Car("yellow", "renault"); |
35 | | - final String carJson = mapper.writeValueAsString(car); |
36 | | - Logger.debug("car as json = " + carJson); |
37 | | - } catch (final Exception e) { |
38 | | - Logger.error(e.toString()); |
39 | | - } |
40 | | - try { |
41 | | - ObjectMapper mapper = new ObjectMapper(); |
42 | | - final SimpleModule module = new SimpleModule("CustomCarDeserializer", new Version(1, 0, 0, null, null, null)); |
43 | | - module.addDeserializer(Car.class, new CustomCarDeserializer()); |
44 | | - mapper = new ObjectMapper(); |
45 | | - mapper.registerModule(module); |
46 | | - final Car car = mapper.readValue(json, Car.class); |
47 | | - Logger.debug("car type = " + car.getType()); |
48 | | - Logger.debug("car color = " + car.getColor()); |
49 | | - } catch (final Exception e) { |
50 | | - Logger.error(e.toString()); |
51 | | - } |
| 30 | + public void testExample() throws Exception { |
| 31 | + final ObjectMapper mapper = new ObjectMapper(); |
| 32 | + final SimpleModule serializerModule = new SimpleModule("CustomSerializer", new Version(1, 0, 0, null, null, null)); |
| 33 | + serializerModule.addSerializer(Car.class, new CustomCarSerializer()); |
| 34 | + mapper.registerModule(serializerModule); |
| 35 | + final Car car = new Car("yellow", "renault"); |
| 36 | + final String carJson = mapper.writeValueAsString(car); |
| 37 | + assertThat(carJson, containsString("renault")); |
| 38 | + assertThat(carJson, containsString("model")); |
| 39 | + |
| 40 | + final SimpleModule deserializerModule = new SimpleModule("CustomCarDeserializer", new Version(1, 0, 0, null, null, null)); |
| 41 | + deserializerModule.addDeserializer(Car.class, new CustomCarDeserializer()); |
| 42 | + mapper.registerModule(deserializerModule); |
| 43 | + final Car carResult = mapper.readValue(EXAMPLE_JSON, Car.class); |
| 44 | + assertNotNull(carResult); |
| 45 | + assertThat(carResult.getColor(), equalTo("Black")); |
52 | 46 | } |
53 | 47 | } |
0 commit comments