jefffhaynes / BinarySerializer

A declarative serialization framework for controlling formatting of data at the byte and bit level using field bindings, converters, and code.
MIT License
292 stars 62 forks source link

Correct way of getting parent value during deserialization. #145

Closed mikedavison closed 4 years ago

mikedavison commented 4 years ago

I previously raised this in a complex way (see #88) but would like a more succinct answer. Pseudo Code:

Class A {
   [FieldOrder(0)] 
    pubic int intOfA;
   [FieldOrder(1)]
    public B myB;
}
Class B {
   [Ignore]
   public A parent;
   [FieldOrder(0)]
   [SerializeWhen("intOfA",1)]
   public double intOfB;
}

As i understand your code during deserialization , it would construct A, read "intOfA", then construct B and conditionally read "intOfB. My questions are:

  1. How can I set parent field prior to deserializing B? Normally I would set this by calling the class constructor ( public B(A parent) { this.parent = parent} )
  2. What is the correct way get the value of parent's intOfA to test whether to deserialize intOfB?
mikedavison commented 4 years ago

Ok so I have a handle on how the application works and figured out how to do what I need in a round about way. Lengthy description to help others. in a nutshell there seems no way to set the parent because the class object is only created after all of the data required is read from the deserializer. In the example above assuming no serializewhen , the sequence is:

  1. serializer reads value of intOfA
  2. serializer then reads value of intOfB
  3. serializer creates class Bobject
  4. serializer populates value of intOfB into class B object B
  5. now it has both fields for A it creates class A and adds intOfAvalue and object B

So, this means that I cannot set parentfield in Buntil after Ahas been created right at the end and this cannot be done in the confines of the serializer.

How did I get around my problem. I worked around some of the issues by using the serializer context feature. I still had to go over the objects and set parent references post deserialization.

jefffhaynes commented 4 years ago

Is intOfA something that is contained in the data source? If not, then the only way to do this is to pass in a serialization context that specifies intOfA for a given deserialization operation.