/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.pulsar.client.impl.schema;

import com.fasterxml.jackson.core.JsonProcessingException;
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 lombok.extern.slf4j.Slf4j;
import org.apache.avro.reflect.ReflectData;
import org.apache.pulsar.client.api.Schema;
import org.apache.pulsar.client.api.SchemaSerializationException;
import org.apache.pulsar.common.schema.SchemaInfo;
import org.apache.pulsar.common.schema.SchemaType;

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;

    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();
        this.schemaInfo.setName("");
        this.schemaInfo.setProperties(properties);
        this.schemaInfo.setType(SchemaType.JSON);
        this.schemaInfo.setSchema(this.schema.toString().getBytes());
    }

    @Override
    public byte[] encode(T message) throws SchemaSerializationException {

        try {
            return this.gson.toJson(message).getBytes();
        } catch (RuntimeException e) {
            throw new SchemaSerializationException(e);
        }
    }

    @Override
    public T decode(byte[] bytes) {
        try {
            return this.gson.fromJson(new String(bytes), this.pojo);
        } catch (RuntimeException e) {
            throw new RuntimeException(new SchemaSerializationException(e));
        }
    }

    @Override
    public SchemaInfo getSchemaInfo() {
        return this.schemaInfo;
    }

    /**
     * Implemented for backwards compatibility reasons
     * since the original schema generated by JSONSchema was based off the json schema standard
     * since then we have standardized on Avro
     * @return
     */
    public SchemaInfo getBackwardsCompatibleJsonSchemaInfo() {
        SchemaInfo backwardsCompatibleSchemaInfo;
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            JsonSchemaGenerator schemaGen = new JsonSchemaGenerator(objectMapper);
            JsonSchema jsonBackwardsCompatibileSchema = schemaGen.generateSchema(pojo);
            backwardsCompatibleSchemaInfo = new SchemaInfo();
            backwardsCompatibleSchemaInfo.setName("");
            backwardsCompatibleSchemaInfo.setProperties(properties);
            backwardsCompatibleSchemaInfo.setType(SchemaType.JSON);
            backwardsCompatibleSchemaInfo.setSchema(objectMapper.writeValueAsBytes(jsonBackwardsCompatibileSchema));
        } catch (JsonProcessingException ex) {
            throw new RuntimeException(ex);
        }
        return backwardsCompatibleSchemaInfo;
    }

    public static <T> JSONSchema<T> of(Class<T> pojo) {
        return new JSONSchema<>(pojo, Collections.emptyMap());
    }

    public static <T> JSONSchema<T> of(Class<T> pojo, Map<String, String> properties) {
        return new JSONSchema<>(pojo, properties);
    }
}

@@ -882,36 +882,151 @@
 son.
-core.JsonProcessingException
+annotation.JsonInclude;%0Aimport com.fasterxml.jackson.core.JsonProcessingException;%0Aimport com.fasterxml.jackson.databind.DeserializationFeature
 ;%0Aim
@@ -1213,146 +1213,48 @@
 ort 
-com.google.gson.ExclusionStrategy;%0Aimport com.google.gson.FieldAttributes;%0Aimport com.google.gson.Gson;%0Aimport com.google.gson.GsonBuilder
+io.netty.util.concurrent.FastThreadLocal
 ;%0Aim
@@ -1542,16 +1542,44 @@
 aType;%0A%0A
+import java.io.IOException;%0A
 import j
@@ -1621,59 +1621,11 @@
 til.
-HashSet;%0Aimport java.util.Map;%0Aimport java.util.Set
+Map
 ;%0A%0A@
@@ -1774,37 +1774,8 @@
 fo;%0A
-    private final Gson gson;%0A
     
@@ -1848,16 +1848,666 @@
 rties;%0A%0A
+    // Cannot use org.apache.pulsar.common.util.ObjectMapperFactory.getThreadLocal() because it does not%0A    // return shaded version of object mapper%0A    private static final FastThreadLocal%3CObjectMapper%3E JSON_MAPPER = new FastThreadLocal%3CObjectMapper%3E() %7B%0A        @Override%0A        protected ObjectMapper initialValue() throws Exception %7B%0A            ObjectMapper mapper = new ObjectMapper();%0A            mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);%0A            mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);%0A            return mapper;%0A        %7D%0A    %7D;%0A%0A    private final ObjectMapper objectMapper;%0A%0A
     priv
@@ -2637,40 +2637,8 @@
 ies;
-%0A        this.gson = new Gson();
 %0A%0A  
@@ -2950,24 +2950,71 @@
 etBytes());%0A
+        this.objectMapper = JSON_MAPPER.get();%0A
     %7D%0A%0A    @
@@ -3134,43 +3134,46 @@
 urn 
-this.gson.toJson(message).getBytes(
+objectMapper.writeValueAsBytes(message
 );%0A 
@@ -3180,39 +3180,46 @@
        %7D catch (
-Runtime
+JsonProcessing
 Exception e) %7B%0A 
@@ -3376,44 +3376,36 @@
 urn 
-this.gson.fromJson(new String
+objectMapper.readValue
 (bytes
-)
 , th
@@ -3431,23 +3431,18 @@
  catch (
-Runtime
+IO
 Exceptio

Use jackson for serialization and deserialization of json since its faster than gson (#3163)