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

Preserve order of struct fields? #159

Open
Marlin-Na opened this issue Mar 12, 2021 · 0 comments
Open

Preserve order of struct fields? #159

Marlin-Na opened this issue Mar 12, 2021 · 0 comments

Comments

@Marlin-Na
Copy link

Hi, would you consider using OrderedDict instead of Dict when reading struct, so that the order of struct fields can be preserved? The following patch has worked for my particular application. (Though I have been wishing Julia's default Dict implementation can preserve order)

diff --git a/src/MAT_HDF5.jl b/src/MAT_HDF5.jl
index cfacf3e..f0c8d90 100644
--- a/src/MAT_HDF5.jl
+++ b/src/MAT_HDF5.jl
@@ -32,6 +32,7 @@ using HDF5, SparseArrays
 
 import Base: names, read, write, close
 import HDF5: Reference
+import OrderedCollections: OrderedDict
 
 const HDF5Parent = Union{HDF5.File, HDF5.Group}
 const HDF5BitsOrBool = Union{HDF5.BitsType,Bool}
@@ -230,12 +231,8 @@ function m_read(g::HDF5.Group)
             error("Cannot read from a non-struct group, type was $mattype")
         end
     end
-    if haskey(g, "MATLAB_fields")
-        fn = [join(f) for f in read_attribute(g, "MATLAB_fields")]
-    else
-        fn = keys(g)
-    end
-    s = Dict{String, Any}()
+    fn = [join(f) for f in read_attribute(g, "MATLAB_fields")]
+    s = OrderedDict{String, Any}()
     for i = 1:length(fn)
         dset = g[fn[i]]
         try
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

1 participant