-
Notifications
You must be signed in to change notification settings - Fork 175
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
public val/var in constructor produces 'No String-argument constructor/factory method to deserialize from String value' #91
Comments
I debugged this as well, and it appears to occur because code in Therefore in Hiding the property getters/setters changes this behaviour. I expected adding an empty constructor to the class in question would provide a workaround but it doesn't seem to.
|
Are you intending to use the single string constructor model from Jackson where the string is passed to the constructor that then parses it to set the values? Or are you trying to basically I think you are intending to use the unwrapped, or can you provide a sample that isn't ambiguous as to the intent. |
Hey @apatrida the former case you described is what I have and I'm running into exactly this issue, minimal example: import com.fasterxml.jackson.annotation.JsonValue
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
data class ValueObject(@JsonValue val value: String)
fun main(args: Array<String>) {
val mapper = jacksonObjectMapper()
val vo = mapper.readValue<ValueObject>(""""value"""")
check(vo == ValueObject("value"))
} Deserialization fails with:
The only workaround I found so far is the following: import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonValue
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
data class ValueObject(@JsonValue val value: String) {
private companion object {
@JsonCreator
@JvmStatic
fun valueOf(value: String) = ValueObject(value)
}
}
fun main(args: Array<String>) {
val mapper = jacksonObjectMapper()
val vo = mapper.readValue<ValueObject>(""""value"""")
check(vo == ValueObject("value"))
} |
Using import com.fasterxml.jackson.annotation.JsonValue
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
data class ValueObject(@get:JsonValue val value: String)
fun main(args: Array<String>) {
val mapper = jacksonObjectMapper()
val vo = mapper.readValue<ValueObject>(""""value"""")
check(vo == ValueObject("value"))
} This is definitely much better. |
|
1 similar comment
|
@Fleshgrinder your issue is unrelated to this issue, and works with 2.10.x version of the module (at least on the 2.10 branch it passes) |
I am wondering if what @robpridham-bbc is doing is instead trying to use the ideas of
If that is not the case, maybe is intending to use But really I think he just wants the single string constructor to work as-is, so I think this is the solution here:
Closing this issue because nothing reported here is an actual bug. |
As per FasterXML/jackson-dataformat-xml#254, I have a very similar problem. I'm on 2.9.1. The case is so simple that I'm not sure my usage is correct, but the functioning workaround suggests it's legitimate.
The following should serve as a self-contained example.
This fails with the following:
If I rework DataClass2 to have
private val
in the constructor, or indeed noval
keyword in its constructor at all, then this works.The text was updated successfully, but these errors were encountered: