Skip to content

Commit c9f7941

Browse files
committed
Merge branch '2.12'
2 parents 2a8ff0b + 40701f1 commit c9f7941

File tree

2 files changed

+48
-34
lines changed

2 files changed

+48
-34
lines changed

src/main/java/com/fasterxml/jackson/databind/deser/std/FromStringDeserializer.java

+44-34
Original file line numberDiff line numberDiff line change
@@ -152,33 +152,49 @@ public T deserialize(JsonParser p, DeserializationContext ctxt) throws IOExcepti
152152
{
153153
// Let's get textual value, possibly via coercion from other scalar types
154154
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);
159159
}
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;
180175
}
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+
{
182198
// [databind#381]
183199
if (t == JsonToken.START_ARRAY) {
184200
return _deserializeFromArray(p, ctxt);
@@ -190,19 +206,13 @@ public T deserialize(JsonParser p, DeserializationContext ctxt) throws IOExcepti
190206
return null;
191207
}
192208
if (_valueClass.isAssignableFrom(ob.getClass())) {
193-
return (T) ob;
209+
return ob;
194210
}
195-
return (T) _deserializeEmbedded(ob, ctxt);
211+
return _deserializeEmbedded(ob, ctxt);
196212
}
197-
return (T) ctxt.handleUnexpectedToken(getValueType(ctxt), p);
213+
return ctxt.handleUnexpectedToken(getValueType(ctxt), p);
198214
}
199215

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-
206216
/**
207217
* Overridable method to allow coercion from embedded value that is neither
208218
* {@code null} nor directly assignable to target type.

src/main/java/com/fasterxml/jackson/databind/deser/std/StringDeserializer.java

+4
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOEx
5757
// otherwise, try conversion using toString()...
5858
return ob.toString();
5959
}
60+
// 29-Jun-2020, tatu: New! "Scalar from Object" (mostly for XML)
61+
if (t == JsonToken.START_OBJECT) {
62+
return ctxt.extractScalarFromObject(p, this, _valueClass);
63+
}
6064
// allow coercions for other scalar types
6165
// 17-Jan-2018, tatu: Related to [databind#1853] avoid FIELD_NAME by ensuring it's
6266
// "real" scalar

0 commit comments

Comments
 (0)