refactor: #I7W7AG 使用jackson替代json

This commit is contained in:
songdragon 2023-08-27 00:12:51 +08:00
parent bd840fa160
commit d26827075b
6 changed files with 68 additions and 39 deletions

View File

@ -56,12 +56,6 @@
<artifactId>logback-classic</artifactId> <artifactId>logback-classic</artifactId>
<version>1.4.11</version> <version>1.4.11</version>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20230618</version>
</dependency>
<dependency> <dependency>
<groupId>com.ibm.icu</groupId> <groupId>com.ibm.icu</groupId>
<artifactId>icu4j</artifactId> <artifactId>icu4j</artifactId>

View File

@ -12,7 +12,6 @@ module org.jcnc.jnotepad {
requires ch.qos.logback.core; requires ch.qos.logback.core;
requires ch.qos.logback.classic; requires ch.qos.logback.classic;
requires com.ibm.icu; requires com.ibm.icu;
requires org.json;
exports org.jcnc.jnotepad.app.config; exports org.jcnc.jnotepad.app.config;
exports org.jcnc.jnotepad; exports org.jcnc.jnotepad;
exports org.jcnc.jnotepad.tool; exports org.jcnc.jnotepad.tool;

View File

@ -1,10 +1,12 @@
package org.jcnc.jnotepad.constants; package org.jcnc.jnotepad.constants;
import org.json.JSONArray; import com.fasterxml.jackson.databind.node.ArrayNode;
import org.json.JSONObject; import com.fasterxml.jackson.databind.node.ObjectNode;
import org.jcnc.jnotepad.tool.JsonUtil;
import java.util.Map; import java.util.Map;
/** /**
* 文本常量 * 文本常量
* <p> * <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() { private static String createShortcutKeyJsonString() {
JSONObject json = new JSONObject(); return JsonUtil.toJsonString(createShortcutKeyJson());
}
public static ObjectNode createShortcutKeyJson() {
ObjectNode json = JsonUtil.OBJECT_MAPPER.createObjectNode();
json.put(LOWER_LANGUAGE, CHINESE); 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(BUTTON_NAME, "newItem");
newItem.put(SHORTCUT_KEY_VALUE, "ctrl+n"); 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(BUTTON_NAME, "openItem");
openItem.put(SHORTCUT_KEY_VALUE, "ctrl+o"); 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(BUTTON_NAME, "saveItem");
saveItem.put(SHORTCUT_KEY_VALUE, "ctrl+s"); 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(BUTTON_NAME, "saveAsItem");
saveAsItem.put(SHORTCUT_KEY_VALUE, "ctrl+alt+s"); 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(BUTTON_NAME, "lineFeedItem");
lineFeedItem.put(SHORTCUT_KEY_VALUE, ""); 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(BUTTON_NAME, "openConfigItem");
openConfigItem.put(SHORTCUT_KEY_VALUE, "alt+s"); 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(BUTTON_NAME, "addItem");
addItem.put(SHORTCUT_KEY_VALUE, ""); 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(BUTTON_NAME, "countItem");
countItem.put(SHORTCUT_KEY_VALUE, ""); countItem.put(SHORTCUT_KEY_VALUE, "");
shortcutKeyArray.put(countItem); shortcutKeyArray.add(countItem);
json.put("shortcutKey", shortcutKeyArray);
return json; return json;
} }

View File

@ -9,4 +9,9 @@ public class AppException extends RuntimeException {
public AppException(String message) { public AppException(String message) {
super(message); super(message);
} }
public AppException(Throwable cause) {
super(cause);
}
} }

View 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);
}
}
}

View File

@ -3,6 +3,8 @@ package org.jcnc.jnotepad.ui.menu;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; 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.event.ActionEvent;
import javafx.scene.control.*; import javafx.scene.control.*;
import javafx.stage.Stage; 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.app.config.LocalizationConfig;
import org.jcnc.jnotepad.controller.event.handler.*; import org.jcnc.jnotepad.controller.event.handler.*;
import org.jcnc.jnotepad.exception.AppException; import org.jcnc.jnotepad.exception.AppException;
import org.jcnc.jnotepad.tool.JsonUtil;
import org.jcnc.jnotepad.tool.LogUtil; import org.jcnc.jnotepad.tool.LogUtil;
import org.jcnc.jnotepad.ui.status.JNotepadStatusBox; import org.jcnc.jnotepad.ui.status.JNotepadStatusBox;
import org.jcnc.jnotepad.ui.tab.JNotepadTab; import org.jcnc.jnotepad.ui.tab.JNotepadTab;
import org.jcnc.jnotepad.ui.tab.JNotepadTabPane; import org.jcnc.jnotepad.ui.tab.JNotepadTabPane;
import org.jcnc.jnotepad.view.init.View; import org.jcnc.jnotepad.view.init.View;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger; import org.slf4j.Logger;
import java.io.*; import java.io.*;
@ -278,10 +279,10 @@ public class JNotepadMenuBar extends MenuBar {
*/ */
private void setCurrentLanguage(String language) { private void setCurrentLanguage(String language) {
boolean flag = false; boolean flag = false;
JSONObject json = new JSONObject(); ObjectNode json = JsonUtil.OBJECT_MAPPER.createObjectNode();
// 获取本地配置文件 // 获取本地配置文件
logger.info("尝试读取本地配置文件!"); logger.info("尝试读取本地配置文件!");
StringBuffer jsonData = new StringBuffer(); StringBuilder jsonData = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(CONFIG_NAME)))) { try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(CONFIG_NAME)))) {
String line; String line;
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
@ -292,20 +293,21 @@ public class JNotepadMenuBar extends MenuBar {
flag = true; flag = true;
} }
if (!flag) { if (!flag) {
ObjectMapper objectMapper = new ObjectMapper(); ObjectMapper objectMapper = JsonUtil.OBJECT_MAPPER;
JsonNode jsonNode; JsonNode jsonNode;
try { try {
jsonNode = objectMapper.readTree(jsonData.toString()); jsonNode = objectMapper.readTree(jsonData.toString());
} catch (JsonProcessingException e) { } catch (JsonProcessingException e) {
throw new AppException(e.getMessage()); throw new AppException(e.getMessage());
} }
JSONObject finalJson = json; final ObjectNode finalJson = json;
jsonNode.fields().forEachRemaining(entry -> { jsonNode.fields().forEachRemaining(entry -> {
String key = entry.getKey(); String key = entry.getKey();
JsonNode childNode = entry.getValue(); JsonNode childNode = entry.getValue();
if (!LOWER_LANGUAGE.equals(key)) { if (!LOWER_LANGUAGE.equals(key)) {
if (childNode.isArray()) { if (childNode.isArray()) {
finalJson.put(key, new JSONArray(childNode.toString())); ArrayNode arrayNode = finalJson.putArray(key);
arrayNode.add(childNode.toString());
} else { } else {
finalJson.put(key, childNode.toString()); 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())))) { try (BufferedWriter writer = new BufferedWriter(new FileWriter(CONFIG_NAME, Charset.forName(UTF8.name())))) {
// 更新语言值为 language 并写回本地 // 更新语言值为 language 并写回本地
writer.write(json.put(LOWER_LANGUAGE, language).toString(4)); json.put(LOWER_LANGUAGE, language);
writer.write(JsonUtil.toJsonString(json));
// 刷新文件 // 刷新文件
writer.flush(); writer.flush();
// 重新加载语言包和快捷键 // 重新加载语言包和快捷键