rikimaru0345 / Ceras

Universal binary serializer for a wide variety of scenarios https://discord.gg/FGaCX4c
MIT License
484 stars 53 forks source link

Conversion callback for version tolerance #95

Open rikimaru0345 opened 3 years ago

rikimaru0345 commented 3 years ago

When serializes some class with version tolerance and then changes the type (and only the type) of some field inside that class, Ceras won't be able to do the conversion on its own.

Even for simple stuff it'd need to make assumptions, e.g. rounding for float a; => int a;. And for stuff like string a; => SomeClass a; its pretty much impossible.

Ceras should probably have a callback that allows the user to convert some old properties to a new type. The conversion callback stuff could look something like

void Convert(ConversionContext ctx); // provided by user

class ConversionContext {
  struct DeserializedEntry {
    string TypeName; // full type name
    string MemberName; // name of field or property
    object Value; // deserialized value; schema is embedded so ceras should be able to read it
  }

  string DeserializedTypeName; // type name that was originally used to write that data
  List<DeserializedEntry> DeserializedData;

  // Probably also need some way to tell the user where to put the result (target object, and where to set it, on what field/property in that target object). But that wouldn't really be compatible with structs...
  // maybe the context could be a ref struct or something?
}

For types that only contain primitive types that should be easy. But for types that contain class/struct members that won't work. So we'll probably have to make some sort of DeserializedObjectData that contains the type name and all fields... But that most likely won't be super comfortable to use for the end-user implementing the conversion.