@@ -152,33 +152,49 @@ public T deserialize(JsonParser p, DeserializationContext ctxt) throws IOExcepti
152
152
{
153
153
// Let's get textual value, possibly via coercion from other scalar types
154
154
String text = p .getValueAsString ();
155
- if (text != null ) { // has String representation
156
- if ( text . length () == 0 || ( text = text . trim ()). length () == 0 ) {
157
- // 09-Jun-2020, tatu: Commonly `null` but may coerce to "empty" as well
158
- return (T ) _deserializeFromEmptyString ( ctxt );
155
+ if (text == null ) {
156
+ JsonToken t = p . currentToken ();
157
+ if ( t != JsonToken . START_OBJECT ) {
158
+ return (T ) _deserializeFromOther ( p , ctxt , t );
159
159
}
160
- Exception cause = null ;
161
- try {
162
- // 19-May-2017, tatu: Used to require non-null result (assuming `null`
163
- // indicated error; but that seems wrong. Should be able to return
164
- // `null` as value.
165
- return _deserialize (text , ctxt );
166
- } catch (IllegalArgumentException | MalformedURLException e ) {
167
- cause = e ;
168
- }
169
- // note: `cause` can't be null
170
- String msg = "not a valid textual representation" ;
171
- String m2 = cause .getMessage ();
172
- if (m2 != null ) {
173
- msg = msg + ", problem: " +m2 ;
174
- }
175
- // 05-May-2016, tatu: Unlike most usage, this seems legit, so...
176
- JsonMappingException e = ctxt .weirdStringException (text , _valueClass , msg );
177
- e .initCause (cause );
178
- throw e ;
179
- // nothing to do here, yet? We'll fail anyway
160
+ // 29-Jun-2020, tatu: New! "Scalar from Object" (mostly for XML)
161
+ text = ctxt .extractScalarFromObject (p , this , _valueClass );
162
+ }
163
+ if (text .length () == 0 || (text = text .trim ()).length () == 0 ) {
164
+ // 09-Jun-2020, tatu: Commonly `null` but may coerce to "empty" as well
165
+ return (T ) _deserializeFromEmptyString (ctxt );
166
+ }
167
+ Exception cause = null ;
168
+ try {
169
+ // 19-May-2017, tatu: Used to require non-null result (assuming `null`
170
+ // indicated error; but that seems wrong. Should be able to return
171
+ // `null` as value.
172
+ return _deserialize (text , ctxt );
173
+ } catch (IllegalArgumentException | MalformedURLException e ) {
174
+ cause = e ;
180
175
}
181
- JsonToken t = p .currentToken ();
176
+ // note: `cause` can't be null
177
+ String msg = "not a valid textual representation" ;
178
+ String m2 = cause .getMessage ();
179
+ if (m2 != null ) {
180
+ msg = msg + ", problem: " +m2 ;
181
+ }
182
+ // 05-May-2016, tatu: Unlike most usage, this seems legit, so...
183
+ JsonMappingException e = ctxt .weirdStringException (text , _valueClass , msg );
184
+ e .initCause (cause );
185
+ throw e ;
186
+ }
187
+
188
+ /**
189
+ * Main method from trying to deserialize actual value from non-empty
190
+ * String.
191
+ */
192
+ protected abstract T _deserialize (String value , DeserializationContext ctxt ) throws IOException ;
193
+
194
+ // @since 2.12
195
+ protected Object _deserializeFromOther (JsonParser p , DeserializationContext ctxt ,
196
+ JsonToken t ) throws IOException
197
+ {
182
198
// [databind#381]
183
199
if (t == JsonToken .START_ARRAY ) {
184
200
return _deserializeFromArray (p , ctxt );
@@ -190,19 +206,13 @@ public T deserialize(JsonParser p, DeserializationContext ctxt) throws IOExcepti
190
206
return null ;
191
207
}
192
208
if (_valueClass .isAssignableFrom (ob .getClass ())) {
193
- return ( T ) ob ;
209
+ return ob ;
194
210
}
195
- return ( T ) _deserializeEmbedded (ob , ctxt );
211
+ return _deserializeEmbedded (ob , ctxt );
196
212
}
197
- return ( T ) ctxt .handleUnexpectedToken (getValueType (ctxt ), p );
213
+ return ctxt .handleUnexpectedToken (getValueType (ctxt ), p );
198
214
}
199
215
200
- /**
201
- * Main method from trying to deserialize actual value from non-empty
202
- * String.
203
- */
204
- protected abstract T _deserialize (String value , DeserializationContext ctxt ) throws IOException ;
205
-
206
216
/**
207
217
* Overridable method to allow coercion from embedded value that is neither
208
218
* {@code null} nor directly assignable to target type.
0 commit comments