Object instance from number String can typecast Integer, not Long... Why?
See original GitHub issue public static void main(String[] args) throws JsonProcessingException {
final String temp = "10";
final ObjectMapper objectMapper = new ObjectMapper();
final Object o = objectMapper.readValue(temp, Object.class);
System.out.println(o instanceof Long); // false
System.out.println(o instanceof Integer); // true
}
I deserialized an integer string via ObjectMapper
to Object
type.
Deserialized instances can be typecast to Integer
but not to Long
.
If you have a cause for this or a reference document explaining it, please share.
Issue Analytics
- State:
- Created a year ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Why cannot cast Integer to String in java? - Stack Overflow
No. Every object can be casted to an java.lang.Object , not a String ...
Read more >Java Program to Convert a String to Int - GeeksforGeeks
In Java, while operating upon strings, there are times when we need to convert a number represented as a string into an integer...
Read more >Convert.ToString Method (System) - Microsoft Learn
Returns the specified string instance; no actual conversion is performed. ToString(UInt16). Converts the value of the specified 16-bit unsigned integer to ...
Read more >Integer (Java SE 17 & JDK 17) - Oracle Help Center
The Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field...
Read more >JavaScript data types and data structures - MDN Web Docs
For example, the Number object provides methods like toExponential() . ... Outside this range, JavaScript can no longer safely represent integers; they will ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@doljae That does not really make tons of sense because
DefaultTyping
is for enabling polymorphic type handling – in which JSON contains actual explicit class name to indicate intended type – andUSE_LONG_FOR_INTS
suggest coercion in “untyped” cases.So with DefaultTyping type is forced strictly.
But make sure you do understand why you would be using Default Typing – people tend to overuse it; and it can be an actual security risk if accepting external content. Even if you do need polymorphic handling it is possible to use
@JsonTypeInfo
annotation either on abstract base classes (to use for that type and subtypes), or on properties (to use for values of that property).I mean, it has to be returned as some type, it can’t be Long and Integer at the same time. Unless you configure USE_LONG_FOR_INTS, it will use Integer for small numbers.