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

serialization of CDateTime data types fail. Uisng .net 5 in library project #164

Closed ColdFusionShare closed 3 years ago

ColdFusionShare commented 3 years ago

All members of datatype/class CDateTime fail with following error:

Message "There was an error reflecting member 'lastLogon'" string InnerException {"There was an error reflecting member 'Date'"} System.Exception {System.InvalidOperationException}

I assume de serialization would have the same issue.

I'd like to know if this is a bug or if I need to override your serialization classes to handle CDateTime types/classes? This is in a .net 5 library project since MS depreciated binary serialization/de-serialization!

If this does require overriding or extending a class an idea of where/how to do this would be helpful.

This is to move a production library for our school board to .net 5.

Thanks,

Peter

jefffhaynes commented 3 years ago

That is "as designed". The intent of the serializer is not to serialize arbitrary objects (which may not actually be serializable in a meaningful way, as you've discovered), but to allow you to control the formatting of the serialized data from POCO (plain-ol' CLR objects) that can be crafted to match the serialized data. This is typically used for interoperability with legacy systems and the like (for example, people have used this library to read old save game formats, old protocols, etc.). If you're simply looking to serialize data, there are likely better ways of doing that (e.g. JSON, a database, etc.).

If you do want to use BinarySerializer for this, I would recommend writing an object graph yourself with everything the way you want it. But this is more demanding than other serialization approaches as BinarySerializer has no concept of how to represent a DateTime, for example. Again, that's intentional as there is no standard way of encoding time in a binary format. That's not true in something like json where the representation of those types are (mostly) part of the semantic language definition.

BinaryFormatter was always weird b/c it deeply encoded a lot of type information that was quite fragile across domains/versions. In order to serialize arbitrary objects, BinarySerializer would have to do something similar, and that's not really the goal.

ColdFusionShare commented 3 years ago

CDateTime has been built into vs for at least 15 years. No question about representation for binary in out, just save ticks. Since I would need to rewrite everything it does does not make sense to use your project unfortunately.

Thanks for the quick reply!

Peter

Sent from my iPhone

On Nov 20, 2020, at 1:34 PM, Jeff Haynes notifications@github.com wrote:

 Closed #164.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

jefffhaynes commented 3 years ago

I doubt very much that BinaryFormatter is serializing CDateTime as ticks as the underlying type is time_t, which is seconds since the epoch, not ticks. To be honest, I'm not clear as to which "CDateTime" you're referring as I don't believe there is a built-in primitive of that name.

Regardless, if you want BinaryFomatter to serialize date/time as ticks, you can do something like

public class MyCDateTime
{
   public long Ticks => DateTime.Ticks;

   [Ignore]
   public DateTime DateTime { get; set; }
}

But yes, you would have to convert to this approach.