@@ -3,6 +3,7 @@ package tools.jackson.module.kotlin
33import  tools.jackson.core.JsonParser 
44import  tools.jackson.core.TreeNode 
55import  tools.jackson.core.type.TypeReference 
6+ import  tools.jackson.databind.DatabindException 
67import  tools.jackson.databind.JsonNode 
78import  tools.jackson.databind.MappingIterator 
89import  tools.jackson.databind.ObjectMapper 
@@ -51,21 +52,131 @@ fun jacksonMapperBuilder(initializer: KotlinModule.Builder.() -> Unit = {}): Jso
5152
5253inline  fun  <reified  T > jacksonTypeRef (): TypeReference <T > =  object  :  TypeReference <T >() {}
5354
55+ /* *
56+  * It is public due to Kotlin restrictions, but should not be used externally. 
57+  */  
58+ inline  fun  <reified  T > Any?.checkTypeMismatch (): T  {
59+     //  Basically, this check assumes that T is non-null and the value is null.
60+     //  Since this can be caused by both input or ObjectMapper implementation errors,
61+     //  a more abstract DatabindException is thrown.
62+     if  (this  !is  T ) {
63+         val  nullability =  if  (null  is  T ) " ?"   else  " (non-null)" 
64+ 
65+         throw  DatabindException .from(
66+             null  as  JsonParser ? ,
67+             " Deserialized value did not match the specified type; "   + 
68+                     " specified ${T ::class .qualifiedName}${nullability}  but was ${this ?.let  { it::class .qualifiedName }} " 
69+         )
70+     }
71+     return  this 
72+ }
73+ 
74+ /* *
75+  * Shorthand for [ObjectMapper.readValue]. 
76+  * @throws DatabindException Especially if [T] is non-null and the value read is null. 
77+  *   Other cases where the read value is of a different type than [T] 
78+  *   due to an incorrect customization to [ObjectMapper]. 
79+  */  
5480inline  fun  <reified  T > ObjectMapper.readValue (jp :  JsonParser ): T  =  readValue(jp, jacksonTypeRef<T >())
55- inline  fun  <reified  T > ObjectMapper.readValues (jp :  JsonParser ): MappingIterator <T > =  readValues(jp, jacksonTypeRef<T >())
81+     .checkTypeMismatch()
82+ /* *
83+  * Shorthand for [ObjectMapper.readValues]. 
84+  * @throws DatabindException Especially if [T] is non-null and the value read is null. 
85+  *   Other cases where the read value is of a different type than [T] 
86+  *   due to an incorrect customization to [ObjectMapper]. 
87+  */  
88+ inline  fun  <reified  T > ObjectMapper.readValues (jp :  JsonParser ): MappingIterator <T > {
89+     val  values =  readValues(jp, jacksonTypeRef<T >())
90+ 
91+     return  object  :  MappingIterator <T >(values) {
92+         override  fun  nextValue (): T  =  super .nextValue().checkTypeMismatch()
93+     }
94+ }
5695
57- inline  fun  <reified  T > ObjectMapper.readValue (src :  File ): T  =  readValue(src, jacksonTypeRef<T >())
58- inline  fun  <reified  T > ObjectMapper.readValue (src :  URL ): T  =  readValue(src, jacksonTypeRef<T >())
96+ /* *
97+  * Shorthand for [ObjectMapper.readValue]. 
98+  * @throws DatabindException Especially if [T] is non-null and the value read is null. 
99+  *   Other cases where the read value is of a different type than [T] 
100+  *   due to an incorrect customization to [ObjectMapper]. 
101+  */  
102+ inline  fun  <reified  T > ObjectMapper.readValue (src :  File ): T  =  readValue(src, jacksonTypeRef<T >()).checkTypeMismatch()
103+ /* *
104+  * Shorthand for [ObjectMapper.readValue]. 
105+  * @throws DatabindException Especially if [T] is non-null and the value read is null. 
106+  *   Other cases where the read value is of a different type than [T] 
107+  *   due to an incorrect customization to [ObjectMapper]. 
108+  */  
109+ inline  fun  <reified  T > ObjectMapper.readValue (src :  URL ): T  =  readValue(src, jacksonTypeRef<T >()).checkTypeMismatch()
110+ /* *
111+  * Shorthand for [ObjectMapper.readValue]. 
112+  * @throws DatabindException Especially if [T] is non-null and the value read is null. 
113+  *   Other cases where the read value is of a different type than [T] 
114+  *   due to an incorrect customization to [ObjectMapper]. 
115+  */  
59116inline  fun  <reified  T > ObjectMapper.readValue (content :  String ): T  =  readValue(content, jacksonTypeRef<T >())
60- inline  fun  <reified  T > ObjectMapper.readValue (src :  Reader ): T  =  readValue(src, jacksonTypeRef<T >())
117+     .checkTypeMismatch()
118+ /* *
119+  * Shorthand for [ObjectMapper.readValue]. 
120+  * @throws DatabindException Especially if [T] is non-null and the value read is null. 
121+  *   Other cases where the read value is of a different type than [T] 
122+  *   due to an incorrect customization to [ObjectMapper]. 
123+  */  
124+ inline  fun  <reified  T > ObjectMapper.readValue (src :  Reader ): T  =  readValue(src, jacksonTypeRef<T >()).checkTypeMismatch()
125+ /* *
126+  * Shorthand for [ObjectMapper.readValue]. 
127+  * @throws DatabindException Especially if [T] is non-null and the value read is null. 
128+  *   Other cases where the read value is of a different type than [T] 
129+  *   due to an incorrect customization to [ObjectMapper]. 
130+  */  
61131inline  fun  <reified  T > ObjectMapper.readValue (src :  InputStream ): T  =  readValue(src, jacksonTypeRef<T >())
132+     .checkTypeMismatch()
133+ /* *
134+  * Shorthand for [ObjectMapper.readValue]. 
135+  * @throws DatabindException Especially if [T] is non-null and the value read is null. 
136+  *   Other cases where the read value is of a different type than [T] 
137+  *   due to an incorrect customization to [ObjectMapper]. 
138+  */  
62139inline  fun  <reified  T > ObjectMapper.readValue (src :  ByteArray ): T  =  readValue(src, jacksonTypeRef<T >())
63- 
140+     .checkTypeMismatch()
141+ 
142+ /* *
143+  * Shorthand for [ObjectMapper.readValue]. 
144+  * @throws DatabindException Especially if [T] is non-null and the value read is null. 
145+  *   Other cases where the read value is of a different type than [T] 
146+  *   due to an incorrect customization to [ObjectMapper]. 
147+  */  
64148inline  fun  <reified  T > ObjectMapper.treeToValue (n :  TreeNode ): T  =  readValue(this .treeAsTokens(n), jacksonTypeRef<T >())
149+     .checkTypeMismatch()
150+ /* *
151+  * Shorthand for [ObjectMapper.convertValue]. 
152+  * @throws DatabindException Especially if [T] is non-null and the value read is null. 
153+  *   Other cases where the read value is of a different type than [T] 
154+  *   due to an incorrect customization to [ObjectMapper]. 
155+  */  
65156inline  fun  <reified  T > ObjectMapper.convertValue (from :  Any? ): T  =  convertValue(from, jacksonTypeRef<T >())
66- 
67- inline  fun  <reified  T > ObjectReader.readValueTyped (jp :  JsonParser ): T  =  forType(jacksonTypeRef<T >()).readValue(jp)
68- inline  fun  <reified  T > ObjectReader.readValuesTyped (jp :  JsonParser ): Iterator <T > =  readValues(jp, jacksonTypeRef<T >())
157+     .checkTypeMismatch()
158+ 
159+ /* *
160+  * Shorthand for [ObjectReader.readValue]. 
161+  * @throws DatabindException Especially if [T] is non-null and the value read is null. 
162+  *   Other cases where the read value is of a different type than [T] 
163+  *   due to an incorrect customization to [ObjectReader]. 
164+  */  
165+ inline  fun  <reified  T > ObjectReader.readValueTyped (jp :  JsonParser ): T  =  forType(jacksonTypeRef<T >()).readValue<T >(jp)
166+     .checkTypeMismatch()
167+ /* *
168+  * Shorthand for [ObjectReader.readValues]. 
169+  * @throws DatabindException Especially if [T] is non-null and the value read is null. 
170+  *   Other cases where the read value is of a different type than [T] 
171+  *   due to an incorrect customization to [ObjectReader]. 
172+  */  
173+ inline  fun  <reified  T > ObjectReader.readValuesTyped (jp :  JsonParser ): Iterator <T > {
174+     val  values =  readValues(jp, jacksonTypeRef<T >())
175+ 
176+     return  object  :  Iterator <T > by values {
177+         override  fun  next (): T  =  values.next().checkTypeMismatch<T >()
178+     }
179+ }
69180inline  fun  <reified  T > ObjectReader.treeToValue (jp :  TreeNode ): T ?  =  forType(jacksonTypeRef<T >()).readValue(this .treeAsTokens(jp))
70181
71182inline  fun  <reified  T , reified  U > JsonMapper.Builder.addMixIn (): JsonMapper .Builder  =  this .addMixIn(T ::class .java, U ::class .java)
0 commit comments