Skip to content
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

ArrayType serialization with 2D+ arrays #14

Open
Byrth opened this issue Sep 21, 2020 · 3 comments
Open

ArrayType serialization with 2D+ arrays #14

Byrth opened this issue Sep 21, 2020 · 3 comments

Comments

@Byrth
Copy link

Byrth commented Sep 21, 2020

Currently ArrayType() iterates element-wise over an array type using the default iterate(x::T) method when serializing. This has the effect of flattening higher dimensional arrays into vectors. Matrices, for instance, become vectors with no information about their original dimensions.

The JSON standard maintains array order, so it seems safe for me to serialize matrices as vectors of vectors. However, my only way to affect serialization is by overloading the iterate(x::T) function and doing so for the Matrix type seems likely to recompile large parts of Julia base (as well as break my own code.)

Then on deserialization, I would just construct(::Type{Matrix}, x::Vector) = hcat(x...)

It makes sense on some level that this would be natively unsupported because it makes the serialized representation of matrices and vectors of vectors indistinguishable (and thus deserialize(serialize(matrix)) != matrix without an additional construct definition), but that code doesn't work right now either and it seems like there should be some way to for users to enable support for it because matrices are so common. Perhaps there should be something analogous to construct() for serialization?

@tpapp
Copy link
Contributor

tpapp commented Apr 15, 2021

I agree that it would be great to support this natively. A lot of formats (JSON, TOML, ...) just support flat vectors.

I am wondering if <: AbstractArray types except <: AbstractVector could be serialized as a StructTypes.Struct() instead, with the axes and the elements (as a vector) as fields. Perhaps even rename the current ArrayType to VectorType.

@disberd
Copy link

disberd commented Mar 18, 2022

Would this feature also inherently enable multidimensional array support in JSON3? quinnj/JSON3.jl#196

@quinnj
Copy link
Member

quinnj commented Apr 14, 2022

Yeah, we could perhaps explore implementing @tpapp's idea of serializing higher-order arrays as a struct and see how that goes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants