实现对象化json
This commit is contained in:
parent
1849f01b0e
commit
815e2b01a8
@ -23,4 +23,6 @@ module org.jcnc.jnotepad {
|
|||||||
exports org.jcnc.jnotepad.constants;
|
exports org.jcnc.jnotepad.constants;
|
||||||
exports org.jcnc.jnotepad.ui;
|
exports org.jcnc.jnotepad.ui;
|
||||||
exports org.jcnc.jnotepad.app.init;
|
exports org.jcnc.jnotepad.app.init;
|
||||||
|
exports org.jcnc.jnotepad.json; // 导出 JSON 相关的包,以便 Jackson 库可以访问
|
||||||
|
opens org.jcnc.jnotepad.json; // 打开 JSON 相关的包,以便 Jackson 库可以使用反射
|
||||||
}
|
}
|
||||||
@ -1,8 +1,12 @@
|
|||||||
package org.jcnc.jnotepad.constants;
|
package org.jcnc.jnotepad.constants;
|
||||||
|
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectWriter;
|
||||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||||
|
import org.jcnc.jnotepad.json.DataGenerator;
|
||||||
|
import org.jcnc.jnotepad.json.MyData;
|
||||||
import org.jcnc.jnotepad.tool.JsonUtil;
|
import org.jcnc.jnotepad.tool.JsonUtil;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@ -76,58 +80,32 @@ public class TextConstants {
|
|||||||
/**
|
/**
|
||||||
* 内置配置文件
|
* 内置配置文件
|
||||||
*/
|
*/
|
||||||
public static final String JNOTEPAD_CONFIG = createShortcutKeyJsonString();
|
public static final String JNOTEPAD_CONFIG;
|
||||||
|
|
||||||
private static String createShortcutKeyJsonString() {
|
static {
|
||||||
|
try {
|
||||||
|
JNOTEPAD_CONFIG = createShortcutKeyJsonString();
|
||||||
|
} catch (JsonProcessingException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String createShortcutKeyJsonString() throws JsonProcessingException {
|
||||||
return JsonUtil.toJsonString(createShortcutKeyJson());
|
return JsonUtil.toJsonString(createShortcutKeyJson());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ObjectNode createShortcutKeyJson() {
|
public static ObjectNode createShortcutKeyJson() throws JsonProcessingException {
|
||||||
ObjectNode json = JsonUtil.OBJECT_MAPPER.createObjectNode();
|
MyData myData = DataGenerator.generateMyData();
|
||||||
json.put(LOWER_LANGUAGE, CHINESE);
|
|
||||||
|
|
||||||
ArrayNode shortcutKeyArray = json.putArray("shortcutKey");
|
// 创建 ObjectMapper 和 ObjectWriter 来将对象转换为 JSON
|
||||||
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
ObjectWriter writer = objectMapper.writerWithDefaultPrettyPrinter();
|
||||||
|
|
||||||
ObjectNode newItem = json.objectNode();
|
// 将 MyData 对象转换为 JSON 字符串
|
||||||
newItem.put(BUTTON_NAME, "newItem");
|
String json = writer.writeValueAsString(myData);
|
||||||
newItem.put(SHORTCUT_KEY_VALUE, "ctrl+n");
|
|
||||||
shortcutKeyArray.add(newItem);
|
|
||||||
|
|
||||||
ObjectNode openItem = json.objectNode();
|
// 将 JSON 字符串转换为 ObjectNode 对象
|
||||||
openItem.put(BUTTON_NAME, "openItem");
|
|
||||||
openItem.put(SHORTCUT_KEY_VALUE, "ctrl+o");
|
|
||||||
shortcutKeyArray.add(openItem);
|
|
||||||
|
|
||||||
ObjectNode saveItem = json.objectNode();
|
return objectMapper.readValue(json, ObjectNode.class);
|
||||||
saveItem.put(BUTTON_NAME, "saveItem");
|
|
||||||
saveItem.put(SHORTCUT_KEY_VALUE, "ctrl+s");
|
|
||||||
shortcutKeyArray.add(saveItem);
|
|
||||||
|
|
||||||
ObjectNode saveAsItem = json.objectNode();
|
|
||||||
saveAsItem.put(BUTTON_NAME, "saveAsItem");
|
|
||||||
saveAsItem.put(SHORTCUT_KEY_VALUE, "ctrl+alt+s");
|
|
||||||
shortcutKeyArray.add(saveAsItem);
|
|
||||||
|
|
||||||
ObjectNode lineFeedItem = json.objectNode();
|
|
||||||
lineFeedItem.put(BUTTON_NAME, "lineFeedItem");
|
|
||||||
lineFeedItem.put(SHORTCUT_KEY_VALUE, "");
|
|
||||||
shortcutKeyArray.add(lineFeedItem);
|
|
||||||
|
|
||||||
ObjectNode openConfigItem = json.objectNode();
|
|
||||||
openConfigItem.put(BUTTON_NAME, "openConfigItem");
|
|
||||||
openConfigItem.put(SHORTCUT_KEY_VALUE, "alt+s");
|
|
||||||
shortcutKeyArray.add(openConfigItem);
|
|
||||||
|
|
||||||
ObjectNode addItem = json.objectNode();
|
|
||||||
addItem.put(BUTTON_NAME, "addItem");
|
|
||||||
addItem.put(SHORTCUT_KEY_VALUE, "");
|
|
||||||
shortcutKeyArray.add(addItem);
|
|
||||||
|
|
||||||
ObjectNode countItem = json.objectNode();
|
|
||||||
countItem.put(BUTTON_NAME, "countItem");
|
|
||||||
countItem.put(SHORTCUT_KEY_VALUE, "");
|
|
||||||
shortcutKeyArray.add(countItem);
|
|
||||||
|
|
||||||
return json;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
53
src/main/java/org/jcnc/jnotepad/json/DataGenerator.java
Normal file
53
src/main/java/org/jcnc/jnotepad/json/DataGenerator.java
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
package org.jcnc.jnotepad.json;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据生成类,用于生成示例数据。
|
||||||
|
*/
|
||||||
|
public class DataGenerator {
|
||||||
|
// 快捷键常量
|
||||||
|
private static final String CTRL_N = "ctrl+n";
|
||||||
|
private static final String CTRL_O = "ctrl+o";
|
||||||
|
private static final String CTRL_S = "ctrl+s";
|
||||||
|
private static final String CTRL_ALT_S = "ctrl+alt+s";
|
||||||
|
private static final String ALT_S = "alt+s";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成示例 MyData 对象。
|
||||||
|
*
|
||||||
|
* @return 示例 MyData 对象
|
||||||
|
*/
|
||||||
|
public static MyData generateMyData() {
|
||||||
|
MyData myData = new MyData();
|
||||||
|
myData.setLanguage("chinese");
|
||||||
|
|
||||||
|
List<MyData.ShortcutKey> shortcutKeys = new ArrayList<>();
|
||||||
|
shortcutKeys.add(createShortcutKey("newItem", CTRL_N));
|
||||||
|
shortcutKeys.add(createShortcutKey("openItem", CTRL_O));
|
||||||
|
shortcutKeys.add(createShortcutKey("saveItem", CTRL_S));
|
||||||
|
shortcutKeys.add(createShortcutKey("saveAsItem", CTRL_ALT_S));
|
||||||
|
shortcutKeys.add(createShortcutKey("lineFeedItem", ""));
|
||||||
|
shortcutKeys.add(createShortcutKey("openConfigItem", ALT_S));
|
||||||
|
shortcutKeys.add(createShortcutKey("addItem", ""));
|
||||||
|
shortcutKeys.add(createShortcutKey("countItem", ""));
|
||||||
|
|
||||||
|
myData.setShortcutKey(shortcutKeys);
|
||||||
|
return myData;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建 ShortcutKey 对象。
|
||||||
|
*
|
||||||
|
* @param buttonName 按钮名称
|
||||||
|
* @param shortcutKeyValue 快捷键值
|
||||||
|
* @return ShortcutKey 对象
|
||||||
|
*/
|
||||||
|
private static MyData.ShortcutKey createShortcutKey(String buttonName, String shortcutKeyValue) {
|
||||||
|
MyData.ShortcutKey shortcutKey = new MyData.ShortcutKey();
|
||||||
|
shortcutKey.setButtonName(buttonName);
|
||||||
|
shortcutKey.setShortcutKeyValue(shortcutKeyValue);
|
||||||
|
return shortcutKey;
|
||||||
|
}
|
||||||
|
}
|
||||||
55
src/main/java/org/jcnc/jnotepad/json/MyData.java
Normal file
55
src/main/java/org/jcnc/jnotepad/json/MyData.java
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
package org.jcnc.jnotepad.json;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据模型类,用于表示 MyData 对象的数据结构。
|
||||||
|
*/
|
||||||
|
public class MyData {
|
||||||
|
private String language;
|
||||||
|
private List<ShortcutKey> shortcutKey;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ShortcutKey 类,用于表示快捷键信息。
|
||||||
|
*/
|
||||||
|
public static class ShortcutKey {
|
||||||
|
private String buttonName;
|
||||||
|
private String shortcutKeyValue;
|
||||||
|
|
||||||
|
public ShortcutKey() {}
|
||||||
|
|
||||||
|
public String getButtonName() {
|
||||||
|
return buttonName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setButtonName(String buttonName) {
|
||||||
|
this.buttonName = buttonName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getShortcutKeyValue() {
|
||||||
|
return shortcutKeyValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setShortcutKeyValue(String shortcutKeyValue) {
|
||||||
|
this.shortcutKeyValue = shortcutKeyValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public MyData() {}
|
||||||
|
|
||||||
|
public String getLanguage() {
|
||||||
|
return language;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLanguage(String language) {
|
||||||
|
this.language = language;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<ShortcutKey> getShortcutKey() {
|
||||||
|
return shortcutKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setShortcutKey(List<ShortcutKey> shortcutKey) {
|
||||||
|
this.shortcutKey = shortcutKey;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -260,13 +260,21 @@ public class JNotepadMenuBar extends MenuBar {
|
|||||||
englishItem.setOnAction(new LocalizationHandler() {
|
englishItem.setOnAction(new LocalizationHandler() {
|
||||||
@Override
|
@Override
|
||||||
public void handle(ActionEvent actionEvent) {
|
public void handle(ActionEvent actionEvent) {
|
||||||
setCurrentLanguage(ENGLISH);
|
try {
|
||||||
|
setCurrentLanguage(ENGLISH);
|
||||||
|
} catch (JsonProcessingException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
chineseItem.setOnAction(new LocalizationHandler() {
|
chineseItem.setOnAction(new LocalizationHandler() {
|
||||||
@Override
|
@Override
|
||||||
public void handle(ActionEvent actionEvent) {
|
public void handle(ActionEvent actionEvent) {
|
||||||
setCurrentLanguage(CHINESE);
|
try {
|
||||||
|
setCurrentLanguage(CHINESE);
|
||||||
|
} catch (JsonProcessingException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -277,7 +285,7 @@ public class JNotepadMenuBar extends MenuBar {
|
|||||||
* @param language 要设置的语言
|
* @param language 要设置的语言
|
||||||
* @since 2023/8/26 16:16
|
* @since 2023/8/26 16:16
|
||||||
*/
|
*/
|
||||||
private void setCurrentLanguage(String language) {
|
private void setCurrentLanguage(String language) throws JsonProcessingException {
|
||||||
boolean flag = false;
|
boolean flag = false;
|
||||||
ObjectNode json = JsonUtil.OBJECT_MAPPER.createObjectNode();
|
ObjectNode json = JsonUtil.OBJECT_MAPPER.createObjectNode();
|
||||||
// 获取本地配置文件
|
// 获取本地配置文件
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user