Skip to content

Commit

Permalink
feat: add Jackson serder
Browse files Browse the repository at this point in the history
  • Loading branch information
loicmathieu committed Sep 2, 2019
1 parent 485f498 commit de8b33d
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 30 deletions.
26 changes: 2 additions & 24 deletions docs/src/main/asciidoc/mongodb-panache-guide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -448,31 +448,9 @@ you need to provide the value by yourself.
====

`ObjectId` can be difficult to use if you want to expose its value in your rest service.
So we create JSON-B providers to serialize/deserialize them as String.
These providers are not registered by default so you need to register them with a JAX-RS `ContextResolver`:
So we create JSON-B providers to serialize/deserialize them as String and also Jackson equivalent.

[source,java]
--
import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.json.bind.JsonbConfig;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;

import io.quarkus.mongodb.panache.jsonb.ObjectIdDeserializer;
import io.quarkus.mongodb.panache.jsonb.ObjectIdSerializer;

@Provider
public class QuarkusJsonbContextResolver implements ContextResolver<Jsonb> {

public Jsonb getContext(Class clazz) {
JsonbConfig config = new JsonbConfig();
config.withSerializers(new ObjectIdSerializer()).withDeserializers(new ObjectIdDeserializer());
return JsonbBuilder.create(config);
}

}
--
JSON-B providers and Jackson serializer/deserializer are automatically registered as soon as one of Resteasy with JSON-B or Resteasy with Jackson extensions are used.

== How and why we simplify MongoDB API

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
import io.quarkus.deployment.Capabilities;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.ExecutionTime;
import io.quarkus.deployment.annotations.Record;
import io.quarkus.deployment.builditem.ApplicationIndexBuildItem;
import io.quarkus.deployment.builditem.BytecodeTransformerBuildItem;
import io.quarkus.deployment.builditem.CombinedIndexBuildItem;
Expand All @@ -26,6 +24,7 @@
import io.quarkus.mongodb.panache.PanacheMongoEntityBase;
import io.quarkus.mongodb.panache.PanacheMongoRepository;
import io.quarkus.mongodb.panache.PanacheMongoRepositoryBase;
import io.quarkus.mongodb.panache.jackson.ObjectMapperProducer;
import io.quarkus.mongodb.panache.jsonb.PanacheMongoJsonbContextResolver;
import io.quarkus.panache.common.deployment.PanacheFieldAccessEnhancer;
import io.quarkus.resteasy.common.spi.ResteasyJaxrsProviderBuildItem;
Expand All @@ -44,6 +43,13 @@ FeatureBuildItem featureBuildItem() {
}

@BuildStep
AdditionalBeanBuildItem registerJacksonSerDer(Capabilities capabilities) {
if (capabilities.isCapabilityPresent("io.quarkus.resteasy.jackson")) {
return new AdditionalBeanBuildItem(ObjectMapperProducer.class);
}
return null;
}

@BuildStep
ResteasyJaxrsProviderBuildItem registerJsonbSerDer(Capabilities capabilities) {
if (capabilities.isCapabilityPresent("io.quarkus.resteasy.jsonb")) {
Expand Down
7 changes: 7 additions & 0 deletions extensions/panache/mongodb-panache/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@
<artifactId>quarkus-resteasy-jsonb</artifactId>
<optional>true</optional>
</dependency>
<!-- This is optional to avoid pulling libraries if jackson is not on the path-->
<!-- But needed to provides default serializer/deserializer for Jackson -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-jackson</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.quarkus.mongodb.panache.jackson;

import java.io.IOException;

import org.bson.types.ObjectId;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

public class ObjectIdDeserializer extends JsonDeserializer<ObjectId> {

@Override
public ObjectId deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
throws IOException, JsonProcessingException {
String value = jsonParser.getValueAsString();
if (value != null) {
return new ObjectId(value);
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.quarkus.mongodb.panache.jackson;

import java.io.IOException;

import org.bson.types.ObjectId;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

public class ObjectIdSerializer extends JsonSerializer<ObjectId> {

@Override
public void serialize(ObjectId objectId, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
throws IOException {
if (objectId != null) {
jsonGenerator.writeString(objectId.toString());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.quarkus.mongodb.panache.jackson;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
import javax.inject.Singleton;

import org.bson.types.ObjectId;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;

/**
* This ObjectMapperProducer will produce an ObjectMapper that will override the default one from resteasy-jackson.
*/
@ApplicationScoped
public class ObjectMapperProducer {

@Singleton
@Produces
public ObjectMapper objectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
SimpleModule module = new SimpleModule("ObjectIdModule");
module.addSerializer(ObjectId.class, new ObjectIdSerializer());
module.addDeserializer(ObjectId.class, new ObjectIdDeserializer());
objectMapper.registerModule(module);
return objectMapper;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class ResteasyJacksonProcessor {

private static final String QUARKUS_CONTEXT_RESOLVER_NAME = "io.quarkus.resteasy.jackson.runtime.QuarkusObjectMapperContextResolver";

@BuildStep
@BuildStep(providesCapabilities = "io.quarkus.resteasy.jackson")
void build(BuildProducer<FeatureBuildItem> feature) {
feature.produce(new FeatureBuildItem(FeatureBuildItem.RESTEASY_JACKSON));
}
Expand Down
2 changes: 1 addition & 1 deletion extensions/smallrye-opentracing/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-deployment</artifactId>
<artifactId>quarkus-resteasy-common-spi</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.FeatureBuildItem;
import io.quarkus.deployment.builditem.substrate.ReflectiveMethodBuildItem;
import io.quarkus.resteasy.common.deployment.ResteasyJaxrsProviderBuildItem;
import io.quarkus.resteasy.common.spi.ResteasyJaxrsProviderBuildItem;
import io.quarkus.smallrye.opentracing.runtime.QuarkusSmallRyeTracingDynamicFeature;
import io.quarkus.smallrye.opentracing.runtime.TracerProducer;
import io.quarkus.undertow.deployment.FilterBuildItem;
Expand Down
4 changes: 4 additions & 0 deletions extensions/spring-web/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-deployment</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-common-spi</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-undertow-deployment</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import io.quarkus.deployment.util.ServiceUtil;
import io.quarkus.gizmo.ClassOutput;
import io.quarkus.resteasy.common.deployment.ResteasyCommonProcessor;
import io.quarkus.resteasy.common.deployment.ResteasyJaxrsProviderBuildItem;
import io.quarkus.resteasy.common.spi.ResteasyJaxrsProviderBuildItem;
import io.quarkus.resteasy.server.common.deployment.AdditionalJaxRsResourceDefiningAnnotationBuildItem;
import io.quarkus.resteasy.server.common.deployment.AdditionalJaxRsResourceMethodAnnotationsBuildItem;
import io.quarkus.resteasy.server.common.deployment.AdditionalJaxRsResourceMethodParamAnnotations;
Expand Down

0 comments on commit de8b33d

Please sign in to comment.