根据json格式配置校验对象属性值是否符合

pom依赖

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.13.0</version> <!-- 使用最新版本 -->
</dependency>

具体方法

jsonConfig 例:

String jsonConfig = "[{\"expression\":\"!=\",\"fields\":\"status\",\"value\":\"CAMPAIGN_STATUS_ENABLE\"}," + "{\"expression\":\">\",\"fields\":\"budget\",\"value\":\"99999\"}]";
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class YourClass {

    // ...

    public boolean checkConditions(String jsonConfig, Object ocAdGroup) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode rootNode = objectMapper.readTree(jsonConfig);

            for (JsonNode conditionNode : rootNode) {
                String expression = conditionNode.get("expression").asText();
                String fields = conditionNode.get("fields").asText();
                String value = conditionNode.get("value").asText();

                // Check if the 'fields' property exists in the OcAdGroup class
                if (!hasProperty(ocAdGroup, fields)) {
                    throw new IllegalArgumentException("Field '" + fields + "' does not exist in OcAdGroup.");
                }

                // Use reflection to get the value of the 'fields' property in OcAdGroup
                Object fieldValue = getProperty(ocAdGroup, fields);

                // Perform the comparison based on the 'expression' and 'value' in the condition
                boolean conditionSatisfied = checkCondition(expression, fieldValue, value);

                if (!conditionSatisfied) {
                    return false; // Condition not satisfied
                }
            }

            return true; // All conditions satisfied
        } catch (Exception e) {
            // Handle exceptions (e.g., JSON parsing, reflection errors)
            e.printStackTrace();
            return false; // An error occurred
        }
    }

    private boolean hasProperty(Object object, String propertyName) {
        try {
            object.getClass().getDeclaredField(propertyName);
            return true;
        } catch (NoSuchFieldException e) {
            return false;
        }
    }

    private Object getProperty(Object object, String propertyName) throws Exception {
        java.lang.reflect.Field field = object.getClass().getDeclaredField(propertyName);
        field.setAccessible(true);
        return field.get(object);
    }

    private boolean checkCondition(String expression, Object fieldValue, String value) {
        // Implement your condition checking logic here
        // For example, if expression is '!=':
        // return !fieldValue.equals(value);
        // For other expressions, implement the corresponding checks.
        return false;
    }

    // ...
}

补充 checkCondition()方法

private static boolean checkCondition(String expression, Object fieldValue, String value) {
        try {
            if (fieldValue instanceof String) {
                String strValue = (String) fieldValue;
                switch (expression) {
                    case "==":
                        return strValue.equals(value);
                    case "!=":
                        return !strValue.equals(value);
                    default:
                        return false;
                }
            } else if (fieldValue instanceof Integer || fieldValue instanceof Long) {
                long longValue = ((Number) fieldValue).longValue();
                long longConditionValue = Long.parseLong(value);
                switch (expression) {
                    case "==":
                        return longValue == longConditionValue;
                    case "!=":
                        return longValue != longConditionValue;
                    case ">":
                        return longValue > longConditionValue;
                    case "<":
                        return longValue < longConditionValue;
                    case ">=":
                        return longValue >= longConditionValue;
                    case "<=":
                        return longValue <= longConditionValue;
                    default:
                        return false;
                }
            } else if (fieldValue instanceof Double || fieldValue instanceof Float) {
                double doubleValue = ((Number) fieldValue).doubleValue();
                double doubleConditionValue = Double.parseDouble(value);
                switch (expression) {
                    case "==":
                        return doubleValue == doubleConditionValue;
                    case "!=":
                        return doubleValue != doubleConditionValue;
                    case ">":
                        return doubleValue > doubleConditionValue;
                    case "<":
                        return doubleValue < doubleConditionValue;
                    case ">=":
                        return doubleValue >= doubleConditionValue;
                    case "<=":
                        return doubleValue <= doubleConditionValue;
                    default:
                        return false;
                }
            } else {
                return false;
            }
        } catch (NumberFormatException e) {
            return false;
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值