Skip to content

Commit 1dbd58d

Browse files
authored
Merge pull request #4030 from JPMKTest/Configurable-sorting-of-JSON-keys-in-editor-window
Configurable sorting of JSON keys
2 parents 632a80d + 4f2b4a1 commit 1dbd58d

File tree

6 files changed

+98
-4
lines changed

6 files changed

+98
-4
lines changed

src/EditDialog.cpp

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <json.hpp>
2525

2626
using json = nlohmann::json;
27+
using ordered_json = nlohmann::ordered_json;
2728

2829
EditDialog::EditDialog(QWidget* parent)
2930
: QDialog(parent),
@@ -101,6 +102,7 @@ EditDialog::EditDialog(QWidget* parent)
101102
EditDialog::~EditDialog()
102103
{
103104
Settings::setValue("databrowser", "indent_compact", mustIndentAndCompact);
105+
Settings::setValue("databrowser", "sort_keys", mustSortKeys);
104106
Settings::setValue("databrowser", "auto_switch_mode", ui->buttonAutoSwitchMode->isChecked());
105107
Settings::setValue("databrowser", "editor_word_wrap", ui->actionWordWrap->isChecked());
106108
delete ui;
@@ -607,10 +609,13 @@ void EditDialog::accept()
607609

608610
QString newData;
609611
bool proceed = true;
610-
json jsonDoc;
612+
ordered_json jsonDoc;
611613

612614
try {
613-
jsonDoc = json::parse(sciEdit->text().toStdString());
615+
if (mustSortKeys)
616+
jsonDoc = json::parse(sciEdit->text().toStdString());
617+
else
618+
jsonDoc = ordered_json::parse(sciEdit->text().toStdString());
614619
} catch(json::parse_error& parseError) {
615620
sciEdit->setErrorIndicator(static_cast<int>(parseError.byte - 1));
616621

@@ -740,10 +745,14 @@ void EditDialog::setDataInBuffer(const QByteArray& bArrdata, DataSources source)
740745
{
741746
sciEdit->clearErrorIndicators();
742747

743-
json jsonDoc;
748+
ordered_json jsonDoc;
744749

745750
try {
751+
if (mustSortKeys)
746752
jsonDoc = json::parse(std::string(bArrdata.constData(), static_cast<size_t>(bArrdata.size())));
753+
else
754+
jsonDoc = ordered_json::parse(std::string(bArrdata.constData(), static_cast<size_t>(bArrdata.size())));
755+
747756
} catch(json::parse_error& parseError) {
748757
sciEdit->setErrorIndicator(static_cast<int>(parseError.byte - 1));
749758
}
@@ -801,8 +810,9 @@ void EditDialog::setDataInBuffer(const QByteArray& bArrdata, DataSources source)
801810
void EditDialog::editModeChanged(int newMode)
802811
{
803812
ui->actionIndent->setEnabled(newMode == JsonEditor || newMode == XmlEditor);
813+
ui->actionSortKeys->setEnabled(newMode == JsonEditor);
804814
setStackCurrentIndex(newMode);
805-
815+
806816
// Change focus from the mode combo to the editor to start typing.
807817
if (ui->comboMode->hasFocus())
808818
setFocus();
@@ -924,6 +934,8 @@ void EditDialog::editTextChanged()
924934
void EditDialog::setMustIndentAndCompact(bool enable)
925935
{
926936
mustIndentAndCompact = enable;
937+
938+
ui->actionSortKeys->setEnabled(enable);
927939

928940
// Indent or compact if necessary. If data has changed (button Apply indicates so), reload from the widget, else from the table.
929941
if (ui->buttonApply->isEnabled()) {
@@ -932,6 +944,19 @@ void EditDialog::setMustIndentAndCompact(bool enable)
932944
setCurrentIndex(m_currentIndex);
933945
}
934946

947+
void EditDialog::setMustSortKeys(bool enable)
948+
{
949+
mustSortKeys = enable;
950+
951+
// Must order the json keys. If data has changed (button Apply indicates so), reload from the widget, else from the table.
952+
if (ui->buttonApply->isEnabled()) {
953+
setDataInBuffer(sciEdit->text().toUtf8(), SciBuffer);
954+
}
955+
else
956+
setCurrentIndex(m_currentIndex);
957+
}
958+
959+
935960
// Determine the type of data in the cell
936961
int EditDialog::checkDataType(const QByteArray& bArrdata) const
937962
{
@@ -1132,6 +1157,9 @@ void EditDialog::reloadSettings()
11321157
mustIndentAndCompact = Settings::getValue("databrowser", "indent_compact").toBool();
11331158
ui->actionIndent->setChecked(mustIndentAndCompact);
11341159

1160+
mustSortKeys = Settings::getValue("databrowser", "sort_keys").toBool();
1161+
ui->actionSortKeys->setChecked(mustSortKeys);
1162+
11351163
ui->buttonAutoSwitchMode->setChecked(Settings::getValue("databrowser", "auto_switch_mode").toBool());
11361164

11371165
// Set the (SQL) editor font for hex editor, since it needs a

src/EditDialog.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ private slots:
4848
void switchEditorMode(bool autoSwitchForType);
4949
void updateCellInfoAndMode(const QByteArray& bArrdata);
5050
void setMustIndentAndCompact(bool enable);
51+
void setMustSortKeys(bool enable);
5152
void openPrintDialog();
5253
void copyHexAscii();
5354
void setWordWrapping(bool value);
@@ -67,6 +68,7 @@ private slots:
6768
int dataType;
6869
bool isReadOnly;
6970
bool mustIndentAndCompact;
71+
bool mustSortKeys;
7072
QByteArray removedBom;
7173

7274
enum DataSources {

src/EditDialog.ui

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@
151151
</property>
152152
<addaction name="actionWordWrap"/>
153153
<addaction name="actionIndent"/>
154+
<addaction name="actionSortKeys"/>
154155
<addaction name="actionImport"/>
155156
<addaction name="actionExport"/>
156157
<addaction name="actionOpenInExternal"/>
@@ -313,6 +314,24 @@
313314
<string>When enabled, the auto-format feature formats the data on loading, breaking the text in lines and indenting it for maximum readability. On data saving, the auto-format feature compacts the data removing end of lines, and unnecessary whitespace.</string>
314315
</property>
315316
</action>
317+
<action name="actionSortKeys">
318+
<property name="checkable">
319+
<bool>true</bool>
320+
</property>
321+
<property name="icon">
322+
<iconset resource="icons/icons.qrc">
323+
<normaloff>:/icons/text_sort_ascending</normaloff>:/icons/text_sort_ascending</iconset>
324+
</property>
325+
<property name="text">
326+
<string>SortKeys</string>
327+
</property>
328+
<property name="toolTip">
329+
<string>Sort Keys: when pretty print is on, will sort the json keys alphabetically.</string>
330+
</property>
331+
<property name="whatsThis">
332+
<string>When enabled alongside pretty print, the json keys will be ordered alphabetically by their name</string>
333+
</property>
334+
</action>
316335
<action name="actionExport">
317336
<property name="icon">
318337
<iconset resource="icons/icons.qrc">
@@ -504,6 +523,22 @@
504523
</hint>
505524
</hints>
506525
</connection>
526+
<connection>
527+
<sender>actionSortKeys</sender>
528+
<signal>toggled(bool)</signal>
529+
<receiver>EditDialog</receiver>
530+
<slot>setMustSortKeys(bool)</slot>
531+
<hints>
532+
<hint type="sourcelabel">
533+
<x>-1</x>
534+
<y>-1</y>
535+
</hint>
536+
<hint type="destinationlabel">
537+
<x>308</x>
538+
<y>190</y>
539+
</hint>
540+
</hints>
541+
</connection>
507542
<connection>
508543
<sender>buttonAutoSwitchMode</sender>
509544
<signal>toggled(bool)</signal>

src/Settings.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,8 @@ QVariant Settings::getDefaultValue(const std::string& group, const std::string&
305305
return false;
306306
if(name == "indent_compact")
307307
return false;
308+
if (name == "sort_keys")
309+
return true;
308310
if(name == "auto_switch_mode")
309311
return true;
310312
if(name == "editor_word_wrap")

src/icons/icons.qrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
<file alias="text_bold">text_bold.svg</file>
9999
<file alias="text_italic">text_italic.svg</file>
100100
<file alias="text_indent">text_padding_left.svg</file>
101+
<file alias="text_sort_ascending">text_sort_ascending.svg</file>
101102
<file alias="comment_block">text_padding_top.svg</file>
102103
<file alias="foreground_color">color_wheel.svg</file>
103104
<file alias="text_replace">find_edit.svg</file>

src/icons/text_sort_ascending.svg

Lines changed: 26 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)