0

For example I have following Apex class:

public class ToDeserialize {

    public Wrapper Wrapper;

    public class Wrapper {
        public Long myLong; 
    }
}

And I have JSON which I want to deserialize to the ToDeserialize object:

'{"Wrapper": {"myLong": null} }'

Then I'm trying to deserialize it and it works fine and print myLong to System.debug:

ToDeserialize result = (ToDeserialize) JSON.deserialize(result, ToDeserialize.class);
Long var = result.wrapper.myLong;
System.debug(var);

But when I'm trying to assure that var is instance of Long I'm getting following error:

System.debug(var instanceof Long);
Operation instanceof is always false since an instance of String is never an instance of Long

What the reason? And what I'm doing wrong?

5
  • 2
    I've tried your example and haven't faced the same result, and the result was as expected: Operation instanceof is always true since an instance of Long is always an instance of Long. You might have missed something. Apr 2, 2021 at 9:11
  • @OlehBerehovskyi Yes my real code is much complicated and the Long variable is inside another object in JSON and a little changed example. Probably problem is that Long variable is inside another object Apr 2, 2021 at 10:47
  • Can you try var instanceof Decimal? I'm guessing apex is not differiating numerical types. Another suggestion is to change the variable name, eg myvar. Apr 2, 2021 at 10:55
  • I've even tried your updated wrapper with inner class deser and I'm still having Operation instanceof is always true since an instance of Long is always an instance of Long Apr 2, 2021 at 11:12
  • 2
    Your code is incorrect, as wrapper does not exist as a variable. You may have oversimplified your code. As written, we expect a compiler error that a Long is a Long, because a Long is a Long, and you can't make trivial comparisons using instanceOf.
    – sfdcfox
    Apr 2, 2021 at 11:19

0

You must log in to answer this question.

Browse other questions tagged .