Skip to content

Commit cea308a

Browse files
dustindustin
authored andcommitted
Added Multion pattern to instantiate just one object of each *Format.cs type. This object is then utilized and controlled by the Formatter class.
1 parent a0a276e commit cea308a

24 files changed

+24159
-167
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,5 @@ $RECYCLE.BIN/
8080

8181
# Windows shortcuts
8282
*.lnk
83+
/Assets/Experica/Serialization/JSONFormat.cs.meta
84+
*.meta

Assets/Experica/Experiment.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2828
using System;
2929
using Fasterflect;
3030
using MsgPack.Serialization;
31+
using Newtonsoft.Json;
3132

3233
namespace Experica
3334
{
@@ -96,9 +97,11 @@ public class Experiment
9697
public string Subject_Log { get; set; } = "";
9798

9899
public string EnvPath { get; set; } = "";
100+
[JsonIgnore] // Json cannont serialize this property
99101
[MessagePackRuntimeCollectionItemType]
100102
public Dictionary<string, object> EnvParam { get; set; } = new Dictionary<string, object>();
101103
public string CondPath { get; set; } = "";
104+
[JsonIgnore] // Json cannont serialze this property
102105
[MessagePackRuntimeCollectionItemType]
103106
public Dictionary<string, IList> Cond { get; set; }
104107
public string ExLogicPath { get; set; } = "";
@@ -130,6 +133,7 @@ public class Experiment
130133
public List<CONDTESTPARAM> NotifyParam { get; set; }
131134
public List<string> ExInheritParam { get; set; } = new List<string>();
132135
public List<string> EnvInheritParam { get; set; } = new List<string>();
136+
[JsonIgnore] // Json cannont serialize this property
133137
[MessagePackRuntimeCollectionItemType]
134138
public Dictionary<string, object> Param { get; set; } = new Dictionary<string, object>();
135139
public double TimerDriftSpeed { get; set; }
@@ -139,14 +143,19 @@ public class Experiment
139143
public uint Version { get; set; } = 2;
140144
public CommandConfig Config { get; set; }
141145

146+
[JsonIgnore]
142147
[MessagePackIgnore]
143148
public Dictionary<CONDTESTPARAM, List<object>> CondTest { get; set; }
149+
[JsonIgnore]
144150
[MessagePackIgnore]
145151
public CONDTESTSHOWLEVEL CondTestShowLevel { get; set; }
152+
[JsonIgnore]
146153
[MessagePackIgnore]
147154
public bool SendMail { get; set; } = false;
155+
[JsonIgnore]
148156
[MessagePackIgnore]
149157
public static readonly Dictionary<string, PropertyAccess> Properties;
158+
[JsonIgnore]
150159
[MessagePackIgnore]
151160
public Action<string, object> OnNotifyUI;
152161

Assets/Experica/ExperimentLogic.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,17 +316,17 @@ public virtual void SaveData()
316316
ex.Config = config;
317317
}
318318
ex.EnvParam = envmanager.GetActiveParams();
319-
320-
//
321319
switch (config.SaveDataFormat)
322320
{
323321
// Currently Not Implemented.
324322
case DataFormat.EXPERICA:
325323
string serialzedData = Formatter.Instance.SerialzeDataToFormat(ex, DataFormat.EXPERICA);
326324
File.WriteAllText(DataPath(DataFormat.EXPERICA), serialzedData);
327325
break;
328-
//case DataFormat.HDF5:
329-
//DataPath(DataFormat.YAML).WriteToFile(ex);
326+
case DataFormat.JSON:
327+
serialzedData = Formatter.Instance.SerialzeDataToFormat(ex, DataFormat.JSON);
328+
File.WriteAllText(DataPath(DataFormat.JSON), serialzedData);
329+
break;
330330
// Save the Experiment, enviroment, and data as a YAML File.
331331
default:
332332
serialzedData = Formatter.Instance.SerialzeDataToFormat(ex, DataFormat.YAML);

Assets/Experica/Extension.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ public enum CONDTESTPARAM
5757
public enum DataFormat
5858
{
5959
YAML,
60-
EXPERICA
60+
EXPERICA,
61+
JSON
6162
}
6263

6364
public static class Extension

Assets/Experica/Serialization/Formatter.cs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,27 @@ OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2424

2525
namespace Experica
2626
{
27+
/// <summary>
28+
/// The purpose of this class is to have a multion capable of formatting (serializing/deserializing) in
29+
/// any format class that is derived from IFormat. This class will have a list of singleton objects
30+
/// corresponding to each format possible. To add a type of format, simply create the class, have it
31+
/// end in Format, and add the type to the enum DataType in Extension.cs
32+
/// </summary>
2733
public sealed class Formatter
2834
{
29-
// This can be static in a console/desktop application, just be wary of potential memory issues
3035
private Dictionary<DataFormat, IFormat> _formats = new Dictionary<DataFormat, IFormat>();
3136

37+
// With lazy instantiation, it is only created once referenced.
3238
private static readonly Lazy<Formatter> lazy = new Lazy<Formatter>(() => new Formatter());
3339

40+
// The singleton Instance of the Formatter, its thread safe.
3441
public static Formatter Instance { get { return lazy.Value; } }
3542

3643
/// <summary>
37-
/// Get the current active policy of the given type.
44+
/// Gets the active singleton object for the corresponding data format
3845
/// </summary>
39-
/// <param name="type">The type of policy to retrieve.</param>
40-
/// <returns>The current active policy of the given type</returns>
46+
/// <param name="type">The type of DataFormat to retrieve.</param>
47+
/// <returns>The current active DataFormat of the given type</returns>
4148
private IFormat GetActiveFormat(DataFormat format)
4249
{
4350
if (!_formats.ContainsKey(format))
@@ -51,12 +58,26 @@ private IFormat GetActiveFormat(DataFormat format)
5158
return _formats[format];
5259
}
5360

61+
/// <summary>
62+
/// Serializes the object using a specific type of DataFormat
63+
/// </summary>
64+
/// <typeparam name="T">type of the object to serialize.</typeparam>
65+
/// <param name="obj">Object to serialize.</param>
66+
/// <param name="format">The format to serialize the Object to.</param>
67+
/// <returns></returns>
5468
public string SerialzeDataToFormat<T>(T obj, DataFormat format)
5569
{
5670
IFormat formatToUse = GetActiveFormat(format);
5771
return formatToUse.Serialize(obj);
5872
}
5973

74+
/// <summary>
75+
/// Deserializes an object
76+
/// </summary>
77+
/// <typeparam name="T">Type to deserialize to</typeparam>
78+
/// <param name="data">The data in the specific format to deserialize</param>
79+
/// <param name="format">The format the the string is in to deserialize.</param>
80+
/// <returns></returns>
6081
public T DeserializeUsingFormat<T>(string data, DataFormat format)
6182
{
6283
IFormat formatToUse = GetActiveFormat(format);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using Experica;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace Experica
9+
{
10+
class HDF5Format : IFormat
11+
{
12+
/// <summary>
13+
/// NOT IMPLEMENTED. Will deserialize a string of text into an object.
14+
/// </summary>
15+
/// <typeparam name="T"></typeparam>
16+
/// <param name="data"></param>
17+
/// <returns></returns>
18+
public T Deserialize<T>(string data)
19+
{
20+
throw new NotImplementedException();
21+
}
22+
23+
/// <summary>
24+
/// NOT IMPLEMENTED. Will Serialize an object into a serialize of bytes/text in hdf5 format
25+
/// </summary>
26+
/// <typeparam name="T"></typeparam>
27+
/// <param name="obj"></param>
28+
/// <returns></returns>
29+
public string Serialize<T>(T obj)
30+
{
31+
throw new NotImplementedException();
32+
}
33+
}
34+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using Experica;
2+
using Newtonsoft.Json;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace Experica
10+
{
11+
class JSONFormat : IFormat
12+
{
13+
14+
public T Deserialize<T>(string data)
15+
{
16+
T obj = JsonConvert.DeserializeObject<T>(data);
17+
return obj;
18+
}
19+
20+
public string Serialize<T>(T obj)
21+
{
22+
string json = JsonConvert.SerializeObject(obj, Formatting.Indented, new JsonSerializerSettings()
23+
{
24+
// This handles self-referencing loops.
25+
//PreserveReferencesHandling = PreserveReferencesHandling.All
26+
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
27+
//PreserveReferencesHandling = PreserveReferencesHandling.Objects
28+
});
29+
return json;
30+
}
31+
}
32+
}

Assets/Experica/Serialization/YAMLFormat.cs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -65,26 +65,23 @@ public YAMLFormat()
6565
deserializer = new DeserializerBuilder().IgnoreUnmatchedProperties().IgnoreFields().WithTypeConverter(yamlvlabconverter).Build();
6666
}
6767

68-
//public static void WriteYamlFile<T>(this string path, T data)
69-
//{
70-
// File.WriteAllText(path, serializer.Serialize(data));
71-
//}
72-
73-
//public static T ReadYamlFile<T>(string path)
74-
//{
75-
// return deserializer.Deserialize<T>(File.ReadAllText(path));
76-
//}
77-
78-
//public static T DeserializeYaml<T>(string data)
79-
//{
80-
// return deserializer.Deserialize<T>(data);
81-
//}
82-
68+
/// <summary>
69+
/// Serializes a serializable object
70+
/// </summary>
71+
/// <typeparam name="T">The type of the object to serialize</typeparam>
72+
/// <param name="obj">The object to serialize</param>
73+
/// <returns>A string in YAML format forrepsonding to the object.</returns>
8374
public string Serialize<T>(T obj)
8475
{
8576
return serializer.Serialize(obj);
8677
}
8778

79+
/// <summary>
80+
/// Deserialize the Yaml string
81+
/// </summary>
82+
/// <typeparam name="T">Type returned</typeparam>
83+
/// <param name="data">The data to deserialize.</param>
84+
/// <returns>Deserialized Object of type T</returns>
8885
public T Deserialize<T>(string data)
8986
{
9087
return deserializer.Deserialize<T>(data);

Assets/Experica/Yaml.cs

Lines changed: 0 additions & 93 deletions
This file was deleted.

Assets/Experica/Yaml.cs.meta

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)