Skip to content

Commit

Permalink
refactor: NumberJsonFormat移除simpleTypeSupport
Browse files Browse the repository at this point in the history
  • Loading branch information
livk-cloud committed Dec 15, 2023
1 parent ee3304d commit 4172117
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.text.DecimalFormat;

/**
* Number类型数据Jackson序列化处理注解
Expand All @@ -40,13 +41,8 @@
/**
* 序列化表达式
* @return the string
* @see DecimalFormat#DecimalFormat(String)
*/
String pattern() default "#0.00";

/**
* 是否支持基础类型
* @return the boolean
*/
boolean simpleTypeSupport() default true;

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Optional;
import java.util.Set;

/**
* NumberJsonFormat序列化处理
Expand All @@ -41,8 +41,8 @@
@NoArgsConstructor
public class NumberJsonSerializer extends JsonSerializer<Number> implements ContextualSerializer {

private static final List<Class<?>> SUPPORT_PRIMITIVE_CLASS = List.of(byte.class, short.class, int.class,
long.class, float.class, double.class);
private static final Set<Class<?>> SUPPORT_PRIMITIVE_CLASS = Set.of(byte.class, short.class, int.class, long.class,
float.class, double.class);

private String format;

Expand All @@ -57,15 +57,14 @@ public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty
Class<?> rawClass = property.getType().getRawClass();
NumberJsonFormat jsonFormat = Optional.ofNullable(property.getAnnotation((NumberJsonFormat.class)))
.orElse(property.getContextAnnotation(NumberJsonFormat.class));
if (Number.class.isAssignableFrom(rawClass)
|| this.simpleTypeSupport(jsonFormat.simpleTypeSupport(), rawClass)) {
if (Number.class.isAssignableFrom(rawClass) || this.simpleTypeSupport(rawClass)) {
return new NumberJsonSerializer(jsonFormat.pattern());
}
return prov.findValueSerializer(property.getType(), property);
}

private boolean simpleTypeSupport(boolean support, Class<?> rawClass) {
return support && rawClass.isPrimitive() && SUPPORT_PRIMITIVE_CLASS.contains(rawClass);
private boolean simpleTypeSupport(Class<?> rawClass) {
return rawClass.isPrimitive() && SUPPORT_PRIMITIVE_CLASS.contains(rawClass);
}

}

0 comments on commit 4172117

Please sign in to comment.