package cn.itcast.protocol; import com.google.gson.*; import java.io.*; import java.lang.reflect.Type; import java.nio.charset.StandardCharsets; /** * 用于扩展序列化、反序列化算法 */ public interface Serializer { // 反序列化方法 <T> T deserialize(Class<T> clazz, byte[] bytes); // 序列化方法 <T> byte[] serialize(T object); enum Algorithm implements Serializer { Java { @Override public <T> T deserialize(Class<T> clazz, byte[] bytes) { try { ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes)); return (T) ois.readObject(); } catch (IOException | ClassNotFoundException e) { throw new RuntimeException("反序列化失败", e); } } @Override public <T> byte[] serialize(T object) { try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(object); return bos.toByteArray(); } catch (IOException e) { throw new RuntimeException("序列化失败", e); } } }, Json { @Override public <T> T deserialize(Class<T> clazz, byte[] bytes) { Gson gson = new GsonBuilder().registerTypeAdapter(Class.class, new Serializer.ClassCodec()).create(); String json = new String(bytes, StandardCharsets.UTF_8); return gson.fromJson(json, clazz); } @Override public <T> byte[] serialize(T object) { Gson gson = new GsonBuilder().registerTypeAdapter(Class.class, new Serializer.ClassCodec()).create(); String json = gson.toJson(object); return json.getBytes(StandardCharsets.UTF_8); } } } class ClassCodec implements JsonSerializer<Class<?>>, JsonDeserializer<Class<?>> { @Override public Class<?> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { try { String str = json.getAsString(); return Class.forName(str); } catch (ClassNotFoundException e) { throw new JsonParseException(e); } } @Override // String.class public JsonElement serialize(Class<?> src, Type typeOfSrc, JsonSerializationContext context) { // class -> json return new JsonPrimitive(src.getName()); } } }
标签:netty,return,Class,---,json,new,序列化,public From: https://www.cnblogs.com/yolinzhao2/p/16813875.html