Skip to content

Commit

Permalink
Improved serialization/deserialization for v 15+
Browse files Browse the repository at this point in the history
  • Loading branch information
rexcardan committed Jul 24, 2018
1 parent 4fe113e commit 7f5a144
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 68 deletions.
11 changes: 9 additions & 2 deletions ESAPIX/Facade/Serialization/DoseValueConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,15 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist

// Read the properties which will be used as constructor parameters
var value = (double) jo["Dose"];
var unit = jo["Unit"].ToObject<DoseValue.DoseUnit>();

var unitVal = jo["Unit"].Value<string>();
DoseValue.DoseUnit unit = DoseValue.DoseUnit.Unknown;
switch (unitVal)
{
case "%": unit = DoseValue.DoseUnit.Percent;break;
case "cGy": unit = DoseValue.DoseUnit.cGy;break;
case "Gy": unit = DoseValue.DoseUnit.Gy;break;
}

// Construct the Result object using the non-default constructor
var dv = new DoseValue(value, unit);

Expand Down
130 changes: 68 additions & 62 deletions ESAPIX/Facade/Serialization/ESAPIContractResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows.Media.Media3D;
using ESAPIX.Facade.API;
using ESAPIX.Interfaces;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using VMS.TPS.Common.Model.Types;

#endregion

Expand All @@ -17,70 +19,74 @@ public class ESAPIContractResolver : DefaultContractResolver
{
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
{
//INFINITE LOOP
// //INFINITE LOOP
var excludingNames = new List<string>();
switch (type.Name)
{
#if !VMS110
case nameof(IScriptContext.Course):
excludingNames.Add(nameof(Course.Patient));
break;
case nameof(IScriptContext.ExternalPlanSetup):
excludingNames.Add(nameof(ExternalPlanSetup.Course));
break;
case nameof(IScriptContext.BrachyPlanSetup):
excludingNames.Add(nameof(BrachyPlanSetup.Course));
break;
case nameof(PlanSum):
excludingNames.Add(nameof(PlanSum.Course));
break;
#endif
#if (VMS150 || VMS151 || VMS155)
case nameof(IScriptContext.IonPlanSetup):
excludingNames.Add(nameof(IonPlanSetup.Course));
break;
case nameof(BeamCalculationLog):
excludingNames.Add(nameof(Beam));
break;
case nameof(EstimatedDVH):
excludingNames.Add(nameof(EstimatedDVH.PlanSetup));
excludingNames.Add(nameof(EstimatedDVH.Structure));
break;
case nameof(OptimizationObjective):
excludingNames.Add(nameof(OptimizationObjective.Structure));
break;
#endif
#if (VMS155)
case nameof(PlanTreatmentSession):
excludingNames.Add(nameof(PlanTreatmentSession.PlanSetup));
break;
case nameof(TreatmentSession):
excludingNames.Add(nameof(TreatmentSession.SessionPlans));
break;
#endif
case nameof(IScriptContext.PlanSetup):
excludingNames.Add(nameof(PlanSetup.Course));
break;
case nameof(Image.Series):
excludingNames.Add(nameof(Series.Images));
break;
case nameof(Series.Study):
excludingNames.Add(nameof(Study.Series));
break;
}
// switch (type.Name)
// {
//#if !VMS110
// case nameof(IScriptContext.Course):
// excludingNames.Add(nameof(Course.Patient));
// break;
// case nameof(IScriptContext.ExternalPlanSetup):
// excludingNames.Add(nameof(ExternalPlanSetup.Course));
// break;
// case nameof(IScriptContext.BrachyPlanSetup):
// excludingNames.Add(nameof(BrachyPlanSetup.Course));
// break;
// case nameof(PlanSum):
// excludingNames.Add(nameof(PlanSum.Course));
// break;

//MESH GEOMETRY
if (type == typeof(MeshGeometry3D))
{
var mesh = new MeshGeometry3D();
excludingNames.Add(nameof(mesh.Bounds));
excludingNames.Add(nameof(mesh.CanFreeze));
excludingNames.Add(nameof(mesh.DependencyObjectType));
excludingNames.Add(nameof(mesh.Dispatcher));
excludingNames.Add(nameof(mesh.HasAnimatedProperties));
excludingNames.Add(nameof(mesh.IsFrozen));
excludingNames.Add(nameof(mesh.IsSealed));
}
//#endif
//#if (VMS150 || VMS151 || VMS155)
// case nameof(ControlPoint):
// excludingNames.Add(nameof(ControlPoint.Beam));
// break;
// case nameof(IScriptContext.IonPlanSetup):
// excludingNames.Add(nameof(IonPlanSetup.Course));
// break;
// case nameof(BeamCalculationLog):
// excludingNames.Add(nameof(Beam));
// break;
// case nameof(EstimatedDVH):
// excludingNames.Add(nameof(EstimatedDVH.PlanSetup));
// excludingNames.Add(nameof(EstimatedDVH.Structure));
// break;
// case nameof(OptimizationObjective):
// excludingNames.Add(nameof(OptimizationObjective.Structure));
// break;
//#endif
//#if (VMS155)
// case nameof(PlanTreatmentSession):
// excludingNames.Add(nameof(PlanTreatmentSession.PlanSetup));
// break;
// case nameof(TreatmentSession):
// excludingNames.Add(nameof(TreatmentSession.SessionPlans));
// break;
//#endif
// case nameof(IScriptContext.PlanSetup):
// excludingNames.Add(nameof(PlanSetup.Course));
// break;
// case nameof(Image.Series):
// excludingNames.Add(nameof(Series.Images));
// break;
// case nameof(Series.Study):
// excludingNames.Add(nameof(Study.Series));
// break;
// }

// //MESH GEOMETRY
// if (type == typeof(MeshGeometry3D))
// {
// var mesh = new MeshGeometry3D();
// excludingNames.Add(nameof(mesh.Bounds));
// excludingNames.Add(nameof(mesh.CanFreeze));
// excludingNames.Add(nameof(mesh.DependencyObjectType));
// excludingNames.Add(nameof(mesh.Dispatcher));
// excludingNames.Add(nameof(mesh.HasAnimatedProperties));
// excludingNames.Add(nameof(mesh.IsFrozen));
// excludingNames.Add(nameof(mesh.IsSealed));
// }

//IVMS Thread
excludingNames.Add(nameof(IScriptContext.Thread));
Expand Down
63 changes: 63 additions & 0 deletions ESAPIX/Facade/Serialization/FacadeSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
using Newtonsoft.Json;
using ESAPIX.Common;
using ESAPIX.Extensions;
using Newtonsoft.Json.Serialization;
using System.Xml;
using System.Text;
using System.Xml.Serialization;
using ESAPIX.Facade.API;

#endregion

Expand Down Expand Up @@ -54,6 +59,64 @@ public static JsonSerializerSettings SerializeSettings
}
}

