Skip to content

Commit a622823

Browse files
committed
Add DebugToolBarJSONDecoder class for better organization of code
1 parent f3ac881 commit a622823

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

debug_toolbar/panels/sql/decoders.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import base64
2+
import json
3+
4+
5+
class DebugToolbarJSONDecoder(json.JSONDecoder):
6+
"""Custom JSON decoder that reconstructs binary data during parsing."""
7+
8+
def decode(self, s):
9+
"""Override decode to apply reconstruction after parsing."""
10+
obj = super().decode(s)
11+
return self._reconstruct_params(obj)
12+
13+
def _reconstruct_params(self, params):
14+
"""Reconstruct parameters, handling lists and dicts recursively."""
15+
if isinstance(params, list):
16+
return [self._reconstruct_params(param) for param in params]
17+
elif isinstance(params, dict):
18+
if "__djdt_binary__" in params:
19+
return base64.b64decode(params["__djdt_binary__"])
20+
else:
21+
return {
22+
key: self._reconstruct_params(value)
23+
for key, value in params.items()
24+
}
25+
else:
26+
return params

debug_toolbar/panels/sql/forms.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from django.utils.functional import cached_property
88
from django.utils.translation import gettext_lazy as _
99

10+
from debug_toolbar.panels.sql.decoders import DebugToolbarJSONDecoder
1011
from debug_toolbar.panels.sql.utils import is_select_query, reformat_sql
1112
from debug_toolbar.toolbar import DebugToolbar
1213

@@ -90,7 +91,7 @@ def clean(self):
9091
def _get_query_params(self):
9192
"""Get reconstructed parameters for the current query"""
9293
query = self.cleaned_data["query"]
93-
return _reconstruct_params(json.loads(query["params"]))
94+
return json.loads(query["params"], cls=DebugToolbarJSONDecoder)
9495

9596
def select(self):
9697
query = self.cleaned_data["query"]

0 commit comments

Comments
 (0)