10/29/2023 0 Comments Sealed class serializable kotlin![]() ![]() The one-time setup for this solution is small.Sealed case classes only need to implement the marker interface, no further configuration needed. The solution has almost no boilerplate on the domain classes.The solution properly serializes and deserializes sealed cases classes (see Tests).Implementation details and discussion are provided afterĭefine a single marker interface in your domain module. The following example demonstrates the solution. Most importantly, it correctly deserializes an previously serialized value. The solution has minimal boilerplate and the class naming strategy can beĬonfigured once and for all case classes. This repository presents a solution based on a single marker interface that fulfills the three initially Proposed solution for handling sealed cases classes Therefore, we aim for a solution that works that configures the object mapper to work without annotations. However, that would be verbose and clutter an otherwise concise domain representation. These issues might be resolvable by annotations on SealedClass and the subtypes A, B, and C. Deserialization of a value into SealedClass fails because the serialized json value does not contain type information (i.e.That means, the must property is not fulfilled. Deserialization of 'kotlin object' creates new instances, ie.ĪssertThat(deserialize(serialized(C)), equalTo(C)) will fail.Without a custom configuration of the object mapper, there are several issues: Issues without custom mapper configuration (COULD) The class name ("A", "B", or "C") is used as-is in the json string, without further prefixes.(SHOULD) The amount of boiler plate on such domain classes is kept minimal (ideally none at all).(MUST) A value that is serialized and deserialized compares equal to the initial value, i.e.ĪssertThat(deserialize(serialized(value)), equalTo(value)).Some desirable properties for an object mapper are: Data class A( val name : String) : SealedClass()ĭata class B( val name : Double, val age : Int) : SealedClass() ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |