关于FastJSON序列化Bean时对get方法调用的细节

编程入门 行业动态 更新时间:2024-10-11 05:24:56

关于FastJSON序列化Bean时对get方法调用的<a href=https://www.elefans.com/category/jswz/34/1769720.html style=细节"/>

关于FastJSON序列化Bean时对get方法调用的细节

结论

使用JSON.toJSONString去序列化Bean的时候
FastJSON会把Bean里面的get开头有返回值没有参数的方法都调用一遍。

看代码

package org.example.domain;import lombok.Getter;
import lombok.Setter;/*** @program: parent_pro* @description:* @author: 渭水* @create: 2023/10/31*/
@Getter
@Setter
public class OtherPerson {private Integer age;public String getPerson2(String s) {System.out.println("getPerson");return "abc";}public void getPerson4() {System.out.println("getPerson4");}public void getPerson5(String s) {System.out.println("getPerson5");}public String getPerson3() {System.out.println("getPerson3");return "abc";}
}

测试类如下

package org.example;import com.alibaba.fastjson2.JSON;import org.example.domain.OtherPerson;/*** @program: parent_pro* @description:* @author: 渭水* @create: 2023/11/03*/
public class JSONTest {public static void main(String[] args) {OtherPerson otherPerson = new OtherPerson();otherPerson.setAge(18);System.out.println(JSON.toJSONString(otherPerson));}
}

运行结果

关于ArrayIndexOutOfBoundsException

另外这里用的FastJSON版本是

    <dependency><groupId>com.alibaba.fastjson2</groupId><artifactId>fastjson2</artifactId><version>2.0.41</version></dependency>

如果版本低于2.0.35,且bean里面有返回值为void的且get开头的方法就会报错:java.lang.ArrayIndexOutOfBoundsException

更多推荐

关于FastJSON序列化Bean时对get方法调用的细节

本文发布于:2023-11-30 12:31:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1650120.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:细节   序列化   方法   FastJSON   Bean

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!