标签:bean The 名称 seo mes token int filter https
该注解隶属于阿里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以上版本
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);
}
}
@JSONField(alternateNames = {"user", "person"})
public String name;
给输出结构体中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();
}
标签:bean The 名称 seo mes token int filter https
原文地址:https://www.cnblogs.com/andy1202go/p/9875324.html