Skip to content

Commit

Permalink
Improve DDB bytes encoding (#10740)
Browse files Browse the repository at this point in the history
  • Loading branch information
giograno committed May 2, 2024
1 parent 2f911c3 commit 3c06fd4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
5 changes: 3 additions & 2 deletions localstack/utils/json.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import base64
import decimal
import json
import logging
Expand Down Expand Up @@ -47,11 +48,11 @@ def default(self, o):


class BytesEncoder(json.JSONEncoder):
"""Helper class that converts JSON documents with bytes"""
"""Specialized JSON encoder that encode bytes into Base64 strings."""

def default(self, obj):
if isinstance(obj, bytes):
return to_str(obj, errors="replace")
return to_str(base64.b64encode(obj))
return super().default(obj)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ def _get_lambda_logs_event(function_name, expected_num_events, retries=30):
"$..TableDescription.ProvisionedThroughput.LastIncreaseDateTime",
"$..TableDescription.StreamSpecification",
"$..TableDescription.TableStatus",
"$..Records..dynamodb.NewImage.binary_key.B",
"$..Records..dynamodb.SizeBytes",
"$..Records..eventVersion",
],
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/utils/test_json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import json

from localstack.utils.json import BytesEncoder


def test_json_encoder():
payload = {"foo": b"foobar"}
result = json.dumps(payload, cls=BytesEncoder)
assert result == '{"foo": "Zm9vYmFy"}'

0 comments on commit 3c06fd4

Please sign in to comment.