1

I have a simple REST API call sent from apex class, I a trying to handle the response body with the bellow code:

String jsonPayLoad = '{"Accts":[{"Name":"ABC","Exp":25,"Languages":[{"Name":"Apex","version": []},{"Name":"Java","versions":[{"version":1.8,"certification":true,"placeholder":{"target":"reached"}}]}]}]}';
Map<String, Object> fieldMap = (Map<String, Object>)JSON.deserializeUntyped(jsonPayLoad);

It is working perfectly, but when I change the "Exp" attribute to be bigger than an Integger, for exemple :

String jsonPayLoad = '{"Accts":[{"Name":"ABC","Exp":2545534543535345435345,"Languages":[{"Name":"Apex","version": []},{"Name":"Java","versions":[{"version":1.8,"certification":true,"placeholder":{"target":"reached"}}]}]}]}';
Map<String, Object> fieldMap = (Map<String, Object>)JSON.deserializeUntyped(jsonPayLoad);

I get the error : System.JSONException: For input string: "2545534543535345435345"

Does the deserializeUntyped don't know how to handle Long? Is it even ok from the server to return parameter like that?

Edit: Does the deserializeUntyped don't know how to handle big number ? Is it even ok from the server to return parameter like that?

2 Answers 2

1

The maximum value of a long is:

9223372036854775807

but your number is bigger than that:

2545534543535345435345
5
  • Thank you, so there is no way to handle that? Do you think it is a mistake from the server?
    – Raphael D
    Dec 6, 2023 at 12:17
  • So it should be handle in with Decimal data type.
    – Raphael D
    Dec 6, 2023 at 12:25
  • Yes. But you'll have to write a small bit of code to parse to that type as the default deserialize appears hard coded to use Long (or maybe even Integer).
    – Keith C
    Dec 6, 2023 at 12:30
  • thank you, do you have any idea how to handle that in an open and dynamic way as the deserializeUntyped allows? If create a class, the structure will be close, but the response structure could potentially changed.
    – Raphael D
    Dec 6, 2023 at 13:25
  • @RaphaelD, You can write a parser that outputs nested lists and maps so that the structure is kept open. The code is made recursive to support an open ended depth. Essentially what deserializeUntyped does.
    – Keith C
    Dec 6, 2023 at 16:27
1

Long has a range of -263 to +263-1. This value is sufficient for any transaction you're every likely to do with an external system. For example, the total estimated global wealth is $454,400,000,000,000, or about 0.00005% of the maximum value of a Long. And, for the record, JSON.deserializeUntyped does support long properly:

Long l = (Long)JSON.deserializeUntyped('1000000000000');
System.debug(l);

You shouldn't worry about Long not being big enough until you start talking about numbers like the number of atoms in the planet Earth or something like that. Long can even count the number of grains of sand on a beach or in a desert. It's a very large number.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .