Skip to content

Commit

Permalink
java8 - introduced new restx-jongo-java8 for java8 datetime serializa…
Browse files Browse the repository at this point in the history
…tion support with jongo.

This fixes #204
  • Loading branch information
fcamblor committed Aug 22, 2017
1 parent b4d8650 commit e980981
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 0 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,7 @@
</activation>
<modules>
<module>restx-core-java8</module>
<module>restx-jongo-java8</module>
<module>restx-specs-tests-java8</module>
<module>restx-samplest-java8</module>
</modules>
Expand Down
16 changes: 16 additions & 0 deletions restx-jongo-java8/md.restx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"parent": "io.restx:restx-parent:${restx.version}",
"module": "io.restx:restx-jongo-java8:${restx.version}",

"properties": {
"@files": ["../restx.build.properties.json"],
"java.version": "1.8"
},

"dependencies": {
"compile": [
"io.restx:restx-factory:${restx.version}",
"io.restx:restx-jongo:${restx.version}"
]
}
}
20 changes: 20 additions & 0 deletions restx-jongo-java8/module.ivy
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<ivy-module version="2.0" xmlns:ea="http://www.easyant.org">
<info organisation="io.restx" module="restx-jongo-java8" revision="0.35" status="integration">
<ea:build organisation="org.apache.easyant.buildtypes" module="build-std-java" revision="0.9"
compile.java.source.version="1.8"
compile.java.target.version="1.8"
/>
</info>
<configurations>
<conf name="default"/>
<conf name="runtime"/>
<conf name="test"/>
</configurations>
<publications>
<artifact type="jar"/>
</publications>
<dependencies>
<dependency org="io.restx" name="restx-factory" rev="latest.integration" conf="default" />
<dependency org="io.restx" name="restx-jongo" rev="latest.integration" conf="default" />
</dependencies>
</ivy-module>
31 changes: 31 additions & 0 deletions restx-jongo-java8/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>

<parent>
<groupId>io.restx</groupId>
<artifactId>restx-parent</artifactId>
<version>0.35-SNAPSHOT</version>
</parent>

<artifactId>restx-jongo-java8</artifactId>
<name>restx-jongo-java8</name>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>io.restx</groupId>
<artifactId>restx-factory</artifactId>
</dependency>
<dependency>
<groupId>io.restx</groupId>
<artifactId>restx-jongo</artifactId>
</dependency>
</dependencies>
</project>
40 changes: 40 additions & 0 deletions restx-jongo-java8/src/main/java/restx/jongo/BsonJSR310Module.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package restx.jongo;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import de.undercouch.bson4jackson.BsonGenerator;
import de.undercouch.bson4jackson.serializers.BsonSerializer;

import java.io.IOException;
import java.time.Instant;
import java.util.Date;

public class BsonJSR310Module extends SimpleModule {
public BsonJSR310Module() {
super("BsonJSR310Module");

addSerializer(Instant.class, new BsonSerializer<Instant>() {
@Override
public void serialize(Instant date, BsonGenerator bsonGenerator, SerializerProvider serializerProvider)
throws IOException {
if (date == null) {
serializerProvider.defaultSerializeNull(bsonGenerator);
} else {
bsonGenerator.writeDateTime(new Date(date.toEpochMilli()));
}
}
});

addDeserializer(Instant.class, new JsonDeserializer<Instant>() {
@Override
public Instant deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
Date date = (Date) jp.getEmbeddedObject();
return Instant.ofEpochMilli(date.getTime());
}
});
}
}
27 changes: 27 additions & 0 deletions restx-jongo-java8/src/main/java/restx/jongo/JongoJava8Module.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package restx.jongo;

import org.jongo.Mapper;
import org.jongo.marshall.jackson.JacksonMapper;
import restx.factory.Module;
import restx.factory.Provides;
import restx.jackson.BsonJodaTimeModule;
import restx.jackson.Views;

import javax.inject.Named;

/**
* User: xavierhanin
* Date: 1/19/13
* Time: 12:12 AM
*/
@Module(priority = -10)
public class JongoJava8Module {
@Provides @Named("Mapper")
public Mapper mapper() {
return new JacksonMapper.Builder()
.registerModule(new BsonJodaTimeModule()) // to keep compatibility with joda time
.registerModule(new BsonJSR310Module())
.withView(Views.Private.class)
.build();
}
}
4 changes: 4 additions & 0 deletions restx-samplest/src/main/java/samplest/autostartable/Blah.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package samplest.autostartable;

public class Blah {
}

0 comments on commit e980981

Please sign in to comment.