Skip to content

Instantly share code, notes, and snippets.

@aioobe
Created August 3, 2015 16:23
Show Gist options
  • Save aioobe/4acf96fd3582823b3a54 to your computer and use it in GitHub Desktop.
Save aioobe/4acf96fd3582823b3a54 to your computer and use it in GitHub Desktop.
Jackson / Jongo / MongoDB / JSR-310 reproducer
package jacksonrepro;
import java.net.UnknownHostException;
import java.time.Instant;
import java.util.Arrays;
import org.jongo.Jongo;
import org.jongo.Mapper;
import org.jongo.MongoCollection;
import org.jongo.marshall.jackson.JacksonMapper.Builder;
import com.fasterxml.jackson.datatype.jsr310.JSR310Module;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
public class JacksonJSR310Reproducer {
// Field that causes trouble
Instant i = Instant.now();
// Reproducer method
public static void main(String[] args) throws UnknownHostException {
// Mapper
Mapper mapper = new Builder().registerModule(new JSR310Module()).build();
// Create a Jongo wrapper for the database and the mapper
Jongo jongo = new Jongo(new MongoClient().getDB("mydb"), mapper);
MongoCollection testColl = jongo.getCollection("testColl");
// Save an object
testColl.save(new JacksonJSR310Reproducer());
// Result in mongo db looks as follows (note that i contains a string):
//
// > db.testColl.find().pretty()
// {
// "_id" : ObjectId("55bf8f564bd24cf6666e80ca"),
// "i" : "1438617430.458000000"
// }
// Try to load it:
testColl.findOne().as(JacksonJSR310Reproducer.class);
// Throws:
//
// Exception in thread "main" org.jongo.marshall.MarshallingException: Unable to unmarshall result to class jacksonrepro.JacksonJSR310Reproducer from content { "_id" : { "$oid" : "55bf8f564bd24cf6666e80ca"} , "i" : "1438617430.458000000"}
// at org.jongo.marshall.jackson.JacksonEngine.unmarshall(JacksonEngine.java:45)
// at org.jongo.ResultHandlerFactory$UnmarshallingResultHandler.map(ResultHandlerFactory.java:43)
// at org.jongo.FindOne.map(FindOne.java:51)
// at org.jongo.FindOne.as(FindOne.java:46)
// at jacksonrepro.JacksonJSR310Reproducer.main(JacksonJSR310Reproducer.java:45)
// Caused by: com.fasterxml.jackson.databind.JsonMappingException: Text '1438617430.458000000' could not be parsed at index 0 (through reference chain: jacksonrepro.JacksonJSR310Reproducer["i"])
// at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:232)
// at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:197)
// at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.wrapAndThrow(BeanDeserializerBase.java:1415)
// at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:244)
// at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:118)
// at com.fasterxml.jackson.databind.ObjectReader._bindAndClose(ObjectReader.java:1269)
// at com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:928)
// at org.jongo.marshall.jackson.JacksonEngine.unmarshall(JacksonEngine.java:42)
// ... 4 more
// Caused by: java.time.format.DateTimeParseException: Text '1438617430.458000000' could not be parsed at index 0
// at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1947)
// at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1849)
// at java.time.Instant.parse(Instant.java:395)
// at com.fasterxml.jackson.datatype.jsr310.deser.InstantDeserializer$$Lambda$1/733957003.apply(Unknown Source)
// at com.fasterxml.jackson.datatype.jsr310.deser.InstantDeserializer.deserialize(InstantDeserializer.java:119)
// at com.fasterxml.jackson.datatype.jsr310.deser.InstantDeserializer.deserialize(InstantDeserializer.java:40)
// at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:538)
// at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:106)
// at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:242)
// ... 8 more
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.aioobe</groupId>
<artifactId>jacksonrepro</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>JacksonJSR310</name>
<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.13.2</version>
</dependency>
<dependency>
<groupId>org.jongo</groupId>
<artifactId>jongo</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.4.0</version>
</dependency>
</dependencies>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment