Repository navigation
feat(utils): add NumpyJSONDecoder for deserializing encoded types - #8623
Hardik K (hardikkaurani) wants to merge 1 commit 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! |
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.