Description
Use case
In some event classes (such as SQS, Kafka, and others), certain string fields (such as body
) can be converted to JSON using the json.loads
function. However, it is possible that the customer may have created the message with a JSON serialization method that differs from the default json
class. To cover these scenarios, we need to provide customers with the flexibility to use a custom deserialization function of their choice.
This is important because by allowing customers to use their preferred deserialization function, we can ensure that their messages are properly deserialized into the intended format.
The default if nothing is informed will continue to be json.loads
Solution/User Experience
To address the issue, we can modify the DictWrapper class to introduce a new parameter. This parameter would allow customers to provide their preferred deserialization function for converting string fields to JSON.
class DictWrapper(Mapping):
"""Provides a single read only access to a wrapper dict"""
def __init__(self, data: Dict[str, Any], json_deserializer: Optional[Callable] = None):
self._data = data
self._json_data: Optional[Any] = None
self._json_deserializer = json_deserializer or json.loads
Alternative solutions
No response
Acknowledgment
- This feature request meets Lambda Powertools Tenets
- Should this be considered in other Lambda Powertools languages? i.e. Java, TypeScript, and .NET