Skip to content

Fix serialization of complex 2D arrays

Vicentini Filippo requested to merge serialization into master

Created by: femtobit

This PR fixes a bug encountered when serializing certain 2D arrays of complex dtypes to JSON using orjson, such as

a = np.array([[1.0j], [1.0j]])

This happened because we use a custom default function to encode complex dtype arrays to JSON as

{"real": obj.real, "imag": obj.imag}

While this looks like it should work together with orjsons built-in numpy support (which can handle real dtype arrays), it can fail in some cases. This is because obj.real can be a non-contiguous array and those are unsupported by orjson. This is fixed here by calling np.ascontiguousarray on the returned arrays.

Merge request reports