GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — fastjson_1_upgrade_cn fastjson_1_upgrade_cn
Shaojin Wen edited this page
FASTJSON v2项目目前处于活跃状态,会不定期发布新版本,你可以在fastjson2发布地址中获得最新版本 https://github.com/alibaba/fastjson2/releases 可以两种模式升级:
升级可以通过兼容模式升级,兼容模式不需要改代码,但在深度使用的场景,不能做到完全兼容,通过这样的模式升级虽然省事,请认证测试,遇到问题反馈到 https://github.com/alibaba/fastjson2/issues
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>${fastjson2.version}</version> </dependency> 使用新API是建议的升级方式,使用新的API能获得更多的功能。
import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONObject; import com.alibaba.fastjson2.JSONArray;
<dependency> <groupId>com.alibaba.fastjson2</groupId> <artifactId>fastjson2</artifactId> <version>${fastjson2.version}</version> </dependency> 如果你需要用到spring支持的功能,还需要依赖 <dependency> <groupId>com.alibaba.fastjson2</groupId> <artifactId>fastjson2-extension</artifactId> <version>${fastjson2.version}</version> </dependency> 如果你是在kotlin中使用fastjson,需要依赖 <dependency> <groupId>com.alibaba.fastjson2</groupId> <artifactId>fastjson2-kotlin</artifactId> <version>${fastjson2.version}</version> </dependency> 在2.x版本中,ParserConfig添加autoType白名单的功能在ObjectReaderProvider中提供,可以如下的方式配置autoType白名单。 JSONFactory.getDefaultObjectReaderProvider().addAutoTypeAccept("com.mycompany.xxx"); FASTJSON v2中有比较完善的扩展机制,如下:
fastjson2的JSONWriter.Feature没有和UseISO8601DateFormat的Feature,代替方法是使用format="iso8601",如下: import com.alibaba.fastjson2.JSON; String format = "iso8601"; JSON.toJSONString(obj, format); 在fastjson2中,代替的是JSONWriter.Feature.ReferenceDetection,但语义相反,缺省不一样。fastjson2中的JSONWriter.Feature.ReferenceDetection缺省是关闭的,而fastjson1缺省是打开的。 不需要,在fastjson2中,JSONObject继承自LinkedHashMap,不需要配置这个Feature 在fastjson2中的缺省行为就是使用dateFormat,如果要修改为成和fastjson 1.x一样的行为,需要配置format = "millis",如下: import com.alibaba.fastjson2.JSON; String format = "millis"; JSON.toJSONString(obj, format); 也可以使用2.0.58增加的 import static com.alibaba.fastjson2.JSONWriter.Feature.WriterUtilDateAsMillis; JSON.toJSONString(obj, WriterUtilDateAsMillis); Clone this wiki locally | ||||||||||||||||||||||||||||||||||
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |