Unresolved forward references when trying to deserialize JSON into java objects

11 views
Skip to first unread message

Marcin Kwapisz

unread,
Jul 26, 2019, 4:46:15 PM7/26/19
to jackson-user
A few days ago I stuck with the problem in the topic. I found a solution and described it on Stackoverflow.

Here I would like to ask why I cannot use @JsonCreator annotated methods or constructors and why a parameterless constructor is required on one side. This is the first time I use Jackson, just because I had to serialize/deserialize Java objects with circular references

Best Regards
Marcin

Tatu Saloranta

unread,
Jul 30, 2019, 2:43:21 PM7/30/19
to jackson-user
It is difficult to answer the question without digging deeper, but
here are some notes:

1. Use of constructors / factory methods requires buffering of
properties and may result in cyclic cases where it is literally
impossible to construct where there is a cycle between values needed
for creator methods (cosntructor / factory). For example:

public class A {
@JsonCreator A(B child) { ... }
}

public class B {
@JsonCreator B(A parent) { .... }
}

is impossible to resolve. So sometimes it may be necessary to
leave out one particular dependency from Creator, and pass it via
setter (or directly assignt o field)

2. Use of `@JsonCreator` is optional if (and only if) all Creator
parameters have explicit marker (and unless parameter names available,
name) -- that is, `@JsonProperty`.

3. Factory methods are not auto-detected, and must be `@JsonCreator`
annotated; also must return type of declaring class (or subtype).

4. If no Creator method is discovered, then (but only then) a
no-arguments Constructor is required

Not sure if this helps answering the question. I can not see any
obvious problem in your original example, so it might be worth filing
an issue (against `jackson-databind`) for.

-+ Tatu +-
Reply all
Reply to author
Forward
0 new messages