Skip to content

Commit

Permalink
Throwable serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
martinrosstmc committed Nov 17, 2018
1 parent db7ba74 commit 00747d5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@ protected static FSTConfiguration initDefaultFstConfigurationInternal(FSTConfigu
reg.putSerializer(Hashtable.class, new FSTMapSerializer(), true);
reg.putSerializer(ConcurrentHashMap.class, new FSTMapSerializer(), true);
reg.putSerializer(FSTStruct.class, new FSTStructSerializer(), true);
reg.putSerializer(Throwable.class, new FSTThrowableSerializer(),true);

reg.putSerializer(BitSet.class, new FSTBitSetSerializer(),true);
reg.putSerializer(Timestamp.class, new FSTTimestampSerializer(),true);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.nustaq.serialization.serializers;

import org.nustaq.serialization.FSTBasicObjectSerializer;
import org.nustaq.serialization.FSTClazzInfo;
import org.nustaq.serialization.FSTObjectInput;
import org.nustaq.serialization.FSTObjectOutput;

import java.io.IOException;
import java.lang.reflect.Constructor;

public class FSTThrowableSerializer extends FSTBasicObjectSerializer {
@Override
public void writeObject(FSTObjectOutput out, Object toWrite, FSTClazzInfo clzInfo,
FSTClazzInfo.FSTFieldInfo referencedBy, int streamPosition) throws IOException {
Throwable t = (Throwable)toWrite;
out.writeStringUTF(t.getMessage());
StackTraceElement[] ste = t.getStackTrace();
out.writeObject(ste);
out.writeObject(t.getCause());
out.writeObject(t.getSuppressed());
}


@Override
public Object instantiate(Class objectClass, FSTObjectInput in, FSTClazzInfo serializationInfo,
FSTClazzInfo.FSTFieldInfo referencee, int streamPosition) throws Exception {
Constructor<? extends Throwable> constructor = objectClass.getConstructor(String.class);
Throwable t = constructor.newInstance(in.readStringUTF()); // This causes stack trace to be filled in twice but not an easy way to solve
StackTraceElement[] ste = (StackTraceElement[]) in.readObject();
if (ste!=null)
t.setStackTrace(ste);
t.initCause((Throwable) in.readObject());
Throwable[] suppressed = (Throwable[]) in.readObject();
for (Throwable s : suppressed)
t.addSuppressed(s);
return t;
}
}

0 comments on commit 00747d5

Please sign in to comment.