FastJson

FastJson,第1张

哔哩哔哩学习:黑马FastJson教程https://www.bilibili.com/video/BV1Hy4y1x7ri?spm_id_from=333.1007.top_right_bar_window_history.content.click

Jackson学习https://blog.csdn.net/qq_40728285/article/details/124405831


一、FastJson介绍

FastJson是阿里巴巴的开源JSON解析库,它可以解析JSON格式字符串,支持将Java Bean 序列化为JSON字符串,也可以从JSON字符串反序列化到JavaBean

FastJson的优点
  • 速度快

  • 使用广泛

  • 测试完备

  • 使用简单

  • 功能完善
    支持泛型,支持流处理超大文本,支持枚举,支持序列化和反序列化扩展


二、FastJson序列化和反序列化
0、实体类
@Data
public class DgjTestEntity {
    private String name;
    private String sex;
    private int age;
    private String email;
    private String address;
    private Date birthday;
}
1、Java对象转Json字符串 
public void objectToJson(){
    DgjTestEntity entity = new DgjTestEntity();
    entity.setName("DGS");
    entity.setAge(23);
    entity.setEmail("dgs@qq.com");
    // 对象 转换 Json字符串
    String jsonString = JSON.toJSONString(entity);
    System.out.println(jsonString);
    //{"age":23,"email":"dgs@qq.com","name":"DGS"}
}
2、List集合转Json字符串 
public void listToJson(){
    List list = new ArrayList<>();
    DgjTestEntity entity1 = new DgjTestEntity();
    entity1.setName("DGS");
    entity1.setAge(23);
    entity1.setEmail("dgs@qq.com");

    DgjTestEntity entity2 = new DgjTestEntity();
    entity2 = new DgjTestEntity();
    entity2.setName("DGJ");
    entity2.setAge(23);
    entity2.setEmail("dgs@qq.com");

    list.add(entity1);
    list.add(entity2);
    // List集合 转换 Json字符串
    String jsonString = JSON.toJSONString(list);
    System.out.println(jsonString);
    //[{"age":23,"email":"dgs@qq.com","name":"DGS"},{"age":23,"email":"dgs@qq.com","name":"DGJ"}]
}
 3、Map集合转Json字符串
public void mapToJson(){
    Map map = new HashMap<>();

    DgjTestEntity entity1 = new DgjTestEntity();
    entity1.setName("DGS");
    entity1.setAge(23);
    entity1.setEmail("dgs@qq.com");

    DgjTestEntity entity2 = new DgjTestEntity();
    entity2 = new DgjTestEntity();
    entity2.setName("DGJ");
    entity2.setAge(23);
    entity2.setEmail("dgs@qq.com");

    map.put("entity1",entity1);
    map.put("entity2",entity2);
    // List集合 转换 Json字符串
    String jsonString = JSON.toJSONString(map);
    System.out.println(jsonString);
    //{"entity1":{"age":23,"email":"dgs@qq.com","name":"DGS"},"entity2":{"age":23,"email":"dgs@qq.com","name":"DGJ"}}
}
4、Json格式字符串 反序列化 Java对象
public void jsonToObject(){
    String jsonString = "{\"age\":23,\"email\":\"dgs@qq.com\",\"name\":\"DGS\"}";
    DgjTestEntity entity = JSON.parseObject(jsonString, DgjTestEntity.class);
    System.out.println(entity);
    //DgjTestEntity{name='DGS', sex='null', age=23, email='dgs@qq.com', address='null', birthday=null}
}
5、Json格式字符串 反序列化 List集合
public void jsonToList(){
    String jsonString = "[{\"age\":23,\"email\":\"dgs@qq.com\",\"name\":\"DGS\"},{\"age\":23,\"email\":\"dgs@qq.com\",\"name\":\"DGJ\"}]";
    List list = JSON.parseArray(jsonString, DgjTestEntity.class);
    System.out.println(list);
    //[DgjTestEntity{name='DGS', sex='null', age=23, email='dgs@qq.com', address='null', birthday=null}, DgjTestEntity{name='DGJ', sex='null', age=23, email='dgs@qq.com', address='null', birthday=null}]
}
 6、Json格式字符串 反序列化 Map集合
public void jsonToMap(){
    String jsonString = "{\"entity1\":{\"age\":23,\"email\":\"dgs@qq.com\",\"name\":\"DGS\"},\"entity2\":{\"age\":23,\"email\":\"dgs@qq.com\",\"name\":\"DGJ\"}}";
    //JSON类的静态方法parseObject
    //直接进行反序列化,Map集合没有泛型的,泛型没有是不安全的集合
    //转后的集合必须要有泛型
    //调用parseObject,传递参数,TypeReference类型,在TypeReference类的泛型中,传递转正后的Map集合
    //没有parseMap 只有 调用parseObject 和 调用parseArray
    Map map = JSON.parseObject(jsonString, new TypeReference>() {});
    for (String key : map.keySet()){
        System.out.println(key+" "+ map.get(key));
    }
    //entity1 DgjTestEntity{name='DGS', sex='null', age=23, email='dgs@qq.com', address='null', birthday=null}
    //entity2 DgjTestEntity{name='DGJ', sex='null', age=23, email='dgs@qq.com', address='null', birthday=null}
}

