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