!36 refactor: #I7W7AG 使用jackson替代json
Merge pull request !36 from songdragon/refactor-I7W7AG
This commit is contained in:
commit
1849f01b0e
6
pom.xml
6
pom.xml
@ -56,12 +56,6 @@
|
||||
<artifactId>logback-classic</artifactId>
|
||||
<version>1.4.11</version>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/org.json/json -->
|
||||
<dependency>
|
||||
<groupId>org.json</groupId>
|
||||
<artifactId>json</artifactId>
|
||||
<version>20230618</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.ibm.icu</groupId>
|
||||
<artifactId>icu4j</artifactId>
|
||||
|
||||
@ -12,7 +12,6 @@ module org.jcnc.jnotepad {
|
||||
requires ch.qos.logback.core;
|
||||
requires ch.qos.logback.classic;
|
||||
requires com.ibm.icu;
|
||||
requires org.json;
|
||||
exports org.jcnc.jnotepad.app.config;
|
||||
exports org.jcnc.jnotepad;
|
||||
exports org.jcnc.jnotepad.tool;
|
||||
|
||||
@ -1,10 +1,12 @@
|
||||
package org.jcnc.jnotepad.constants;
|
||||
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import org.jcnc.jnotepad.tool.JsonUtil;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 文本常量
|
||||
* <p>
|
||||
@ -74,56 +76,57 @@ public class TextConstants {
|
||||
/**
|
||||
* 内置配置文件
|
||||
*/
|
||||
public static final String JNOTEPAD_CONFIG = createShortcutKeyJson().toString(4);
|
||||
public static final String JNOTEPAD_CONFIG = createShortcutKeyJsonString();
|
||||
|
||||
public static JSONObject createShortcutKeyJson() {
|
||||
JSONObject json = new JSONObject();
|
||||
private static String createShortcutKeyJsonString() {
|
||||
return JsonUtil.toJsonString(createShortcutKeyJson());
|
||||
}
|
||||
|
||||
public static ObjectNode createShortcutKeyJson() {
|
||||
ObjectNode json = JsonUtil.OBJECT_MAPPER.createObjectNode();
|
||||
json.put(LOWER_LANGUAGE, CHINESE);
|
||||
|
||||
JSONArray shortcutKeyArray = new JSONArray();
|
||||
ArrayNode shortcutKeyArray = json.putArray("shortcutKey");
|
||||
|
||||
JSONObject newItem = new JSONObject();
|
||||
ObjectNode newItem = json.objectNode();
|
||||
newItem.put(BUTTON_NAME, "newItem");
|
||||
newItem.put(SHORTCUT_KEY_VALUE, "ctrl+n");
|
||||
shortcutKeyArray.put(newItem);
|
||||
shortcutKeyArray.add(newItem);
|
||||
|
||||
JSONObject openItem = new JSONObject();
|
||||
ObjectNode openItem = json.objectNode();
|
||||
openItem.put(BUTTON_NAME, "openItem");
|
||||
openItem.put(SHORTCUT_KEY_VALUE, "ctrl+o");
|
||||
shortcutKeyArray.put(openItem);
|
||||
shortcutKeyArray.add(openItem);
|
||||
|
||||
JSONObject saveItem = new JSONObject();
|
||||
ObjectNode saveItem = json.objectNode();
|
||||
saveItem.put(BUTTON_NAME, "saveItem");
|
||||
saveItem.put(SHORTCUT_KEY_VALUE, "ctrl+s");
|
||||
shortcutKeyArray.put(saveItem);
|
||||
shortcutKeyArray.add(saveItem);
|
||||
|
||||
JSONObject saveAsItem = new JSONObject();
|
||||
ObjectNode saveAsItem = json.objectNode();
|
||||
saveAsItem.put(BUTTON_NAME, "saveAsItem");
|
||||
saveAsItem.put(SHORTCUT_KEY_VALUE, "ctrl+alt+s");
|
||||
shortcutKeyArray.put(saveAsItem);
|
||||
shortcutKeyArray.add(saveAsItem);
|
||||
|
||||
JSONObject lineFeedItem = new JSONObject();
|
||||
ObjectNode lineFeedItem = json.objectNode();
|
||||
lineFeedItem.put(BUTTON_NAME, "lineFeedItem");
|
||||
lineFeedItem.put(SHORTCUT_KEY_VALUE, "");
|
||||
shortcutKeyArray.put(lineFeedItem);
|
||||
shortcutKeyArray.add(lineFeedItem);
|
||||
|
||||
JSONObject openConfigItem = new JSONObject();
|
||||
ObjectNode openConfigItem = json.objectNode();
|
||||
openConfigItem.put(BUTTON_NAME, "openConfigItem");
|
||||
openConfigItem.put(SHORTCUT_KEY_VALUE, "alt+s");
|
||||
shortcutKeyArray.put(openConfigItem);
|
||||
shortcutKeyArray.add(openConfigItem);
|
||||
|
||||
JSONObject addItem = new JSONObject();
|
||||
ObjectNode addItem = json.objectNode();
|
||||
addItem.put(BUTTON_NAME, "addItem");
|
||||
addItem.put(SHORTCUT_KEY_VALUE, "");
|
||||
shortcutKeyArray.put(addItem);
|
||||
shortcutKeyArray.add(addItem);
|
||||
|
||||
JSONObject countItem = new JSONObject();
|
||||
ObjectNode countItem = json.objectNode();
|
||||
countItem.put(BUTTON_NAME, "countItem");
|
||||
countItem.put(SHORTCUT_KEY_VALUE, "");
|
||||
shortcutKeyArray.put(countItem);
|
||||
|
||||
json.put("shortcutKey", shortcutKeyArray);
|
||||
shortcutKeyArray.add(countItem);
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
@ -9,4 +9,9 @@ public class AppException extends RuntimeException {
|
||||
public AppException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public AppException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
25
src/main/java/org/jcnc/jnotepad/tool/JsonUtil.java
Normal file
25
src/main/java/org/jcnc/jnotepad/tool/JsonUtil.java
Normal file
@ -0,0 +1,25 @@
|
||||
package org.jcnc.jnotepad.tool;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import org.jcnc.jnotepad.exception.AppException;
|
||||
|
||||
/**
|
||||
* jackson解析器的facade类,主要提供objectMapper对象
|
||||
*
|
||||
* @author songdragon
|
||||
*/
|
||||
public class JsonUtil {
|
||||
|
||||
public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
|
||||
;
|
||||
|
||||
public static String toJsonString(Object o) {
|
||||
try {
|
||||
return OBJECT_MAPPER.writeValueAsString(o);
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new AppException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -3,6 +3,8 @@ package org.jcnc.jnotepad.ui.menu;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import javafx.event.ActionEvent;
|
||||
import javafx.scene.control.*;
|
||||
import javafx.stage.Stage;
|
||||
@ -11,13 +13,12 @@ import org.jcnc.jnotepad.app.config.GlobalConfig;
|
||||
import org.jcnc.jnotepad.app.config.LocalizationConfig;
|
||||
import org.jcnc.jnotepad.controller.event.handler.*;
|
||||
import org.jcnc.jnotepad.exception.AppException;
|
||||
import org.jcnc.jnotepad.tool.JsonUtil;
|
||||
import org.jcnc.jnotepad.tool.LogUtil;
|
||||
import org.jcnc.jnotepad.ui.status.JNotepadStatusBox;
|
||||
import org.jcnc.jnotepad.ui.tab.JNotepadTab;
|
||||
import org.jcnc.jnotepad.ui.tab.JNotepadTabPane;
|
||||
import org.jcnc.jnotepad.view.init.View;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import java.io.*;
|
||||
@ -278,10 +279,10 @@ public class JNotepadMenuBar extends MenuBar {
|
||||
*/
|
||||
private void setCurrentLanguage(String language) {
|
||||
boolean flag = false;
|
||||
JSONObject json = new JSONObject();
|
||||
ObjectNode json = JsonUtil.OBJECT_MAPPER.createObjectNode();
|
||||
// 获取本地配置文件
|
||||
logger.info("尝试读取本地配置文件!");
|
||||
StringBuffer jsonData = new StringBuffer();
|
||||
StringBuilder jsonData = new StringBuilder();
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(CONFIG_NAME)))) {
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
@ -292,20 +293,21 @@ public class JNotepadMenuBar extends MenuBar {
|
||||
flag = true;
|
||||
}
|
||||
if (!flag) {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
ObjectMapper objectMapper = JsonUtil.OBJECT_MAPPER;
|
||||
JsonNode jsonNode;
|
||||
try {
|
||||
jsonNode = objectMapper.readTree(jsonData.toString());
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new AppException(e.getMessage());
|
||||
}
|
||||
JSONObject finalJson = json;
|
||||
final ObjectNode finalJson = json;
|
||||
jsonNode.fields().forEachRemaining(entry -> {
|
||||
String key = entry.getKey();
|
||||
JsonNode childNode = entry.getValue();
|
||||
if (!LOWER_LANGUAGE.equals(key)) {
|
||||
if (childNode.isArray()) {
|
||||
finalJson.put(key, new JSONArray(childNode.toString()));
|
||||
ArrayNode arrayNode = finalJson.putArray(key);
|
||||
arrayNode.add(childNode.toString());
|
||||
} else {
|
||||
finalJson.put(key, childNode.toString());
|
||||
}
|
||||
@ -320,7 +322,8 @@ public class JNotepadMenuBar extends MenuBar {
|
||||
}
|
||||
try (BufferedWriter writer = new BufferedWriter(new FileWriter(CONFIG_NAME, Charset.forName(UTF8.name())))) {
|
||||
// 更新语言值为 language 并写回本地
|
||||
writer.write(json.put(LOWER_LANGUAGE, language).toString(4));
|
||||
json.put(LOWER_LANGUAGE, language);
|
||||
writer.write(JsonUtil.toJsonString(json));
|
||||
// 刷新文件
|
||||
writer.flush();
|
||||
// 重新加载语言包和快捷键
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user