Skip to content

Commit e8d95fd

Browse files
committed
Refactor helper classes
- Move the majority of the logic from the Design project to the main project - Call the helper classes directly from the main project, instead of redirecting from the Design to the main project
1 parent 2b40f6f commit e8d95fd

File tree

5 files changed

+143
-55
lines changed

5 files changed

+143
-55
lines changed

OpenEphys.Onix1.Design/ChannelConfigurationDialog.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ internal T OpenAndParseConfigurationFile<T>() where T : ProbeGroup
313313

314314
if (ofd.ShowDialog() == DialogResult.OK && File.Exists(ofd.FileName))
315315
{
316-
var newConfiguration = DesignHelper.DeserializeString<T>(File.ReadAllText(ofd.FileName));
316+
var newConfiguration = JsonHelper.DeserializeString<T>(File.ReadAllText(ofd.FileName));
317317

318318
return newConfiguration;
319319
}
@@ -927,7 +927,7 @@ private void MenuItemSaveFile(object sender, EventArgs e)
927927

928928
if (sfd.ShowDialog() == DialogResult.OK)
929929
{
930-
DesignHelper.SerializeObject(ProbeGroup, sfd.FileName);
930+
JsonHelper.SerializeObject(ProbeGroup, sfd.FileName);
931931
}
932932
}
933933

