How to deserialize Instant with Json.decodevalue();

4,370 views
Skip to first unread message

javadevmtl

unread,
Apr 25, 2017, 7:07:48 PM4/25/17
to vert.x
Hi, I have the following entity...

public class Period {
Instant startsAt;
Instant endsAt;
}

And I use...

final Period period = Json.decodeValue(rc.getBodyAsString(), Period.class);

The json...

{
"startsAt": "2017-02-17T19:00:00Z",
"endsAt": "2017-02-20T06:00:00Z"
}


But I get the following exception...

io.vertx.core.json.DecodeException: Failed to decode:Can not instantiate value of type [simple type, class java.time.Instant] from String value ('2017-02-17T19:00:00Z'); no single-String constructor/factory method
 at [Source: {
"startsAt": "2017-02-17T19:00:00Z",
"endsAt": "2017-02-20T06:00:00Z"
}
}; line: 2, column: 15] (through reference chain: com.xxx.xxx.search.Period["startsAt"])
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:255) ~[jackson-databind-2.7.4.jar:2.7.4]
...

Do I need to do something extra or include extra jar for jackson with vertx?


javadevmtl

unread,
Apr 25, 2017, 8:45:16 PM4/25/17
to vert.x
I did some reading around. We need to add as a dependency:

compile 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.7.4'

But that doesn't seem enough we also have to register the module. Does that mean that https://github.com/eclipse/vert.x/blob/master/src/main/java/io/vertx/core/json/Json.java needs to be modified? Is there a way to do this currently?

javadevmtl

unread,
Apr 25, 2017, 9:51:25 PM4/25/17
to vert.x
myJsonObject.getInstant() works because internally, it's just mapping a string directly to an Instant.

But myJsonObject.mapTo() also throws exception.

I guess Instant support needs to be added to serialization/deserialization of POJOs.

Tim Fox

unread,
Apr 26, 2017, 2:50:36 AM4/26/17
to vert.x
If you're trying to convert some JSON String value to a Java object, this is well documented in the Jackson docs how to do it with their APIs, not really a Vert.x question.

javadevmtl

unread,
Apr 26, 2017, 9:06:29 AM4/26/17
to vert.x
I'm using the tools provided by my favorite framework vertx.io ;) I.e: Json.
decodevalue() and JsonObject.mapTo() which decode 98% of my Json to my "comeplexe" pojo. Only Instant doesn't work.

We need the jackson datatype module, but we don't have access to the json ObjectMapper to register the module from Json class in vertx.

1- I can leave as string and convert to Instant on my own.
2- Register my own Object mapper for just 2 properties and use JsonObject everywhere else.
3- Pass around JsonObject to the "services" since it supports getInstant. I suppose its the most interoperable way.

javadevmtl

unread,
Apr 26, 2017, 9:20:19 AM4/26/17
to vert.x
Actually I will use 3 but inside my service implementation I would still want to decode it with either Json.decodevalue() or JsonObject.mapTo()

javadevmtl

unread,
Apr 26, 2017, 4:52:49 PM4/26/17
to vert.x
The question is should Vertx json support Instant and java 8 date time?

Psycho Punch

unread,
Apr 26, 2017, 11:38:52 PM4/26/17
to vert.x
I think you can work around this issue by creating a setter that accepts a String representing an Instant, then you write the code yourself to convert it. Tell Jackson that you want to use the setter during deserialization by marking it with @JsonSetter.


private Instant startsAt;

@JsonSetter
public setStartAt(String instantString) {
   
//do your conversion
}


javadevmtl

unread,
Apr 27, 2017, 1:52:58 PM4/27/17
to vert.x
That's another option.

Sebastian Müller

unread,
Jul 9, 2018, 10:25:17 AM7/9/18
to vert.x
You can call the following for registration:

Json.mapper.registerModule(new JavaTimeModule());

Julien Viet

unread,
Jul 10, 2018, 4:49:17 AM7/10/18
to ve...@googlegroups.com
there is currently a PR for that Instant in master

-- 
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
Visit this group at https://groups.google.com/group/vertx.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/15299d76-bc86-4981-a791-a8a644f055e4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Emad Alblueshi

unread,
Jul 12, 2018, 4:43:41 AM7/12/18
to vert.x
Reply all
Reply to author
Forward
0 new messages