Skip to content

Commit 696f65d

Browse files
authored
Remove unnecessary Unicode decoding before json.loads() (jpadilla#542)
Since Python 3.6, json.loads() accepts both Unicode and byte strings. https://docs.python.org/3/library/json.html#json.loads > Changed in version 3.6: s can now be of type bytes or bytearray. The > input encoding should be UTF-8, UTF-16 or UTF-32.
1 parent 529647a commit 696f65d

File tree

3 files changed

+6
-6
lines changed

3 files changed

+6
-6
lines changed

jwt/api_jws.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ def _load(self, jwt):
188188
raise DecodeError("Invalid header padding") from err
189189

190190
try:
191-
header = json.loads(header_data.decode("utf-8"))
191+
header = json.loads(header_data)
192192
except ValueError as e:
193193
raise DecodeError("Invalid header string: %s" % e) from e
194194

jwt/api_jwt.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ def decode(
9595

9696
try:
9797
if complete:
98-
payload = json.loads(decoded["payload"].decode("utf-8"))
98+
payload = json.loads(decoded["payload"])
9999
else:
100-
payload = json.loads(decoded.decode("utf-8"))
100+
payload = json.loads(decoded)
101101
except ValueError as e:
102102
raise DecodeError("Invalid payload string: %s" % e)
103103
if not isinstance(payload, dict):

tests/test_api_jws.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ def test_decodes_valid_es384_jws(self, jws):
232232
decoded_payload = jws.decode(
233233
example_jws, example_pubkey, algorithms=["ES256"]
234234
)
235-
json_payload = json.loads(force_unicode(decoded_payload))
235+
json_payload = json.loads(decoded_payload)
236236

237237
assert json_payload == example_payload
238238

@@ -262,7 +262,7 @@ def test_decodes_valid_rs384_jws(self, jws):
262262
decoded_payload = jws.decode(
263263
example_jws, example_pubkey, algorithms=["RS384"]
264264
)
265-
json_payload = json.loads(force_unicode(decoded_payload))
265+
json_payload = json.loads(decoded_payload)
266266

267267
assert json_payload == example_payload
268268

@@ -699,7 +699,7 @@ def default(self, o):
699699
)
700700

701701
header = force_bytes(force_unicode(token).split(".")[0])
702-
header = json.loads(force_unicode(base64url_decode(header)))
702+
header = json.loads(base64url_decode(header))
703703

704704
assert "some_decimal" in header
705705
assert header["some_decimal"] == "it worked"

0 commit comments

Comments
 (0)