5
5
import java .util .UUID ;
6
6
7
7
import com .fasterxml .jackson .core .Base64Variants ;
8
-
8
+ import com . fasterxml . jackson . core . JsonParser ;
9
9
import com .fasterxml .jackson .databind .DeserializationContext ;
10
+ import com .fasterxml .jackson .databind .JsonMappingException ;
11
+ import com .fasterxml .jackson .databind .exc .InvalidFormatException ;
10
12
11
13
public class UUIDDeserializer extends FromStringDeserializer <UUID >
12
14
{
@@ -37,24 +39,24 @@ protected UUID _deserialize(String id, DeserializationContext ctxt) throws IOExc
37
39
byte [] stuff = Base64Variants .getDefaultVariant ().decode (id );
38
40
return _fromBytes (stuff , ctxt );
39
41
}
40
- _badFormat (id );
42
+ _badFormat (id , ctxt );
41
43
}
42
44
43
45
// verify hyphens first:
44
46
if ((id .charAt (8 ) != '-' ) || (id .charAt (13 ) != '-' )
45
47
|| (id .charAt (18 ) != '-' ) || (id .charAt (23 ) != '-' )) {
46
- _badFormat (id );
48
+ _badFormat (id , ctxt );
47
49
}
48
- long l1 = intFromChars (id , 0 );
50
+ long l1 = intFromChars (id , 0 , ctxt );
49
51
l1 <<= 32 ;
50
- long l2 = ((long ) shortFromChars (id , 9 )) << 16 ;
51
- l2 |= shortFromChars (id , 14 );
52
+ long l2 = ((long ) shortFromChars (id , 9 , ctxt )) << 16 ;
53
+ l2 |= shortFromChars (id , 14 , ctxt );
52
54
long hi = l1 + l2 ;
53
55
54
- int i1 = (shortFromChars (id , 19 ) << 16 ) | shortFromChars (id , 24 );
56
+ int i1 = (shortFromChars (id , 19 , ctxt ) << 16 ) | shortFromChars (id , 24 , ctxt );
55
57
l1 = i1 ;
56
58
l1 <<= 32 ;
57
- l2 = intFromChars (id , 28 );
59
+ l2 = intFromChars (id , 28 , ctxt );
58
60
l2 = (l2 << 32 ) >>> 32 ; // sign removal, Java-style. Ugh.
59
61
long lo = l1 | l2 ;
60
62
@@ -71,19 +73,26 @@ protected UUID _deserializeEmbedded(Object ob, DeserializationContext ctxt) thro
71
73
return null ; // never gets here
72
74
}
73
75
74
- private void _badFormat (String uuidStr ) {
75
- throw new NumberFormatException ("UUID has to be represented by the standard 36-char representation" );
76
+ private void _badFormat (String uuidStr , DeserializationContext ctxt )
77
+ throws JsonMappingException
78
+ {
79
+ throw InvalidFormatException .from (ctxt .getParser (),
80
+ "UUID has to be represented by standard 36-char representation" ,
81
+ uuidStr , handledType ());
76
82
}
77
83
78
- static int intFromChars (String str , int index ) {
79
- return (byteFromChars (str , index ) << 24 ) + (byteFromChars (str , index +2 ) << 16 ) + (byteFromChars (str , index +4 ) << 8 ) + byteFromChars (str , index +6 );
84
+ static int intFromChars (String str , int index , DeserializationContext ctxt ) throws JsonMappingException {
85
+ return (byteFromChars (str , index , ctxt ) << 24 )
86
+ + (byteFromChars (str , index +2 , ctxt ) << 16 )
87
+ + (byteFromChars (str , index +4 , ctxt ) << 8 )
88
+ + byteFromChars (str , index +6 , ctxt );
80
89
}
81
90
82
- static int shortFromChars (String str , int index ) {
83
- return (byteFromChars (str , index ) << 8 ) + byteFromChars (str , index +2 );
91
+ static int shortFromChars (String str , int index , DeserializationContext ctxt ) throws JsonMappingException {
92
+ return (byteFromChars (str , index , ctxt ) << 8 ) + byteFromChars (str , index +2 , ctxt );
84
93
}
85
94
86
- static int byteFromChars (String str , int index )
95
+ static int byteFromChars (String str , int index , DeserializationContext ctxt ) throws JsonMappingException
87
96
{
88
97
final char c1 = str .charAt (index );
89
98
final char c2 = str .charAt (index +1 );
@@ -95,20 +104,23 @@ static int byteFromChars(String str, int index)
95
104
}
96
105
}
97
106
if (c1 > 127 || HEX_DIGITS [c1 ] < 0 ) {
98
- return _badChar (str , index , c1 );
107
+ return _badChar (str , index , ctxt , c1 );
99
108
}
100
- return _badChar (str , index +1 , c2 );
109
+ return _badChar (str , index +1 , ctxt , c2 );
101
110
}
102
111
103
- static int _badChar (String uuidStr , int index , char c ) {
104
- throw new NumberFormatException ("Non-hex character '" +c +"', not valid character for a UUID String"
105
- +"' (value 0x" +Integer .toHexString (c )+") for UUID String \" " +uuidStr +"\" " );
112
+ static int _badChar (String uuidStr , int index , DeserializationContext ctxt , char c ) throws JsonMappingException {
113
+ String msg = String .format (
114
+ "Non-hex character '%c' (value 0x%s), not valid for UUID String: input String '%s'" ,
115
+ c , Integer .toHexString (c ), uuidStr );
116
+ throw InvalidFormatException .from (ctxt .getParser (), msg , uuidStr , UUID .class );
106
117
}
107
118
108
- private UUID _fromBytes (byte [] bytes , DeserializationContext ctxt ) throws IOException {
119
+ private UUID _fromBytes (byte [] bytes , DeserializationContext ctxt ) throws JsonMappingException {
109
120
if (bytes .length != 16 ) {
110
- ctxt .mappingException ("Can only construct UUIDs from byte[16]; got %d bytes" ,
111
- bytes .length );
121
+ throw InvalidFormatException .from (ctxt .getParser (),
122
+ "Can only construct UUIDs from byte[16]; got " +bytes .length +" bytes" ,
123
+ bytes , handledType ());
112
124
}
113
125
return new UUID (_long (bytes , 0 ), _long (bytes , 8 ));
114
126
}
0 commit comments