Skip to content

Commit 87e83a9

Browse files
committed
Update Yaml.
1 parent ea3a078 commit 87e83a9

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package me.scoretwo.fastscript.config
2+
3+
import com.alibaba.fastjson.JSON
4+
import com.alibaba.fastjson.JSONObject
5+
import me.scoretwo.fastscript.utils.FileUtils
6+
import org.yaml.snakeyaml.Yaml
7+
import java.io.File
8+
import java.io.FileInputStream
9+
import java.io.InputStream
10+
11+
class YAMLObject(val file: File): JSONObject() {
12+
13+
val yaml = Yaml()
14+
15+
private val clazz: Class<JSONObject> = javaClass
16+
private val mapField = clazz.getDeclaredField("map")
17+
18+
fun load(inputStream: InputStream) {
19+
mapField.isAccessible = true
20+
mapField.set(this, JSON.toJSON(yaml.load(inputStream)))
21+
}
22+
23+
fun save(file: File = this.file) {
24+
mapField.isAccessible = true
25+
FileUtils.save(file, toString())
26+
}
27+
28+
override fun toString(): String {
29+
return yaml.dumpAsMap(mapField.get(this))
30+
}
31+
32+
companion object {
33+
34+
fun loadConfiguration(file: File): YAMLObject {
35+
val yamlObject = YAMLObject(file)
36+
37+
try {
38+
val fileInputStream = FileInputStream(file)
39+
yamlObject.load(fileInputStream)
40+
fileInputStream.close()
41+
} catch (e: Exception) {
42+
e.printStackTrace()
43+
}
44+
45+
return yamlObject
46+
}
47+
48+
fun loadConfiguration(file: File, inputStream: InputStream): YAMLObject {
49+
val yamlObject = YAMLObject(file)
50+
51+
yamlObject.load(inputStream)
52+
53+
return yamlObject
54+
}
55+
56+
fun saveToFile(file: File, yamlObject: YAMLObject) {
57+
yamlObject.save(file)
58+
}
59+
60+
fun saveToFile(yamlObject: YAMLObject) {
61+
yamlObject.save()
62+
}
63+
64+
}
65+
66+
}

0 commit comments

Comments
 (0)