|
1 | 1 | package com.fasterxml.jackson.databind.convert;
|
2 | 2 |
|
| 3 | +import java.io.IOException; |
3 | 4 | import java.util.*;
|
4 | 5 |
|
5 | 6 | import com.fasterxml.jackson.annotation.JsonView;
|
6 |
| -import com.fasterxml.jackson.databind.BaseMapTest; |
7 |
| -import com.fasterxml.jackson.databind.ObjectMapper; |
| 7 | +import com.fasterxml.jackson.core.*; |
| 8 | +import com.fasterxml.jackson.databind.*; |
| 9 | +import com.fasterxml.jackson.databind.deser.std.StdDeserializer; |
| 10 | +import com.fasterxml.jackson.databind.module.SimpleModule; |
8 | 11 |
|
9 | 12 | import static org.junit.Assert.assertArrayEquals;
|
10 | 13 |
|
@@ -44,7 +47,40 @@ public class Updateable {
|
44 | 47 | @JsonView(TextView.class)
|
45 | 48 | public String str;
|
46 | 49 | }
|
47 |
| - |
| 50 | + |
| 51 | + // for [databind#744] |
| 52 | + static class DataA { |
| 53 | + public int i = 1; |
| 54 | + public int j = 2; |
| 55 | + |
| 56 | + } |
| 57 | + |
| 58 | + static class DataB { |
| 59 | + public DataA da = new DataA(); |
| 60 | + public int k = 3; |
| 61 | + } |
| 62 | + |
| 63 | + static class DataADeserializer extends StdDeserializer<DataA> { |
| 64 | + private static final long serialVersionUID = 1L; |
| 65 | + |
| 66 | + DataADeserializer() { |
| 67 | + super(DataA.class); |
| 68 | + } |
| 69 | + |
| 70 | + public DataA deserialize(JsonParser jp, DeserializationContext ctxt) |
| 71 | + throws JsonProcessingException, IOException { |
| 72 | + if (jp.getCurrentToken() != JsonToken.START_OBJECT) { |
| 73 | + throw ctxt.mappingException("Wrong current token, expected START_OBJECT, got: " |
| 74 | + +jp.getCurrentToken()); |
| 75 | + } |
| 76 | + /*JsonNode node =*/ jp.readValueAsTree(); |
| 77 | + |
| 78 | + DataA da = new DataA(); |
| 79 | + da.i = 5; |
| 80 | + return da; |
| 81 | + } |
| 82 | + } |
| 83 | + |
48 | 84 | /*
|
49 | 85 | /********************************************************
|
50 | 86 | /* Unit tests
|
@@ -146,4 +182,45 @@ public void testUpdatingWithViews() throws Exception
|
146 | 182 | assertEquals(100, bean.num);
|
147 | 183 | assertEquals("foobar", bean.str);
|
148 | 184 | }
|
| 185 | + |
| 186 | + // [databind#744] |
| 187 | + public void testIssue744() throws IOException |
| 188 | + { |
| 189 | + ObjectMapper mapper = new ObjectMapper(); |
| 190 | + SimpleModule module = new SimpleModule(); |
| 191 | + module.addDeserializer(DataA.class, new DataADeserializer()); |
| 192 | + mapper.registerModule(module); |
| 193 | + |
| 194 | + DataB db = new DataB(); |
| 195 | + db.da.i = 11; |
| 196 | + db.k = 13; |
| 197 | + String jsonBString = mapper.writeValueAsString(db); |
| 198 | + JsonNode jsonBNode = mapper.valueToTree(db); |
| 199 | + |
| 200 | + // create parent |
| 201 | + DataB dbNewViaString = mapper.readValue(jsonBString, DataB.class); |
| 202 | + assertEquals(5, dbNewViaString.da.i); |
| 203 | + assertEquals(13, dbNewViaString.k); |
| 204 | + |
| 205 | + DataB dbNewViaNode = mapper.treeToValue(jsonBNode, DataB.class); |
| 206 | + assertEquals(5, dbNewViaNode.da.i); |
| 207 | + assertEquals(13, dbNewViaNode.k); |
| 208 | + |
| 209 | + // update parent |
| 210 | + DataB dbUpdViaString = new DataB(); |
| 211 | + DataB dbUpdViaNode = new DataB(); |
| 212 | + |
| 213 | + assertEquals(1, dbUpdViaString.da.i); |
| 214 | + assertEquals(3, dbUpdViaString.k); |
| 215 | + mapper.readerForUpdating(dbUpdViaString).readValue(jsonBString); |
| 216 | + assertEquals(5, dbUpdViaString.da.i); |
| 217 | + assertEquals(13, dbUpdViaString.k); |
| 218 | + |
| 219 | + assertEquals(1, dbUpdViaNode.da.i); |
| 220 | + assertEquals(3, dbUpdViaNode.k); |
| 221 | + |
| 222 | + mapper.readerForUpdating(dbUpdViaNode).readValue(jsonBNode); |
| 223 | + assertEquals(5, dbUpdViaNode.da.i); |
| 224 | + assertEquals(13, dbUpdViaNode.k); |
| 225 | + } |
149 | 226 | }
|
0 commit comments