下面列出了怎么用com.alibaba.fastjson.annotation.JSONType的API类实例代码及写法,或者点击链接到github查看源代码。
public SerializeBeanInfo(Class<?> beanType, //
JSONType jsonType, //
String typeName, //
String typeKey,
int features,
FieldInfo[] fields, //
FieldInfo[] sortedFields
){
this.beanType = beanType;
this.jsonType = jsonType;
this.typeName = typeName;
this.typeKey = typeKey;
this.features = features;
this.fields = fields;
this.sortedFields = sortedFields;
}
public static Class<?> getBuilderClass(Class<?> clazz, JSONType type) {
if (clazz != null && clazz.getName().equals("org.springframework.security.web.savedrequest.DefaultSavedRequest")) {
return TypeUtils.loadClass("org.springframework.security.web.savedrequest.DefaultSavedRequest$Builder");
}
if (type == null) {
return null;
}
Class<?> builderClass = type.builder();
if (builderClass == Void.class) {
return null;
}
return builderClass;
}
private static boolean isJSONTypeIgnore(Class<?> clazz, String propertyName){
JSONType jsonType = TypeUtils.getAnnotation(clazz,JSONType.class);
if(jsonType != null){
// 1、新增 includes 支持,如果 JSONType 同时设置了includes 和 ignores 属性,则以includes为准。
// 2、个人认为对于大小写敏感的Java和JS而言,使用 equals() 比 equalsIgnoreCase() 更好,改动的唯一风险就是向后兼容性的问题
// 不过,相信开发者应该都是严格按照大小写敏感的方式进行属性设置的
String[] fields = jsonType.includes();
if(fields.length > 0){
for(int i = 0; i < fields.length; i++){
if(propertyName.equals(fields[i])){
return false;
}
}
return true;
} else{
fields = jsonType.ignores();
for(int i = 0; i < fields.length; i++){
if(propertyName.equals(fields[i])){
return true;
}
}
}
}
if(clazz.getSuperclass() != Object.class && clazz.getSuperclass() != null){
if(isJSONTypeIgnore(clazz.getSuperclass(), propertyName)){
return true;
}
}
return false;
}
/**
* @deprecated
*/
public static int getSerializeFeatures(Class<?> clazz){
JSONType annotation = TypeUtils.getAnnotation(clazz,JSONType.class);
if(annotation == null){
return 0;
}
return SerializerFeature.of(annotation.serialzeFeatures());
}
public static int getParserFeatures(Class<?> clazz){
JSONType annotation = TypeUtils.getAnnotation(clazz,JSONType.class);
if(annotation == null){
return 0;
}
return Feature.of(annotation.parseFeatures());
}
private void _if_write_null(MethodVisitor mw, FieldInfo fieldInfo, Context context) {
Class<?> propertyClass = fieldInfo.fieldClass;
Label _if = new Label();
Label _else = new Label();
Label _write_null = new Label();
Label _end_if = new Label();
mw.visitLabel(_if);
JSONField annotation = fieldInfo.getAnnotation();
int features = 0;
if (annotation != null) {
features = SerializerFeature.of(annotation.serialzeFeatures());
}
JSONType jsonType = context.beanInfo.jsonType;
if (jsonType != null) {
features |= SerializerFeature.of(jsonType.serialzeFeatures());
}
int writeNullFeatures;
if (propertyClass == String.class) {
writeNullFeatures = SerializerFeature.WriteMapNullValue.getMask()
| SerializerFeature.WriteNullStringAsEmpty.getMask();
} else if (Number.class.isAssignableFrom(propertyClass)) {
writeNullFeatures = SerializerFeature.WriteMapNullValue.getMask()
| SerializerFeature.WriteNullNumberAsZero.getMask();
} else if (Collection.class.isAssignableFrom(propertyClass)) {
writeNullFeatures = SerializerFeature.WriteMapNullValue.getMask()
| SerializerFeature.WriteNullListAsEmpty.getMask();
} else if (Boolean.class == propertyClass) {
writeNullFeatures = SerializerFeature.WriteMapNullValue.getMask()
| SerializerFeature.WriteNullBooleanAsFalse.getMask();
} else {
writeNullFeatures = SerializerFeature.WRITE_MAP_NULL_FEATURES;
}
if ((features & writeNullFeatures) == 0) {
mw.visitVarInsn(ALOAD, context.var("out"));
mw.visitLdcInsn(writeNullFeatures);
mw.visitMethodInsn(INVOKEVIRTUAL, SerializeWriter, "isEnabled", "(I)Z");
mw.visitJumpInsn(IFEQ, _else);
}
mw.visitLabel(_write_null);
mw.visitVarInsn(ALOAD, context.var("out"));
mw.visitVarInsn(ILOAD, context.var("seperator"));
mw.visitMethodInsn(INVOKEVIRTUAL, SerializeWriter, "write", "(I)V");
_writeFieldName(mw, context);
mw.visitVarInsn(ALOAD, context.var("out"));
mw.visitLdcInsn(features);
// features
if (propertyClass == String.class || propertyClass == Character.class) {
mw.visitLdcInsn(SerializerFeature.WriteNullStringAsEmpty.mask);
} else if (Number.class.isAssignableFrom(propertyClass)) {
mw.visitLdcInsn(SerializerFeature.WriteNullNumberAsZero.mask);
} else if (propertyClass == Boolean.class) {
mw.visitLdcInsn(SerializerFeature.WriteNullBooleanAsFalse.mask);
} else if (Collection.class.isAssignableFrom(propertyClass) || propertyClass.isArray()) {
mw.visitLdcInsn(SerializerFeature.WriteNullListAsEmpty.mask);
} else {
mw.visitLdcInsn(0);
}
mw.visitMethodInsn(INVOKEVIRTUAL, SerializeWriter, "writeNull", "(II)V");
// seperator = ',';
_seperator(mw, context);
mw.visitJumpInsn(GOTO, _end_if);
mw.visitLabel(_else);
mw.visitLabel(_end_if);
}
public void visitAnnotation(String desc) {
if (JSONType.equals(desc)) {
jsonType = true;
}
}
public static List<FieldInfo> computeGetters(Class<?> clazz, Map<String,String> aliasMap, boolean sorted){
JSONType jsonType = TypeUtils.getAnnotation(clazz,JSONType.class);
Map<String,Field> fieldCacheMap = new HashMap<String,Field>();
ParserConfig.parserAllFieldToCache(clazz, fieldCacheMap);
return computeGetters(clazz, jsonType, aliasMap, fieldCacheMap, sorted, PropertyNamingStrategy.CamelCase);
}
/**
* @deprecated
*/
public static Class<?> getBuilderClass(JSONType type) {
return getBuilderClass(null, type);
}