Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapter Serialisation: DataType now serialises properly #149

Merged
merged 6 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 8 additions & 16 deletions LadybugTools_Adapter/Convert/FromBHoM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,8 @@
*/

using BH.oM.LadybugTools;
using System;
using BH.Engine.Serialiser;
using System.Collections.Generic;
using System.Text;
using BH.oM.Base;
using System.IO;
using BH.oM.Adapter;
using BH.Engine.Adapter;
using BH.oM.Base.Debugging;

namespace BH.Adapter.LadybugTools
{
public static partial class Convert
Expand All @@ -50,42 +42,42 @@ public static string ICustomify(this ILadybugTools lbtObject)
return Jsonify(lbtObject as dynamic);
}

private static string Jsonify(this oM.LadybugTools.AnalysisPeriod analysisPeriod)
private static string Jsonify(this AnalysisPeriod analysisPeriod)
{
return FromAnalysisPeriod(analysisPeriod).ToJson();
}

private static string Jsonify(this oM.LadybugTools.DataType dataType)
private static string Jsonify(this DataType dataType)
{
return FromDataType(dataType).ToJson();
}

private static string Jsonify(this oM.LadybugTools.EnergyMaterial energyMaterial)
private static string Jsonify(this EnergyMaterial energyMaterial)
{
return FromEnergyMaterial(energyMaterial).ToJson();
}

private static string Jsonify(this oM.LadybugTools.EnergyMaterialVegetation energyMaterial)
private static string Jsonify(this EnergyMaterialVegetation energyMaterial)
{
return FromEnergyMaterialVegetation(energyMaterial).ToJson();
}

private static string Jsonify(this oM.LadybugTools.EPW epw)
private static string Jsonify(this EPW epw)
{
return FromEPW(epw);
}

private static string Jsonify(this oM.LadybugTools.Header header)
private static string Jsonify(this Header header)
{
return FromHeader(header).ToJson();
}

private static string Jsonify(this oM.LadybugTools.HourlyContinuousCollection collection)
private static string Jsonify(this HourlyContinuousCollection collection)
{
return FromHourlyContinuousCollection(collection);
}

private static string Jsonify(this oM.LadybugTools.Location location)
private static string Jsonify(this Location location)
{
return FromLocation(location).ToJson();
}
Expand Down
13 changes: 11 additions & 2 deletions LadybugTools_Adapter/Convert/MetaData/DataType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,22 @@ public static Dictionary<string, object> FromDataType(BH.oM.LadybugTools.DataTyp
{
Dictionary<string, object> returnDict = new Dictionary<string, object>
{
{ "type", "GenericDataType" },
{ "name", dataType.Name },
{ "data_type", dataType.Data_Type }
};

string type;
Tom-Kingstone marked this conversation as resolved.
Show resolved Hide resolved

if (dataType.BaseUnit != "")
if (dataType.Data_Type == "GenericType")
{
type = "GenericDataType";
returnDict.Add("base_unit", dataType.BaseUnit);
}
else
{
type = "DataType";
}
Tom-Kingstone marked this conversation as resolved.
Show resolved Hide resolved
returnDict.Add("type", type);

return returnDict;
}
Expand Down
11 changes: 11 additions & 0 deletions LadybugTools_Adapter/Convert/ToBHoM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,34 @@
public static List<IBHoMObject> ToBHoM(this FileSettings jsonFile)
{
string json = File.ReadAllText(jsonFile.GetFullFileName());
if (!json.StartsWith("["))
{
json = "[" + json;
}
if (!json.EndsWith("]"))
{
json = json + "]";
}
Tom-Kingstone marked this conversation as resolved.
Show resolved Hide resolved
IEnumerable<object> objs = Engine.Serialiser.Convert.FromJsonArray(json);
List<IBHoMObject> returnObjects = new List<IBHoMObject>();
foreach (var obj in objs)
{
Dictionary<string, object> lbtObject = null;
if (obj.GetType() == typeof(CustomObject))
{
lbtObject = (obj as CustomObject).CustomData;
}
else if (obj.GetType() == typeof(Dictionary<string, object>))
{
BH.Engine.Base.Compute.RecordWarning("The object was not deserialised as a CustomObject, are you sure that this file came from a LadybugTools Python object? \n Trying to cast to Dictionary...");
lbtObject = obj as Dictionary<string, object>;
}
else
{
BH.Engine.Base.Compute.RecordWarning($"One of the objects in the json given already deserialises to a BHoM object of type: {obj.GetType().FullName}. Returning this object.");
returnObjects.Add((IBHoMObject)obj);
continue;
}

Check warning on line 68 in LadybugTools_Adapter/Convert/ToBHoM.cs

View check run for this annotation

BHoMBot-CI / code-compliance

LadybugTools_Adapter/Convert/ToBHoM.cs#L54-L68

The use of CustomData within the code is discouraged except in circumstances where volatile data is being used. - For more information see https://bhom.xyz/documentation/DevOps/Code%20Compliance%20and%20CI/Compliance%20Checks/IsUsingCustomData
if (lbtObject.ContainsKey("type"))
{
switch (lbtObject["type"] as string)
Expand All @@ -68,6 +76,9 @@
case "GenericDataType":
returnObjects.Add(ToDataType(lbtObject));
break;
case "DataType":
returnObjects.Add(ToDataType(lbtObject));
break;
case "EnergyMaterial":
returnObjects.Add(ToEnergyMaterial(lbtObject));
break;
Expand Down
3 changes: 0 additions & 3 deletions LadybugTools_oM/Collections/EPW.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ public class EPW : BHoMObject, ILadybugTools
[Description("The data collections within this EPW.")]
public virtual List<HourlyContinuousCollection> DataCollections { get; set; } = new List<HourlyContinuousCollection>();

[Description("The data source and uncertainty field of this EPW.")]
public virtual List<string> DataSourceUncertainty { get; set; } = new List<string>();

[Description("Metadata associated with this EPW.")]
public virtual Dictionary<string, object> Metadata { get; set; } = new Dictionary<string, object>();

Expand Down