OpenEphys.Onix1.Design/DesignHelper.cs

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -10,57 +10,6 @@ namespace OpenEphys.Onix1.Design
1010
{
1111
static class DesignHelper
1212
{
13-
/// <summary>
14-
/// Given a string with a valid JSON structure, deserialize the string to the given type.
15-
/// </summary>
16-
/// <typeparam name="T"></typeparam>
17-
/// <param name="jsonString"></param>
18-
/// <returns></returns>
19-
#nullable enable
20-
public static T? DeserializeString<T>(string jsonString)
21-
{
22-
var errors = new List<string>();
23-
24-
var serializerSettings = new JsonSerializerSettings()
25-
{
26-
Error = delegate(object sender, Newtonsoft.Json.Serialization.ErrorEventArgs args)
27-
{
28-
errors.Add(args.ErrorContext.Error.Message);
29-
args.ErrorContext.Handled = true;
30-
}
31-
};
32-
33-
var obj = JsonConvert.DeserializeObject<T>(jsonString, serializerSettings);
34-
35-
if (errors.Count > 0)
36-
{
37-
MessageBox.Show($"There were errors encountered while parsing a JSON string. Check the console " +
38-
$"for an error log.", "JSON Parse Error");
39-
40-
foreach (var e in errors)
41-
{
42-
Console.Error.WriteLine(e);
43-
}
44-
45-
return default;
46-
}
47-
48-
return obj;
49-
}
50-
#nullable disable
51-
52-
public static void SerializeObject(object _object, string filepath)
53-
{
54-
var serializerSettings = new JsonSerializerSettings()
55-
{
56-
NullValueHandling = NullValueHandling.Ignore,
57-
};
58-
59-
var stringJson = JsonConvert.SerializeObject(_object, Formatting.Indented, serializerSettings);
60-
61-
File.WriteAllText(filepath, stringJson);
62-
}
63-
6413
public static IEnumerable<Control> GetAllControls(this Control root)
6514
{
6615
var stack = new Stack<Control>();

OpenEphys.Onix1.Design/Rhs2116StimulusSequenceDialog.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,7 @@ int GetNumberOfSteps(double amplitude, Rhs2116StepSize stepSize)
875875

876876
internal override void SerializeStimulusSequence(string fileName)
877877
{
878-
DesignHelper.SerializeObject(Sequence, fileName);
878+
JsonHelper.SerializeObject(Sequence, fileName);
879879
}
880880

881881
internal override bool IsSequenceValid()
@@ -885,7 +885,7 @@ internal override bool IsSequenceValid()
885885

886886
internal override void DeserializeStimulusSequence(string fileName)
887887
{
888-
var newSequence = DesignHelper.DeserializeString<Rhs2116StimulusSequencePair>(File.ReadAllText(fileName));
888+
var newSequence = JsonHelper.DeserializeString<Rhs2116StimulusSequencePair>(File.ReadAllText(fileName));
889889

890890
if (newSequence != null && newSequence.Stimuli.Length == 32)
891891
{

OpenEphys.Onix1/JsonHelper.cs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using Newtonsoft.Json;
5+
6+
namespace OpenEphys.Onix1
7+
{
8+
internal static class JsonHelper
9+
{
10+
public static T DeserializeString<T>(string jsonString) where T : class
11+
{
12+
var errors = new List<string>();
13+
14+
var serializerSettings = new JsonSerializerSettings()
15+
{
16+
Error = delegate (object sender, Newtonsoft.Json.Serialization.ErrorEventArgs args)
17+
{
18+
errors.Add(args.ErrorContext.Error.Message);
19+
args.ErrorContext.Handled = true;
20+
}
21+
};
22+
23+
try
24+
{
25+
var obj = JsonConvert.DeserializeObject<T>(jsonString, serializerSettings);
26+
27+
if (errors.Count > 0)
28+
{
29+
Console.WriteLine("There were errors encountered while parsing a JSON string.\n");
30+
foreach (var e in errors)
31+
{
32+
Console.Error.WriteLine(e);
33+
}
34+
return null;
35+
}
36+
37+
return obj;
38+
}
39+
catch (JsonReaderException e)
40+
{
41+
throw new InvalidDataException("Invalid JSON format", e);
42+
}
43+
catch (JsonSerializationException e)
44+
{
45+
throw new InvalidDataException("Failed to deserialize JSON", e);
46+
}
47+
}
48+
49+
public static void SerializeObject(object obj, string filepath)
50+
{
51+
if (string.IsNullOrEmpty(filepath))
52+
return;
53+
54+
var serializerSettings = new JsonSerializerSettings()
55+
{
56+
NullValueHandling = NullValueHandling.Ignore,
57+
};
58+
59+
var stringJson = JsonConvert.SerializeObject(obj, Formatting.Indented, serializerSettings);
60+
61+
try
62+
{
63+
File.WriteAllText(filepath, stringJson);
64+
}
65+
catch (UnauthorizedAccessException e)
66+
{
67+
throw new IOException($"Access denied writing to '{filepath}'. Check file permissions.", e);
68+
}
69+
catch (DirectoryNotFoundException e)
70+
{
71+
throw new IOException($"Directory not found for '{filepath}'. Ensure the directory exists.", e);
72+
}
73+
catch (PathTooLongException e)
74+
{
75+
throw new IOException($"File path '{filepath}' exceeds system maximum length.", e);
76+
}
77+
catch (IOException e)
78+
{
79+
throw new IOException($"Unable to write to '{filepath}'. The file may be in use.", e);
80+
}
81+
catch (Exception e)
82+
{
83+
throw new IOException($"Unexpected error writing to '{filepath}'.", e);
84+
}
85+
}
86+
}
87+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
using System.IO;
3+
using OpenEphys.ProbeInterface.NET;
4+
5+
namespace OpenEphys.Onix1
6+
{
7+
internal static class ProbeInterfaceHelper
8+
{
9+
public const string ProbeInterfaceFileNameFilter = "ProbeInterface Files|*.json|All Files|*.*";
10+
11+
public static T LoadExternalProbeInterfaceFile<T>(string probeInterfaceFileName) where T : ProbeGroup
12+
{
13+
if (string.IsNullOrEmpty(probeInterfaceFileName))
14+
{
15+
throw new ArgumentNullException(nameof(probeInterfaceFileName), "ProbeInterface file path cannot be null or empty.");
16+
}
17+
18+
if (!File.Exists(probeInterfaceFileName))
19+
{
20+
throw new FileNotFoundException($"The ProbeInterface file '{probeInterfaceFileName}' does not exist.");
21+
}
22+
23+
try
24+
{
25+
string jsonContent = File.ReadAllText(probeInterfaceFileName);
26+
var result = JsonHelper.DeserializeString<T>(jsonContent) ?? throw new InvalidDataException($"Failed to parse ProbeInterface file: {probeInterfaceFileName}");
27+
return result;
28+
}
29+
catch (UnauthorizedAccessException e)
30+
{
31+
throw new IOException($"Access denied reading '{probeInterfaceFileName}'. Check file permissions.", e);
32+
}
33+
catch (PathTooLongException e)
34+
{
35+
throw new IOException($"File path '{probeInterfaceFileName}' exceeds system maximum length.", e);
36+
}
37+
catch (IOException e)
38+
{
39+
throw new IOException($"Unable to read '{probeInterfaceFileName}'. The file may be in use.", e);
40+
}
41+
catch (Exception e)
42+
{
43+
throw new IOException($"Unexpected error reading '{probeInterfaceFileName}'.", e);
44+
}
45+
}
46+
47+
public static void SaveExternalProbeInterfaceFile(ProbeGroup probeGroup, string probeInterfaceFile)
48+
{
49+
JsonHelper.SerializeObject(probeGroup, probeInterfaceFile);
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)