Skip to content

Commit

Permalink
Fix bug of wrong values when importing JSON files
Browse files Browse the repository at this point in the history
  • Loading branch information
g3rzi committed Aug 23, 2023
1 parent 6a57664 commit 907f210
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion PipeViewer/App.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/>
</startup>
</configuration>
21 changes: 15 additions & 6 deletions PipeViewer/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1213,8 +1213,9 @@ private void saveToolStripMenuItem_Click(object sender, EventArgs e)
saveDialog.Title = "Save results";
saveDialog.InitialDirectory = @"c:\";
saveDialog.Filter = "CSV files (*.csv)|*.csv|JSON files (*.json)|*.json|All files (*.*)|*.*";
saveDialog.FilterIndex = 1;
saveDialog.FilterIndex = 2;
saveDialog.RestoreDirectory = true;
saveDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
if (saveDialog.ShowDialog() == DialogResult.OK)
{
string filePath = saveDialog.FileName;
Expand All @@ -1237,8 +1238,9 @@ private void importToolStripMenuItem_Click(object sender, EventArgs e)
importDialog.Title = "Import CSV\\JSON file";
importDialog.InitialDirectory = @"c:\";
importDialog.Filter = "CSV files (*.csv)|*.csv|JSON files (*.json)|*.json|All files (*.*)|*.*";
importDialog.FilterIndex = 1;
importDialog.FilterIndex = 2;
importDialog.RestoreDirectory = true;
importDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

if (importDialog.ShowDialog() == DialogResult.OK)
{
Expand All @@ -1254,6 +1256,9 @@ private void importToolStripMenuItem_Click(object sender, EventArgs e)
importDataGridViewToJSON(filePath);
}
}

// because we import the data without the color.
this.m_showPermissionColor = false;
}

// Taken from https://stackoverflow.com/a/26259909/2153777
Expand All @@ -1274,7 +1279,7 @@ private void exportDataGridViewToCSV(string filename)
File.WriteAllText(filename, dataObject.GetText(TextDataFormat.CommaSeparatedValue).Replace("\n", ""));
}

// TODO: maybe had option to make it as one line? liki mini JSON?
// TODO: maybe had option to make it as one line? like mini JSON?
// less readable but might be better for loading.
private void exportDataGridViewToJSON(string filename)
{
Expand All @@ -1291,7 +1296,7 @@ private void exportDataGridViewToJSON(string filename)
continue;
}

string columnName = cell.OwningColumn.Name;
string columnName = cell.OwningColumn.Name.Substring("Column".Length);
object cellValue = cell.Value ?? DBNull.Value; // Use DBNull for null values

rowData.Add(columnName, cellValue);
Expand All @@ -1300,8 +1305,12 @@ private void exportDataGridViewToJSON(string filename)
}
}

// Help to keep the strings as they are (instead of showing 8192 for "Medium" it will show the string)
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());

// Serialize the data to JSON and write it to the file
string jsonData = JsonConvert.SerializeObject(rows, Formatting.Indented);
string jsonData = JsonConvert.SerializeObject(rows, Formatting.Indented, settings);
File.WriteAllText(filename, jsonData);
}
private void importDataGridViewToCSV(string filename)
Expand Down Expand Up @@ -1331,7 +1340,7 @@ private void importDataGridViewToJSON(string filename)
// Add columns to DataGridView
foreach (DataColumn column in dataTable.Columns)
{
dataGridView1.Columns.Add(column.ColumnName, column.ColumnName);
dataGridView1.Columns.Add("Column"+column.ColumnName, column.ColumnName);
}

// Add rows to DataGridView
Expand Down
2 changes: 1 addition & 1 deletion PipeViewer/PipeViewer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<OutputType>WinExe</OutputType>
<RootNamespace>PipeViewer</RootNamespace>
<AssemblyName>PipeViewer</AssemblyName>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
Expand Down
2 changes: 1 addition & 1 deletion PipeViewer/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion PipeViewer/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 907f210

Please sign in to comment.