-
Notifications
You must be signed in to change notification settings - Fork 140
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
Added the ability to specify the JSON encoder #25
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,7 @@ class GraphQLView(View): | |
graphiql_template = None | ||
middleware = None | ||
batch = False | ||
json_encoder = None | ||
|
||
methods = ['GET', 'POST', 'PUT', 'DELETE'] | ||
|
||
|
@@ -142,10 +143,10 @@ def get_response(self, request, data, show_graphiql=False): | |
def json_encode(self, request, d, show_graphiql=False): | ||
pretty = self.pretty or show_graphiql or request.args.get('pretty') | ||
if not pretty: | ||
return json.dumps(d, separators=(',', ':')) | ||
return json.dumps(d, separators=(',', ':'), cls=self.json_encoder) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason to continue using return self.json_encoder(separators=(',' ':')).encode(d) I believe they're functionally equivalent but the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not an expert on json encoding/decoding in Python. What you've said sounds great but I'd rather leave this to someone in a better position to comment. |
||
|
||
return json.dumps(d, sort_keys=True, | ||
indent=2, separators=(',', ': ')) | ||
return json.dumps(d, sort_keys=True, indent=2, | ||
separators=(',', ': '), cls=self.json_encoder) | ||
|
||
# noinspection PyBroadException | ||
def parse_body(self, request): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from json import JSONEncoder | ||
|
||
|
||
class TestJSONEncoder(JSONEncoder): | ||
def encode(self, o): | ||
return 'TESTSTRING' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this also include a
json_decoder
member? Roughly equivalent change with thejson.loads
->self.json_decoder(…).decode(s)
a couple places below.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Completely agree, I've just updated the branch with json_decoder support.