|
4 | 4 | import java.lang.annotation.Annotation;
|
5 | 5 |
|
6 | 6 | import com.fasterxml.jackson.core.*;
|
7 |
| - |
| 7 | +import com.fasterxml.jackson.core.JsonParser.NumberType; |
| 8 | +import com.fasterxml.jackson.core.io.NumberInput; |
8 | 9 | import com.fasterxml.jackson.databind.*;
|
9 | 10 | import com.fasterxml.jackson.databind.deser.*;
|
10 | 11 | import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
|
@@ -144,52 +145,213 @@ public int getOptimizedIndex() {
|
144 | 145 | protected final boolean _deserializeBoolean(JsonParser p, DeserializationContext ctxt) throws IOException
|
145 | 146 | {
|
146 | 147 | JsonToken t = p.getCurrentToken();
|
147 |
| - if (t == JsonToken.VALUE_TRUE) { |
148 |
| - return true; |
| 148 | + if (t == JsonToken.VALUE_TRUE) return true; |
| 149 | + if (t == JsonToken.VALUE_FALSE) return false; |
| 150 | + if (t == JsonToken.VALUE_NULL) return false; |
| 151 | + |
| 152 | + // [JACKSON-78]: should accept ints too, (0 == false, otherwise true) |
| 153 | + if (t == JsonToken.VALUE_NUMBER_INT) { |
| 154 | + // 11-Jan-2012, tatus: May be outside of int... |
| 155 | + if (p.getNumberType() == NumberType.INT) { |
| 156 | + return (p.getIntValue() != 0); |
| 157 | + } |
| 158 | + return _deserializeBooleanFromOther(p, ctxt); |
149 | 159 | }
|
150 |
| - if (t == JsonToken.VALUE_FALSE) { |
151 |
| - return false; |
| 160 | + // And finally, let's allow Strings to be converted too |
| 161 | + if (t == JsonToken.VALUE_STRING) { |
| 162 | + String text = p.getText().trim(); |
| 163 | + // [#422]: Allow aliases |
| 164 | + if ("true".equals(text) || "True".equals(text)) { |
| 165 | + return true; |
| 166 | + } |
| 167 | + if ("false".equals(text) || "False".equals(text) || text.length() == 0) { |
| 168 | + return false; |
| 169 | + } |
| 170 | + if (_hasTextualNull(text)) { |
| 171 | + return false; |
| 172 | + } |
| 173 | + throw ctxt.weirdStringException(text, Boolean.TYPE, "only \"true\" or \"false\" recognized"); |
152 | 174 | }
|
153 |
| - return p.getValueAsBoolean(); |
| 175 | + // [databind#381] |
| 176 | + if (t == JsonToken.START_ARRAY && ctxt.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)) { |
| 177 | + p.nextToken(); |
| 178 | + final boolean parsed = _deserializeBooleanFromOther(p, ctxt); |
| 179 | + t = p.nextToken(); |
| 180 | + if (t != JsonToken.END_ARRAY) { |
| 181 | + throw ctxt.wrongTokenException(p, JsonToken.END_ARRAY, |
| 182 | + "Attempted to unwrap single value array for single 'boolean' value but there was more than a single value in the array"); |
| 183 | + } |
| 184 | + return parsed; |
| 185 | + } |
| 186 | + // Otherwise, no can do: |
| 187 | + throw ctxt.mappingException(Boolean.TYPE, t); |
154 | 188 | }
|
155 | 189 |
|
156 |
| - protected final String _deserializeString(JsonParser p, DeserializationContext ctxt) throws IOException |
| 190 | + protected final short _deserializeShort(JsonParser jp, DeserializationContext ctxt) |
| 191 | + throws IOException |
157 | 192 | {
|
158 |
| - String text = p.getValueAsString(); |
159 |
| - if (text != null) { |
160 |
| - return text; |
| 193 | + int value = _deserializeInt(jp, ctxt); |
| 194 | + // So far so good: but does it fit? |
| 195 | + if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) { |
| 196 | + throw ctxt.weirdStringException(String.valueOf(value), |
| 197 | + Short.TYPE, "overflow, value can not be represented as 16-bit value"); |
161 | 198 | }
|
162 |
| - return _convertToString(p, ctxt); |
| 199 | + return (short) value; |
163 | 200 | }
|
164 | 201 |
|
165 |
| - /** |
166 |
| - * Helper method for coercing JSON values other than Strings into |
167 |
| - * Java String value. |
168 |
| - */ |
169 |
| - protected final String _convertToString(JsonParser p, DeserializationContext ctxt) throws IOException |
| 202 | + protected final int _deserializeInt(JsonParser p, DeserializationContext ctxt) |
| 203 | + throws IOException |
170 | 204 | {
|
171 |
| - JsonToken curr = p.getCurrentToken(); |
172 |
| - if (curr == JsonToken.VALUE_NULL) { // should this ever happen? |
173 |
| - if (_valueDeserializer == null) { |
174 |
| - return null; |
| 205 | + if (p.hasToken(JsonToken.VALUE_NUMBER_INT)) { |
| 206 | + return p.getIntValue(); |
| 207 | + } |
| 208 | + JsonToken t = p.getCurrentToken(); |
| 209 | + if (t == JsonToken.VALUE_STRING) { // let's do implicit re-parse |
| 210 | + String text = p.getText().trim(); |
| 211 | + if (_hasTextualNull(text)) { |
| 212 | + return 0; |
| 213 | + } |
| 214 | + try { |
| 215 | + int len = text.length(); |
| 216 | + if (len > 9) { |
| 217 | + long l = Long.parseLong(text); |
| 218 | + if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) { |
| 219 | + throw ctxt.weirdStringException(text, Integer.TYPE, |
| 220 | + "Overflow: numeric value ("+text+") out of range of int ("+Integer.MIN_VALUE+" - "+Integer.MAX_VALUE+")"); |
| 221 | + } |
| 222 | + return (int) l; |
| 223 | + } |
| 224 | + if (len == 0) { |
| 225 | + return 0; |
| 226 | + } |
| 227 | + return NumberInput.parseInt(text); |
| 228 | + } catch (IllegalArgumentException iae) { |
| 229 | + throw ctxt.weirdStringException(text, Integer.TYPE, "not a valid int value"); |
175 | 230 | }
|
176 |
| - return (String) _valueDeserializer.getNullValue(ctxt); |
177 | 231 | }
|
178 |
| - if (curr == JsonToken.VALUE_EMBEDDED_OBJECT) { |
| 232 | + if (t == JsonToken.VALUE_NUMBER_FLOAT) { |
| 233 | + if (!ctxt.isEnabled(DeserializationFeature.ACCEPT_FLOAT_AS_INT)) { |
| 234 | + _failDoubleToIntCoercion(p, ctxt, "int"); |
| 235 | + } |
| 236 | + return p.getValueAsInt(); |
| 237 | + } |
| 238 | + if (t == JsonToken.VALUE_NULL) { |
| 239 | + return 0; |
| 240 | + } |
| 241 | + if (t == JsonToken.START_ARRAY && ctxt.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)) { |
| 242 | + p.nextToken(); |
| 243 | + final int parsed = _deserializeInt(p, ctxt); |
| 244 | + t = p.nextToken(); |
| 245 | + if (t != JsonToken.END_ARRAY) { |
| 246 | + throw ctxt.wrongTokenException(p, JsonToken.END_ARRAY, |
| 247 | + "Attempted to unwrap single value array for single 'int' value but there was more than a single value in the array"); |
| 248 | + } |
| 249 | + return parsed; |
| 250 | + } |
| 251 | + // Otherwise, no can do: |
| 252 | + throw ctxt.mappingException(Integer.TYPE, t); |
| 253 | + } |
| 254 | + |
| 255 | + protected final long _deserializeLong(JsonParser p, DeserializationContext ctxt) |
| 256 | + throws IOException |
| 257 | + { |
| 258 | + switch (p.getCurrentTokenId()) { |
| 259 | + case JsonTokenId.ID_NUMBER_INT: |
| 260 | + return p.getLongValue(); |
| 261 | + case JsonTokenId.ID_NUMBER_FLOAT: |
| 262 | + if (!ctxt.isEnabled(DeserializationFeature.ACCEPT_FLOAT_AS_INT)) { |
| 263 | + _failDoubleToIntCoercion(p, ctxt, "long"); |
| 264 | + } |
| 265 | + return p.getValueAsLong(); |
| 266 | + case JsonTokenId.ID_STRING: |
| 267 | + String text = p.getText().trim(); |
| 268 | + if (text.length() == 0 || _hasTextualNull(text)) { |
| 269 | + return 0L; |
| 270 | + } |
| 271 | + try { |
| 272 | + return NumberInput.parseLong(text); |
| 273 | + } catch (IllegalArgumentException iae) { } |
| 274 | + throw ctxt.weirdStringException(text, Long.TYPE, "not a valid long value"); |
| 275 | + case JsonTokenId.ID_NULL: |
| 276 | + return 0L; |
| 277 | + case JsonTokenId.ID_START_ARRAY: |
| 278 | + if (ctxt.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)) { |
| 279 | + p.nextToken(); |
| 280 | + final long parsed = _deserializeLong(p, ctxt); |
| 281 | + JsonToken t = p.nextToken(); |
| 282 | + if (t != JsonToken.END_ARRAY) { |
| 283 | + throw ctxt.wrongTokenException(p, JsonToken.END_ARRAY, |
| 284 | + "Attempted to unwrap single value array for single 'long' value but there was more than a single value in the array"); |
| 285 | + } |
| 286 | + return parsed; |
| 287 | + } |
| 288 | + break; |
| 289 | + } |
| 290 | + throw ctxt.mappingException(Long.TYPE, p.getCurrentToken()); |
| 291 | + } |
| 292 | + |
| 293 | + protected final String _deserializeString(JsonParser p, DeserializationContext ctxt) throws IOException |
| 294 | + { |
| 295 | + switch (p.getCurrentTokenId()) { |
| 296 | + case JsonTokenId.ID_STRING: |
| 297 | + return p.getText(); |
| 298 | + case JsonTokenId.ID_NULL: |
| 299 | + return null; |
| 300 | + case JsonTokenId.ID_START_ARRAY: |
| 301 | + if (ctxt.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)) { |
| 302 | + p.nextToken(); |
| 303 | + final String parsed = _deserializeString(p, ctxt); |
| 304 | + if (p.nextToken() != JsonToken.END_ARRAY) { |
| 305 | + throw ctxt.wrongTokenException(p, JsonToken.END_ARRAY, |
| 306 | + "Attempted to unwrap single value array for single 'String' value but there was more than a single value in the array"); |
| 307 | + } |
| 308 | + return parsed; |
| 309 | + } |
| 310 | + break; |
| 311 | + case JsonTokenId.ID_EMBEDDED_OBJECT: |
179 | 312 | Object ob = p.getEmbeddedObject();
|
180 | 313 | if (ob == null) {
|
181 | 314 | return null;
|
182 | 315 | }
|
183 | 316 | if (ob instanceof byte[]) {
|
184 | 317 | return Base64Variants.getDefaultVariant().encode((byte[]) ob, false);
|
185 | 318 | }
|
| 319 | + // otherwise, try conversion using toString()... |
186 | 320 | return ob.toString();
|
| 321 | + default: |
| 322 | + // allow coercions for other scalar types |
| 323 | + String text = p.getValueAsString(); |
| 324 | + if (text != null) { |
| 325 | + return text; |
| 326 | + } |
187 | 327 | }
|
188 |
| - // Can deserialize any scalar value |
189 |
| - if (curr.isScalarValue()) { // should have been handled earlier, but just in case... |
190 |
| - return p.getText(); |
| 328 | + throw ctxt.mappingException(String.class, p.getCurrentToken()); |
| 329 | + } |
| 330 | + |
| 331 | + protected final boolean _deserializeBooleanFromOther(JsonParser p, DeserializationContext ctxt) |
| 332 | + throws IOException |
| 333 | + { |
| 334 | + if (p.getNumberType() == NumberType.LONG) { |
| 335 | + return (p.getLongValue() == 0L) ? Boolean.FALSE : Boolean.TRUE; |
191 | 336 | }
|
192 |
| - // but not markers: |
193 |
| - throw ctxt.mappingException(String.class); |
| 337 | + // no really good logic; let's actually resort to textual comparison |
| 338 | + String str = p.getText(); |
| 339 | + if ("0.0".equals(str) || "0".equals(str)) { |
| 340 | + return Boolean.FALSE; |
| 341 | + } |
| 342 | + return Boolean.TRUE; |
| 343 | + } |
| 344 | + |
| 345 | + // // More helper methods from StdDeserializer |
| 346 | + |
| 347 | + protected void _failDoubleToIntCoercion(JsonParser p, DeserializationContext ctxt, |
| 348 | + String type) throws IOException |
| 349 | + { |
| 350 | + throw ctxt.mappingException("Can not coerce a floating-point value ('%s') into %s; enable `DeserializationFeature.ACCEPT_FLOAT_AS_INT` to allow", |
| 351 | + p.getValueAsString(), type); |
| 352 | + } |
| 353 | + |
| 354 | + protected boolean _hasTextualNull(String value) { |
| 355 | + return "null".equals(value); |
194 | 356 | }
|
195 | 357 | }
|
0 commit comments