@JSONField
该注解隶属于阿里fastjson,方便fastjson处理对象时的一些操作
源码
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER })
public @interface JSONField {
/**
* config encode/decode ordinal
* @since 1.1.42
* @return
*/
int ordinal() default 0;
String name() default "";
String format() default "";
boolean serialize() default true;
boolean deserialize() default true;
SerializerFeature[] serialzeFeatures() default {};
Feature[] parseFeatures() default {};
String label() default "";
/**
* @since 1.2.12
*/
boolean jsonDirect() default false;
/**
* Serializer class to use for serializing associated value.
*
* @since 1.2.16
*/
Class<?> serializeUsing() default Void.class;
/**
* Deserializer class to use for deserializing associated value.
*
* @since 1.2.16
*/
Class<?> deserializeUsing() default Void.class;
/**
* @since 1.2.21
* @return the alternative names of the field when it is deserialized
*/
String[] alternateNames() default {};
/**
* @since 1.2.31
*/
boolean unwrapped() default false;
}
- ordinal() 顺序设置。
缺省fastjson序列化一个java bean,是根据fieldName的字母序进行序列化的,你可以通过ordinal指定字段的顺序。这个特性需要1.1.42以上版本
- name() 指定名字。指定后,序列化后字段名变为指定名称,反序列化同理;
- format() 指定日期格式;
- serialize() deserialize() 是否序列化和反序列化,默认true。
- serialzeFeatures() 序列化特性选择,具体参考后面文章吧;通过枚举选择
- parseFeatures() 解释特性选择;通过枚举选择
- label() 打标记,可定制化输出
@JSONField(label = "normal")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@JSONField(label = "normal")
public String getName() {
return name;
}
System.out.println(JSON.toJSONString(v, Labels.includes("normal")));
- jsonDirect() 直接输出而不经过json转译
在fastjson-1.2.12版本中,JSONField支持一个新的配置项jsonDirect,它的用途是:当你有一个字段是字符串类型,里面是json格式数据,你希望直接输入,而不是经过转义之后再输出。
- serializeUsing() deserializeUsing() 指定序列化、反序列化使用自定义Serialize、Parser(注意,直接写在类中即可,格式固定,关键是取值)
//示例1
public static class TimeStampDeserializer implements ObjectDeserializer {
@Override
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
String time = parser.getLexer().stringVal();
if (StringUtils.isBlank(time)) {
return (T)String.valueOf(new Date().getTime());
} else {
return (T)(time + "000");
}
}
@Override
public int getFastMatchToken() {
return 0;
}
}
//示例2
public static class Model {
@JSONField(serializeUsing = ModelValueSerializer.class)
public int value;
}
public static class ModelValueSerializer implements ObjectSerializer {
@Override
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType,
int features) throws IOException {
Integer value = (Integer) object;
String text = value + "元";
serializer.write(text);
}
}
- alternateNames() 多名字。允许多个名字的变量转成一个
@JSONField(alternateNames = {"user", "person"})
public String name;
- unwrapped() 不封箱
搭配JSON.parseObject
给输出结构体中field配置上@JSONField(name="aaa")等,直接转出得到的json串
Response response = JSON.parseObject(res, Response.class);
@Setter
@Getter
@NoArgsConstructor
@ToString
public class Response {
@JSONField(name = "answerList")
private List<Answer> answers;
@JSONField(name = "timeStamp")
private String timeStamp;
@JSONField(name = "type")
private String answerType;
@JSONField(name = "id")
private String erp;
@JSONField(name = "session")
private String sessionId;
@JSONField(name = "topic")
private String topic;
@JSONField(deserialize = false, serialize = false)
private String answerType = AnswerTypeEnum.ANSWER.getCode();
}
参考文章
- Fastjson JSONField
- JSONField_jsonDirect_cn
- FastJson 自定义Serialize、Parser
- fastjson使用(八) -- LabelFilter的使用
- fastjson的@JSONField注解的一点问题
- fastjson SerializerFeature详解
原文地址:https://www.cnblogs.com/andy1202go/p/9875324.html
时间: 2024-11-14 11:59:47