10
10
import com .fasterxml .jackson .databind .*;
11
11
import com .fasterxml .jackson .databind .annotation .JacksonStdImpl ;
12
12
import com .fasterxml .jackson .databind .cfg .CoercionAction ;
13
+ import com .fasterxml .jackson .databind .cfg .CoercionInputShape ;
13
14
import com .fasterxml .jackson .databind .jsontype .TypeDeserializer ;
14
15
import com .fasterxml .jackson .databind .type .LogicalType ;
15
16
import com .fasterxml .jackson .databind .util .AccessPattern ;
@@ -423,24 +424,37 @@ public CharacterDeserializer(Class<Character> cls, Character nvl)
423
424
public Character deserialize (JsonParser p , DeserializationContext ctxt )
424
425
throws IOException
425
426
{
427
+ CoercionAction act ;
428
+
426
429
switch (p .currentTokenId ()) {
427
430
case JsonTokenId .ID_NUMBER_INT : // ok iff Unicode value
428
- // 12-Jun-2020, tatu: inlined from `StdDeserializer`
429
- if (!ctxt .isEnabled (MapperFeature .ALLOW_COERCION_OF_SCALARS )) {
430
- ctxt .reportInputMismatch (this ,
431
- "Cannot coerce Integer value to `Character` (enable `MapperFeature.ALLOW_COERCION_OF_SCALARS` to allow)" );
431
+ act = ctxt .findCoercionAction (logicalType (), _valueClass , CoercionInputShape .Integer );
432
+ switch (act ) {
433
+ case Fail :
434
+ _checkCoercionActionFail (ctxt , act , "Integer value (" +p .getText ()+")" );
435
+ break ;
436
+ case AsNull :
437
+ return getNullValue (ctxt );
438
+ case AsEmpty :
439
+ return (Character ) getEmptyValue (ctxt );
440
+ default :
432
441
}
433
442
int value = p .getIntValue ();
434
443
if (value >= 0 && value <= 0xFFFF ) {
435
444
return Character .valueOf ((char ) value );
436
445
}
437
446
break ;
438
- case JsonTokenId .ID_STRING : // this is the usual type
439
-
447
+ case JsonTokenId .ID_STRING :
448
+ // 23-Jun-2020, tatu: Unlike real numeric types, Character/char does not
449
+ // have canonical shape in JSON, and String in particular does not need
450
+ // coercion -- as long as it has length of 1.
440
451
String text = p .getText ();
441
- CoercionAction act = _checkFromStringCoercion (ctxt , text );
452
+ if (text .length () == 1 ) {
453
+ return Character .valueOf (text .charAt (0 ));
454
+ }
455
+ act = _checkFromStringCoercion (ctxt , text );
442
456
if (act == CoercionAction .AsNull ) {
443
- return ( Character ) getNullValue (ctxt );
457
+ return getNullValue (ctxt );
444
458
}
445
459
if (act == CoercionAction .AsEmpty ) {
446
460
return (Character ) getEmptyValue (ctxt );
@@ -449,10 +463,6 @@ public Character deserialize(JsonParser p, DeserializationContext ctxt)
449
463
if (_checkTextualNull (ctxt , text )) {
450
464
return (Character ) getNullValue (ctxt );
451
465
}
452
- // But does it have to be exactly one char?
453
- if (text .length () == 1 ) {
454
- return Character .valueOf (text .charAt (0 ));
455
- }
456
466
break ;
457
467
case JsonTokenId .ID_NULL :
458
468
if (_primitive ) {
@@ -463,7 +473,7 @@ public Character deserialize(JsonParser p, DeserializationContext ctxt)
463
473
return _deserializeFromArray (p , ctxt );
464
474
default :
465
475
}
466
- return (Character ) ctxt .handleUnexpectedToken (_valueClass , p );
476
+ return (Character ) ctxt .handleUnexpectedToken (getValueType ( ctxt ) , p );
467
477
}
468
478
}
469
479
0 commit comments