Hi. I have a problem with deserialising `data clas...
# announcements
e
Hi. I have a problem with deserialising
data class
contains params with
inline
classes. For deserialisation I use
jackson
and it fail with it. But then I try to discover a problem I found that this
data class
compile to class with private main constructor. Is it expected behaviour or bug (for me at lease, and feature for others)? It seems not right solutions for it, because for initialisation of
Outer
class kotlin compile use synthetic method with
DefaultConstructorMarker
witch doesn’t contains any additional check for inline params and only calls private constructor after. Why just not make main constructor
public
and use it for initialisation? Simple example for reproduce is below:
Copy code
inline class InlineValue(val value: String)

data class Outer(
    val value: Int,
    val inlineValue: InlineValue
)
and in will be compiled to:
Copy code
public final class Outer {
   ...
   private Outer(int value, String inlineValue) {
      this.value = value;
      this.inlineValue = inlineValue;
   }
    ...
}
d
The reason is to specifically disallow the calling of the constructor (from e.g. Java) and just passing the underlying type of the inline class. In the future inline classes will have
init
blocks, which requires special logic when calling methods/constructors with inline-class parameters.
If you want to allow creation of the class with just the underlying value you should provide a separate factory method (or constructor). You can then mark that for Jackson to use using
@JsonCreator
.
Otherwise if you are just "abusing" inline classes to get a particular JSON representation you should use things like
@JsonValue
in the class you call
InlineValue
instead.
e
But in Java you could call synthetic method, and as I suppose they also be public in future. Why just not to add checks for inline parameters to data class constructor, and leave a constructor public?
d
You can't call a synthetic method from Java.