feat: The registration center in config mode supports JSON format. (#1551)

pull/1552/head
song 2 weeks ago committed by GitHub
parent 2c6fbefe13
commit 5ebe215f5f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -34,6 +34,7 @@ public final class ConfigParserHandler {
}
PARSERS.add(new PropertiesConfigParser());
PARSERS.add(new YamlConfigParser());
PARSERS.add(new JsonConfigParser());
}
public Map<Object, Object> parseConfig(String content, String type) throws IOException {

@ -0,0 +1,96 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.hippo4j.threadpool.dynamic.mode.config.parser;
import cn.hippo4j.common.toolkit.CollectionUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import java.io.IOException;
import java.util.*;
/**
* Json config parser.
*/
public class JsonConfigParser extends AbstractConfigParser {
private static final ObjectMapper MAPPER;
private static final String DOT = ".";
private static final String LEFT_BRACE = "{";
private static final String RIGHT_BRACE = "{";
private static final String LEFT_BRACKET = "[";
private static final String RIGHT_BRACKET = "]";
static {
MAPPER = new ObjectMapper();
}
public Map<Object, Object> doParse(String content, String prefix) throws IOException {
Map<String, Object> originMap = MAPPER.readValue(content, LinkedHashMap.class);
Map<Object, Object> result = new HashMap<>();
flatMap(result, originMap, prefix);
return result;
}
private void flatMap(Map<Object, Object> result, Map<String, Object> dataMap, String prefix) {
if (MapUtils.isEmpty(dataMap)) {
return;
}
dataMap.forEach((k, v) -> {
String fullKey = genFullKey(prefix, k);
if (v instanceof Map) {
flatMap(result, (Map<String, Object>) v, fullKey);
return;
} else if (v instanceof Collection) {
int count = 0;
for (Object obj : (Collection<Object>) v) {
String kk = LEFT_BRACKET + (count++) + RIGHT_BRACKET;
flatMap(result, Collections.singletonMap(kk, obj), fullKey);
}
return;
}
result.put(fullKey, v);
});
}
private String genFullKey(String prefix, String key) {
if (StringUtils.isEmpty(prefix)) {
return key;
}
return key.startsWith(LEFT_BRACE) ? prefix.concat(key) : prefix.concat(DOT).concat(key);
}
@Override
public Map<Object, Object> doParse(String content) throws IOException {
if (StringUtils.isEmpty(content)) {
return new HashMap<>(1);
}
return doParse(content,"");
}
@Override
public List<ConfigFileTypeEnum> getConfigFileTypes() {
return CollectionUtil.newArrayList(ConfigFileTypeEnum.JSON, ConfigFileTypeEnum.JSON);
}
}
Loading…
Cancel
Save