77
88from dataclasses import dataclass
99from typing import Any
10+ import json
1011
1112
1213@dataclass
@@ -34,34 +35,13 @@ def json(self) -> dict[str, Any] | None:
3435 """
3536 Return the response body as a JSON dictionary.
3637
37- If the body is already a dict (parsed JSON), returns it directly.
38- If the body is a string or bytes, attempts to parse it as JSON.
39- Returns None if body is None or cannot be parsed.
38+ The body is already parsed during the request, so this typically
39+ just returns the body if it's a dict, or None otherwise.
4040
4141 :return: Parsed JSON dictionary or None
4242 """
43- if self .body is None :
44- return None
45-
4643 if isinstance (self .body , dict ):
4744 return self .body
48-
49- if isinstance (self .body , str ):
50- import json
51-
52- try :
53- return json .loads (self .body )
54- except (json .JSONDecodeError , ValueError ):
55- return None
56-
57- if isinstance (self .body , bytes ):
58- import json
59-
60- try :
61- return json .loads (self .body .decode ("utf-8" ))
62- except (json .JSONDecodeError , ValueError , UnicodeDecodeError ):
63- return None
64-
6545 return None
6646
6747 def text (self ) -> str | None :
@@ -83,8 +63,6 @@ def text(self) -> str | None:
8363 return self .body .decode ("utf-8" , errors = "replace" )
8464
8565 if isinstance (self .body , dict ):
86- import json
87-
8866 return json .dumps (self .body )
8967
9068 return str (self .body )
0 commit comments