Open golivax opened 7 years ago
Issue is, that @conditional might mess up the order classes occur in a serialized stream, I did not thought of this side effect when I introduced this annotations (like 5 years ago :) ). Anyway as its not covered by test it might simply be broken in the current release..
I see, that makes sense. Do you think there's anything I can do get away with it? Like a custom serializer or something? Accomplishing this would speed up my deserialization process a lot (based on tests I've done). Thanks!
Yes, you could use a custom serializer, but it will be somewhat tricky: You need to be able to calculate the endposition of the stream after having written it to the stream. At read time depending on your condition you either read the long string or call inputStream.getCodec().skip(XX) to just ignore the String. Unfortunately its hard to calculate the actual stream length of a given String in advance (optimizations, UTF-8), so you need a custom string-write loop.
pseudocode
public void writeObject(FSTObjectOutput out, Object toWrite, FSTClazzInfo clzInfo, FSTClazzInfo.FSTFieldInfo referencedBy, int streamPosition)
throws IOException;
{
out.defaultWriteObject(mylist);
byte b[] = longString.getBytes("UTF-8");
out.writeInt(b.length());
out.write(b);
}
public void readObject(FSTObjectInput in, Object toRead, FSTClazzInfo clzInfo, FSTClazzInfo.FSTFieldInfo referencedBy)
throws Exception;
{
mylist = in.defaultReadObject();
int len = in.readInt();
if ( CONDITION ) {
in.getCodec().skip(len);
} else {
byte b[] = new byte[len];
in.read(b);
mylongstring = new String(b,"UTF-8");
}
}
remove @Conditional as its limitations are too hard to understand / handle
Is it possible to use the
@Conditional
annotation in a String field? My class structure is something like this:I would like to add
@Conditional
to theveryLongString
field in class A. I tried to follow your examples, but I'm getting an exception. More specifically, just to try it out, I tried creating a call back that would always return false (i.e., it should never skip the field).The exception is as follows:
I don't exactly understand why it is complaining about the
loader:sun.misc.Launcher$AppClassLoader
class. Maybe my scenario violates the pre-conditions you mentioned (to be honest, I don't exactly understand what they mean, sorry):If I change the callback to always return true, I get another exception (I can post it if necessary). What I would really want to accomplish is skipping the
A.veryLongString
field based on the value ofA.parent.name.
In reality, the problem is not that
veryLongString
is actually that long. What happens is that I have a bunch of A objects with a bunch of X objects, and I only needveryLongString
for very few As...Thanks in advance. Please let me know if there is anything I could do to get around it or if you need more information about anything.