/// <summary>
/// Serialize to JSON string
/// </summary>
/// <param name="o">object to be serialized</param>
/// <returns>json string of object</returns>
public static string Serialize(IXmlSerializable apiObject)
{
var xml = SerializeToXML(apiObject);
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);

//Convert to JSON
var json = JsonConvert.SerializeXmlNode(doc.FirstChild, Newtonsoft.Json.Formatting.None, true);
json = json.Replace("@", "");
return json;
}

/// <summary>
/// Serialize to XML string
/// </summary>
/// <param name="o">object to be serialized</param>
/// <returns>json string of object</returns>
public static string SerializeToXML(IXmlSerializable apiObject)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.ConformanceLevel = ConformanceLevel.Fragment;
settings.CloseOutput = false;
var ms = new StringBuilder();
{
using (XmlWriter writer = XmlWriter.Create(ms, settings))
{

//Write to XML first (built into ESAPI)
var @class = apiObject.GetType().FullName;
writer.WriteStartElement(@class);
apiObject.WriteXml(writer);
writer.WriteFullEndElement();
writer.Close();
var xml = ms.ToString();
return xml;
}
}
}
/// <summary>
/// Serialize to JSON string
/// </summary>
/// <param name="o">object to be serialized</param>
/// <returns>json string of object</returns>
public static string SerializeStructureWithGeometry(Structure st)
{
var mesh = st.MeshGeometry;
var meshJson = JsonConvert.SerializeObject(mesh, SerializeSettings);
var jsonNonGeometry = Serialize(st);
var recovered = JsonConvert.DeserializeObject<Structure>(jsonNonGeometry, DeserializeSettings);
recovered.MeshGeometry = mesh;
return JsonConvert.SerializeObject(recovered, SerializeSettings);
}

/// <summary>
/// Serialize to JSON string
/// </summary>
Expand Down
30 changes: 26 additions & 4 deletions ESAPIX/Facade/Serialization/IEnumerableJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@ public class IEnumerableJsonConverter : JsonConverter
typeof(IEnumerable<StructureSet>),
typeof(IEnumerable<Registration>),
typeof(IEnumerable<Isodose>),
typeof(IEnumerable<string>)
typeof(IEnumerable<string>),
typeof(IEnumerable<ReferencePoint>),
#if VMS155
typeof(IEnumerable<ImageApprovalHistoryEntry>),
typeof(IEnumerable<PlanTreatmentSession>),
typeof(IEnumerable<StructureApprovalHistoryEntry>)
#endif
};

public override bool CanRead
Expand All @@ -73,6 +79,7 @@ public override bool CanConvert(Type objectType)
public override object ReadJson(JsonReader reader, Type objectType, object existingValue,
JsonSerializer serializer)
{

var typ = objectType.GenericTypeArguments.FirstOrDefault();

if (typ != null)
Expand All @@ -94,11 +101,26 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist

try
{
var tempList = JArray.Load(reader)
var item = JObject.Load(reader);
var val = item.Value<JObject>();


try
{
var arrayr = val.First.CreateReader();
var tempList2 = JArray.Load(arrayr)
.Select(i => (dynamic)serializer.Deserialize(((JObject)i).CreateReader(), typ))
.ToList();

tempList.ForEach(i => { list.Add(i); });
tempList2.ForEach(i => { list.Add(i); });
}
catch(Exception e)
{
var arrayr = val.First.First.CreateReader();
var tempList2 = JArray.Load(arrayr)
.Select(i => (dynamic)serializer.Deserialize(((JObject)i).CreateReader(), typ))
.ToList();
tempList2.ForEach(i => { list.Add(i); });
}
}
catch (Exception e)
{
Expand Down

0 comments on commit 7f5a144

Please sign in to comment.