Question: are immutable objects with builders supported?
In our code, we have a lot of DTOs like this:
@lombok.Value
@lombok.Builder
class Entity {
String requiredField;
@lombok.Builder.Default
String requiredFieldWithDefaultValue = "default value";
}
For the provided example, Lombok generates a private constructor, getters, and a builder with a default value for requiredFieldWithDefaultValue field.
Will such an object be correctly deserialized by this library? By "correctly" I mean:
- Deserialization works with a private constructor.
- If the
requiredFieldWithDefaultValuefield was added only in the second version of the class, deserializing content from the first version (without this field) will have"default value"as a default value for the field.
- Deserialization works with a private constructor.
Yes, that will work.
- If the
requiredFieldWithDefaultValuefield was added only in the second version of the class, deserializing content from >the first version (without this field) will have"default value"as a default value for the field.
No, the default value will be ignored, as the original serialized version did not have the field it will be set to ‘null’
The Serializer will not call constructors or other methods of the (de)serialized classes if no specialized handler is implemented.
@hg-ms, thanks for the answer!
Could you please elaborate a bit on how it works? Does this library generate bytecode? Does it use a private all-args constructor? Any other way? You can just share a link to the class(es) that does it.
Thanks!
By default we use sun.misc.Unsafe.allocateInstance(Class<?>) to create object instance and later on the fields and primitive values are populated using the “put” methods of sun.misc.Unsafe. This is implemented in org.eclipse.serializer.memory.sun.JdkMemoryAccessor. There is also an alternative MemoryAccessor “MemoryAccessorGeneric” that uses reflections instead of Unsafe.
The object creation is done by our TypeHandlers that are generated dynamically if there is no specialized handler for a class provided, they also define how objects are (de)serialized. You may have a look into BinaryHandlerGenericType.java if you are interested in details.
There has been no further communication since March, and no actions have been taken on our end. If you encounter another problem, please feel free to create a new issue or reopen this one.