You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2018/12/12 07:08:19 UTC

[GitHub] jerrypeng closed pull request #3163: Use jackson for serialization and deserialization of json since its faster than gson

jerrypeng closed pull request #3163: Use jackson for serialization and deserialization of json since its faster than gson
URL: https://github.com/apache/pulsar/pull/3163
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/pulsar-client-schema/src/main/java/org/apache/pulsar/client/impl/schema/JSONSchema.java b/pulsar-client-schema/src/main/java/org/apache/pulsar/client/impl/schema/JSONSchema.java
index 02185ec555..5ef1dc153f 100644
--- a/pulsar-client-schema/src/main/java/org/apache/pulsar/client/impl/schema/JSONSchema.java
+++ b/pulsar-client-schema/src/main/java/org/apache/pulsar/client/impl/schema/JSONSchema.java
@@ -18,14 +18,13 @@
  */
 package org.apache.pulsar.client.impl.schema;
 
+import com.fasterxml.jackson.annotation.JsonInclude;
 import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.DeserializationFeature;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.module.jsonSchema.JsonSchema;
 import com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator;
-import com.google.gson.ExclusionStrategy;
-import com.google.gson.FieldAttributes;
-import com.google.gson.Gson;
-import com.google.gson.GsonBuilder;
+import io.netty.util.concurrent.FastThreadLocal;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.avro.reflect.ReflectData;
 import org.apache.pulsar.client.api.Schema;
@@ -33,24 +32,35 @@
 import org.apache.pulsar.common.schema.SchemaInfo;
 import org.apache.pulsar.common.schema.SchemaType;
 
+import java.io.IOException;
 import java.util.Collections;
-import java.util.HashSet;
 import java.util.Map;
-import java.util.Set;
 
 @Slf4j
 public class JSONSchema<T> implements Schema<T>{
 
     private final org.apache.avro.Schema schema;
     private final SchemaInfo schemaInfo;
-    private final Gson gson;
     private final Class<T> pojo;
     private Map<String, String> properties;
 
+    // Cannot use org.apache.pulsar.common.util.ObjectMapperFactory.getThreadLocal() because it does not
+    // return shaded version of object mapper
+    private static final FastThreadLocal<ObjectMapper> JSON_MAPPER = new FastThreadLocal<ObjectMapper>() {
+        @Override
+        protected ObjectMapper initialValue() throws Exception {
+            ObjectMapper mapper = new ObjectMapper();
+            mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
+            mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
+            return mapper;
+        }
+    };
+
+    private final ObjectMapper objectMapper;
+
     private JSONSchema(Class<T> pojo, Map<String, String> properties) {
         this.pojo = pojo;
         this.properties = properties;
-        this.gson = new Gson();
 
         this.schema = ReflectData.AllowNull.get().getSchema(pojo);
         this.schemaInfo = new SchemaInfo();
@@ -58,14 +68,15 @@ private JSONSchema(Class<T> pojo, Map<String, String> properties) {
         this.schemaInfo.setProperties(properties);
         this.schemaInfo.setType(SchemaType.JSON);
         this.schemaInfo.setSchema(this.schema.toString().getBytes());
+        this.objectMapper = JSON_MAPPER.get();
     }
 
     @Override
     public byte[] encode(T message) throws SchemaSerializationException {
 
         try {
-            return this.gson.toJson(message).getBytes();
-        } catch (RuntimeException e) {
+            return objectMapper.writeValueAsBytes(message);
+        } catch (JsonProcessingException e) {
             throw new SchemaSerializationException(e);
         }
     }
@@ -73,8 +84,8 @@ private JSONSchema(Class<T> pojo, Map<String, String> properties) {
     @Override
     public T decode(byte[] bytes) {
         try {
-            return this.gson.fromJson(new String(bytes), this.pojo);
-        } catch (RuntimeException e) {
+            return objectMapper.readValue(bytes, this.pojo);
+        } catch (IOException e) {
             throw new RuntimeException(new SchemaSerializationException(e));
         }
     }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services