三、SerializerFeature枚举

SerializerFeature 枚举:进行序列化时,可以自己定义特殊需求

作用于:JSON静态方法 toJSONString()

        方法的参数:第一个是要序列化的对象

        方法的参数:第二个参数是SerializerFeature枚举类型的可变参数

SerializerFeature枚举的常量,做序列化的个性需求

1、WriteMapNullValue 序列化Null的字段
public void objectToJson(){
    DgjTestEntity entity = new DgjTestEntity();
    entity.setName("DGS");
    entity.setAge(23);
    entity.setEmail("dgs@qq.com");
    String jsonString = JSON.toJSONString(entity, SerializerFeature.WriteMapNullValue);
    System.out.println(jsonString);
    //{"address":null,"age":23,"birthday":null,"email":"dgs@qq.com","name":"DGS","sex":null}
}
2、WriteNullStringAsEmpty 序列化字符串为Null的字段为空
    public void objectToJson(){
        DgjTestEntity entity = new DgjTestEntity();
        entity.setName("DGS");
        entity.setAge(23);
        entity.setEmail("dgs@qq.com");
        entity.setBirthday(new Date());
        String jsonString = JSON.toJSONString(entity,SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNullStringAsEmpty,SerializerFeature.WriteDateUseDateFormat);
        System.out.println(jsonString);
        //{"address":"","age":23,"birthday":"2022-04-25 13:11:38","email":"dgs@qq.com","name":"DGS","sex":""}
    }
 3、WriteNullNumberAsZero 序列化数值为Null的字段为0
public void objectToJson(){
    DgjTestEntity entity = new DgjTestEntity();
    entity.setName("DGS");
    entity.setEmail("dgs@qq.com");
    String jsonString = JSON.toJSONString(entity,SerializerFeature.WriteNullNumberAsZero);
    System.out.println(jsonString);
    //{"age":0,"email":"dgs@qq.com","name":"DGS"}
}
SerializerFeature.WriteNullBooleanAsFalse布尔值为Null,序列化为false
SerializerFeature.WriteDateUseDateFormat日期格式化:"birthday":"2022-04-25 13:11:38"
 SerializerFeature.PrettyFormat

JSON输出更加美观

{
    "age":0,
    "email":"dgs@qq.com",
    "name":"DGS"
}


四、@JSONField注解 【作用于方法、字段和参数上面】

@JSONField(name = "userName")
private String name;


private int age;


private Date birthday;
private String email;

指定序列化后的名字:name ==> userName
{
    "age":0,
    "birthday":1650865987972,
    "email":"dgs@qq.com",
    "userName":"DGS"
}

@JSONField(name = "userName",ordinal = 0)
private String name;
@JSONField(ordinal = 1)
private int age;

private Date birthday;
private String email;

指定序列化后字段顺序,属性值越小,顺序越靠前

{
    "birthday":1650866030820,
    "email":"dgs@qq.com",
    "userName":"DGS",
    "age":0

}

@JSONField(name = "userName",ordinal = 0)
private String name;
@JSONField(ordinal = 1)
private int age;


@JSONField(format = "yyyy-MM-dd")
private Date birthday;

注解属性 format 指定序列化后的格式

{
    "birthday":"2022-04-25",
    "email":"dgs@qq.com",
    "userName":"DGS",
    "age":0
}

@JSONField(name = "userName",ordinal = 0)
private String name;
@JSONField(ordinal = 1)
private int age;
@JSONField(format = "yyyy-MM-dd")
private Date birthday;
@JSONField(serialize = false)
private String email;

注解 serialize , 指定是否序列化该字段

{
    "birthday":"2022-04-25",
    "userName":"DGS",
    "age":0
}

注解属性:deserialize,是否反序列化该字段
注解属性:serialzeFeatures 徐丽华时的特性定义


五、@JSONType注解【作用于类上,定制序列和反序列化】
@Data
@JSONType
public class DgjTestEntity {
    private String name;
    private int age;
    private Date birthday;
    private String email;
    private String sex;
    private String address;
}
@JSONType(includes={"name","age","birthday"})
public class DgjTestEntity {
    private String name;
    private int age;
    private Date birthday;
    private String email;
    private String sex;
    private String address;
}

注解属性:includes 要被序列化的字段

{
    "age":0,
    "birthday":1650866941706,
    "name":"DGS"
}

@Data
@JSONType(includes = {"name","age","birthday"},
        orders = {"name","age","birthday"})
public class DgjTestEntity {
    private String name;
    private int age;
    private Date birthday;
    private String email;
    private String sex;
    private String address;
}

注解属性:orders 序列化后的顺序

{
    "name":"DGS",
    "age":0,
    "birthday":1650867226406
}

注解属性:serialzeFeatures序列化时的特性定义

六、 FastJson 依赖

    com.alibaba
    fastjson
    1.2.62

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/743436.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-29
下一篇 2022-04-29

发表评论

登录后才能评论

评论列表(0条)

保存