io.github.aooohan.mq.serializer.DefaultRedisMqSerializer Maven / Gradle / Ivy
/*
* Copyright 2023 [lihan [email protected]]
*
* 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
*
* 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 io.github.aooohan.mq.serializer;
/**
* @author : lihan
* @date : 2023/6/23 14:15
*/
public class DefaultRedisMqSerializer implements RedisMqSerializer{
@Override
public String serialize(Object object) throws Exception {
if (object instanceof String) {
return (String) object;
}
if (object instanceof byte[]) {
return new String((byte[]) object);
}
if (object instanceof Integer) {
return String.valueOf(object);
}
if (object instanceof Long) {
return String.valueOf(object);
}
if (object instanceof Double) {
return String.valueOf(object);
}
if (object instanceof Float) {
return String.valueOf(object);
}
if (object instanceof Boolean) {
return String.valueOf(object);
}
if (object instanceof Character) {
return String.valueOf(object);
}
if (object instanceof Short) {
return String.valueOf(object);
}
if (object instanceof Byte) {
return String.valueOf(object);
}
throw new UnsupportedOperationException("Unsupported type: " + object.getClass().getName());
}
@Override
@SuppressWarnings("unchecked")
public T deserialize(String string, Class clazz) throws Exception {
if (clazz == String.class) {
return (T) string;
}
if (clazz == byte[].class) {
return (T) string.getBytes();
}
if (clazz == Integer.class) {
return (T) Integer.valueOf(string);
}
if (clazz == Long.class) {
return (T) Long.valueOf(string);
}
if (clazz == Double.class) {
return (T) Double.valueOf(string);
}
if (clazz == Float.class) {
return (T) Float.valueOf(string);
}
if (clazz == Boolean.class) {
return (T) Boolean.valueOf(string);
}
if (clazz == Character.class) {
return (T) Character.valueOf(string.charAt(0));
}
if (clazz == Short.class) {
return (T) Short.valueOf(string);
}
if (clazz == Byte.class) {
return (T) Byte.valueOf(string);
}
throw new UnsupportedOperationException("Unsupported type: " + clazz.getName());
}
}