-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: resolve ClassCastException when supporting map type serializatio…
…n under fastutil package (#307) fix: resolve ClassCastException when supporting map type serialization under fastutil package
- Loading branch information
Showing
5 changed files
with
94 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...ndation/src/main/java/io/arex/foundation/serializer/custom/FastUtilMapTypeSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package io.arex.foundation.serializer.custom; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import com.google.gson.JsonDeserializationContext; | ||
import com.google.gson.JsonDeserializer; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.TypeAdapter; | ||
import com.google.gson.reflect.TypeToken; | ||
import io.arex.agent.bootstrap.model.ParameterizedTypeImpl; | ||
import io.arex.inst.runtime.log.LogManager; | ||
import java.lang.reflect.ParameterizedType; | ||
import java.lang.reflect.Type; | ||
import java.util.Map; | ||
|
||
public class FastUtilMapTypeSerializer implements JsonDeserializer<Map<Object, Object>> { | ||
|
||
private static final String IMPL_MAP_SUFFIX = "OpenHashMap"; | ||
private static final GsonBuilder GSON_BUILDER = new GsonBuilder().enableComplexMapKeySerialization(); | ||
private static final Gson GSON = GSON_BUILDER.create(); | ||
|
||
public Map<Object, Object> deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) { | ||
try { | ||
if (type instanceof ParameterizedType) { | ||
final String implName = getImplClassName(type); | ||
final Class<?> implClass = Thread.currentThread().getContextClassLoader().loadClass(implName); | ||
final ParameterizedTypeImpl recreateType = ParameterizedTypeImpl.make(implClass, | ||
((ParameterizedType) type).getActualTypeArguments(), ((ParameterizedType) type).getOwnerType()); | ||
return GSON.fromJson(jsonElement, recreateType); | ||
} | ||
return null; | ||
} catch (Exception e) { | ||
LogManager.warn("FastUtilMapTypeSerialzer.deserialize", e); | ||
return null; | ||
} | ||
} | ||
|
||
public static <T> TypeAdapter<T> getAdapter(final TypeToken<T> type) { | ||
return GSON_BUILDER.registerTypeAdapter(type.getRawType(), new FastUtilMapTypeSerializer()).create() | ||
.getAdapter(type); | ||
} | ||
|
||
private String getImplClassName(Type type) { | ||
final String rawClassName = ((ParameterizedType) type).getRawType().getTypeName(); | ||
final String prefix = rawClassName.substring(0, rawClassName.length() - 3); | ||
return prefix + IMPL_MAP_SUFFIX; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters