fastjson @JSONField 使用


fastjson @JSONField 使用

 

应用:标注在类的属性字段上,对字段属性序列化

 

 

******************

相关注解

 

@JSONField

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER })
public @interface JSONField {

    int ordinal() default 0;            //序列化字段在类中的输出顺序
    String name() default "";           //字段输出名称
    String format() default "";         //字段输出格式,常用于日期格式化

    boolean serialize() default true;   //是否序列化
    boolean deserialize() default true; //是否反序列化

    Class<?> serializeUsing() default Void.class;      //自定义序列化类
    Class<?> deserializeUsing() default Void.class;    //自定义反序列化类


    SerializerFeature[] serialzeFeatures() default {};
    Feature[] parseFeatures() default {};
    
    String label() default "";
    boolean jsonDirect() default false;
    
    String[] alternateNames() default {};
    boolean unwrapped() default false;
    String defaultValue() default "";
}

 

 

******************

示例 1

 

@Data
class People{

    private String id;
    private String name;
    private Integer age;
}

@Data
class Person{

    @JSONField(serialize = false)
    private String id;

    @JSONField(name = "person.name")
    private String name;

    @JSONField(ordinal = 1)
    private Integer age;
}

public class Test {

    public static void main(String[] args){
        People people=new People();
        people.setId("1");
        people.setName("瓜田李下");
        people.setAge(20);

        Person person=new Person();
        person.setId("2");
        person.setName("瓜田李下");
        person.setAge(20);

        System.out.println(people);
        System.out.println(person);

        System.out.println(JSON.toJSONString(people));
        System.out.println(JSON.toJSONString(person));
    }
}

 

***************

控制台输出

 

People(id=1, name=瓜田李下, age=20)
Person(id=2, name=瓜田李下, age=20)
{"age":20,"id":"1","name":"瓜田李下"}
{"person.name":"瓜田李下","age":20}

 

 

******************

示例 2

 

@Data
class Order{

    private String orderId;

    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;

    private LocalDateTime payTime;

    private Double price;
    private Integer amount;
}

public class Test2 {

    public static void main(String[] args){
        Order order=new Order();
        order.setOrderId("1");
        order.setCreateTime(LocalDateTime.now());
        order.setPayTime(LocalDateTime.now().plusMinutes(2));
        order.setPrice(10d);
        order.setAmount(8);

        System.out.println(order);
        System.out.println(JSON.toJSONString(order));
    }
}

 

***************

控制台输出

 

Order(orderId=1, createTime=2020-07-18T12:42:05.271725600, payTime=2020-07-18T12:44:05.271725600, price=10.0, amount=8)
{"amount":8,"createTime":"2020-07-18 12:42:05","orderId":"1","payTime":"2020-07-18T12:44:05.271725600","price":10.0}

localDateTime的输出格式设置为 yyyy-MM-dd HH:mm:ss

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值