Skip to content
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

@JsonProperty and @JsonIdentityInfo require ordered data #195

Closed
bsmulders opened this issue Nov 29, 2018 · 1 comment
Closed

@JsonProperty and @JsonIdentityInfo require ordered data #195

bsmulders opened this issue Nov 29, 2018 · 1 comment

Comments

@bsmulders
Copy link

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",
...
@k163377
Copy link
Contributor

k163377 commented Feb 17, 2023

Issues last updated before 2020 are closed.
If the problem is reproduced in the latest version, please reopen.

@k163377 k163377 closed this as completed Feb 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants