JSON deserialize initialize all null objects

apexjson

Is there a way or any method (other than write a long helper for each object) that could initialize all objects within an object even if the JSON was not received aka:

@AuraEnabled
public SomeObject objectHere;

I would want this to be automatically substantiated with an empty version of that object so when I go objectHere.id I don't have to null check the object then the field.

Best Answer

You can use the Safe Navigation Operator (?.) to avoid null checks:

String someValue = inputParam?.objectHere?.someProperty;

This is likely the best choice in many cases, unless you need to actually use the object for output, then you might want to go with Derek's answer.

Related Topic