Fix typed arrays serialization deserialization#2927
Conversation
There was a problem hiding this comment.
I can think of 2 better (not orthogonal) alternatives:
vtkImageData.indexToWorldandworldToIndexmust not be typed array but regular arrays, which are 64 bits anyway.vtkImageData.indexToWorldandworldToIndexshall not be serialized but must be flagged as protected by adding_prefix and recomputed at deserialization.
|
to me those are not alternatives to the fix I'm proposing which is necessary but definitely you have a good point. |
I agree, your fix is also necessary. |
|
we actually uses |
|
The image direction matrices are a consequence of using gl-matrix for construction and allocation, since it defaults to Float32Arrays. I partially agree with @finetjul that the index/world transform matrices should be hidden from serialization, but having them is nice for downstream code to not have to always recompute the transforms upon deserialization (e.g. sending over the network). |
the behavior of JSON.stringify for typed arrays is to serialize them as objects with numeric keys. This cause crashes when re-building the object since it would try to fit an object where a typed array is required. This changes makes it so getState replaces typed arrays with plain javascript arrays.
fb62f4c to
0ede1a2
Compare
|
Thanks for review and feedback guys! |
|
Let's leave it as is. |
|
🎉 This PR is included in version 28.12.4 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Context
The behavior of JSON.stringify for typed arrays is to serialize them as objects with numeric keys. This cause crashes when re-building the object since it would try to fit an object where a typed array is required. This changes makes it so getState replaces typed arrays with plain javascript arrays.
Typical example is
vtkImageDatadirection property, which is by default Float64Arrayminimal reproduction:
see also Kitware/glance#480
Results
makes serialization/deserialization possible and successful
Changes
This change makes it so we serialize typed arrays as arrays.
PR and Code Checklist
npm run reformatto have correctly formatted codeTesting