alibaba / fastjson2

🚄 FASTJSON2 is a Java JSON library with excellent performance.
Apache License 2.0
3.74k stars 484 forks source link

[QUESTION]自定义接口实现序列化与反序列化问题 #807

Open gengzhy opened 2 years ago

gengzhy commented 2 years ago

没找到自定义序列化和反序列化

之前V1.X版本支持实现接口com.alibaba.fastjson.parser.deserializer.ObjectDeserializer实现反序列化。 自从审计到V2.X版本后没找到这个接口类。请知道的大佬指点一下,谢谢 image

为什么fastjson2 的@JSONField注解只提供了 com.alibaba.fastjson2.annotation.JSONField#writeUsing 属性 而没有提供反序列化的属性呢?怎么自定义反序列化呢?

MoshiCoCo commented 2 years ago

@gengzhy 可以通过实现ObjectWriter和ObjectReader实现定制序列化和反序列化.

MoshiCoCo commented 2 years ago

@gengzhy

ObjectSerializer 和 ObjectDeserializer 被移除了,有什么新的代替方案 FASTJSON v2中有比较完善的扩展机制,如下:

Annotation介绍 https://alibaba.github.io/fastjson2/annotations_cn Annotation注入介绍 https://alibaba.github.io/fastjson2/mixin_cn Feature介绍 https://alibaba.github.io/fastjson2/features_cn 使用Mixin注入Anntation定制序列化和反序列化 https://alibaba.github.io/fastjson2/mixin_cn 实现ObjectWriter和ObjectReader实现定制序列化和反序列化 https://alibaba.github.io/fastjson2/register_custom_reader_writer_cn

gengzhy commented 2 years ago

感谢大佬,我研究下

gengzhy commented 2 years ago

“ObjectSerializer 和 ObjectDeserializer 被移除了,有什么新的代替方案” 这个需要针对每一个序列化反序列化的类使用JSON.register()吧,这样很不灵活。每次我都得写一次。比如针对某个枚举字典。感觉没以前灵活了。针对这方面,有具体的实例吗?

wenshao commented 2 years ago

只需要注册一次,不需要每次都注册的

wenshao commented 2 years ago

https://github.com/alibaba/fastjson2/wiki/register_custom_reader_writer_cn 这里的1.4和2.4看是否是你要的

rockjing commented 5 months ago

没找到自定义序列化和反序列化

之前V1.X版本支持实现接口com.alibaba.fastjson.parser.deserializer.ObjectDeserializer实现反序列化。 自从审计到V2.X版本后没找到这个接口类。请知道的大佬指点一下,谢谢 image

为什么fastjson2 的@JSONField注解只提供了 com.alibaba.fastjson2.annotation.JSONField#writeUsing 属性 而没有提供反序列化的属性呢?怎么自定义反序列化呢?

RocK: 我也觉得,FastJson2 这个改动非常不友好。 本来对某个指定字段做一个deserializeUsing 就OK的事情,为什么还要去注册一下? 相比之前FastJson1 的解决方案更简洁。

rockjing commented 5 months ago

实现ObjectWriter和ObjectReader实现定制序列化和反序列化 https://alibaba.github.io/fastjson2/register_custom_reader_writer_cn

可能是由于我水平低了,我实在搞不懂,怎么来通过FastJson2 实现类似 FastJson1 里面,对某个字段做反序列化的方式。