JuliaIO / JLD2.jl

HDF5-compatible file format in pure Julia
Other
547 stars 85 forks source link

preprocess on a struct before saving it #426

Closed dpinol closed 1 year ago

dpinol commented 1 year ago

Imagine that I have this struct

struct S
   ....many fields
   function S(arguments different than the field)
        new(....)
   end 
end

Is there a way to serialize S with the default mechanism but just altering the value of 1 field? I tried using writeas, but

  1. I cannot create a SSerialization struct having S as its only field, since it gets into a loop
  2. I cannot persist the fields one by one, since the inner constructor is hiding the default constructor.

thanks

JonasIsensee commented 1 year ago

Hi @dpinol ,

I don't quite understand what you are trying to achieve. Could you try to create a a simple "working" example? E.g. a struct definition and the converted version that you would like to see?

dpinol commented 1 year ago

I have a data structure with circular references (struct S has a reference to struct T, which has a reference to struct S), and hence I need to break the loop so that JLD2 does not StackOverflow. In a simplified case, I'd like to do persist struct S with one of its fields set to nothing.

struct S
  f
  fieldToHide
  S(g) = new(g*2)
end 

I'd like to do @set s.fieldToHide=nothing (using Accessors.jl) before writing S. If S was my top struct, it would be easy, but since S is an inner struct in my data structure, I'd like to be able to define something like: JLD2.beforeWrite(s::S) = @set s.fieldToHide=nothing

thanks

JonasIsensee commented 1 year ago

JLD2 is usually able to detect circular references and should correctly save them.

Have you tried this? Can you show me error messages?

dpinol commented 1 year ago

JLD2 is usually able to detect circular references and should correctly save them.

Have you tried this? Can you show me error messages?

Thanks for your quick answer. I created #427, as it's an different issue