[core-serialiser] Is serialisation of Optional values supported?
See original GitHub issueIs serialisation of Optional
values supported?
I can see some references to supporting optional values in the codebase – such as BinarySerializers.ofOptional
– but a naive attempt to serialise an optional value fails with:
Exception in thread "main" java.lang.IllegalArgumentException: Method not found: <init> []
at io.activej.codegen.Context.findMethod(Context.java:387)
at io.activej.codegen.Context.invokeConstructor(Context.java:324)
at io.activej.codegen.Context.invokeConstructor(Context.java:318)
at io.activej.codegen.expression.ExpressionConstructor.load(ExpressionConstructor.java:40)
at io.activej.codegen.expression.ExpressionLet.load(ExpressionLet.java:34)
at io.activej.codegen.expression.ExpressionSequence.load(ExpressionSequence.java:44)
at io.activej.codegen.expression.ExpressionLet.load(ExpressionLet.java:34)
at io.activej.codegen.expression.ExpressionSequence.load(ExpressionSequence.java:44)
at io.activej.codegen.ClassBuilder.defineNewClass(ClassBuilder.java:380)
at io.activej.codegen.ClassBuilder.build(ClassBuilder.java:293)
at io.activej.codegen.ClassBuilder.buildClassAndCreateNewInstance(ClassBuilder.java:447)
at io.activej.serializer.SerializerBuilder.buildImpl(SerializerBuilder.java:854)
at io.activej.serializer.SerializerBuilder.build(SerializerBuilder.java:316)
at io.activej.serializer.SerializerBuilder.build(SerializerBuilder.java:310)
This uses:
public static class OptionalTest
{
@Serialize(order = 0)
public final Optional<String> optional;
public OptionalTest(@Deserialize("optional") final Optional<String> optional)
{
this.optional = optional;
}
}
and
final OptionalTest optionalTest = new OptionalTest(Optional.of("hello, world"));
final BinarySerializer<OptionalTest> serializer =
SerializerBuilder.create()
.build(OptionalTest.class);
final StreamOutput stream = StreamOutput.create(new ByteArrayOutputStream());
serializer.encode(stream.getBinaryOutput(), optionalTest);
How do we serialise Optional
values?
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Serialize/De-Serialize Optional values using IoBuffer
In our project we are using the org.apache.mina.core.buffer.IoBuffer to serialize and deserialize objects and send it through the network.
Read more >Default serialization format for TimeSpan - .NET
This support changes the default serialization format for TimeSpan values in source generators. Previous behavior. In .NET 6 GA, source ...
Read more >Serialization in Java - DigitalOcean
Serialization in Java allows us to convert an Object to stream that ... Notice that the method arguments work with Object that is...
Read more >Using Optional with Jackson - Baeldung
If we think about it, what we want is for actual the value of the subtitle field to be serialized. 2.4. Deserialization.
Read more >Java Object Serialization Specification: 1 - System Architecture
Objects to be saved in the stream may support either the Serializable or the ... are saved or restored, and write and read...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@gkopff I forgot to add a casting to
Foo
inSerializerDefOptional#encoder
. Here is an updated version ofSerializerDefOptional
. Now, Optionals are serialized/deserialized correctly.Here’s the whole thing (it also needs your SerializerDefOptional).
The two commented out
main
methods work.The first serialises a
FooHolder
which contains aFoo
. This works and utilises aSerializerDefFoo
to serialise theFoo
that’s inside theFooHolder
.The second serialises a
OptionalStringHolder
which contains anOptional<String>
. This works and utilises yourSerializerDefOptional
to serialise theOptional<String>
that’s inside theOptionalStringHolder
.Finally, the third one – the one not commented out – tries to serialise a
OptionalFooHolder
which contains aOptional<Foo>
. However, this fails with: