All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.kafka.support.serializer.JsonSerde Maven / Gradle / Ivy

There is a newer version: 3.1.4
Show newest version
/*
 * Copyright 2017-2020 the original author or authors.
 *
 * Licensed 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
 *
 *      https://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.springframework.kafka.support.serializer;

import java.util.Map;

import org.apache.kafka.common.serialization.Deserializer;
import org.apache.kafka.common.serialization.Serde;
import org.apache.kafka.common.serialization.Serializer;

import org.springframework.core.ResolvableType;
import org.springframework.kafka.support.JacksonUtils;
import org.springframework.kafka.support.converter.Jackson2JavaTypeMapper;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * A {@link org.apache.kafka.common.serialization.Serde} that provides serialization and
 * deserialization in JSON format.
 * 

* The implementation delegates to underlying {@link JsonSerializer} and * {@link JsonDeserializer} implementations. * * @param target class for serialization/deserialization * * @author Marius Bogoevici * @author Elliot Kennedy * @author Gary Russell * @author Ivan Ponomarev * * @since 1.1.5 */ public class JsonSerde implements Serde { private final JsonSerializer jsonSerializer; private final JsonDeserializer jsonDeserializer; public JsonSerde() { this((ObjectMapper) null); } public JsonSerde(Class targetType) { this(targetType, null); } public JsonSerde(ObjectMapper objectMapper) { this(null, objectMapper); } @SuppressWarnings("unchecked") public JsonSerde(@Nullable Class targetTypeArg, @Nullable ObjectMapper objectMapperArg) { ObjectMapper objectMapper = objectMapperArg; Class targetType = (Class) targetTypeArg; if (objectMapper == null) { objectMapper = JacksonUtils.enhancedObjectMapper(); } this.jsonSerializer = new JsonSerializer<>(objectMapper); if (targetType == null) { targetType = (Class) ResolvableType.forClass(getClass()).getSuperType().resolveGeneric(0); } this.jsonDeserializer = new JsonDeserializer<>(targetType, objectMapper); } public JsonSerde(JsonSerializer jsonSerializer, JsonDeserializer jsonDeserializer) { Assert.notNull(jsonSerializer, "'jsonSerializer' must not be null."); Assert.notNull(jsonDeserializer, "'jsonDeserializer' must not be null."); this.jsonSerializer = jsonSerializer; this.jsonDeserializer = jsonDeserializer; } @Override public void configure(Map configs, boolean isKey) { this.jsonSerializer.configure(configs, isKey); this.jsonDeserializer.configure(configs, isKey); } @Override public void close() { this.jsonSerializer.close(); this.jsonDeserializer.close(); } @Override public Serializer serializer() { return this.jsonSerializer; } @Override public Deserializer deserializer() { return this.jsonDeserializer; } // Fluent API /** * Designate this Serde for serializing/deserializing keys (default is values). * @return the serde. * @since 2.3 */ public JsonSerde forKeys() { this.jsonSerializer.forKeys(); this.jsonDeserializer.forKeys(); return this; } /** * Configure the serializer to not add type information. * @return the serde. * @since 2.3 */ public JsonSerde noTypeInfo() { this.jsonSerializer.noTypeInfo(); return this; } /** * Don't remove type information headers after deserialization. * @return the serde. * @since 2.3 */ public JsonSerde dontRemoveTypeHeaders() { this.jsonDeserializer.dontRemoveTypeHeaders(); return this; } /** * Ignore type information headers and use the configured target class. * @return the serde. * @since 2.3 */ public JsonSerde ignoreTypeHeaders() { this.jsonDeserializer.ignoreTypeHeaders(); return this; } /** * Use the supplied {@link Jackson2JavaTypeMapper}. * @param mapper the mapper. * @return the serde. * @since 2.3 */ public JsonSerde typeMapper(Jackson2JavaTypeMapper mapper) { this.jsonSerializer.setTypeMapper(mapper); this.jsonDeserializer.setTypeMapper(mapper); return this; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy