@@ -26,7 +26,13 @@ class TestJacksonWithKotlin {
26
26
val wrongName: Boolean
27
27
val createdDt: Date
28
28
29
- fun validate (nameField : String = name, ageField : Int = age, addressField : String = primaryAddress, wrongNameField : Boolean = wrongName, createDtField : Date = createdDt) {
29
+ fun validate (
30
+ nameField : String = name,
31
+ ageField : Int = age,
32
+ addressField : String = primaryAddress,
33
+ wrongNameField : Boolean = wrongName,
34
+ createDtField : Date = createdDt
35
+ ) {
30
36
assertThat(nameField, equalTo(" Frank" ))
31
37
assertThat(ageField, equalTo(30 ))
32
38
assertThat(addressField, equalTo(" something here" ))
@@ -50,7 +56,10 @@ class TestJacksonWithKotlin {
50
56
51
57
// ==================
52
58
53
- private class DefaultAndSpecificConstructor (override var name : String = " " , override var age : Int = 0 ) : TestFields {
59
+ private class DefaultAndSpecificConstructor (
60
+ override var name : String = " " ,
61
+ override var age : Int = 0
62
+ ) : TestFields {
54
63
@JsonProperty(" renamed" )
55
64
override var wrongName: Boolean = false
56
65
@@ -65,7 +74,13 @@ class TestJacksonWithKotlin {
65
74
66
75
// ==================
67
76
68
- private class NoFailWithoutJsonCreator (override val name : String , override val age : Int , override val primaryAddress : String , val renamed : Boolean , override val createdDt : Date ) : TestFields {
77
+ private class NoFailWithoutJsonCreator (
78
+ override val name : String ,
79
+ override val age : Int ,
80
+ override val primaryAddress : String ,
81
+ val renamed : Boolean ,
82
+ override val createdDt : Date
83
+ ) : TestFields {
69
84
@JsonIgnore
70
85
override val wrongName = renamed // here for the test validation only
71
86
}
@@ -77,7 +92,13 @@ class TestJacksonWithKotlin {
77
92
78
93
// ==================
79
94
80
- private data class StateObjectAsDataClassExplicitJsonCreator @JsonCreator constructor(override val name : String , override val age : Int , override val primaryAddress : String , val renamed : Boolean , override val createdDt : Date ) : TestFields {
95
+ private data class StateObjectAsDataClassExplicitJsonCreator @JsonCreator constructor(
96
+ override val name : String ,
97
+ override val age : Int ,
98
+ override val primaryAddress : String ,
99
+ val renamed : Boolean ,
100
+ override val createdDt : Date
101
+ ) : TestFields {
81
102
@JsonIgnore
82
103
override val wrongName = renamed // here for the test validation only
83
104
}
@@ -95,11 +116,18 @@ class TestJacksonWithKotlin {
95
116
96
117
// ==================
97
118
98
- private data class StateObjectAsDataClassWithJsonCreatorAndJsonProperty @JsonCreator constructor(override val name : String , override val age : Int , override val primaryAddress : String , @JsonProperty(" renamed" ) override val wrongName : Boolean , override val createdDt : Date ) : TestFields
119
+ private data class StateObjectAsDataClassWithJsonCreatorAndJsonProperty @JsonCreator constructor(
120
+ override val name : String ,
121
+ override val age : Int ,
122
+ override val primaryAddress : String ,
123
+ @JsonProperty(" renamed" ) override val wrongName : Boolean ,
124
+ override val createdDt : Date
125
+ ) : TestFields
99
126
100
127
@Test fun testDataClassWithExplicitJsonCreatorAndJsonProperty () {
101
128
// data class with JsonCreator and JsonProperty
102
- val stateObj = normalCasedMapper.readValue<StateObjectAsDataClassWithJsonCreatorAndJsonProperty >(normalCasedJson)
129
+ val stateObj = normalCasedMapper
130
+ .readValue<StateObjectAsDataClassWithJsonCreatorAndJsonProperty >(normalCasedJson)
103
131
stateObj.validate()
104
132
105
133
val test1out = normalCasedMapper.writeValueAsString(stateObj)
@@ -108,7 +136,13 @@ class TestJacksonWithKotlin {
108
136
109
137
// ==================
110
138
111
- private class StateObjectAsNormalClass @JsonCreator constructor(override val name : String , override val age : Int , override val primaryAddress : String , @JsonProperty(" renamed" ) override val wrongName : Boolean , override val createdDt : Date ) : TestFields
139
+ private class StateObjectAsNormalClass @JsonCreator constructor(
140
+ override val name : String ,
141
+ override val age : Int ,
142
+ override val primaryAddress : String ,
143
+ @JsonProperty(" renamed" ) override val wrongName : Boolean ,
144
+ override val createdDt : Date
145
+ ) : TestFields
112
146
113
147
@Test fun testNormalClassWithJsonCreator () {
114
148
// normal class
@@ -118,7 +152,11 @@ class TestJacksonWithKotlin {
118
152
119
153
// ==================
120
154
121
- private class StateObjectWithPartialFieldsInConstructor (override val name : String , override val age : Int , override val primaryAddress : String ) : TestFields {
155
+ private class StateObjectWithPartialFieldsInConstructor (
156
+ override val name : String ,
157
+ override val age : Int ,
158
+ override val primaryAddress : String
159
+ ) : TestFields {
122
160
@JsonProperty(" renamed" ) override var wrongName: Boolean = false
123
161
override var createdDt: Date by Delegates .notNull()
124
162
}
@@ -131,13 +169,15 @@ class TestJacksonWithKotlin {
131
169
132
170
// ==================
133
171
134
- private class StateObjectAsDataClassConfusingConstructor constructor (@Suppress(" UNUSED_PARAMETER" ) nonField : String? ,
135
- override val name : String ,
136
- @Suppress(" UNUSED_PARAMETER" ) yearOfBirth : Int ,
137
- override val age : Int ,
138
- override val primaryAddress : String ,
139
- @JsonProperty(" renamed" ) override val wrongName : Boolean ,
140
- override val createdDt : Date ) : TestFields
172
+ private class StateObjectAsDataClassConfusingConstructor constructor(
173
+ @Suppress(" UNUSED_PARAMETER" ) nonField : String? ,
174
+ override val name : String ,
175
+ @Suppress(" UNUSED_PARAMETER" ) yearOfBirth : Int ,
176
+ override val age : Int ,
177
+ override val primaryAddress : String ,
178
+ @JsonProperty(" renamed" ) override val wrongName : Boolean ,
179
+ override val createdDt : Date
180
+ ) : TestFields
141
181
142
182
@Test fun testDataClassWithNonFieldParametersInConstructor () {
143
183
// data class with non fields appearing as parameters in constructor, this works but null values or defaults for primitive types are passed to
@@ -162,10 +202,22 @@ class TestJacksonWithKotlin {
162
202
163
203
// ==================
164
204
165
- private class StateObjectWithFactory private constructor (override val name : String , override val age : Int , override val primaryAddress : String , override val wrongName : Boolean , override val createdDt : Date ) : TestFields {
205
+ private class StateObjectWithFactory private constructor(
206
+ override val name : String ,
207
+ override val age : Int ,
208
+ override val primaryAddress : String ,
209
+ override val wrongName : Boolean ,
210
+ override val createdDt : Date
211
+ ) : TestFields {
166
212
var factoryUsed: Boolean = false
167
213
companion object {
168
- @JvmStatic @JsonCreator fun create (@JsonProperty(" name" ) nameThing : String , @JsonProperty(" age" ) age : Int , @JsonProperty(" primaryAddress" ) primaryAddress : String , @JsonProperty(" renamed" ) wrongName : Boolean , @JsonProperty(" createdDt" ) createdDt : Date ): StateObjectWithFactory {
214
+ @JvmStatic @JsonCreator fun create (
215
+ @JsonProperty(" name" ) nameThing : String ,
216
+ @JsonProperty(" age" ) age : Int ,
217
+ @JsonProperty(" primaryAddress" ) primaryAddress : String ,
218
+ @JsonProperty(" renamed" ) wrongName : Boolean ,
219
+ @JsonProperty(" createdDt" ) createdDt : Date
220
+ ): StateObjectWithFactory {
169
221
val obj = StateObjectWithFactory (nameThing, age, primaryAddress, wrongName, createdDt)
170
222
obj.factoryUsed = true
171
223
return obj
@@ -179,9 +231,21 @@ class TestJacksonWithKotlin {
179
231
assertThat(stateObj.factoryUsed, equalTo(true ))
180
232
}
181
233
182
- private class StateObjectWithFactoryNoParamAnnotations (val name : String , val age : Int , val primaryAddress : String , val renamed : Boolean , val createdDt : Date ) {
234
+ private class StateObjectWithFactoryNoParamAnnotations (
235
+ val name : String ,
236
+ val age : Int ,
237
+ val primaryAddress : String ,
238
+ val renamed : Boolean ,
239
+ val createdDt : Date
240
+ ) {
183
241
companion object {
184
- @JvmStatic @JsonCreator fun create (name : String , age : Int , primaryAddress : String , renamed : Boolean , createdDt : Date ): StateObjectWithFactoryNoParamAnnotations {
242
+ @JvmStatic @JsonCreator fun create (
243
+ name : String ,
244
+ age : Int ,
245
+ primaryAddress : String ,
246
+ renamed : Boolean ,
247
+ createdDt : Date
248
+ ): StateObjectWithFactoryNoParamAnnotations {
185
249
return StateObjectWithFactoryNoParamAnnotations (name, age, primaryAddress, renamed, createdDt)
186
250
}
187
251
}
@@ -197,10 +261,22 @@ class TestJacksonWithKotlin {
197
261
}
198
262
}
199
263
200
- private class StateObjectWithFactoryOnNamedCompanion private constructor (override val name : String , override val age : Int , override val primaryAddress : String , override val wrongName : Boolean , override val createdDt : Date ) : TestFields {
264
+ private class StateObjectWithFactoryOnNamedCompanion private constructor(
265
+ override val name : String ,
266
+ override val age : Int ,
267
+ override val primaryAddress : String ,
268
+ override val wrongName : Boolean ,
269
+ override val createdDt : Date
270
+ ) : TestFields {
201
271
var factoryUsed: Boolean = false
202
- companion object Named {
203
- @JvmStatic @JsonCreator fun create (@JsonProperty(" name" ) nameThing : String , @JsonProperty(" age" ) age : Int , @JsonProperty(" primaryAddress" ) primaryAddress : String , @JsonProperty(" renamed" ) wrongName : Boolean , @JsonProperty(" createdDt" ) createdDt : Date ): StateObjectWithFactoryOnNamedCompanion {
272
+ private companion object Named {
273
+ @JvmStatic @JsonCreator fun create (
274
+ @JsonProperty(" name" ) nameThing : String ,
275
+ @JsonProperty(" age" ) age : Int ,
276
+ @JsonProperty(" primaryAddress" ) primaryAddress : String ,
277
+ @JsonProperty(" renamed" ) wrongName : Boolean ,
278
+ @JsonProperty(" createdDt" ) createdDt : Date
279
+ ): StateObjectWithFactoryOnNamedCompanion {
204
280
val obj = StateObjectWithFactoryOnNamedCompanion (nameThing, age, primaryAddress, wrongName, createdDt)
205
281
obj.factoryUsed = true
206
282
return obj
0 commit comments