-
Notifications
You must be signed in to change notification settings - Fork 70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
serialize non primitive types #235
Comments
You got the right idea that you need a custom serializer for the ECP class. There are a few comments though. I looked at the source code of the Here is the updated public class CustomSerializer extends AbstractSerializerDef {
private static final Expression ARRAY_LEN = value(BIG.MODBYTES + 1);
@Override
public Class<?> getEncodeType() {
return ECP.class;
}
@Override
public Expression encoder(final StaticEncoders staticEncoders,
final Expression buf,
final Variable pos,
final Expression ecp,
final int version,
final CompatibilityLevel compatibilityLevel) {
return let(arrayNew(byte[].class, ARRAY_LEN),
array -> sequence(
call(ecp, "toBytes", array, value(true)),
writeBytes(buf, pos, array)));
}
@Override
public Expression decoder(final StaticDecoders staticDecoders,
final Expression in,
final int version,
final CompatibilityLevel compatibilityLevel) {
return let(arrayNew(byte[].class, ARRAY_LEN),
array -> sequence(
readBytes(in, array),
staticCall(ECP.class, "fromBytes", array)));
}
} BTW, when writing your own SerializerDef it is sometimes useful to create a SerializerBuilder.create(
DefiningClassLoader.create()
.withDebugOutputDir(debugPath)
) That way you can inspect the debug folder to see the generated classes. |
@eduard-vasinskyi Thank you very much for your wonderful answer it saves my day. i really appreciate it. You did an awesome job with activeJ i really admire your work and i already use a lot of things as libraries in my project. wish you the best, my friend have a great day |
BTW, we have added an abstract class SimpleSerializerDef that you can extend in order to simplify the creation of serializers for custom classes. This class allows you to define With public class CustomSerializer extends SimpleSerializerDef<ECP> {
@Override
protected BinarySerializer<ECP> createSerializer(int version, CompatibilityLevel compatibilityLevel) {
return new BinarySerializer<ECP>() {
@Override
public void encode(BinaryOutput out, ECP item) {
byte[] bytes = new byte[BIG.MODBYTES + 1];
item.toBytes(bytes, true);
out.write(bytes);
}
@Override
public ECP decode(BinaryInput in) throws CorruptedDataException {
byte[] bytes = new byte[BIG.MODBYTES + 1];
in.read(bytes);
return ECP.fromBytes(bytes);
}
};
}
} This change is part of ActiveJ v5.4.3, which was released today. |
@eduard-vasinskyi
|
There is no built-in way to pre-calculate the size of the buffer. You could add an external method for estimating a buffer size individually for the serialized object (if you can predict the size beforehand). Another approach that we use for serializing a stream of data is to start from the small buffer size and then increase it if we underestimated the size of the serialized object. A |
@eduard-vasinskyi ok thnx for your time i will search it further. Your approach is not bad i will consider implement it |
I use this library in my project and works very well with strings, integers,bytes, etc and i am very happy but when i tried to use object classes from dependencies, the serialization did not work well.
Assuming this library and a simple example
The output is this:
but the two comparable values are not equal, also the data seems lost during serialization why this has happened?
In order to solve my problem I was trying to write custom serializers but with no success but I am barely sure that I am close this is my custom serializer class:
i am trying to seriliaze
tobytes
method and decodefrombytes
. this is the source code to take look at this method.BinarySerializer<Ar> enc = SerializerBuilder.create().with(ECP.class, ctx -> new CustomSerializer()).build(Ar.class);
The exception that i got is this:
I think the problem of the exception is that i call
toBytes
method which is void, i must somehow to get the first parameter of void as an expression something like this, i am not sure, how to deal with it?The text was updated successfully, but these errors were encountered: