Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:open
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

cowtowncodercommented, Jul 16, 2022

@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 – and USE_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).

yawkatcommented, Jul 15, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found