Given the following two lines of code produce the same output, which of the two methods is better practice?
TJSONArray *resultsArray = (TJSONArray*) response->Get("results")->JsonValue;
TJSONArray *resultsArray = (TJSONArray*) response->GetValue("results");
GetValue()is functionally the same as callingGet()->JsonValue, but with an extra check to make sure the requested key actually exists before accessing itsJsonValue.TJSONObjecthas a protectedGetPairByName()method, which returns a pointer to theTJSONPairobject of the requested key if found, orNULLif the key is not found.Get()simply callsGetPairByName()and returns the pointer as-is:If you know for sure that the key exists, using
Get()->JsonValueis perfectly safe. But, if there is any possibility that the key may not exist, you need to check the return value forNULLbefore accessing any of theTJSONPair's members.GetValue()callsGetPairByName()and returns theJsonValueof the returnedTJSONPaironly if the key is found, otherwise it returnsNULL:If there is any possibility that the key may not exist, it is cleaner to call
GetValue()instead ofGet()->JsonValue.