You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I use @JsonProperty and @JsonIdentityInfo to link a child and parent object together. This works, but I have to re-arange the JSON string before I feed it to jacksonObjectMapper.readValue() to avoid a UnresolvedForwardReference exception. There is no cyclic dependency between the two objects, the child only refers to the parent, not the other way around.
A complete and standalone testcase is attached below. Note how I changed the order of the "parents" and "children" elements around in the JSON string.
import com.fasterxml.jackson.annotation.JsonIdentityInfo
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.annotation.ObjectIdGenerators
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
data class Family(val children: List<Child>, val parents: List<Parent>)
data class Child(val id: Long, val name: String, @JsonProperty("parentID") val parent: Parent)
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator::class, property = "id")
data class Parent(val id: Long, val name: String)
fun main(args: Array<String>) {
val orderedJson = """
{
"parents": [
{
"name": "Matthew",
"id": 101
},
{
"name": "Sandra",
"id": 102
}
],
"children": [
{
"name": "Peter",
"id": 201,
"parentID": 101
},
{
"name": "Eva",
"id": 202,
"parentID": 101
},
{
"name": "Dave",
"id": 203,
"parentID": 102
}
]
}
"""
val orderedFamily: Family = jacksonObjectMapper().readValue(orderedJson)
println(orderedFamily.children.get(0).name)
println(orderedFamily.children.get(0).id)
println(orderedFamily.children.get(0).parent.name)
val unorderedJson = """
{
"children": [
{
"name": "Peter",
"id": 201,
"parentID": 101
},
{
"name": "Eva",
"id": 202,
"parentID": 101
},
{
"name": "Dave",
"id": 203,
"parentID": 102
}
],
"parents": [
{
"name": "Matthew",
"id": 101
},
{
"name": "Sandra",
"id": 102
}
]
}
"""
val unorderedFamily: Family = jacksonObjectMapper().readValue(unorderedJson)
println(unorderedFamily.children.get(0).name)
println(unorderedFamily.children.get(0).id)
println(unorderedFamily.children.get(0).parent.name)
}
Output:
Peter
201
Matthew
Exception in thread "main" com.fasterxml.jackson.databind.deser.UnresolvedForwardReference: Could not resolve Object Id [101] (for [simple type, class com.bsmulders.onverhardkaardgenerator.Parent]).
at [Source: (String)"
{
"children": [
{
"name": "Peter",
...
The text was updated successfully, but these errors were encountered:
I use @JsonProperty and @JsonIdentityInfo to link a child and parent object together. This works, but I have to re-arange the JSON string before I feed it to jacksonObjectMapper.readValue() to avoid a UnresolvedForwardReference exception. There is no cyclic dependency between the two objects, the child only refers to the parent, not the other way around.
A complete and standalone testcase is attached below. Note how I changed the order of the "parents" and "children" elements around in the JSON string.
Output:
The text was updated successfully, but these errors were encountered: