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