@@ -2,8 +2,8 @@ package com.fasterxml.jackson.module.scala.deser
2
2
3
3
import com .fasterxml .jackson .annotation .JsonMerge
4
4
import com .fasterxml .jackson .core .`type` .TypeReference
5
- import com .fasterxml .jackson .databind .ObjectMapper
6
- import com .fasterxml .jackson .module .scala .{DefaultScalaModule , ScalaObjectMapper }
5
+ import com .fasterxml .jackson .databind .{ ObjectMapper , ObjectReader }
6
+ import com .fasterxml .jackson .module .scala .{DefaultScalaModule }
7
7
import org .junit .runner .RunWith
8
8
import org .scalatestplus .junit .JUnitRunner
9
9
@@ -20,13 +20,13 @@ class MergeTest extends DeserializerTest {
20
20
21
21
val module : DefaultScalaModule .type = DefaultScalaModule
22
22
23
- def newScalaMapper : ObjectMapper with ScalaObjectMapper = {
24
- val mapper = new ObjectMapper with ScalaObjectMapper
23
+ def newScalaMapper : ObjectMapper = {
24
+ val mapper = new ObjectMapper
25
25
mapper.registerModule(module)
26
26
mapper
27
27
}
28
28
29
- def newMergeableScalaMapper : ObjectMapper with ScalaObjectMapper = {
29
+ def newMergeableScalaMapper : ObjectMapper = {
30
30
val mapper = newScalaMapper
31
31
mapper.setDefaultMergeable(true )
32
32
mapper
@@ -35,57 +35,65 @@ class MergeTest extends DeserializerTest {
35
35
behavior of " The DefaultScalaModule when reading for updating"
36
36
37
37
it should " merge both lists" in {
38
- val initial = deserialize(classJson(firstListJson), classOf [ClassWithLists ])
39
- val result = newMergeableScalaMapper.updateValue(initial, classJson(secondListJson))
38
+ val typeReference = new TypeReference [ClassWithLists ] {}
39
+ val initial = deserialize(classJson(firstListJson), typeReference)
40
+ val result = updateValue(newMergeableScalaMapper, initial, typeReference, classJson(secondListJson))
40
41
41
42
result shouldBe ClassWithLists (mergedList, mergedList)
42
43
}
43
44
44
45
it should " merge only the annotated list" in {
45
- val initial = deserialize(classJson(firstListJson), classOf [ClassWithLists ])
46
- val result = newScalaMapper.updateValue(initial, classJson(secondListJson))
46
+ val typeReference = new TypeReference [ClassWithLists ] {}
47
+ val initial = deserialize(classJson(firstListJson), typeReference)
48
+ val result = updateValue(newScalaMapper, initial, typeReference, classJson(secondListJson))
47
49
48
50
result shouldBe ClassWithLists (secondList, mergedList)
49
51
}
50
52
51
53
it should " merge both string maps" in {
52
- val initial = deserialize(classJson(firstStringMapJson), classOf [ClassWithMaps [String ]])
53
- val result = newMergeableScalaMapper.updateValue(initial, classJson(secondStringMapJson))
54
+ val typeReference = new TypeReference [ClassWithMaps [String ]] {}
55
+ val initial = deserialize(classJson(firstStringMapJson), typeReference)
56
+ val result = updateValue(newMergeableScalaMapper, initial, typeReference, classJson(secondStringMapJson))
54
57
55
58
result shouldBe ClassWithMaps (mergedStringMap, mergedStringMap)
56
59
}
57
60
58
61
it should " merge only the annotated string map" in {
59
- val initial = deserialize(classJson(firstStringMapJson), classOf [ClassWithMaps [String ]])
60
- val result = newScalaMapper.updateValue(initial, classJson(secondStringMapJson))
62
+ val typeReference = new TypeReference [ClassWithMaps [String ]] {}
63
+ val initial = deserialize(classJson(firstStringMapJson), typeReference)
64
+ val result = updateValue(newScalaMapper, initial, typeReference, classJson(secondStringMapJson))
61
65
62
66
result shouldBe ClassWithMaps (secondStringMap, mergedStringMap)
63
67
}
64
68
65
69
it should " merge both pair maps" in {
66
- val initial = deserialize(classJson(firstPairMapJson), new TypeReference [ClassWithMaps [Pair ]]{})
67
- val result = newMergeableScalaMapper.updateValue(initial, classJson(secondPairMapJson))
70
+ val typeReference = new TypeReference [ClassWithMaps [Pair ]] {}
71
+ val initial = deserialize(classJson(firstPairMapJson), typeReference)
72
+ val result = updateValue(newMergeableScalaMapper, initial, typeReference, classJson(secondPairMapJson))
68
73
69
74
result shouldBe ClassWithMaps (mergedPairMap, mergedPairMap)
70
75
}
71
76
72
77
it should " merge only the annotated pair map" in {
73
- val initial = deserialize(classJson(firstPairMapJson), new TypeReference [ClassWithMaps [Pair ]]{})
74
- val result = newScalaMapper.updateValue(initial, classJson(secondPairMapJson))
78
+ val typeReference = new TypeReference [ClassWithMaps [Pair ]]{}
79
+ val initial = deserialize(classJson(firstPairMapJson), typeReference)
80
+ val result = updateValue(newScalaMapper, initial, typeReference, classJson(secondPairMapJson))
75
81
76
82
result shouldBe ClassWithMaps (secondPairMap, mergedPairMap)
77
83
}
78
84
79
85
it should " merge both mutable maps" in {
80
- val initial = deserialize(classJson(firstStringMapJson), classOf [ClassWithMutableMaps [String ]])
81
- val result = newMergeableScalaMapper.updateValue(initial, classJson(secondStringMapJson))
86
+ val typeReference = new TypeReference [ClassWithMutableMaps [String ]]{}
87
+ val initial = deserialize(classJson(firstStringMapJson), typeReference)
88
+ val result = updateValue(newMergeableScalaMapper, initial, typeReference, classJson(secondStringMapJson))
82
89
83
90
result shouldBe ClassWithMutableMaps (mutable.Map () ++ mergedStringMap, mutable.Map () ++ mergedStringMap)
84
91
}
85
92
86
93
it should " merge only the annotated mutable map" in {
87
- val initial = deserialize(classJson(firstStringMapJson), classOf [ClassWithMutableMaps [String ]])
88
- val result = newScalaMapper.updateValue(initial, classJson(secondStringMapJson))
94
+ val typeReference = new TypeReference [ClassWithMutableMaps [String ]]{}
95
+ val initial = deserialize(classJson(firstStringMapJson), typeReference)
96
+ val result = updateValue(newScalaMapper, initial, typeReference, classJson(secondStringMapJson))
89
97
90
98
result shouldBe ClassWithMutableMaps (mutable.Map () ++ secondStringMap, mutable.Map () ++ mergedStringMap)
91
99
}
@@ -106,4 +114,14 @@ class MergeTest extends DeserializerTest {
106
114
val secondPairMapJson = """ {"two":{"first":"22"},"three":{"second":"33"}}"""
107
115
val secondPairMap = Map (" two" -> Pair (" 22" , null ), " three" -> Pair (null , " 33" ))
108
116
val mergedPairMap = Map (" one" -> Pair (" 1" , null ), " two" -> Pair (" 22" , " 2" ), " three" -> Pair (" 3" , " 33" ))
117
+
118
+ private def updateValue [T ](mapper : ObjectMapper , valueToUpdate : T ,
119
+ typeReference : TypeReference [T ], src : String ): T = {
120
+ objectReaderFor(mapper, valueToUpdate, typeReference).readValue(src)
121
+ }
122
+
123
+ private def objectReaderFor [T ](mapper : ObjectMapper , valueToUpdate : T ,
124
+ typeReference : TypeReference [T ]): ObjectReader = {
125
+ mapper.readerForUpdating(valueToUpdate).forType(typeReference)
126
+ }
109
127
}
0 commit comments