alibaba / alibaba/fastjson2

[QUESTION] 是否有一种方式,能够在反序列化的前后做一些拦截

Open
#3,326 0 comments 0 reactions 0 assignees View on GitHub
question
Dominant language
Java
Stars
4.4k
Forks
613
Avg merge
1d 22h
Merged PRs (30d)
6

Description

### 请描述您的问题
*询问有关本项目的使用和其他方面的相关问题。*

目前反序列化时,在`com.alibaba.fastjson2.util.TypeUtils#loadClass`里,目前是有`Thread.currentThread().getContextClassLoader()`这个类加载器来加载类。但是,我目前遇到了一个问题,不能使用这个类加载器,ContextClassLoader在业务里被用来加载另外一个隔离scope的类,而当每次都加载不到的时候,会产生严重的性能问题。

fastjson2反序列化的时候,会在`com.alibaba.fastjson2.reader.ObjectReaderProvider#checkAutoType`里调用一次loadClass,但每次都加载不到,因为这个业务自定义classLoader的问题,出现高性能延迟。

所以希望能只在反序列化前后,把这个自定义的ContextClassLoader替换掉。

除了cglib之外,fastjson2内部是否提供了这样的机制?

整个我需要做类似下面这个事情,但我希望一次性搞定。
```
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
try{
Thread.currentThread().setContextClassLoader(null);
JSON.parseObject(json);
}finally{
Thread.currentThread().setContextClassLoader(contextClassLoader );
}
```

Contributor guide

Open the contributing guide

Research direction

Start by reading com.alibaba.fastjson2.util.TypeUtils#loadClass and tracing its call from com.alibaba.fastjson2.reader.ObjectReaderProvider#checkAutoType during JSON.parseObject. Check whether deserialization exposes lifecycle hooks or class-loader configuration; done means providing or clearly documenting a supported way to avoid the problematic ContextClassLoader without manual wrapping.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.