青岛市网站制作,企业邮箱密码忘了怎么重置密码,wordpress可以建立商城,做简历的网站JSONField注解是阿里巴巴的fastjson框架中的注解#xff0c;用于指定JSON字符串中的属性名和Java对象中的属性名之间的映射关系
JsonProperty注解是Jackson框架中的注解#xff0c;用法类似于JSONField#xff0c;也是指定JSON字符串中的属性名和Java对象中的属性名之间的映…JSONField注解是阿里巴巴的fastjson框架中的注解用于指定JSON字符串中的属性名和Java对象中的属性名之间的映射关系
JsonProperty注解是Jackson框架中的注解用法类似于JSONField也是指定JSON字符串中的属性名和Java对象中的属性名之间的映射关系
一、JSONField
JSONField注解是阿里巴巴的fastjson框架中的注解用于指定JSON字符串中的属性名和Java对象中的属性名之间的映射关系
1、name属性
通过name属性可以指定将Java对象中的属性名映射为JSON对象中的属性名。默认情况下JSON对象中的属性名与Java对象中的属性名相同
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;public class Test1Controller {public static void main(String[] args) {String str {\ID\:11,\Name\:\张三\};User user JSON.parseObject(str, User.class);System.out.println(User对象 user.toString());}
}Data
class User {JSONField(name ID)private Integer userId;JSONField(name Name)private String userName;Overridepublic String toString() {return User{ userId userId , userName userName \ };}
}2、format属性
通过format属性可以指定日期类型属性的格式以及将数字类型转为字符串类型的格式
public class User {JSONField(format yyyy-MM-dd)private Date birthday;JSONField(format #,###.00)private double salary;
}
3、serialize属性
通过serialize属性可以控制属性是否序列化到JSON对象中
当serialize属性为false时属性将不会序列化到JSON对象中默认为true
public class User {JSONField(serialize false)private int userId;private String name;private int age;
}
使用JSONField注解将userId属性从序列化中排除
4、deserialize属性
通过deserialize属性可以控制是否将JSON对象中的属性反序列化到Java对象中
当deserialize属性为false时该属性将不会从JSON对象中反序列化到Java对象中默认为true
public class User {private int userId;private String name;JSONField(deserialize false)private int age;
}
使用JSONField注解将age属性从反序列化中排除
5、ordinal属性
通过ordinal属性可以指定属性的顺序
默认情况下属性的顺序是按照属性在Java对象中的顺序排列的
public class User {JSONField(ordinal 2)private String name;JSONField(ordinal 1)private int age;
}
使用JSONField注解指定了age属性的顺序为1name属性的顺序为2
6、defaultValue属性
通过defaultValue属性可以指定属性在Java对象中的默认值
public class User {JSONField(defaultValue 0)private int userId;JSONField(defaultValue N/A)private String name;private int age;
}
使用JSONField注解指定了userId属性的默认值为0name属性的默认值为“N/A”
7、type属性
通过type属性可以指定属性的类型
public class User {private int userId;JSONField(type FieldType.STRING)private int age;
}
使用JSONField注解指定了age属性的类型为字符串类型
8、jsonDirect属性
通过jsonDirect属性可以指定属性是否应该直接输出为JSON字符串
默认情况下属性将被转化为字符串并以引号标记输出
public class User {private String name;JSONField(jsonDirect true)private String jsonMessage;
}
使用JSONField注解指定了jsonMessage属性应该直接输出为JSON字符串
9、parseFeatures属性和serializeFeatures属性
通过parseFeatures属性和serializeFeatures属性可以配置解析和序列化时的特性
具体可参考阿里巴巴fastjson的文档
public class User {private String name;private int age;JSONField(parseFeatures Feature.AllowSingleQuotes)private String message;
} 二、JsonProperty
JsonProperty注解是Jackson框架中的注解用法类似于JSONField也是指定JSON字符串中的属性名和Java对象中的属性名之间的映射关系
1、value
value属性用于指定序列化后的属性名称
如果未提供value属性则属性名称默认与Java属性名称相同
public class Person {JsonProperty(name)private String fullName;
}
将Java对象中fullName属性序列化为JSON对象中的name属性
2、defaultValue
defaultValue属性用于指定当Java对象属性值为null时序列化为JSON时使用的默认值
该属性仅适用于对象属性而不适用于基本类型属性
public class Person {JsonProperty(defaultValue John)private String firstName;
}
将未设置firstName的Person对象序列化为含有默认值John的JSON属性
3、access
access属性用于指定Java属性的访问级别
默认访问级别为PUBLIC也可以设为READ_ONLY或WRITE_ONLY
public class Person {JsonProperty(access JsonProperty.Access.WRITE_ONLY)private String password;
}
将Java对象中的password属性序列化为JSON时忽略掉
4、required
required属性指定此属性是否为必须字段
如果为true则当将JSON转换回Java对象时如果该属性不存在则将引发异常
public class Person {JsonProperty(required true)private String name;
}
将Java对象中的name属性序列化为确保其在JSON对象中存在
5、defaultValue
defaultValue属性用于指定Java属性的默认值
在将JSON转换回Java对象时如果该属性不存在或为null则使用默认值
public class Person {JsonProperty(defaultValue 30)private int age;
}
将Java对象中的age属性序列化为JSON时如果该属性不存在则使用默认值30
6、index
index属性用于指定序列化的属性在JSON对象中的位置数值越小位置越靠前
public class Person {JsonProperty(index 1)private String firstName;JsonProperty(index 0)private String lastName;
}
将Java对象中的lastName属性序列化为JSON对象中的第一个属性firstName属性序列化为JSON对象中的第二个属性
7、accessMode
accessMode属性用于指定序列化时使用的访问模式
如果未指定则默认为PROPETY模式即访问getter方法获取属性值。另一个可用的模式是FIELD即直接访问Java属性
public class User {private String firstName;JsonProperty(access JsonProperty.Access.READ_ONLY, accessMode JsonProperty.AccessMode.FIELD)private String lastName;
}
将Java对象中lastName属性序列化为JSON属性直接访问Java属性值。
8、ignore
ignore属性用于指定是否忽略该属性
如果为true则在序列化和反序列化时忽略该属性
public class User{private String firstName;JsonProperty(ignore true)private String lastName;
}
将Java对象中的lastName属性忽略掉不进行序列化和反序列化 三、JSONField和JsonProperty区别
1、JSONField注解的使用方式更加简单注解默认的值与属性名相同而JsonProperty需要手动指定属性名
2、JSONField注解支持更多的属性映射选项例如序列化时的日期格式空值的处理方式等
3、JSONField注解的性能较快因为fastjson本身就是一款高性能的JSON处理库
4、在使用Jackson框架时只能使用JsonProperty注解无法使用JSONField注解不然会导致解析失败无法取到值如下
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;public class Test1Controller {public static void main(String[] args) {String str {\ID\:11,\Name\:\张三\};User user JSON.parseObject(str, User.class);System.out.println(User对象 user.toString());}
}Data
class User {JsonProperty(ID)private Integer userId;JsonProperty(Name)private String userName;Overridepublic String toString() {return User{ userId userId , userName userName \ };}
}