Skip to content

Feature request: support custom JSON deserializer in event source classes #2199

Closed

Description

@leandrodamascena

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

Activity

  1. added
    triage
    feature-request
    on May 3, 2023
  2. added
    event_sources
    and removed
    triage
    on May 3, 2023
  3. github-actions commented on May 4, 2023

    @github-actions
    Contributor

    ⚠️COMMENT VISIBILITY WARNING⚠️

    This issue is now closed. Please be mindful that future comments are hard for our team to see.

    If you need more assistance, please either tag a team member or open a new issue that references this one.

    If you wish to keep having a conversation with other community members under this issue feel free to do so.

  4. added
    pending-release
    on May 4, 2023
  5. github-actions commented on May 4, 2023

    @github-actions
    Contributor

    This is now released under 2.15.0 version!

  6. removed
    pending-release
    on May 4, 2023
  7. facu-01 commented on Mar 21, 2024

    @facu-01
    Contributor

    How this can be used in APIGatewayRestResolver? i cant find the way to inject the custom json_deserializer.

    from decimal import Decimal
    from typing_extensions import Annotated
    from pydantic import BaseModel
    from aws_lambda_powertools.event_handler import APIGatewayRestResolver
    from aws_lambda_powertools.event_handler.openapi.params import Body
    
    app = APIGatewayRestResolver(enable_validation=True)
    
    class DataIn(BaseModel):
        current: Decimal
    
    @app.post('/data')
    def save_data(
        data: Annotated[DataIn,  Body()],
    ): 
        
        return data
    
    def lambda_handler(event, context):
        return app.resolve(event, context)
    

    if a make a post with json :

    {
        "currency": 1.00
    }
    

    The last zero is removed, this issue could be solved using json.loads(parse_float=Decimal)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

event_sourcesfeature-request

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions