Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

从1.2.69版本开始,ParserConfig.getGlobalInstance().putDeserializer(String.class, new TrimStringDeserializer());失效了 #3279

Closed
llsi4275 opened this issue Jun 18, 2020 · 4 comments
Milestone

Comments

@llsi4275
Copy link

llsi4275 commented Jun 18, 2020

从1.2.69版本开始,在反序列化时,使用ParserConfig.getGlobalInstance().putDeserializer(String.class, new TrimStringDeserializer());方式对全局的字符串处理不生效了,调用不到了。。。

我们从1.2.60版本使用这个功能,最近换成新版本后,发现失效了...
试了1.2.60-1.2.68之间的所有版本都可以使用。但换成1.2.69之后,就无法使用了。

如果到每个字段上去写,太麻烦,也太low了一点,有没有其它办法呢?

@llsi4275 llsi4275 changed the title 从1.2.69版本开始,在反序列化时,不会调用StringCodec类对字符串处理~~~ 从1.2.69版本开始,ParserConfig.getGlobalInstance().putDeserializer(String.class, new TrimStringDeserializer());失效了 Jun 18, 2020
@llsi4275
Copy link
Author

llsi4275 commented Jun 18, 2020

@Omega-Ariston 在1.2.69+的版本中,指定全局Deserializer的功能又失效了...

@wenshao
Copy link
Member

wenshao commented Jun 27, 2020

https://github.com/alibaba/fastjson/releases/tag/1.2.72
问题已修复,请用新版本

@wenshao wenshao closed this as completed Jun 27, 2020
@llsi4275
Copy link
Author

llsi4275 commented Jun 28, 2020

@wenshao
似乎没有启到作用:
配置代码:
public T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) {
T t = super.deserialze(parser, clazz, fieldName);
if (t instanceof String) {
return (T) trimAndNormalize((String) t);
} else if (t instanceof StringBuilder) {
return (T) trimAndNormalize((StringBuilder) t);
} else if (t instanceof StringBuffer) {
return (T) trimAndNormalize((StringBuffer) t);
}
return t;
}

/**
 * {@inheritDoc}
 */
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    FastJsonHttpMessageConverter msgConverter = new FastJsonHttpMessageConverter();
    msgConverter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_JSON));

    ParserConfig pcfg =  ParserConfig.getGlobalInstance();
    pcfg.putDeserializer(String.class, this);
    pcfg.putDeserializer(StringBuilder.class, this);
    pcfg.putDeserializer(StringBuffer.class, this);
    
    FastJsonConfig cfg = new FastJsonConfig();
    cfg.setParserConfig(pcfg);
    
    msgConverter.setFastJsonConfig(cfg);
    converters.add(msgConverter);
}

传入参数:
{
"name": "张三 ",
"phone": "李四 ",
"skill": "java ",
"recruitment": 4,
"region": "Chengdu ",
"workingYears": "3.5 "
},
仅recruitment进入了自定义的deserialze方法,当将4添加上""后,一个属性都没有进入

@llsi4275
Copy link
Author

llsi4275 commented Jun 28, 2020

@wenshao
可参考测试代码:
https://github.com/llsi4275/fastjsonTest

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants