Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pygeoapi/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ def json_serial(obj: Any) -> str:
return obj.decode('utf-8')
except UnicodeDecodeError:
LOGGER.debug('Returning as base64 encoded JSON object')
return base64.b64encode(obj)
return base64.b64encode(obj).decode('ascii')
elif isinstance(obj, Decimal):
return float(obj)
elif type(obj).__name__ in ['int32', 'int64']:
Expand Down
14 changes: 14 additions & 0 deletions tests/other/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,20 @@ def test_json_serial():
d = uuid.UUID('12345678-1234-5678-1234-567812345678')
assert util.json_serial(d) == '12345678-1234-5678-1234-567812345678'

# Valid UTF-8 bytes round-trip as a plain string
assert util.json_serial(b'hello') == 'hello'

# Non-UTF-8 bytes must produce a base64 *str* (not bytes), so that the
# result can be serialised to JSON without a TypeError.
raw = b'\x80\x81\x82'
result = util.json_serial(raw)
assert isinstance(result, str), "json_serial must return str, not bytes"
import base64 as _b64
assert result == _b64.b64encode(raw).decode('ascii')
import json as _json
# Must be embeddable in a JSON document without raising
_json.dumps({'data': result})

with pytest.raises(TypeError):
util.json_serial('foo')

Expand Down
Loading