JSON工具增加JavaBean转JSON功能

This commit is contained in:
rememberber
2024-11-25 23:15:35 +08:00
parent c84457ba11
commit 48cf813d76
3 changed files with 54 additions and 15 deletions
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.luoboduner.moo.tool.ui.dialog.JsonResultDialog">
<grid id="cbd77" binding="contentPane" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="cbd77" binding="contentPane" layout-manager="GridLayoutManager" row-count="3" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="48" y="54" width="436" height="297"/>
@@ -9,9 +9,9 @@
<border type="none"/>
<children>
<grid id="94766" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="10" bottom="10" right="10"/>
<margin top="10" left="10" bottom="10" right="10"/>
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
@@ -41,13 +41,41 @@
</grid>
</children>
</grid>
<grid id="e3588" binding="mainPanel" layout-manager="BorderLayout" hgap="0" vgap="0">
<grid id="749aa" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="10" left="10" bottom="10" right="10"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children/>
<children>
<component id="52917" class="javax.swing.JLabel" binding="tipsLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
</children>
</grid>
<grid id="b4d96" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="10" bottom="0" right="10"/>
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="e3588" binding="mainPanel" layout-manager="BorderLayout" hgap="0" vgap="0">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="7" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children/>
</grid>
</children>
</grid>
</children>
</grid>
@@ -23,11 +23,12 @@ public class JsonResultDialog extends JDialog {
private JPanel contentPane;
private JButton buttonOK;
private JPanel mainPanel;
private JLabel tipsLabel;
private RegexRSyntaxTextViewer textArea;
private RegexRTextScrollPane rTextScrollPane;
public JsonResultDialog(String contentType) {
public JsonResultDialog(String contentType, String tips, String displayOrInput) {
super(App.mainFrame, "Result");
ComponentUtil.setPreferSizeAndLocateToCenter(this, 0.6, 0.8);
@@ -35,6 +36,7 @@ public class JsonResultDialog extends JDialog {
setModal(true);
getRootPane().setDefaultButton(buttonOK);
tipsLabel.setText(tips);
textArea = new RegexRSyntaxTextViewer();
if ("XML".equals(contentType)) {
textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_XML);
@@ -96,10 +98,10 @@ public class JsonResultDialog extends JDialog {
*/
private void $$$setupUI$$$() {
contentPane = new JPanel();
contentPane.setLayout(new GridLayoutManager(2, 1, new Insets(0, 0, 0, 0), -1, -1));
contentPane.setLayout(new GridLayoutManager(3, 1, new Insets(0, 0, 0, 0), -1, -1));
final JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayoutManager(1, 2, new Insets(0, 10, 10, 10), -1, -1));
contentPane.add(panel1, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, 1, null, null, null, 0, false));
panel1.setLayout(new GridLayoutManager(1, 2, new Insets(10, 10, 10, 10), -1, -1));
contentPane.add(panel1, new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, 1, null, null, null, 0, false));
final Spacer spacer1 = new Spacer();
panel1.add(spacer1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, 1, null, null, null, 0, false));
final JPanel panel2 = new JPanel();
@@ -108,9 +110,18 @@ public class JsonResultDialog extends JDialog {
buttonOK = new JButton();
buttonOK.setText("OK");
panel2.add(buttonOK, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
final JPanel panel3 = new JPanel();
panel3.setLayout(new GridLayoutManager(1, 1, new Insets(10, 10, 10, 10), -1, -1));
contentPane.add(panel3, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, 1, null, null, null, 0, false));
tipsLabel = new JLabel();
tipsLabel.setText("");
panel3.add(tipsLabel, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
final JPanel panel4 = new JPanel();
panel4.setLayout(new GridLayoutManager(1, 1, new Insets(0, 10, 0, 10), -1, -1));
contentPane.add(panel4, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout(0, 0));
contentPane.add(mainPanel, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
panel4.add(mainPanel, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
}
/**
@@ -369,7 +369,7 @@ public class JsonBeautyListener {
jsonBeautyForm.getJsonToXmlButton().addActionListener(e -> {
try {
String jsonText = jsonBeautyForm.getTextArea().getText();
JsonResultDialog jsonResultDialog = new JsonResultDialog("XML");
JsonResultDialog jsonResultDialog = new JsonResultDialog("XML", "JSON转XML", "Display");
String xmlStr = JSONUtil.toXmlStr(JSONUtil.isTypeJSONArray(jsonText) ? JSONUtil.parseArray(jsonText) : JSONUtil.parseObj(jsonText));
xmlStr = "<root>" + xmlStr + "</root>";
jsonResultDialog.setToTextArea(XmlReformatUtil.format(xmlStr));
@@ -385,7 +385,7 @@ public class JsonBeautyListener {
jsonBeautyForm.getXmlToJsonButton().addActionListener(e -> {
try {
String xmlText = jsonBeautyForm.getTextArea().getText();
JsonResultDialog jsonResultDialog = new JsonResultDialog("JSON");
JsonResultDialog jsonResultDialog = new JsonResultDialog("XML", "请输入XML文本:", "Input");
jsonResultDialog.setToTextArea(JSONUtil.toJsonPrettyStr(JSONUtil.xmlToJson(xmlText)));
jsonResultDialog.setVisible(true);
} catch (Exception e1) {
@@ -440,7 +440,7 @@ public class JsonBeautyListener {
String jsonText = jsonBeautyForm.getTextArea().getText();
String jsonPath = jsonBeautyForm.getJsonPathTextField().getText();
if (StringUtils.isNotBlank(jsonPath)) {
JsonResultDialog jsonResultDialog = new JsonResultDialog("JSON");
JsonResultDialog jsonResultDialog = new JsonResultDialog("JSON", "根据JSON Path,取值如下:", "Display");
jsonResultDialog.setToTextArea(JSONUtil.toJsonPrettyStr(JSONUtil.getByPath(JSONUtil.parse(jsonText), jsonPath).toString()));
jsonResultDialog.setVisible(true);
}
@@ -499,7 +499,7 @@ public class JsonBeautyListener {
jsonBeautyForm.getBeanToJsonButton().addActionListener(e -> {
try {
String jsonText = jsonBeautyForm.getTextArea().getText();
JsonResultDialog jsonResultDialog = new JsonResultDialog("JSON");
JsonResultDialog jsonResultDialog = new JsonResultDialog("Java", "请输入JavaBean类代码:", "Input");
jsonResultDialog.setToTextArea(JSONUtil.toJsonPrettyStr(JSONUtil.parseObj(jsonText)));
jsonResultDialog.setVisible(true);
} catch (Exception e1) {