JavaDoc for @JsonCreator

210 views
Skip to first unread message

Ted M. Young [@jitterted]

unread,
Jan 28, 2014, 10:02:48 AM1/28/14
to jackso...@googlegroups.com
I'm trying to understand the use of single-argument constructors that have no @JsonProperty annotation. In the JavaDoc for @JsonCreator (http://fasterxml.github.io/jackson-annotations/javadoc/2.3.0/com/fasterxml/jackson/annotation/JsonCreator.html), it says:

Single-argument constructor/factory method without JsonProperty annotation for the argument: if so, this is so-called "delegate creator", in which case Jackson first binds JSON into type of the argument, and then calls creator

It's my understanding that only factory (e.g., static methods annotated with @JsonCreator) work this way and that single-argument constructors without @JsonProperty can't be used to deserialize a JSON object.

Am I misunderstanding what the JavaDoc is saying, or am I missing something?

;ted

Tatu Saloranta

unread,
Jan 28, 2014, 2:26:00 PM1/28/14
to jackso...@googlegroups.com
JavaDoc is correct, and intended usage something like:

public class Pojo {
  public int x, y;

  @JsonCreator
  public Pojo(Map<String,Object> stuff) {
     x = stuff.path("x").asIntValue();
     y = stuff.path("y").asIntValue();
  }
}

so that you could bind `Pojo` from, say,

  { "x" : 1, "y" : 2 }

and similarly you could alternatively use static factory method; there should be parity between constructors and in-class factory methods (support for external factory methods is something that would be nice to add too, FWIW).

Does this make sense?

Put another way, delegate-style creators can be viewed as reverse of @JsonValue; or perhaps even simplified delegating deserializer (creator method acts as deserializer).

-+ Tatu +-


 
;ted

--
You received this message because you are subscribed to the Google Groups "jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jackson-user...@googlegroups.com.
To post to this group, send email to jackso...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Ted M. Young

unread,
Jan 29, 2014, 4:08:22 PM1/29/14
to jackso...@googlegroups.com
Ahh, ok, so a "single-arg constructor" only works if the argument is a Map<String,Object>? I've also seen reference to List<> as the "inverse" of JsonValue. Are those the only two cases where a single-argument constructor (with @JsonCreator), but without @JsonProperty is valid?

Also, in your example you're using .path()? Is that from some library, or was that just example "magic" to make it easier to read? :)

;ted

Tatu Saloranta

unread,
Jan 29, 2014, 4:36:08 PM1/29/14
to jackso...@googlegroups.com
On Wed, Jan 29, 2014 at 1:08 PM, Ted M. Young <tedy...@gmail.com> wrote:
Ahh, ok, so a "single-arg constructor" only works if the argument is a Map<String,Object>? I've also seen reference to List<> as the "inverse" of JsonValue. Are those the only two cases

No not at all. Rather:
 
where a single-argument constructor (with @JsonCreator), but without @JsonProperty is valid?


In any case where the incoming JSON can be mapped to that single argument. So List<> would work for JSON arrays, JsonNode/Map/POJO for JSON objects, and Date/Time for String, say.

This is overlapping with implicit detection of public single-String/int/long/boolean-arg constructors, which is more of a legacy thing. So maybe that's where confusion comes from?
 
Also, in your example you're using .path()? Is that from some library, or was that just example "magic" to make it easier to read? :)

Whops. I mixed idioms -- my bad. It'd be JsonNode.path(). So I forgot I used a Map instead of JsonNode. :-D

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