Java Code Examples for com.alibaba.fastjson.JSONObject#forEach()

The following examples show how to use com.alibaba.fastjson.JSONObject#forEach() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
/**
 * 根据前端数据进行部分更新
 *
 * @param updateMap    updateMap
 * @param objectUpdate objectUpdate
 * @return String
 */
public List<String> verifyUpdateData(JSONObject updateMap, Object objectUpdate) {
    List<String> fieldList = new ArrayList<>();
    Class objectClass = objectUpdate.getClass();
    updateMap.forEach((String k, Object v) -> {
        try {
            Field field = objectClass.getDeclaredField(k);
            field.setAccessible(true);
            Boolean flag = true;
            Update update = field.getAnnotation(Update.class);
            if (update != null && update.temp()) {
                flag = false;
            }
            flag = handleFieldType(field, objectUpdate, v, flag);
            if (flag && update == null) {
                fieldList.add(k);
            }
            if (update != null && !Objects.equals(update.name(), "")) {
                fieldList.add(update.name());
            }
        } catch (Exception e) {
            throw new CommonException("error.verifyUpdateData.noField", e);
        }
    });
    return fieldList;
}
 
Example 2
Source Project: yue-library   File: Wrapper.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 包装字段名<br>
 * 有时字段与SQL的某些关键字冲突,导致SQL出错,因此需要将字段名用单引号或者反引号包装起来,避免冲突
 * 
 * @param paramJson 被包装的paramJson
 * @return 包装后的字段名
 */
public JSONObject wrap(JSONObject paramJson) {
	if (MapUtils.isEmpty(paramJson)) {
		return paramJson;
	}
	
	// wrap fields
	JSONObject paramJsonWrapped = new JSONObject();
	paramJson.forEach((key, value) -> {
		paramJsonWrapped.put(wrap(key), value);
	});
	
	return paramJsonWrapped;
}
 
Example 3
@Log("新增或修改")
@ApiOperation(value = "新增或修改")
@PostMapping(value = "/yxSystemConfig")
@CacheEvict(cacheNames = ShopConstants.YSHOP_REDIS_INDEX_KEY,allEntries = true)
@PreAuthorize("@el.check('admin','YXSYSTEMCONFIG_ALL','YXSYSTEMCONFIG_CREATE')")
public ResponseEntity create(@RequestBody String jsonStr){

    JSONObject jsonObject = JSON.parseObject(jsonStr);
    jsonObject.forEach(
            (key,value)->{
                YxSystemConfig yxSystemConfig = yxSystemConfigService.getOne(new LambdaQueryWrapper<YxSystemConfig>()
                        .eq(YxSystemConfig::getMenuName,key));
                YxSystemConfig yxSystemConfigModel = new YxSystemConfig();
                yxSystemConfigModel.setMenuName(key);
                yxSystemConfigModel.setValue(value.toString());
                //重新配置微信相关
                if(SystemConfigConstants.WECHAT_APPID.equals(key)){
                    WxMpConfiguration.removeWxMpService();
                    WxPayConfiguration.removeWxPayService();
                }
                if(SystemConfigConstants.WXPAY_MCHID.equals(key) || SystemConfigConstants.WXAPP_APPID.equals(key)){
                    WxPayConfiguration.removeWxPayService();
                }
                RedisUtil.set(key,value.toString(),0);
                if(ObjectUtil.isNull(yxSystemConfig)){
                    yxSystemConfigService.save(yxSystemConfigModel);
                }else{
                    yxSystemConfigModel.setId(yxSystemConfig.getId());
                    yxSystemConfigService.saveOrUpdate(yxSystemConfigModel);
                }
            }
    );

    return new ResponseEntity(HttpStatus.CREATED);
}
 
Example 4
Source Project: tephra   File: RequestAdapterImpl.java    License: MIT License 5 votes vote down vote up
private void fromJson(boolean object) {
    try {
        map = new HashMap<>();
        if (!object)
            return;

        JSONObject obj = BeanFactory.getBean(Json.class).toObject(content);
        if (obj != null)
            obj.forEach((key, value) -> map.put(key, value.toString()));
    } catch (Throwable throwable) {
        getLogger().warn(throwable, "[{}]从JSON内容[{}]中获取参数集异常!", uri, content);
    }
}
 
