Refactoring the YAML parsing section

This commit is contained in:
2026-04-19 11:56:33 +08:00
parent 9c95f00b6b
commit e4211c87e1
9 changed files with 169 additions and 40 deletions

26
.idea/jsonSchemas.xml generated Normal file
View File

@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JsonSchemaMappingsProjectConfiguration">
<state>
<map>
<entry key="JSON 架构 7">
<value>
<SchemaInfo>
<option name="name" value="JSON 架构 7" />
<option name="relativePathToSchema" value="http://json-schema.org/draft-07/schema" />
<option name="schemaVersion" value="JSON 架构 7" />
<option name="applicationDefined" value="true" />
<option name="patterns">
<list>
<Item>
<option name="path" value="test.yaml" />
</Item>
</list>
</option>
</SchemaInfo>
</value>
</entry>
</map>
</state>
</component>
</project>

8
.idea/php.xml generated Normal file
View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="PhpCodeSniffer">
<phpcs_settings>
<phpcs_by_interpreter asDefaultInterpreter="true" interpreter_id="cb2f195d-67ff-4f69-aa31-4e5ccc1eef4f" timeout="30000" />
</phpcs_settings>
</component>
</project>

17
pom.xml
View File

@@ -22,6 +22,23 @@
<scope>system</scope> <scope>system</scope>
<systemPath>${project.basedir}/libs/snakeyaml-2.6.jar</systemPath> <systemPath>${project.basedir}/libs/snakeyaml-2.6.jar</systemPath>
</dependency> </dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.4.2</version>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@@ -0,0 +1,70 @@
package main.YamlLoad;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.yaml.snakeyaml.Yaml;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import static main.util.tools.echo;
public class json {
public static String yamlToJson(String filename) {
String jsonData;
try {
// 创建Yaml对象
Yaml yaml = new Yaml();
// 打开文件输入流
FileInputStream input = new FileInputStream(filename);
// 读取整个文件为一个Map对象,如果yaml文件为列表则数据类型为list
Map<String, Object> data = yaml.load(input);
// 创建ObjectMapper对象用于将数据转换为JSON
ObjectMapper mapper = new ObjectMapper();
// 启用格式化输出
mapper.enable(SerializationFeature.INDENT_OUTPUT);
// 将数据转换为JSON字符串
jsonData = mapper.writeValueAsString(data);
// 返回读取的数据
return jsonData;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
// 解析Json数据输入JsonKey返回JsonValue
public static String getJsonValue(String Json, String JsonKey) {
try {
Object jsonObject = JSONObject.parse(Json);
String[] keys = JsonKey.split("\\.");
Object currentValue = jsonObject;
for (String key : keys) {
if (currentValue instanceof JSONObject) {
currentValue = ((JSONObject) currentValue).get(key);
} else if (currentValue instanceof JSONArray) {
int index = Integer.parseInt(key);
currentValue = ((JSONArray) currentValue).get(index);
} else {
return null;
}
if (currentValue == null) {
return null;
}
}
return currentValue.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}

View File

@@ -1,26 +0,0 @@
package main.YamlLoad;
import org.yaml.snakeyaml.Yaml;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Map;
public class yaml {
public String StringYaml(String YamlFile, String GetKey) {
String YamlValue = "";
try (InputStream input = new FileInputStream(YamlFile)) {
Yaml yaml = new Yaml();
Map<String, Object> data = yaml.load(input);
if (data != null && data.containsKey(GetKey)) {
Object value = data.get(GetKey);
YamlValue = value != null ? value.toString() : "";
}
} catch (Exception e) {
throw new RuntimeException("读取YAML文件失败: " + e.getMessage(), e);
}
return YamlValue;
}
}

View File

@@ -1,6 +1,7 @@
package main; package main;
import main.YamlLoad.yaml; import static main.YamlLoad.json.getJsonValue;
import static main.YamlLoad.json.yamlToJson;
import static main.util.tools.echo; import static main.util.tools.echo;
@@ -11,25 +12,30 @@ public class main {
boolean debug = new main().debug; boolean debug = new main().debug;
if (debug) { if (debug) {
// 打印当前运行环境的系统类型和版本 // 打印当前运行环境的系统类型和版本
System.out.println("系统类型: " + System.getProperty("os.name") + " " + "系统版本: " + System.getProperty("os.version")); System.out.println("系统类型: " + System.getProperty("os.name") + "\n");
System.out.println("Java版本: " + System.getProperty("java.version")); System.out.println("系统版本: " + System.getProperty("os.version") + "\n");
System.out.println("Java Vendor版本: " + System.getProperty("java.vendor")); System.out.println("Java版本: " + System.getProperty("java.version") + "\n");
System.out.println("Java Home位置: " + System.getProperty("java.home")); System.out.println("Java Vendor版本: " + System.getProperty("java.vendor") + "\n");
System.out.println("Java Class位置: " + System.getProperty("java.class.path")); System.out.println("Java Home位置: " + System.getProperty("java.home") + "\n");
System.out.println("运行的用户: " + System.getProperty("user.name")); System.out.println("Java Class位置: " + System.getProperty("java.class.path") + "\n");
System.out.println("运行的用户: " + System.getProperty("user.name") + "\n");
// 获取当前的运行目录 // 获取当前的运行目录
System.out.println("当前运行目录: " + System.getProperty("user.dir")); System.out.println("当前运行目录: " + System.getProperty("user.dir") + "\n");
} }
echo("\nHello World!\n"); echo("Hello World!\n");
new main().YamlTest(); new main().YamlTest();
} }
private void YamlTest() { private void YamlTest() {
yaml y = new yaml(); String json = yamlToJson("test.yaml");
String YamlValue = y.StringYaml("test.yml", "test"); echo("Yaml解析Json: \n" + json);
System.out.println(YamlValue); String yaml = getJsonValue(json, "yaml");
String config = getJsonValue(yaml, "config");
String string = getJsonValue(config, "string");
String bool = getJsonValue(config, "bool");
echo("Json解析的内容: \n" + string + "\n" + bool);
} }
public static String Version() { public static String Version() {

View File

@@ -0,0 +1,26 @@
package main.YamlLoad;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.yaml.snakeyaml.Yaml;
import java.io.FileInputStream;
import java.util.Map;
import java.util.Objects;
public class json {
public static String YamlToJson(String FileName) {
String jsonData;
try {
Yaml yaml = new Yaml();
FileInputStream input =new FileInputStream(FileName);
Map<String, Objects> data = yaml.load(input);
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
jsonData = mapper.writeValueAsString(data);
}catch (Exception err) {
err.printStackTrace();
}
return jsonData;
}
}

View File

@@ -1 +1,4 @@
test: 123456 yaml:
config:
string: 123456
bool: true

View File

@@ -1 +0,0 @@
test: 123456