atilaneves / cerealed

Powerful binary serialisation library for D
BSD 3-Clause "New" or "Revised" License
92 stars 3 forks source link

serialize private fields #10

Open extrawurst opened 8 years ago

extrawurst commented 8 years ago

Is it possible to serialize private fields of classes and structs ?

extrawurst commented 8 years ago

ok i dug a bit deeper and found out __traits(derivedMembers..) and the like do not support accessing private fields, I had the same problem when i wanted to add private autowiring to the dependency injection framework poodinis. the solution as i found out using the irc is using T.tupleof. for reference the change in poodinis is here: https://github.com/Extrawurst/poodinis/commit/7d2ba1bef94aa569e28f8739ffa09fe0e0d42071

atilaneves commented 8 years ago

tupleof doesn't work on classes though, the base classe's variables would be left out. There could be a static if for structs but that could be messy. I'll take a look but I don't know if anything can be done.

A workaround is to mark the private member @NoCereal and use postBlit, or accept to customize it and do it from the struct/class itself.

atilaneves commented 8 years ago

I just tried a little experiment with __traits(getAttributes) and tupleof and it doesn't seem to work. So using it would mean a lot of the functionality that cerealed provides would stop working.

extrawurst commented 8 years ago

we had the same problem with the base classes in poodinis and it can be solved iterating over the base classes also at ct: https://github.com/mbierlee/poodinis/commit/f84b0251100f1a8d84c3e31650e710e24ab07166

i am pretty sure what needs to be done can be done using tupleof and getAttributes, we do the same in poodinis: https://github.com/mbierlee/poodinis/blob/master/source/poodinis/autowire.d#L124

or what is it exactly with __traits(getAttributes) that does not work with tupleof?

atilaneves commented 8 years ago

Ah, I see. Your example does it correctly by using T.tupleof[i] to get the attributes, I used the value instead of a type, which is why it didn't work. Makes sense.

Well, then it seems to be possible, but incredibly annoying and a lot work to do. Given that there's a workaround and I'm not terribly interested in supporting private fields, I'd be more than happy to accept patches, but I don't think I'll work on this myself.