Repository navigation
feat(utils): add NumpyJSONDecoder for deserializing encoded types - #8623
Hardik K (hardikkaurani) wants to merge 2 commits into
Conversation
|
Hello Mikhail Astafev (@astafan8), I have submitted this pull request to resolve issue #2931 by providing a dedicated The decoder inspects encoded dictionary payloads and restores Unit tests covering typed roundtrips, custom hooks, and existing metadata examples have been added to |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #8623 +/- ##
==========================================
+ Coverage 72.58% 72.59% +0.01%
==========================================
Files 307 307
Lines 32409 32422 +13
==========================================
+ Hits 23525 23538 +13
Misses 8884 8884 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Hi Jens Hedegaard Nielsen (@jenshnielsen), Could you please take a look at this PR when you have a moment? It adds All CI matrix jobs (pytest, mypy, docs, pre-commit, codecov) have passed across Python 3.12–3.15 on Linux and Windows. Thank you for your time! |
|
Hardik K (@hardikkaurani) Thanks for the contribution. The code looks fine. The reason we did not originally add a decoder is that there is no way to plug it into the json decoder directly. I assume your intention with this is that the user should call it directly? It would be nice if this worked automatically but I guess a manual decoder is better than nothing but in that case I think we should add an example documenting it |
…wsfragment Document how to use NumpyJSONDecoder with json.loads, json.load, and the default_object_hook. Also add regression tests for stream loading and object hook usage, and add a towncrier newsfragment for PR 8623. Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
|
Hi Jens Hedegaard Nielsen (@jenshnielsen) Thank you for the review and helpful feedback! You're absolutely right that Python's standard-library I've addressed this in commit
All 20 targeted pytest tests pass. Ruff checks, formatting, mypy, and Towncrier validation also pass cleanly. Thank you again for the guidance. Please let me know if any further adjustments are needed! |
Description
Fixes #2931.
QCoDeS provides
NumpyJSONEncoderto support serialization of types that Python's standardjsonmodule cannot handle out of the box, such as complex numbers (encoded as{"__dtype__": "complex", "re": ..., "im": ...}) anduncertainties.UFloatobjects (encoded as{"__dtype__": "UFloat", "nominal_value": ..., "std_dev": ...}).However, there was no corresponding decoder class to reconstruct these objects when reading JSON data back. Users decoding serialized dictionaries were left with raw dictionaries containing
__dtype__keys instead of restored Python objects.This PR introduces
NumpyJSONDecoderas a subclass ofjson.JSONDecoderwith a defaultobject_hookthat recognizes these encoded structures and restores them into their corresponding Python types:__dtype__ == "complex"and"re","im"fields are converted back to Pythoncomplex.__dtype__ == "UFloat"and"nominal_value","std_dev"fields are converted back touncertainties.ufloat.object_hookarguments can still be passed to customize or chain decoding logic.Testing
Added comprehensive unit tests to
tests/helpers/test_json_encoder.py:test_numpy_json_decoder_complex: Verifies decoding and type restoration for complex numbers.test_numpy_json_decoder_ufloat: Verifies roundtrip restoration ofuncertainties.ufloat.test_numpy_json_decoder_roundtrip_examplemetadata: Verifies decoding of complex numbers and plain types in the standardEXAMPLEMETADATAstructure.test_numpy_json_decoder_custom_object_hook: Verifies user-providedobject_hookcustomization.All 17 tests in
tests/helpers/test_json_encoder.pypass cleanly.ruff checkandruff formatare clean across all modified files.