Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve DDB bytes encoding #10740

Merged
merged 1 commit into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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"}'