[SalesForce] Validating JSON size without deserializing

I'm creating a REST endpoint in Salesforce in order to import a big set of contacts (2 million) and apply some custom logic to the insert (which I don't want in a trigger because it's a one-time thing).

So, the issue I'm concerned with is that heap size might be surpassed. Obviously, we're not going to send the 2 million contacts in the same JSON, we will chunk them down in batches of 200 so the endpoint can process them comfortably, but I would still like to add some validation.

I would like to basically be able to check the JSON's size before starting the deserialization, so if it's too big, I can return a relevant response warning the user, or simply denying the batch.

Is this possible? I've checked out the JSONParser class, but I haven't seen anything that might be of use, so that would leave me to the JSON.deserializeUntyped(jsonString) option.

Any help is appreciated.

Best Answer

You can check the answer in this post Calculate the data size of a given object for information about estimating object size. TLDR, you never know for sure what object size is going to be after deserialization. Counting the HttpRequest body size can be an estimate, but for sure not an exact value you can count on.

What you could do is to make sure you have enough space for storage of JSON in the HEAP, which is 6MB for synchronous and 12MB for asynchronous APEX.

Then use JSONParser iterate trough the JSON and deserialize step by step instead of deserializing whole JSON at the same time using JSON.deserialize(), which would effectively double the heap consumption.

You can also use Limits.getHeapSize() and Limits.getLimitHeapSize() to calculate free heap space while parsing.