Example 5
protected Set<Correlation> buildCorrelations(String correlations) {
    if (StringUtils.isEmpty(correlations)) {
        return new LinkedHashSet<>();
    }
    JSONArray correlationsConfig = JSON.parseArray(correlations);
    Set<Correlation> correlations1 = new LinkedHashSet<>();
    for (int i = 0; i < correlationsConfig.size(); i++) {
        JSONObject single = correlationsConfig.getJSONObject(i);

        String target = single.getString("target");
        String alias = single.getString("alias");
        String condition = single.getString("condition");
        Objects.requireNonNull(target);
        Objects.requireNonNull(condition);
        Correlation correlation = new Correlation(target, alias, condition);
        correlation.setJoin(Correlation.JOIN.valueOf(String.valueOf(single.getOrDefault("join", "LEFT")).toUpperCase()));
        JSONObject properties = single.getJSONObject("properties");

        if (properties != null) {
            properties.forEach(correlation::setProperty);
        }
        correlations1.add(correlation);
    }

    return correlations1;

}
 
Example 6
Source Project: g-rule   File: JSONConfigHelper.java    License: Apache License 2.0 5 votes vote down vote up
private static void configFields(JSONObject jo, Class unitClass, Object instance) {
    if (jo != null) {
        jo.forEach( (key, value) -> {
            try {
                Field field = unitClass.getDeclaredField(key);
                field.setAccessible(true);
                field.set(instance, value);
            } catch (NoSuchFieldException | IllegalAccessException e) {
                e.printStackTrace();
            }
        });
    }
}
 
Example 7
@Override
public void init(Class rootClass, Map<String, String> config) throws Exception {
    String path = getConfigPath(rootClass, config);
    this.defaultPath = path;
    fetchConfig(this.defaultPath);

    Path resolve = Paths.get(path).getParent().resolve("globalVariables.json");
    Map from = JsonUtil.from(new String(Files.readAllBytes(resolve)), Map.class);
    JSONObject map = (JSONObject) from.get("map");
    this.globalVariables = new HashMap<>();
    map.forEach((key, v) -> {
        JSONObject jsonObject = (JSONObject) v;
        globalVariables.put(key, jsonObject.get("value"));
    });

    //副配置
    this.config = Files.walk(Paths.get(defaultPath).getParent()).filter(i -> {
        Path fileName = i.getFileName();
        return fileName.endsWith(".yml") || fileName.endsWith(".yaml");
    }).distinct()
            .map(i -> {
                try {
                    return getMycatConfig(i.toString());
                } catch (Throwable e) {
                    logger.warn("skip:" + i);
                    return null;
                }
            }).filter(i -> i != null).reduce(this.config, (main, config2) -> {

                List<ShardingQueryRootConfig.LogicSchemaConfig> logicSchemaConfigs = Optional.ofNullable(config2.getMetadata()).map(i -> i.getSchemas()).orElse(Collections.emptyList());
                List<PatternRootConfig> patternRootConfigs = Optional.ofNullable(config2.getInterceptors()).orElse(Collections.emptyList());
                List<DatasourceRootConfig.DatasourceConfig> datasourceConfigs = Optional.ofNullable(config2.getDatasource()).map(i -> i.getDatasources()).orElse(Collections.emptyList());
                List<ClusterRootConfig.ClusterConfig> clusterConfigs = Optional.ofNullable(config2.getCluster()).map(i -> i.getClusters()).orElse(Collections.emptyList());

                List<ShardingQueryRootConfig.LogicSchemaConfig> schemas = main.getMetadata().getSchemas();
                schemas.addAll(logicSchemaConfigs);

                List<PatternRootConfig> interceptors = main.getInterceptors();
                interceptors.addAll(patternRootConfigs);

                List<DatasourceRootConfig.DatasourceConfig> datasources = main.getDatasource().getDatasources();
                datasources.addAll(datasourceConfigs);

                List<ClusterRootConfig.ClusterConfig> clusters = main.getCluster().getClusters();
                clusters.addAll(clusterConfigs);
                return main;
            });
    logger.warn("----------------------------------Combined configuration----------------------------------");
    logger.info(YamlUtil.dump(this.config));
}