-
-
Notifications
You must be signed in to change notification settings - Fork 921
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
JSON custom default and object hook #1202
JSON custom default and object hook #1202
Conversation
Ah, looks like ujson does not support the object_hook parameter on ujson.loads or the default parameter on ujson.dumps. |
So, I've thought about a few different ways to handle the fact that ujson doesn't support these kwargs, and locally I've implemented what I feel like is the best idea I've come up with. I'm checking once if ujson is installed when the JSONHandler is instantiated, using pkgutil.find_loader. I didn't expect that this would introduce a lot of performance overhead. I'm then conditionally setting the contents of a couple of dictionaries in JSONHandler, which can be unpacked in the call to json.dumps in serialize and the call to json.loads in deserialize (so that the kwargs default/object_hook are not passed if ujson is installed). I'm also raising an exception if either argument is specified when JSONHandler is instantiated if ujson is installed (so that there would be an early indication of a problem, rather than the parameter(s) being silently ignored by JSONHandler). I've also modified the tests I wrote to expect these exceptions when ujson is installed - having the tests set up like this accomplishes what I'm going for, but feels just a little funny. I plan to think about this a little bit more before pushing up these changes. I'm certainly open to other suggestions! |
…as it does not currently support these parameters
Codecov Report
@@ Coverage Diff @@
## master #1202 +/- ##
==========================================
- Coverage 100% 99.83% -0.17%
==========================================
Files 38 38
Lines 2484 2494 +10
Branches 364 366 +2
==========================================
+ Hits 2484 2490 +6
- Misses 0 2 +2
- Partials 0 2 +2
Continue to review full report at Codecov.
|
I'm happy to work on the test coverage a bit, but I think I'll wait for a little feedback on if I'm even headed in the direction we want to go, first. :-) |
…r object_hook when ujson is installed
…json if ujson is installed, as mock is not available < 3.3
Hmm, still struggling with coverage on the exceptions that fire if setting default or object_hook when ujson is installed. I tried using unittest.mock.patch to make it look as though ujson is installed when the tests are run, to force execution down that path and test that the exceptions are raised - but I forgot that mock isn't available in some of the earlier supported Python versions. Not sure if I'm overlooking something here, or if taking a different tack would be appropriate. I'll give it some more thought - and I remain very open to feedback! |
Hi @jhildreth, thanks for your patience. I think that if we implement #1206 it will make your work a lot easier. We can stipulate that if a third-party json library is configured, it must support the hooks in question in order to use this feature. |
Thanks for your work on this! It really helped us compare and contrast the different ways we could approach this problem. That being said, after discussing this with the maintainers team we ultimately decided to go with #1404. If you have a minute to take a look and provide your feedback, that would be great. |
[superseded by #1404] |
Sure thing! I glanced at #1404, but honestly it's been a little while since I looked at this code, so I'm not as familiar with the code as I could be. It looks like a lot of good thought has gone into it! |
fixes #1192
Here's my first attempt at 1192. I made my best guess as to where to document these changes...
A couple questions: