广丰卷烟厂数采质量分析系统
zhuguifei
2026-03-02 80ff784bf60637cd348ae665fc907f7b1e527dd8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package org.dromara.common.json.validate;
 
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.json.utils.JsonUtils;
 
/**
 * JSON 格式校验器
 *
 * @author AprilWind
 */
public class JsonPatternValidator implements ConstraintValidator<JsonPattern, String> {
 
    /**
     * 注解中指定的 JSON 类型枚举
     */
    private JsonType jsonType;
 
    /**
     * 初始化校验器,从注解中提取 JSON 类型
     *
     * @param annotation 注解实例
     */
    @Override
    public void initialize(JsonPattern annotation) {
        this.jsonType = annotation.type();
    }
 
    /**
     * 校验字符串是否为合法 JSON
     *
     * @param value   待校验字符串
     * @param context 校验上下文,可用于自定义错误信息
     * @return true = 合法 JSON 或为空,false = 非法 JSON
     */
    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (StringUtils.isBlank(value)) {
            // 交给 @NotBlank 或 @NotNull 控制是否允许为空
            return true;
        }
        // 根据 JSON 类型进行不同的校验
        return switch (jsonType) {
            case ANY -> JsonUtils.isJson(value);
            case OBJECT -> JsonUtils.isJsonObject(value);
            case ARRAY -> JsonUtils.isJsonArray(value);
        };
    }
 
}