[SalesForce] Most efficient way to transform AggregateResults to String, Object Maps

Question:

What is the most efficient way to transform aggregate results to string object maps?

Constraints:

The keys of the aggregated results are unknown. The transformation has to be performed as efficient as possible.

Best Answer

Suggested solution:

Convert a list of aggregate results to a Json string and deserialize the string as an object.

    List<Map<String, Object>> getAggregateResultAsMap(List<AggregateResult> aggrs)
    {
        String js = Json.serialize(aggrs); // AggregateResult as Map
        List<Map<String, Object>> result = new List<Map<String, Object>>(); // result Object
        Object resultObject = Json.deserializeUntyped(js); // deserialized without type
        for (Object o : (List<Object>) resultObject)
        {
            result.add((Map<String, Object>) o);
        }
        return result;
    }